Search in sources :

Example 6 with ActionFailureException

use of net.micode.notes.gtask.exception.ActionFailureException in project Notes by MiCode.

the class GTaskManager method initGTaskList.

private void initGTaskList() throws NetworkFailureException {
    if (mCancelled)
        return;
    GTaskClient client = GTaskClient.getInstance();
    try {
        JSONArray jsTaskLists = client.getTaskLists();
        // init meta list first
        mMetaList = null;
        for (int i = 0; i < jsTaskLists.length(); i++) {
            JSONObject object = jsTaskLists.getJSONObject(i);
            String gid = object.getString(GTaskStringUtils.GTASK_JSON_ID);
            String name = object.getString(GTaskStringUtils.GTASK_JSON_NAME);
            if (name.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META)) {
                mMetaList = new TaskList();
                mMetaList.setContentByRemoteJSON(object);
                // load meta data
                JSONArray jsMetas = client.getTaskList(gid);
                for (int j = 0; j < jsMetas.length(); j++) {
                    object = (JSONObject) jsMetas.getJSONObject(j);
                    MetaData metaData = new MetaData();
                    metaData.setContentByRemoteJSON(object);
                    if (metaData.isWorthSaving()) {
                        mMetaList.addChildTask(metaData);
                        if (metaData.getGid() != null) {
                            mMetaHashMap.put(metaData.getRelatedGid(), metaData);
                        }
                    }
                }
            }
        }
        // create meta list if not existed
        if (mMetaList == null) {
            mMetaList = new TaskList();
            mMetaList.setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META);
            GTaskClient.getInstance().createTaskList(mMetaList);
        }
        // init task list
        for (int i = 0; i < jsTaskLists.length(); i++) {
            JSONObject object = jsTaskLists.getJSONObject(i);
            String gid = object.getString(GTaskStringUtils.GTASK_JSON_ID);
            String name = object.getString(GTaskStringUtils.GTASK_JSON_NAME);
            if (name.startsWith(GTaskStringUtils.MIUI_FOLDER_PREFFIX) && !name.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META)) {
                TaskList tasklist = new TaskList();
                tasklist.setContentByRemoteJSON(object);
                mGTaskListHashMap.put(gid, tasklist);
                mGTaskHashMap.put(gid, tasklist);
                // load tasks
                JSONArray jsTasks = client.getTaskList(gid);
                for (int j = 0; j < jsTasks.length(); j++) {
                    object = (JSONObject) jsTasks.getJSONObject(j);
                    gid = object.getString(GTaskStringUtils.GTASK_JSON_ID);
                    Task task = new Task();
                    task.setContentByRemoteJSON(object);
                    if (task.isWorthSaving()) {
                        task.setMetaInfo(mMetaHashMap.get(gid));
                        tasklist.addChildTask(task);
                        mGTaskHashMap.put(gid, task);
                    }
                }
            }
        }
    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new ActionFailureException("initGTaskList: handing JSONObject failed");
    }
}
Also used : Task(net.micode.notes.gtask.data.Task) ActionFailureException(net.micode.notes.gtask.exception.ActionFailureException) JSONObject(org.json.JSONObject) MetaData(net.micode.notes.gtask.data.MetaData) TaskList(net.micode.notes.gtask.data.TaskList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 7 with ActionFailureException

use of net.micode.notes.gtask.exception.ActionFailureException in project Notes by MiCode.

the class GTaskManager method refreshLocalSyncId.

private void refreshLocalSyncId() throws NetworkFailureException {
    if (mCancelled) {
        return;
    }
    // get the latest gtask list
    mGTaskHashMap.clear();
    mGTaskListHashMap.clear();
    mMetaHashMap.clear();
    initGTaskList();
    Cursor c = null;
    try {
        c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, "(type<>? AND parent_id<>?)", new String[] { String.valueOf(Notes.TYPE_SYSTEM), String.valueOf(Notes.ID_TRASH_FOLER) }, NoteColumns.TYPE + " DESC");
        if (c != null) {
            while (c.moveToNext()) {
                String gid = c.getString(SqlNote.GTASK_ID_COLUMN);
                Node node = mGTaskHashMap.get(gid);
                if (node != null) {
                    mGTaskHashMap.remove(gid);
                    ContentValues values = new ContentValues();
                    values.put(NoteColumns.SYNC_ID, node.getLastModified());
                    mContentResolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(SqlNote.ID_COLUMN)), values, null, null);
                } else {
                    Log.e(TAG, "something is missed");
                    throw new ActionFailureException("some local items don't have gid after sync");
                }
            }
        } else {
            Log.w(TAG, "failed to query local note to refresh sync id");
        }
    } finally {
        if (c != null) {
            c.close();
            c = null;
        }
    }
}
Also used : ContentValues(android.content.ContentValues) ActionFailureException(net.micode.notes.gtask.exception.ActionFailureException) Node(net.micode.notes.gtask.data.Node) Cursor(android.database.Cursor)

