You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
12 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 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.content.ContentResolver; // 导入类用于访问应用的Content Provider
import android.content.ContentUris; // 导入类用于处理Uri
import android.content.ContentValues; // 导入类用于操作Content Values
import android.content.Context; // 导入类:提供应用程序全局信息的接口
import android.database.Cursor; // 导入类:用于操作数据库查询结果的指针
import android.net.Uri; // 导入类:用于标识唯一内容的位置
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.data.NotesDatabaseHelper.TABLE; // 导入类:定义数据库表名
import net.micode.notes.gtask.exception.ActionFailureException; // 导入类:自定义异常类
import org.json.JSONException; // 导入类处理JSON异常
import org.json.JSONObject; // 导入类处理JSON对象
public class SqlData { // SqlData类声明
private static final String TAG = SqlData.class.getSimpleName(); // 静态常量TAG用于日志输出
private static final int INVALID_ID = -99999; // 静态常量INVALID_ID表示无效的ID
public static final String[] PROJECTION_DATA = new String[]{ // 数组PROJECTION_DATA定义查询数据的列名
DataColumns.ID, DataColumns.MIME_TYPE, DataColumns.CONTENT, DataColumns.DATA1,
DataColumns.DATA3
};
public static final int DATA_ID_COLUMN = 0; // 常量DATA_ID_COLUMN表示数据ID所在列的索引
public static final int DATA_MIME_TYPE_COLUMN = 1; // 常量DATA_MIME_TYPE_COLUMN表示数据MIME类型所在列的索引
public static final int DATA_CONTENT_COLUMN = 2; // 常量DATA_CONTENT_COLUMN表示数据内容所在列的索引
public static final int DATA_CONTENT_DATA_1_COLUMN = 3; // 常量DATA_CONTENT_DATA_1_COLUMN表示数据内容Data1所在列的索引
public static final int DATA_CONTENT_DATA_3_COLUMN = 4; // 常量DATA_CONTENT_DATA_3_COLUMN表示数据内容Data3所在列的索引
private ContentResolver mContentResolver; // 私有属性mContentResolver用于访问应用的Content Provider
private boolean mIsCreate; // 私有属性mIsCreate表示是否创建新的数据
private long mDataId; // 私有属性mDataId表示数据的ID
private String mDataMimeType; // 私有属性mDataMimeType表示数据的MIME类型
private String mDataContent; // 私有属性mDataContent表示数据的内容
private long mDataContentData1; // 私有属性mDataContentData1表示数据内容Data1
private String mDataContentData3; // 私有属性mDataContentData3表示数据内容Data3
private ContentValues mDiffDataValues; // 私有属性mDiffDataValues用于存储不同的数据值
public SqlData(Context context) { // 构造方法接收Context参数
mContentResolver = context.getContentResolver(); // 初始化mContentResolver属性
mIsCreate = true; // 初始化mIsCreate属性为true
mDataId = INVALID_ID; // 初始化mDataId属性为INVALID_ID
mDataMimeType = DataConstants.NOTE; // 初始化mDataMimeType属性为NOTE
mDataContent = ""; // 初始化mDataContent属性为空字符串
mDataContentData1 = 0; // 初始化mDataContentData1属性为0
mDataContentData3 = ""; // 初始化mDataContentData3属性为空字符串
mDiffDataValues = new ContentValues(); // 初始化mDiffDataValues属性为空的ContentValues对象
}
public SqlData(Context context, Cursor c) { // 构造方法接收Context和Cursor参数
mContentResolver = context.getContentResolver(); // 初始化mContentResolver属性
mIsCreate = false; // 初始化mIsCreate属性为false
loadFromCursor(c); // 调用loadFromCursor方法从Cursor中加载数据
mDiffDataValues = new ContentValues(); // 初始化mDiffDataValues属性为空的ContentValues对象
}
private void loadFromCursor(Cursor c) { // 私有方法loadFromCursor从Cursor加载数据
mDataId = c.getLong(DATA_ID_COLUMN); // 从Cursor中获取数据ID并赋值给mDataId属性
mDataMimeType = c.getString(DATA_MIME_TYPE_COLUMN); // 从Cursor中获取数据的MIME类型并赋值给mDataMimeType属性
mDataContent = c.getString(DATA_CONTENT_COLUMN); // 从Cursor中获取数据的内容并赋值给mDataContent属性
mDataContentData1 = c.getLong(DATA_CONTENT_DATA_1_COLUMN); // 从Cursor中获取数据内容Data1并赋值给mDataContentData1属性
mDataContentData3 = c.getString(DATA_CONTENT_DATA_3_COLUMN); // 从Cursor中获取数据内容Data3并赋值给mDataContentData3属性
}
public void setContent(JSONObject js) throws JSONException { // 公共方法setContent接收JSONObject参数并可能抛出JSONException异常
long dataId = js.has(DataColumns.ID) ? js.getLong(DataColumns.ID) : INVALID_ID; // 获取JSONObject中的数据ID如果不存在则使用INVALID_ID默认值为-99999
if (mIsCreate || mDataId != dataId) { // 如果是新创建的数据或者数据ID与当前数据ID不同
mDiffDataValues.put(DataColumns.ID, dataId); // 将数据ID放入mDiffDataValues中
}
mDataId = dataId; // 更新mDataId属性的值为dataId
String dataMimeType = js.has(DataColumns.MIME_TYPE) ? js.getString(DataColumns.MIME_TYPE)
: DataConstants.NOTE; // 获取JSONObject中的数据的MIME类型如果不存在则使用DataConstants.NOTE默认值为"note"
if (mIsCreate || !mDataMimeType.equals(dataMimeType)) { // 如果是新创建的数据或者数据的MIME类型与当前数据的MIME类型不同
mDiffDataValues.put(DataColumns.MIME_TYPE, dataMimeType); // 将数据的MIME类型放入mDiffDataValues中
}
mDataMimeType = dataMimeType; // 更新mDataMimeType属性的值为dataMimeType
String dataContent = js.has(DataColumns.CONTENT) ? js.getString(DataColumns.CONTENT) : ""; // 获取JSONObject中的数据的内容如果不存在则使用空字符串
if (mIsCreate || !mDataContent.equals(dataContent)) { // 如果是新创建的数据,或者数据的内容与当前数据的内容不同
mDiffDataValues.put(DataColumns.CONTENT, dataContent); // 将数据的内容放入mDiffDataValues中
}
mDataContent = dataContent; // 更新mDataContent属性的值为dataContent
long dataContentData1 = js.has(DataColumns.DATA1) ? js.getLong(DataColumns.DATA1) : 0; // 获取JSONObject中的数据内容Data1如果不存在则使用0作为默认值
if (mIsCreate || mDataContentData1 != dataContentData1) { // 如果是新创建的数据或者数据内容Data1与当前数据内容Data1不同
mDiffDataValues.put(DataColumns.DATA1, dataContentData1); // 将数据内容Data1放入mDiffDataValues中
}
mDataContentData1 = dataContentData1; // 更新mDataContentData1属性的值为dataContentData1
String dataContentData3 = js.has(DataColumns.DATA3) ? js.getString(DataColumns.DATA3) : ""; // 获取JSONObject中的数据内容Data3如果不存在则使用空字符串
if (mIsCreate || !mDataContentData3.equals(dataContentData3)) { // 如果是新创建的数据或者数据内容Data3与当前数据内容Data3不同
mDiffDataValues.put(DataColumns.DATA3, dataContentData3); // 将数据内容Data3放入mDiffDataValues中
}
mDataContentData3 = dataContentData3; // 更新mDataContentData3属性的值为dataContentData3
}
public JSONObject getContent() throws JSONException { // 公共方法getContent可能抛出JSONException异常
if (mIsCreate) { // 如果是新创建的数据
Log.e(TAG, "it seems that we haven't created this in database yet"); // 打印错误日志信息
return null; // 返回null
}
JSONObject js = new JSONObject(); // 创建一个新的JSONObject对象
js.put(DataColumns.ID, mDataId); // 将数据ID放入JSONObject中
js.put(DataColumns.MIME_TYPE, mDataMimeType); // 将数据的MIME类型放入JSONObject中
js.put(DataColumns.CONTENT, mDataContent); // 将数据的内容放入JSONObject中
js.put(DataColumns.DATA1, mDataContentData1); // 将数据内容Data1放入JSONObject中
js.put(DataColumns.DATA3, mDataContentData3); // 将数据内容Data3放入JSONObject中
return js; // 返回JSONObject
}
public void commit(long noteId, boolean validateVersion, long version) { // 公共方法commit接收noteId、validateVersion和version作为参数
if (mIsCreate) { // 如果是新创建的数据
if (mDataId == INVALID_ID && mDiffDataValues.containsKey(DataColumns.ID)) { // 如果数据ID无效且mDiffDataValues包含数据ID
mDiffDataValues.remove(DataColumns.ID); // 从mDiffDataValues中移除数据ID
}
mDiffDataValues.put(DataColumns.NOTE_ID, noteId); // 将noteId放入mDiffDataValues中
Uri uri = mContentResolver.insert(Notes.CONTENT_DATA_URI, mDiffDataValues); // 向数据库插入数据并获取返回的Uri对象
try {
mDataId = Long.valueOf(uri.getPathSegments().get(1)); // 从Uri中获取数据ID并赋值给mDataId
} catch (NumberFormatException e) {
Log.e(TAG, "Get note id error :" + e.toString()); // 打印错误日志信息
throw new ActionFailureException("create note failed"); // 抛出ActionFailureException异常
}
} else { // 如果不是新创建的数据
if (mDiffDataValues.size() > 0) { // 如果mDiffDataValues的大小大于0
int result = 0; // 初始化result变量为0
if (!validateVersion) { // 如果不需要验证版本
result = mContentResolver.update(ContentUris.withAppendedId(
Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues, null, null); // 更新数据,并获取更新的结果
} else { // 如果需要验证版本
result = mContentResolver.update(ContentUris.withAppendedId(
Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues,
" ? in (SELECT " + NoteColumns.ID + " FROM " + TABLE.NOTE
+ " WHERE " + NoteColumns.VERSION + "=?)", new String[]{
String.valueOf(noteId), String.valueOf(version)
}); // 更新数据,并进行版本验证,获取更新的结果
}
if (result == 0) { // 如果更新结果为0
Log.w(TAG, "there is no update. maybe user updates note when syncing"); // 打印警告日志信息
}
}
}
mDiffDataValues.clear(); // 清空mDiffDataValues
mIsCreate = false; // 将mIsCreate设置为false
}
public long getId() { // 公共方法getId返回数据ID
return mDataId; // 返回数据ID
}
}