Android Open Source - apps-android-commons Modifier Sequence






From Project

Back to project page apps-android-commons.

License

The source code is released under:

Apache License

If you think the Android project apps-android-commons listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.wikimedia.commons.modifications;
/* w ww .  ja  v  a  2  s . c  o  m*/
import android.content.ContentProviderClient;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.RemoteException;
import android.text.TextUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.wikimedia.commons.contributions.ContributionsContentProvider;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

public class ModifierSequence {
    private Uri mediaUri;
    private ArrayList<PageModifier> modifiers;
    private Uri contentUri;
    private ContentProviderClient client;

    public ModifierSequence(Uri mediaUri) {
        this.mediaUri = mediaUri;
        modifiers = new ArrayList<PageModifier>();
    }

    public ModifierSequence(Uri mediaUri, JSONObject data) {
        this(mediaUri);
        JSONArray modifiersJSON = data.optJSONArray("modifiers");
        for(int i=0; i< modifiersJSON.length(); i++) {
            modifiers.add(PageModifier.fromJSON(modifiersJSON.optJSONObject(i)));
        }
    }

    public Uri getMediaUri() {
        return mediaUri;
    }

    public void queueModifier(PageModifier modifier) {
        modifiers.add(modifier);
    }

    public String executeModifications(String pageName, String pageContents) {
        for(PageModifier modifier: modifiers) {
            pageContents = modifier.doModification(pageName,  pageContents);
        }
        return pageContents;
    }

    public String getEditSummary() {
        StringBuffer editSummary = new StringBuffer();
        for(PageModifier modifier: modifiers) {
            editSummary.append(modifier.getEditSumary()).append(" ");
        }
        editSummary.append("Via Commons Mobile App");
        return editSummary.toString();
    }

    public JSONObject toJSON() {
        JSONObject data = new JSONObject();
        try {
            JSONArray modifiersJSON = new JSONArray();
            for(PageModifier modifier: modifiers) {
                modifiersJSON.put(modifier.toJSON());
            }
            data.put("modifiers", modifiersJSON);
            return data;
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }

    public ContentValues toContentValues() {
        ContentValues cv = new ContentValues();
        cv.put(Table.COLUMN_MEDIA_URI, mediaUri.toString());
        cv.put(Table.COLUMN_DATA, toJSON().toString());
        return cv;
    }

    public static ModifierSequence fromCursor(Cursor cursor) {
        // Hardcoding column positions!
        ModifierSequence ms = null;
        try {
            ms = new ModifierSequence(Uri.parse(cursor.getString(1)), new JSONObject(cursor.getString(2)));
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        ms.contentUri = ModificationsContentProvider.uriForId(cursor.getInt(0));

        return ms;
    }

    public void save() {
        try {
            if(contentUri == null) {
                contentUri = client.insert(ModificationsContentProvider.BASE_URI, this.toContentValues());
            } else {
                client.update(contentUri, toContentValues(), null, null);
            }
        } catch(RemoteException e) {
            throw new RuntimeException(e);
        }
    }

    public void delete() {
        try {
            client.delete(contentUri, null, null);
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }

    public void setContentProviderClient(ContentProviderClient client) {
        this.client = client;
    }

    public static class Table {
        public static final String TABLE_NAME = "modifications";

        public static final String COLUMN_ID = "_id";
        public static final String COLUMN_MEDIA_URI = "mediauri";
        public static final String COLUMN_DATA = "data";

        // NOTE! KEEP IN SAME ORDER AS THEY ARE DEFINED UP THERE. HELPS HARD CODE COLUMN INDICES.
        public static final String[] ALL_FIELDS = {
                COLUMN_ID,
                COLUMN_MEDIA_URI,
                COLUMN_DATA
        };


        private static final String CREATE_TABLE_STATEMENT = "CREATE TABLE " + TABLE_NAME + " ("
                + "_id INTEGER PRIMARY KEY,"
                + "mediauri STRING,"
                + "data STRING"
                + ");";


        public static void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_TABLE_STATEMENT);
        }

        public static void onUpdate(SQLiteDatabase db, int from, int to) {
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        }
    }
}




Java Source Code List

com.gst_sdk.GStreamer.java
com.gst_sdk.GStreamer.java
org.wikimedia.commons.AboutActivity.java
org.wikimedia.commons.CommonsApplication.java
org.wikimedia.commons.EventLog.java
org.wikimedia.commons.HandlerService.java
org.wikimedia.commons.LicenseList.java
org.wikimedia.commons.License.java
org.wikimedia.commons.MediaDataExtractor.java
org.wikimedia.commons.MediaWikiImageView.java
org.wikimedia.commons.Media.java
org.wikimedia.commons.Prefs.java
org.wikimedia.commons.SettingsActivity.java
org.wikimedia.commons.Utils.java
org.wikimedia.commons.WelcomeActivity.java
org.wikimedia.commons.api.MWApi.java
org.wikimedia.commons.auth.AuthenticatedActivity.java
org.wikimedia.commons.auth.LoginActivity.java
org.wikimedia.commons.auth.WikiAccountAuthenticatorService.java
org.wikimedia.commons.auth.WikiAccountAuthenticator.java
org.wikimedia.commons.campaigns.CampaignActivity.java
org.wikimedia.commons.campaigns.CampaignContribution.java
org.wikimedia.commons.campaigns.Campaign.java
org.wikimedia.commons.campaigns.CampaignsContentProvider.java
org.wikimedia.commons.campaigns.CampaignsListAdapter.java
org.wikimedia.commons.campaigns.CampaignsSyncAdapter.java
org.wikimedia.commons.campaigns.CampaignsSyncService.java
org.wikimedia.commons.category.CategorizationFragment.java
org.wikimedia.commons.category.CategoryContentProvider.java
org.wikimedia.commons.category.Category.java
org.wikimedia.commons.contributions.ContributionController.java
org.wikimedia.commons.contributions.ContributionViewHolder.java
org.wikimedia.commons.contributions.Contribution.java
org.wikimedia.commons.contributions.ContributionsActivity.java
org.wikimedia.commons.contributions.ContributionsContentProvider.java
org.wikimedia.commons.contributions.ContributionsListAdapter.java
org.wikimedia.commons.contributions.ContributionsListFragment.java
org.wikimedia.commons.contributions.ContributionsSyncAdapter.java
org.wikimedia.commons.contributions.ContributionsSyncService.java
org.wikimedia.commons.contributions.MediaListAdapter.java
org.wikimedia.commons.data.DBOpenHelper.java
org.wikimedia.commons.media.CategoryImagesLoader.java
org.wikimedia.commons.media.MediaDetailFragment.java
org.wikimedia.commons.media.MediaDetailPagerFragment.java
org.wikimedia.commons.media.MediaDetailSpacer.java
org.wikimedia.commons.modifications.CategoryModifier.java
org.wikimedia.commons.modifications.ModificationsContentProvider.java
org.wikimedia.commons.modifications.ModificationsSyncAdapter.java
org.wikimedia.commons.modifications.ModificationsSyncService.java
org.wikimedia.commons.modifications.ModifierSequence.java
org.wikimedia.commons.modifications.PageModifier.java
org.wikimedia.commons.modifications.TemplateRemoveModifier.java
org.wikimedia.commons.upload.MultipleShareActivity.java
org.wikimedia.commons.upload.MultipleUploadListFragment.java
org.wikimedia.commons.upload.ShareActivity.java
org.wikimedia.commons.upload.SingleUploadFragment.java
org.wikimedia.commons.upload.UploadController.java
org.wikimedia.commons.upload.UploadService.java