Example 8 with ActionFailureException

use of net.micode.notes.gtask.exception.ActionFailureException in project Notes by MiCode.

the class GTaskManager method updateLocalNode.

private void updateLocalNode(Node node, Cursor c) throws NetworkFailureException {
    if (mCancelled) {
        return;
    }
    SqlNote sqlNote;
    // update the note locally
    sqlNote = new SqlNote(mContext, c);
    sqlNote.setContent(node.getLocalJSONFromContent());
    Long parentId = (node instanceof Task) ? mGidToNid.get(((Task) node).getParent().getGid()) : new Long(Notes.ID_ROOT_FOLDER);
    if (parentId == null) {
        Log.e(TAG, "cannot find task's parent id locally");
        throw new ActionFailureException("cannot update local node");
    }
    sqlNote.setParentId(parentId.longValue());
    sqlNote.commit(true);
    // update meta info
    updateRemoteMeta(node.getGid(), sqlNote);
}
Also used : Task(net.micode.notes.gtask.data.Task) ActionFailureException(net.micode.notes.gtask.exception.ActionFailureException) SqlNote(net.micode.notes.gtask.data.SqlNote)

Example 9 with ActionFailureException

use of net.micode.notes.gtask.exception.ActionFailureException in project Notes by MiCode.

the class GTaskManager method addRemoteNode.

private void addRemoteNode(Node node, Cursor c) throws NetworkFailureException {
    if (mCancelled) {
        return;
    }
    SqlNote sqlNote = new SqlNote(mContext, c);
    Node n;
    // update remotely
    if (sqlNote.isNoteType()) {
        Task task = new Task();
        task.setContentByLocalJSON(sqlNote.getContent());
        String parentGid = mNidToGid.get(sqlNote.getParentId());
        if (parentGid == null) {
            Log.e(TAG, "cannot find task's parent tasklist");
            throw new ActionFailureException("cannot add remote task");
        }
        mGTaskListHashMap.get(parentGid).addChildTask(task);
        GTaskClient.getInstance().createTask(task);
        n = (Node) task;
        // add meta
        updateRemoteMeta(task.getGid(), sqlNote);
    } else {
        TaskList tasklist = null;
        // we need to skip folder if it has already existed
        String folderName = GTaskStringUtils.MIUI_FOLDER_PREFFIX;
        if (sqlNote.getId() == Notes.ID_ROOT_FOLDER)
            folderName += GTaskStringUtils.FOLDER_DEFAULT;
        else if (sqlNote.getId() == Notes.ID_CALL_RECORD_FOLDER)
            folderName += GTaskStringUtils.FOLDER_CALL_NOTE;
        else
            folderName += sqlNote.getSnippet();
        Iterator<Map.Entry<String, TaskList>> iter = mGTaskListHashMap.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry<String, TaskList> entry = iter.next();
            String gid = entry.getKey();
            TaskList list = entry.getValue();
            if (list.getName().equals(folderName)) {
                tasklist = list;
                if (mGTaskHashMap.containsKey(gid)) {
                    mGTaskHashMap.remove(gid);
                }
                break;
            }
        }
        // no match we can add now
        if (tasklist == null) {
            tasklist = new TaskList();
            tasklist.setContentByLocalJSON(sqlNote.getContent());
            GTaskClient.getInstance().createTaskList(tasklist);
            mGTaskListHashMap.put(tasklist.getGid(), tasklist);
        }
        n = (Node) tasklist;
    }
    // update local note
    sqlNote.setGtaskId(n.getGid());
    sqlNote.commit(false);
    sqlNote.resetLocalModified();
    sqlNote.commit(true);
    // gid-id mapping
    mGidToNid.put(n.getGid(), sqlNote.getId());
    mNidToGid.put(sqlNote.getId(), n.getGid());
}
Also used : Task(net.micode.notes.gtask.data.Task) ActionFailureException(net.micode.notes.gtask.exception.ActionFailureException) Node(net.micode.notes.gtask.data.Node) TaskList(net.micode.notes.gtask.data.TaskList) HashMap(java.util.HashMap) Map(java.util.Map) SqlNote(net.micode.notes.gtask.data.SqlNote)

