Delete 'SqlData.java'

Tanshuhan
pcoifxtab 5 months ago
parent f7924c097e
commit 4d954734ab

@ -1,243 +0,0 @@
/*
* 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;
import android.content.ContentUris;
import android.content.ContentValues;
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;
import org.json.JSONObject;
/*
* SqlData
*
*/
public class SqlData {
private static final String TAG = SqlData.class.getSimpleName();
private static final int INVALID_ID = -99999; // 无效的 ID
// 数据表的投影字段
public static final String[] PROJECTION_DATA = new String[] {
DataColumns.ID, DataColumns.MIME_TYPE, DataColumns.CONTENT, DataColumns.DATA1,
DataColumns.DATA3
};
// 数据表的列索引
public static final int DATA_ID_COLUMN = 0;
public static final int DATA_MIME_TYPE_COLUMN = 1;
public static final int DATA_CONTENT_COLUMN = 2;
public static final int DATA_CONTENT_DATA_1_COLUMN = 3;
public static final int DATA_CONTENT_DATA_3_COLUMN = 4;
private ContentResolver mContentResolver; // 内容解析器
private boolean mIsCreate; // 是否为创建操作
private long mDataId; // 数据 ID
private String mDataMimeType; // 数据 MIME 类型
private String mDataContent; // 数据内容
private long mDataContentData1; // 数据内容数据 1
private String mDataContentData3; // 数据内容数据 3
private ContentValues mDiffDataValues; // 差异数据值
/*
*
*
* @param context
*/
public SqlData(Context context) {
mContentResolver = context.getContentResolver(); // 获取内容解析器
mIsCreate = true; // 设置为创建操作
mDataId = INVALID_ID; // 初始化数据 ID 为无效值
mDataMimeType = DataConstants.NOTE; // 初始化数据 MIME 类型为笔记类型
mDataContent = ""; // 初始化数据内容为空字符串
mDataContentData1 = 0; // 初始化数据内容数据 1 为 0
mDataContentData3 = ""; // 初始化数据内容数据 3 为空字符串
mDiffDataValues = new ContentValues(); // 初始化差异数据值
}
/*
*
*
* @param context
* @param c
*/
public SqlData(Context context, Cursor c) {
mContentResolver = context.getContentResolver(); // 获取内容解析器
mIsCreate = false; // 设置为非创建操作
loadFromCursor(c); // 从数据库游标加载数据
mDiffDataValues = new ContentValues(); // 初始化差异数据值
}
/*
*
*
* @param c
*/
private void loadFromCursor(Cursor c) {
mDataId = c.getLong(DATA_ID_COLUMN); // 获取数据 ID
mDataMimeType = c.getString(DATA_MIME_TYPE_COLUMN); // 获取数据 MIME 类型
mDataContent = c.getString(DATA_CONTENT_COLUMN); // 获取数据内容
mDataContentData1 = c.getLong(DATA_CONTENT_DATA_1_COLUMN); // 获取数据内容数据 1
mDataContentData3 = c.getString(DATA_CONTENT_DATA_3_COLUMN); // 获取数据内容数据 3
}
/*
*
* JSON
* JSON
* @param js JSON
* @throws JSONException JSON
*/
public void setContent(JSONObject js) throws JSONException {
// 从 JSON 对象中获取数据 ID如果 JSON 对象中没有数据 ID则使用无效 ID
long dataId = js.has(DataColumns.ID) ? js.getLong(DataColumns.ID) : INVALID_ID;
// 如果数据对象是新创建的,或者数据 ID 不同,则更新差异数据值
if (mIsCreate || mDataId != dataId) {
mDiffDataValues.put(DataColumns.ID, dataId);
}
mDataId = dataId; // 更新数据 ID
// 从 JSON 对象中获取数据 MIME 类型,如果 JSON 对象中没有数据 MIME 类型,则使用默认的笔记类型
String dataMimeType = js.has(DataColumns.MIME_TYPE) ? js.getString(DataColumns.MIME_TYPE)
: DataConstants.NOTE;
// 如果数据对象是新创建的,或者数据 MIME 类型不同,则更新差异数据值
if (mIsCreate || !mDataMimeType.equals(dataMimeType)) {
mDiffDataValues.put(DataColumns.MIME_TYPE, dataMimeType);
}
mDataMimeType = dataMimeType; // 更新数据 MIME 类型
// 从 JSON 对象中获取数据内容,如果 JSON 对象中没有数据内容,则使用空字符串
String dataContent = js.has(DataColumns.CONTENT) ? js.getString(DataColumns.CONTENT) : "";
// 如果数据对象是新创建的,或者数据内容不同,则更新差异数据值
if (mIsCreate || !mDataContent.equals(dataContent)) {
mDiffDataValues.put(DataColumns.CONTENT, dataContent);
}
mDataContent = dataContent; // 更新数据内容
// 从 JSON 对象中获取数据内容数据 1如果 JSON 对象中没有数据内容数据 1则使用 0
long dataContentData1 = js.has(DataColumns.DATA1) ? js.getLong(DataColumns.DATA1) : 0;
// 如果数据对象是新创建的,或者数据内容数据 1 不同,则更新差异数据值
if (mIsCreate || mDataContentData1 != dataContentData1) {
mDiffDataValues.put(DataColumns.DATA1, dataContentData1);
}
mDataContentData1 = dataContentData1; // 更新数据内容数据 1
// 从 JSON 对象中获取数据内容数据 3如果 JSON 对象中没有数据内容数据 3则使用空字符串
String dataContentData3 = js.has(DataColumns.DATA3) ? js.getString(DataColumns.DATA3) : "";
// 如果数据对象是新创建的,或者数据内容数据 3 不同,则更新差异数据值
if (mIsCreate || !mDataContentData3.equals(dataContentData3)) {
mDiffDataValues.put(DataColumns.DATA3, dataContentData3);
}
mDataContentData3 = dataContentData3; // 更新数据内容数据 3
}
/*
*
* JSON JSON
* @return JSON
* @throws JSONException JSON
*/
public JSONObject getContent() throws JSONException {
// 如果数据对象是新创建的,则记录错误日志并返回 null
if (mIsCreate) {
Log.e(TAG, "it seems that we haven't created this in database yet");
return null;
}
JSONObject js = new JSONObject(); // 创建一个新的 JSON 对象
js.put(DataColumns.ID, mDataId); // 将数据 ID 添加到 JSON 对象中
js.put(DataColumns.MIME_TYPE, mDataMimeType); // 将数据 MIME 类型添加到 JSON 对象中
js.put(DataColumns.CONTENT, mDataContent); // 将数据内容添加到 JSON 对象中
js.put(DataColumns.DATA1, mDataContentData1); // 将数据内容数据 1 添加到 JSON 对象中
js.put(DataColumns.DATA3, mDataContentData3); // 将数据内容数据 3 添加到 JSON 对象中
return js; // 返回 JSON 对象
}
/*
*
*
*
* @param noteId ID
* @param validateVersion
* @param version
*/
public void commit(long noteId, boolean validateVersion, long version) {
// 如果是创建操作
if (mIsCreate) {
// 如果数据 ID 是无效的,并且差异数据值中包含数据 ID则移除数据 ID
if (mDataId == INVALID_ID && mDiffDataValues.containsKey(DataColumns.ID)) {
mDiffDataValues.remove(DataColumns.ID);
}
// 将笔记 ID 添加到差异数据值中
mDiffDataValues.put(DataColumns.NOTE_ID, noteId);
// 插入数据到数据库中
Uri uri = mContentResolver.insert(Notes.CONTENT_DATA_URI, mDiffDataValues);
try {
// 获取插入数据后的数据 ID
mDataId = Long.valueOf(uri.getPathSegments().get(1));
} catch (NumberFormatException e) {
// 记录错误日志,并抛出操作失败异常
Log.e(TAG, "Get note id error :" + e.toString());
throw new ActionFailureException("create note failed");
}
} else {
// 如果是更新操作,并且差异数据值不为空
if (mDiffDataValues.size() > 0) {
int 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) {
Log.w(TAG, "there is no update. maybe user updates note when syncing");
}
}
}
// 清空差异数据值
mDiffDataValues.clear();
mIsCreate = false; // 设置为非创建操作
}
/*
* ID
* ID
* @return ID
*/
public long getId() {
return mDataId;
}
}
Loading…
Cancel
Save