|
|
|
@ -0,0 +1,395 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.gtask.data;
|
|
|
|
|
|
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.data.Notes;
|
|
|
|
|
import net.micode.notes.data.Notes.DataColumns;
|
|
|
|
|
import net.micode.notes.data.Notes.DataConstants;
|
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
|
import net.micode.notes.gtask.exception.ActionFailureException;
|
|
|
|
|
import net.micode.notes.tool.GTaskStringUtils;
|
|
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class Task extends Node {
|
|
|
|
|
private static final String TAG = Task.class.getSimpleName();
|
|
|
|
|
|
|
|
|
|
private boolean mCompleted;//是否完成
|
|
|
|
|
|
|
|
|
|
private String mNotes;//备注信息
|
|
|
|
|
|
|
|
|
|
private JSONObject mMetaInfo;//将在实例中存储数据的类型
|
|
|
|
|
|
|
|
|
|
private Task mPriorSibling;//对应的优先兄弟Task的指针
|
|
|
|
|
|
|
|
|
|
private TaskList mParent;//所在的任务列表的指针
|
|
|
|
|
|
|
|
|
|
public Task() {
|
|
|
|
|
super();
|
|
|
|
|
mCompleted = false;
|
|
|
|
|
mNotes = null;
|
|
|
|
|
mPriorSibling = null;//TaskList中当前Task前面的Task的指针
|
|
|
|
|
mParent = null;//当前Task所在的TaskList
|
|
|
|
|
mMetaInfo = null;
|
|
|
|
|
}
|
|
|
|
|
//获取创建操作的JSON对象
|
|
|
|
|
public JSONObject getCreateAction(int actionId) {
|
|
|
|
|
JSONObject js = new JSONObject();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// action_type
|
|
|
|
|
// 设置action类型为创建
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE);
|
|
|
|
|
|
|
|
|
|
// action_id
|
|
|
|
|
// 设置action的ID
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);
|
|
|
|
|
|
|
|
|
|
// index
|
|
|
|
|
// 设置任务在父任务中的索引位置
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_INDEX, mParent.getChildTaskIndex(this));
|
|
|
|
|
|
|
|
|
|
// entity_delta
|
|
|
|
|
// 创建一个实体对象,用于存储任务的创建信息
|
|
|
|
|
JSONObject entity = new JSONObject();
|
|
|
|
|
// 设置任务名称
|
|
|
|
|
entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName());
|
|
|
|
|
// 设置任务创建者ID
|
|
|
|
|
entity.put(GTaskStringUtils.GTASK_JSON_CREATOR_ID, "null");
|
|
|
|
|
// 设置实体类型为任务
|
|
|
|
|
entity.put(GTaskStringUtils.GTASK_JSON_ENTITY_TYPE,
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_TYPE_TASK);
|
|
|
|
|
if (getNotes() != null) {
|
|
|
|
|
// 设置任务备注
|
|
|
|
|
entity.put(GTaskStringUtils.GTASK_JSON_NOTES, getNotes());
|
|
|
|
|
}
|
|
|
|
|
// 将实体对象添加到JSON对象中
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity);
|
|
|
|
|
|
|
|
|
|
// parent_id
|
|
|
|
|
// 设置父任务的ID
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_PARENT_ID, mParent.getGid());
|
|
|
|
|
|
|
|
|
|
// dest_parent_type
|
|
|
|
|
// 设置目标父类型的值
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT_TYPE,
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_TYPE_GROUP);
|
|
|
|
|
|
|
|
|
|
// list_id
|
|
|
|
|
// 设置列表ID
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_LIST_ID, mParent.getGid());
|
|
|
|
|
|
|
|
|
|
// prior_sibling_id
|
|
|
|
|
// 如果存在前一个兄弟任务,则设置其ID
|
|
|
|
|
if (mPriorSibling != null) {
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, mPriorSibling.getGid());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("fail to generate task-create jsonobject");
|
|
|
|
|
}
|
|
|
|
|
// 返回包含创建信息的JSON对象
|
|
|
|
|
return js;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public JSONObject getUpdateAction(int actionId) {
|
|
|
|
|
JSONObject js = new JSONObject();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// action_type
|
|
|
|
|
// 设置action类型为更新
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE);
|
|
|
|
|
|
|
|
|
|
// action_id
|
|
|
|
|
// 设置action的ID
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);
|
|
|
|
|
|
|
|
|
|
// id
|
|
|
|
|
// 设置任务的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());
|
|
|
|
|
// 将实体对象添加到JSON对象中
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
// 返回包含更新信息的JSON对象
|
|
|
|
|
return js;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setContentByRemoteJSON(JSONObject js) {
|
|
|
|
|
if (js != null) {
|
|
|
|
|
try {
|
|
|
|
|
// id
|
|
|
|
|
if (js.has(GTaskStringUtils.GTASK_JSON_ID)) {
|
|
|
|
|
setGid(js.getString(GTaskStringUtils.GTASK_JSON_ID));
|
|
|
|
|
}// 设置任务的ID
|
|
|
|
|
|
|
|
|
|
// last_modified
|
|
|
|
|
if (js.has(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)) {
|
|
|
|
|
setLastModified(js.getLong(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED));
|
|
|
|
|
}// 设置任务的最后修改时间
|
|
|
|
|
|
|
|
|
|
// name
|
|
|
|
|
if (js.has(GTaskStringUtils.GTASK_JSON_NAME)) {
|
|
|
|
|
setName(js.getString(GTaskStringUtils.GTASK_JSON_NAME));
|
|
|
|
|
}// 设置任务的名称
|
|
|
|
|
|
|
|
|
|
// notes
|
|
|
|
|
if (js.has(GTaskStringUtils.GTASK_JSON_NOTES)) {
|
|
|
|
|
setNotes(js.getString(GTaskStringUtils.GTASK_JSON_NOTES));
|
|
|
|
|
}// 设置任务的备注
|
|
|
|
|
|
|
|
|
|
// deleted
|
|
|
|
|
if (js.has(GTaskStringUtils.GTASK_JSON_DELETED)) {
|
|
|
|
|
setDeleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_DELETED));
|
|
|
|
|
}// 设置任务是否已删除
|
|
|
|
|
|
|
|
|
|
// completed
|
|
|
|
|
if (js.has(GTaskStringUtils.GTASK_JSON_COMPLETED)) {
|
|
|
|
|
setCompleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_COMPLETED));
|
|
|
|
|
}// 设置任务是否已完成
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("fail to get task content from jsonobject");
|
|
|
|
|
}// 抛出异常,表示从JSON对象获取任务内容失败
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setContentByLocalJSON(JSONObject js) {
|
|
|
|
|
// 检查传入的 JSON 对象是否为空,或者是否包含必要的键值
|
|
|
|
|
if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE)
|
|
|
|
|
|| !js.has(GTaskStringUtils.META_HEAD_DATA)) {
|
|
|
|
|
Log.w(TAG, "setContentByLocalJSON: nothing is avaiable");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 获取 note 和 dataArray 对象
|
|
|
|
|
JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
|
|
|
|
|
JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA);
|
|
|
|
|
// 检查 note 的类型是否为 Notes.TYPE_NOTE
|
|
|
|
|
if (note.getInt(NoteColumns.TYPE) != Notes.TYPE_NOTE) {
|
|
|
|
|
Log.e(TAG, "invalid type");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 遍历 dataArray,查找 MIME_TYPE 为 DataConstants.NOTE 的数据对象
|
|
|
|
|
for (int i = 0; i < dataArray.length(); i++) {
|
|
|
|
|
JSONObject data = dataArray.getJSONObject(i);
|
|
|
|
|
if (TextUtils.equals(data.getString(DataColumns.MIME_TYPE), DataConstants.NOTE)) {
|
|
|
|
|
// 设置名称为找到的数据对象的 CONTENT 字段的值
|
|
|
|
|
setName(data.getString(DataColumns.CONTENT));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public JSONObject getLocalJSONFromContent() {
|
|
|
|
|
// 获取任务名称
|
|
|
|
|
String name = getName();
|
|
|
|
|
try {
|
|
|
|
|
// 如果元信息为空,说明这是一个从网页创建的新任务
|
|
|
|
|
if (mMetaInfo == null) {
|
|
|
|
|
// new task created from web
|
|
|
|
|
// 如果任务名称为空,记录警告日志并返回null
|
|
|
|
|
if (name == null) {
|
|
|
|
|
Log.w(TAG, "the note seems to be an empty one");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
// 创建一个新的JSON对象用于存储任务信息
|
|
|
|
|
JSONObject js = new JSONObject();
|
|
|
|
|
JSONObject note = new JSONObject();
|
|
|
|
|
JSONArray dataArray = new JSONArray();
|
|
|
|
|
JSONObject data = new JSONObject();
|
|
|
|
|
// 将任务名称放入data对象的CONTENT字段中
|
|
|
|
|
data.put(DataColumns.CONTENT, name);
|
|
|
|
|
// 将data对象添加到dataArray中
|
|
|
|
|
dataArray.put(data);
|
|
|
|
|
// 将dataArray放入js对象的META_HEAD_DATA字段中
|
|
|
|
|
js.put(GTaskStringUtils.META_HEAD_DATA, dataArray);
|
|
|
|
|
// 设置note对象的TYPE字段为Notes.TYPE_NOTE
|
|
|
|
|
note.put(NoteColumns.TYPE, Notes.TYPE_NOTE);
|
|
|
|
|
// 将note对象放入js对象的META_HEAD_NOTE字段中
|
|
|
|
|
js.put(GTaskStringUtils.META_HEAD_NOTE, note);
|
|
|
|
|
// 返回包含任务信息的js对象
|
|
|
|
|
return js;
|
|
|
|
|
} else {
|
|
|
|
|
// synced task
|
|
|
|
|
// 如果元信息不为空,说明这是一个已同步的任务
|
|
|
|
|
// 获取note对象和dataArray对象
|
|
|
|
|
JSONObject note = mMetaInfo.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
|
|
|
|
|
JSONArray dataArray = mMetaInfo.getJSONArray(GTaskStringUtils.META_HEAD_DATA);
|
|
|
|
|
// 遍历dataArray,找到类型为DataConstants.NOTE的数据对象
|
|
|
|
|
for (int i = 0; i < dataArray.length(); i++) {
|
|
|
|
|
JSONObject data = dataArray.getJSONObject(i);
|
|
|
|
|
if (TextUtils.equals(data.getString(DataColumns.MIME_TYPE), DataConstants.NOTE)) {
|
|
|
|
|
// 将任务名称放入找到的data对象的CONTENT字段中
|
|
|
|
|
data.put(DataColumns.CONTENT, getName());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 设置note对象的TYPE字段为Notes.TYPE_NOTE
|
|
|
|
|
note.put(NoteColumns.TYPE, Notes.TYPE_NOTE);
|
|
|
|
|
// 返回包含任务信息的mMetaInfo对象
|
|
|
|
|
return mMetaInfo;
|
|
|
|
|
}
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
// 如果发生异常,记录错误日志并返回null
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 设置元信息,将MetaData对象中的notes转换为JSONObject并存储到mMetaInfo中
|
|
|
|
|
public void setMetaInfo(MetaData metaData) {
|
|
|
|
|
if (metaData != null && metaData.getNotes() != null) {
|
|
|
|
|
try {
|
|
|
|
|
mMetaInfo = new JSONObject(metaData.getNotes());
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.w(TAG, e.toString());
|
|
|
|
|
mMetaInfo = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 根据Cursor对象获取同步操作类型
|
|
|
|
|
public int getSyncAction(Cursor c) {
|
|
|
|
|
try {
|
|
|
|
|
JSONObject noteInfo = null;
|
|
|
|
|
if (mMetaInfo != null && mMetaInfo.has(GTaskStringUtils.META_HEAD_NOTE)) {
|
|
|
|
|
noteInfo = mMetaInfo.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (noteInfo == null) {
|
|
|
|
|
Log.w(TAG, "it seems that note meta has been deleted");
|
|
|
|
|
return SYNC_ACTION_UPDATE_REMOTE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!noteInfo.has(NoteColumns.ID)) {
|
|
|
|
|
Log.w(TAG, "remote note id seems to be deleted");
|
|
|
|
|
return SYNC_ACTION_UPDATE_LOCAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// validate the note id now
|
|
|
|
|
// 验证note id是否匹配
|
|
|
|
|
if (c.getLong(SqlNote.ID_COLUMN) != noteInfo.getLong(NoteColumns.ID)) {
|
|
|
|
|
Log.w(TAG, "note id doesn't match");
|
|
|
|
|
return SYNC_ACTION_UPDATE_LOCAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c.getInt(SqlNote.LOCAL_MODIFIED_COLUMN) == 0) {
|
|
|
|
|
// there is no local update
|
|
|
|
|
// 没有本地更新
|
|
|
|
|
if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) {
|
|
|
|
|
// no update both side
|
|
|
|
|
// 两边都没有更新
|
|
|
|
|
return SYNC_ACTION_NONE;
|
|
|
|
|
} else {
|
|
|
|
|
// apply remote to local
|
|
|
|
|
// 应用远程到本地
|
|
|
|
|
return SYNC_ACTION_UPDATE_LOCAL;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// validate gtask id
|
|
|
|
|
// 验证gtask id是否匹配
|
|
|
|
|
if (!c.getString(SqlNote.GTASK_ID_COLUMN).equals(getGid())) {
|
|
|
|
|
Log.e(TAG, "gtask id doesn't match");
|
|
|
|
|
return SYNC_ACTION_ERROR;
|
|
|
|
|
}
|
|
|
|
|
if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) {
|
|
|
|
|
// local modification only
|
|
|
|
|
// 只有本地修改
|
|
|
|
|
return SYNC_ACTION_UPDATE_REMOTE;
|
|
|
|
|
} else {
|
|
|
|
|
return SYNC_ACTION_UPDATE_CONFLICT;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return SYNC_ACTION_ERROR;
|
|
|
|
|
}
|
|
|
|
|
// 判断任务是否有价值保存
|
|
|
|
|
public boolean isWorthSaving() {
|
|
|
|
|
return mMetaInfo != null || (getName() != null && getName().trim().length() > 0)
|
|
|
|
|
|| (getNotes() != null && getNotes().trim().length() > 0);
|
|
|
|
|
}
|
|
|
|
|
// 设置任务完成状态
|
|
|
|
|
public void setCompleted(boolean completed) {
|
|
|
|
|
this.mCompleted = completed;
|
|
|
|
|
}
|
|
|
|
|
// 设置任务备注
|
|
|
|
|
public void setNotes(String notes) {
|
|
|
|
|
this.mNotes = notes;
|
|
|
|
|
}
|
|
|
|
|
// 设置前一个兄弟任务
|
|
|
|
|
public void setPriorSibling(Task priorSibling) {
|
|
|
|
|
this.mPriorSibling = priorSibling;
|
|
|
|
|
}
|
|
|
|
|
// 设置任务所属的任务列表
|
|
|
|
|
public void setParent(TaskList parent) {
|
|
|
|
|
this.mParent = parent;
|
|
|
|
|
}
|
|
|
|
|
// 获取任务完成状态
|
|
|
|
|
public boolean getCompleted() {
|
|
|
|
|
return this.mCompleted;
|
|
|
|
|
}
|
|
|
|
|
// 获取任务备注
|
|
|
|
|
public String getNotes() {
|
|
|
|
|
return this.mNotes;
|
|
|
|
|
}
|
|
|
|
|
// 获取前一个兄弟任务
|
|
|
|
|
public Task getPriorSibling() {
|
|
|
|
|
return this.mPriorSibling;
|
|
|
|
|
}
|
|
|
|
|
// 获取任务所属的任务列表
|
|
|
|
|
public TaskList getParent() {
|
|
|
|
|
return this.mParent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|