Example 10 with ActionFailureException

use of net.micode.notes.gtask.exception.ActionFailureException in project Notes by MiCode.

the class SqlNote method commit.

public void commit(boolean validateVersion) {
    if (mIsCreate) {
        if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) {
            mDiffNoteValues.remove(NoteColumns.ID);
        }
        Uri uri = mContentResolver.insert(Notes.CONTENT_NOTE_URI, mDiffNoteValues);
        try {
            mId = Long.valueOf(uri.getPathSegments().get(1));
        } catch (NumberFormatException e) {
            Log.e(TAG, "Get note id error :" + e.toString());
            throw new ActionFailureException("create note failed");
        }
        if (mId == 0) {
            throw new IllegalStateException("Create thread id failed");
        }
        if (mType == Notes.TYPE_NOTE) {
            for (SqlData sqlData : mDataList) {
                sqlData.commit(mId, false, -1);
            }
        }
    } else {
        if (mId <= 0 && mId != Notes.ID_ROOT_FOLDER && mId != Notes.ID_CALL_RECORD_FOLDER) {
            Log.e(TAG, "No such note");
            throw new IllegalStateException("Try to update note with invalid id");
        }
        if (mDiffNoteValues.size() > 0) {
            mVersion++;
            int result = 0;
            if (!validateVersion) {
                result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "(" + NoteColumns.ID + "=?)", new String[] { String.valueOf(mId) });
            } else {
                result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "(" + NoteColumns.ID + "=?) AND (" + NoteColumns.VERSION + "<=?)", new String[] { String.valueOf(mId), String.valueOf(mVersion) });
            }
            if (result == 0) {
                Log.w(TAG, "there is no update. maybe user updates note when syncing");
            }
        }
        if (mType == Notes.TYPE_NOTE) {
            for (SqlData sqlData : mDataList) {
                sqlData.commit(mId, validateVersion, mVersion);
            }
        }
    }
    // refresh local info
    loadFromCursor(mId);
    if (mType == Notes.TYPE_NOTE)
        loadDataContent();
    mDiffNoteValues.clear();
    mIsCreate = false;
}
Also used : ActionFailureException(net.micode.notes.gtask.exception.ActionFailureException) Uri(android.net.Uri)

Aggregations

ActionFailureException (net.micode.notes.gtask.exception.ActionFailureException)22 JSONException (org.json.JSONException)14 JSONObject (org.json.JSONObject)14 JSONArray (org.json.JSONArray)7 Task (net.micode.notes.gtask.data.Task)5 SqlNote (net.micode.notes.gtask.data.SqlNote)4 TaskList (net.micode.notes.gtask.data.TaskList)4 Node (net.micode.notes.gtask.data.Node)3 Cursor (android.database.Cursor)2 Uri (android.net.Uri)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 MetaData (net.micode.notes.gtask.data.MetaData)2 NetworkFailureException (net.micode.notes.gtask.exception.NetworkFailureException)2 HttpResponse (org.apache.http.HttpResponse)2 ClientProtocolException (org.apache.http.client.ClientProtocolException)2 ContentValues (android.content.ContentValues)1 LinkedList (java.util.LinkedList)1 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)1