Search in sources :

Example 16 with ActionFailureException

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

the class GTaskManager method updateRemoteNode.

private void updateRemoteNode(Node node, Cursor c) throws NetworkFailureException {
    if (mCancelled) {
        return;
    }
    SqlNote sqlNote = new SqlNote(mContext, c);
    // update remotely
    node.setContentByLocalJSON(sqlNote.getContent());
    GTaskClient.getInstance().addUpdateNode(node);
    // update meta
    updateRemoteMeta(node.getGid(), sqlNote);
    // move task if necessary
    if (sqlNote.isNoteType()) {
        Task task = (Task) node;
        TaskList preParentList = task.getParent();
        String curParentGid = mNidToGid.get(sqlNote.getParentId());
        if (curParentGid == null) {
            Log.e(TAG, "cannot find task's parent tasklist");
            throw new ActionFailureException("cannot update remote task");
        }
        TaskList curParentList = mGTaskListHashMap.get(curParentGid);
        if (preParentList != curParentList) {
            preParentList.removeChildTask(task);
            curParentList.addChildTask(task);
            GTaskClient.getInstance().moveTask(task, preParentList, curParentList);
        }
    }
    // clear local modified flag
    sqlNote.resetLocalModified();
    sqlNote.commit(true);
}
Also used : Task(net.micode.notes.gtask.data.Task) ActionFailureException(net.micode.notes.gtask.exception.ActionFailureException) TaskList(net.micode.notes.gtask.data.TaskList) SqlNote(net.micode.notes.gtask.data.SqlNote)

Example 17 with ActionFailureException

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

the class GTaskManager method syncContent.

private void syncContent() throws NetworkFailureException {
    int syncType;
    Cursor c = null;
    String gid;
    Node node;
    mLocalDeleteIdMap.clear();
    if (mCancelled) {
        return;
    }
    // for local deleted note
    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) }, null);
        if (c != null) {
            while (c.moveToNext()) {
                gid = c.getString(SqlNote.GTASK_ID_COLUMN);
                node = mGTaskHashMap.get(gid);
                if (node != null) {
                    mGTaskHashMap.remove(gid);
                    doContentSync(Node.SYNC_ACTION_DEL_REMOTE, node, c);
                }
                mLocalDeleteIdMap.add(c.getLong(SqlNote.ID_COLUMN));
            }
        } else {
            Log.w(TAG, "failed to query trash folder");
        }
    } finally {
        if (c != null) {
            c.close();
            c = null;
        }
    }
    // sync folder first
    syncFolder();
    // for note existing in database
    try {
        c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, "(type=? AND parent_id<>?)", new String[] { String.valueOf(Notes.TYPE_NOTE), String.valueOf(Notes.ID_TRASH_FOLER) }, NoteColumns.TYPE + " DESC");
        if (c != null) {
            while (c.moveToNext()) {
                gid = c.getString(SqlNote.GTASK_ID_COLUMN);
                node = mGTaskHashMap.get(gid);
                if (node != null) {
                    mGTaskHashMap.remove(gid);
                    mGidToNid.put(gid, c.getLong(SqlNote.ID_COLUMN));
                    mNidToGid.put(c.getLong(SqlNote.ID_COLUMN), gid);
                    syncType = node.getSyncAction(c);
                } else {
                    if (c.getString(SqlNote.GTASK_ID_COLUMN).trim().length() == 0) {
                        // local add
                        syncType = Node.SYNC_ACTION_ADD_REMOTE;
                    } else {
                        // remote delete
                        syncType = Node.SYNC_ACTION_DEL_LOCAL;
                    }
                }
                doContentSync(syncType, node, c);
            }
        } else {
            Log.w(TAG, "failed to query existing note in database");
        }
    } finally {
        if (c != null) {
            c.close();
            c = null;
        }
    }
    // go through remaining items
    Iterator<Map.Entry<String, Node>> iter = mGTaskHashMap.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry<String, Node> entry = iter.next();
        node = entry.getValue();
        doContentSync(Node.SYNC_ACTION_ADD_LOCAL, node, null);
    }
    // clear local delete table
    if (!mCancelled) {
        if (!DataUtils.batchDeleteNotes(mContentResolver, mLocalDeleteIdMap)) {
            throw new ActionFailureException("failed to batch-delete local deleted notes");
        }
    }
    // refresh local sync id
    if (!mCancelled) {
        GTaskClient.getInstance().commitUpdate();
        refreshLocalSyncId();
    }
}
Also used : ActionFailureException(net.micode.notes.gtask.exception.ActionFailureException) Node(net.micode.notes.gtask.data.Node) Cursor(android.database.Cursor) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with ActionFailureException

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

the class Task method getUpdateAction.

public JSONObject getUpdateAction(int actionId) {
    JSONObject js = new JSONObject();
    try {
        // action_type
        js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE);
        // action_id
        js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);
        // id
        js.put(GTaskStringUtils.GTASK_JSON_ID, getGid());
        // entity_delta
        JSONObject entity = new JSONObject();
        entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName());
        if (getNotes() != null) {
            entity.put(GTaskStringUtils.GTASK_JSON_NOTES, getNotes());
        }
        entity.put(GTaskStringUtils.GTASK_JSON_DELETED, getDeleted());
        js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity);
    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new ActionFailureException("fail to generate task-update jsonobject");
    }
    return js;
}
Also used : ActionFailureException(net.micode.notes.gtask.exception.ActionFailureException) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException)

Example 19 with ActionFailureException

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

the class TaskList method getUpdateAction.

public JSONObject getUpdateAction(int actionId) {
    JSONObject js = new JSONObject();
    try {
        // action_type
        js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE);
        // action_id
        js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);
        // id
        js.put(GTaskStringUtils.GTASK_JSON_ID, getGid());
        // entity_delta
        JSONObject entity = new JSONObject();
        entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName());
        entity.put(GTaskStringUtils.GTASK_JSON_DELETED, getDeleted());
        js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity);
    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new ActionFailureException("fail to generate tasklist-update jsonobject");
    }
    return js;
}
Also used : ActionFailureException(net.micode.notes.gtask.exception.ActionFailureException) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException)

Example 20 with ActionFailureException

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

the class GTaskClient method deleteNode.

public void deleteNode(Node node) throws NetworkFailureException {
    commitUpdate();
    try {
        JSONObject jsPost = new JSONObject();
        JSONArray actionList = new JSONArray();
        // action_list
        node.setDeleted(true);
        actionList.put(node.getUpdateAction(getActionId()));
        jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
        // client_version
        jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
        postRequest(jsPost);
        mUpdateArray = null;
    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new ActionFailureException("delete node: handing jsonobject failed");
    }
}
Also used : ActionFailureException(net.micode.notes.gtask.exception.ActionFailureException) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

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