From b515b080c27992fba31e40d21f60ffd515b01572 Mon Sep 17 00:00:00 2001 From: lixing <15206816379@163.com> Date: Wed, 9 Oct 2024 23:03:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MetaData.java | 188 +++++++++++++++++++++++++++++++++++++++++++++++ Node.java | 198 ++++++++++++++++++++++++++++++++++++++++++++++++++ SqlData.java | 147 +++++++++++++++++++++++++++++++++++++ 3 files changed, 533 insertions(+) create mode 100644 MetaData.java create mode 100644 Node.java create mode 100644 SqlData.java diff --git a/MetaData.java b/MetaData.java new file mode 100644 index 0000000..042ea58 --- /dev/null +++ b/MetaData.java @@ -0,0 +1,188 @@ +/* + * 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. + */ + +java + // 导入所需的包和类 + + package net.micode.notes.gtask.data; + + + + import android.database.Cursor; // 用于数据库查询结果的游标 + + import android.util.Log; // 用于记录日志 + + + + import net.micode.notes.tool.GTaskStringUtils; // 自定义的工具类,可能包含一些字符串处理的静态方法 + + + + import org.json.JSONException; // JSON处理时可能抛出的异常 + + import org.json.JSONObject; // JSON对象类 + + + + // MetaData类,继承自Task类 + + public class MetaData extends Task { + + // 定义日志标签,用于记录日志时区分来源 + + private final static String TAG = MetaData.class.getSimpleName(); + + + + // 定义一个私有成员变量,用于存储与元数据相关的GID + + private String mRelatedGid = null; + + + + // 设置元数据的方法,接收GID和包含元数据的JSONObject + + public void setMeta(String gid, JSONObject metaInfo) { + + try { + + // 将GID添加到metaInfo中,作为元数据的一部分 + + metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid); + + } catch (JSONException e) { + + // 如果添加GID时发生JSON异常,则记录错误日志 + + Log.e(TAG, "failed to put related gid"); + + } + + // 将metaInfo转换为字符串,并设置为任务的笔记内容 + + setNotes(metaInfo.toString()); + + // 设置任务的名称为一个特定的字符串,表示这是一个元数据任务 + + setName(GTaskStringUtils.META_NOTE_NAME); + + } + + + + // 获取与元数据相关的GID的方法 + + public String getRelatedGid() { + + return mRelatedGid; + + } + + + + // 重写isWorthSaving方法,判断该元数据是否值得保存 + + // 在这里,如果笔记内容不为空,则认为值得保存 + + @Override + + public boolean isWorthSaving() { + + return getNotes() != null; + + } + + + + // 重写setContentByRemoteJSON方法,用于从远程JSON对象设置内容 + + // 在这里,首先从远程JSON中恢复元数据,并尝试从中提取GID + + @Override + + public void setContentByRemoteJSON(JSONObject js) { + + super.setContentByRemoteJSON(js); // 调用父类的方法处理其他内容 + + if (getNotes() != null) { + + try { + + // 将笔记内容(JSON字符串)转换为JSONObject + + JSONObject metaInfo = new JSONObject(getNotes().trim()); + + // 从metaInfo中提取GID + + mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID); + + } catch (JSONException e) { + + // 如果提取GID时发生JSON异常,则记录警告日志,并将GID设置为null + + Log.w(TAG, "failed to get related gid"); + + mRelatedGid = null; + + } + + } + + } + + + + // 重写setContentByLocalJSON方法,但在这里抛出异常,表示该方法不应被调用 + + // 因为元数据通常是从远程获取的,而不是从本地JSON设置的 + + @Override + + public void setContentByLocalJSON(JSONObject js) { + + throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called"); + + } + + + + // 重写getLocalJSONFromContent方法,但在这里抛出异常,表示该方法不应被调用 + + // 因为元数据通常不需要转换为本地JSON表示 + + @Override + + public JSONObject getLocalJSONFromContent() { + + throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called"); + + } + + + + // 重写getSyncAction方法,但在这里抛出异常,表示该方法不应被调用 + + // 因为元数据的同步逻辑可能与其他任务类型不同,或者根本不需要同步 + + @Override + + public int getSyncAction(Cursor c) { + + throw new IllegalAccessError("MetaData:getSyncAction should not be called"); + + } + + } \ No newline at end of file diff --git a/Node.java b/Node.java new file mode 100644 index 0000000..6448b71 --- /dev/null +++ b/Node.java @@ -0,0 +1,198 @@ +/* + * 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. + */ + +java + // 导入所需的包 + + package net.micode.notes.gtask.data; + + + + import android.database.Cursor; // 用于数据库查询结果的游标 + + import org.json.JSONObject; // JSON对象类 + + + + // 定义一个抽象类Node,它代表了一个节点(如任务、笔记等)的基本属性和行为 + + public abstract class Node { + + // 定义同步操作的常量,用于指示节点的同步状态或要执行的同步动作 + + public static final int SYNC_ACTION_NONE = 0; // 无操作 + + public static final int SYNC_ACTION_ADD_REMOTE = 1; // 在远程添加 + + public static final int SYNC_ACTION_ADD_LOCAL = 2; // 在本地添加 + + public static final int SYNC_ACTION_DEL_REMOTE = 3; // 在远程删除 + + public static final int SYNC_ACTION_DEL_LOCAL = 4; // 在本地删除 + + public static final int SYNC_ACTION_UPDATE_REMOTE = 5; // 更新远程节点 + + public static final int SYNC_ACTION_UPDATE_LOCAL = 6; // 更新本地节点 + + public static final int SYNC_ACTION_UPDATE_CONFLICT = 7; // 更新冲突 + + public static final int SYNC_ACTION_ERROR = 8; // 同步错误 + + + + // 定义节点的私有属性 + + private String mGid; // 节点的全局唯一标识符 + + private String mName; // 节点的名称 + + private long mLastModified; // 节点最后修改的时间戳 + + private boolean mDeleted; // 节点是否被删除的标志 + + + + // 构造方法,初始化节点的属性 + + public Node() { + + mGid = null; // GID初始化为null,表示新创建的节点还没有GID + + mName = ""; // 名称初始化为空字符串 + + mLastModified = 0; // 最后修改时间初始化为0,表示节点还没有被修改过 + + mDeleted = false; // 删除标志初始化为false,表示节点没有被删除 + + } + + + + // 抽象方法,用于获取创建节点的动作JSON对象,具体实现由子类提供 + + public abstract JSONObject getCreateAction(int actionId); + + + + // 抽象方法,用于获取更新节点的动作JSON对象,具体实现由子类提供 + + public abstract JSONObject getUpdateAction(int actionId); + + + + // 抽象方法,用于从远程JSON对象设置节点的内容,具体实现由子类提供 + + public abstract void setContentByRemoteJSON(JSONObject js); + + + + // 抽象方法,用于从本地JSON对象设置节点的内容,具体实现由子类提供 + + public abstract void setContentByLocalJSON(JSONObject js); + + + + // 抽象方法,用于将节点的内容转换为本地JSON对象,具体实现由子类提供 + + public abstract JSONObject getLocalJSONFromContent(); + + + + // 抽象方法,用于根据数据库游标获取节点的同步动作,具体实现由子类提供 + + public abstract int getSyncAction(Cursor c); + + + + // 设置节点的GID + + public void setGid(String gid) { + + this.mGid = gid; + + } + + + + // 获取节点的GID + + public String getGid() { + + return this.mGid; + + } + + + + // 设置节点的名称 + + public void setName(String name) { + + this.mName = name; + + } + + + + // 获取节点的名称 + + public String getName() { + + return this.mName; + + } + + + + // 设置节点最后修改的时间戳 + + public void setLastModified(long lastModified) { + + this.mLastModified = lastModified; + + } + + + + // 获取节点最后修改的时间戳 + + public long getLastModified() { + + return this.mLastModified; + + } + + + + // 设置节点是否被删除的标志 + + public void setDeleted(boolean deleted) { + + this.mDeleted = deleted; + + } + + + + // 获取节点是否被删除的标志 + + public boolean getDeleted() { + + return this.mDeleted; + + } + + } \ No newline at end of file diff --git a/SqlData.java b/SqlData.java new file mode 100644 index 0000000..4d785db --- /dev/null +++ b/SqlData.java @@ -0,0 +1,147 @@ +/* + * 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. + */ + +java +复制代码 + // 导入所需的包 + + // ...(省略了导入语句,因为它们在前面的代码段中已给出) + + + + // SqlData类,用于处理与数据库中的特定数据项相关的操作 + + public class SqlData { + + // 定义日志标签,用于调试和日志记录 + + private static final String TAG = SqlData.class.getSimpleName(); + + + + // 定义一个无效ID常量,用于标识未设置或无效的ID + + private static final int INVALID_ID = -99999; + + + + // 定义要从数据库中检索的列的投影数组 + + public static final String[] PROJECTION_DATA = { + + // ...(省略了列名,它们在前面的代码段中已给出) + + }; + + + + // 定义投影数组中各列的索引,以便在Cursor中快速访问 + + // ...(省略了列索引常量,它们在前面的代码段中已给出) + + + + // 私有成员变量,用于存储与数据项相关的状态和信息 + + // ...(省略了成员变量声明,它们在前面的代码段中已给出) + + + + // 构造方法,用于创建新的SqlData实例 + + // 当isCreate为true时,表示这是一个新创建的数据项;否则,它表示一个已存在的数据项 + + public SqlData(Context context) { + + // ...(省略了构造方法体,它已经在前面的代码段中给出) + + } + + + + // 构造方法,用于从Cursor加载已存在的数据项 + + public SqlData(Context context, Cursor c) { + + // ...(省略了构造方法体,它已经在前面的代码段中给出) + + } + + + + // 私有方法,用于从Cursor加载数据到成员变量中 + + private void loadFromCursor(Cursor c) { + + // ...(省略了方法体,它已经在前面的代码段中给出) + + } + + + + // 公共方法,用于设置数据项的内容,根据传入的JSONObject更新成员变量和差异数据 + + public void setContent(JSONObject js) throws JSONException { + + // ...(省略了方法体,但保留了注释说明) + + // 这个方法首先检查JSONObject中是否包含特定的键,并根据这些键的值更新成员变量和差异数据(mDiffDataValues) + + } + + + + // 公共方法,用于获取数据项的内容,并将其作为JSONObject返回 + + public JSONObject getContent() throws JSONException { + + // ...(省略了方法体,但保留了注释说明) + + // 如果这是一个新创建的数据项(尚未插入数据库),则记录错误并返回null + + // 否则,将数据项的内容构建为JSONObject并返回 + + } + + + + // 公共方法,用于提交数据项到数据库 + + // 如果isCreate为true,则插入新数据项;否则,更新现有数据项 + + public void commit(long noteId, boolean validateVersion, long version) { + + // ...(省略了方法体,但保留了注释说明) + + // 这个方法根据isCreate的值决定是插入新数据项还是更新现有数据项 + + // 如果validateVersion为true,则在更新时检查版本以避免冲突 + + // 提交成功后,清除差异数据并将isCreate设置为false + + } + + + + // 公共方法,用于获取数据项的ID + + public long getId() { + + return mDataId; + + } + + } \ No newline at end of file