/*
 * 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.appwidget.AppWidgetManager;
import android.content.ContentResolver;
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.NoteColumns;
import net.micode.notes.gtask.exception.ActionFailureException;
import net.micode.notes.tool.GTaskStringUtils;
import net.micode.notes.tool.ResourceParser;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/*
 * SqlNote �����ڴ��������ݿ���صıʼDz�����
 * ������Ҫ���ڼ��ء����á���ȡ���ύ�ʼ����ݡ�
 */
public class SqlNote {
    private static final String TAG = SqlNote.class.getSimpleName();

    private static final int INVALID_ID = -99999;  // ���� ID
    // �ʼDZ���ͶӰ�ֶ�
    public static final String[] PROJECTION_NOTE = new String[] {
            NoteColumns.ID, NoteColumns.ALERTED_DATE, NoteColumns.BG_COLOR_ID,
            NoteColumns.CREATED_DATE, NoteColumns.HAS_ATTACHMENT, NoteColumns.MODIFIED_DATE,
            NoteColumns.NOTES_COUNT, NoteColumns.PARENT_ID, NoteColumns.SNIPPET, NoteColumns.TYPE,
            NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE, NoteColumns.SYNC_ID,
            NoteColumns.LOCAL_MODIFIED, NoteColumns.ORIGIN_PARENT_ID, NoteColumns.GTASK_ID,
            NoteColumns.VERSION
    };
 // �ʼDZ���������
    public static final int ID_COLUMN = 0;

    public static final int ALERTED_DATE_COLUMN = 1;

    public static final int BG_COLOR_ID_COLUMN = 2;

    public static final int CREATED_DATE_COLUMN = 3;

    public static final int HAS_ATTACHMENT_COLUMN = 4;

    public static final int MODIFIED_DATE_COLUMN = 5;

    public static final int NOTES_COUNT_COLUMN = 6;

    public static final int PARENT_ID_COLUMN = 7;

    public static final int SNIPPET_COLUMN = 8;

    public static final int TYPE_COLUMN = 9;

    public static final int WIDGET_ID_COLUMN = 10;

    public static final int WIDGET_TYPE_COLUMN = 11;

    public static final int SYNC_ID_COLUMN = 12;

    public static final int LOCAL_MODIFIED_COLUMN = 13;

    public static final int ORIGIN_PARENT_ID_COLUMN = 14;

    public static final int GTASK_ID_COLUMN = 15;

    public static final int VERSION_COLUMN = 16;

    private Context mContext;   // Ӧ�ó���������

    private ContentResolver mContentResolver;  // ���ݽ�����

    private boolean mIsCreate;  // �Ƿ�Ϊ��������

    private long mId;  // �ʼ� ID

    private long mAlertDate;  // ��������

    private int mBgColorId;  // ������ɫ ID

    private long mCreatedDate;  // ��������

    private int mHasAttachment;  // �Ƿ��и���

    private long mModifiedDate;  // �޸�����

    private long mParentId;  // ���ʼ� ID

    private String mSnippet;  // �ʼ�ժҪ

    private int mType;   // �ʼ�����

    private int mWidgetId;  // ���� ID

    private int mWidgetType;  // ��������

    private long mOriginParent;  // ԭʼ���ʼ� ID

    private long mVersion;  // �汾��

    private ContentValues mDiffNoteValues;   // ����ʼ�ֵ

    private ArrayList<SqlData> mDataList;   // �����б�
/*
 * ���캯�������ڴ����µıʼǶ���
 * �ù��캯����ʼ���ʼǶ���ĸ������ԣ�������Ϊ����������
 * @param context Ӧ�ó��������ġ�
 */
    public SqlNote(Context context) {
        mContext = context;  // ����Ӧ�ó���������
        mContentResolver = context.getContentResolver();  // ��ȡ���ݽ�����
        mIsCreate = true;  // ������������
        mId = INVALID_ID;  // ��ʼ���ʼ� ID Ϊ��Чֵ
        mAlertDate = 0;  // ��ʼ����������Ϊ 0
        mBgColorId = ResourceParser.getDefaultBgId(context);  // ��ʼ��������ɫ ID ΪĬ��ֵ
        mCreatedDate = System.currentTimeMillis();  // ��ʼ����������Ϊ��ǰʱ��
        mHasAttachment = 0;  // ��ʼ���Ƿ��и���Ϊ 0
        mModifiedDate = System.currentTimeMillis();  // ��ʼ���޸�����Ϊ��ǰʱ��
        mParentId = 0;  // ��ʼ�����ʼ� ID Ϊ 0
        mSnippet = "";  // ��ʼ���ʼ�ժҪΪ���ַ���
        mType = Notes.TYPE_NOTE;  // ��ʼ���ʼ�����Ϊ��ͨ�ʼ�
        mWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;  // ��ʼ��С���� ID Ϊ��Чֵ
        mWidgetType = Notes.TYPE_WIDGET_INVALIDE;  // ��ʼ��С��������Ϊ��Чֵ
        mOriginParent = 0;  // ��ʼ��ԭʼ���ʼ� ID Ϊ 0
        mVersion = 0;  // ��ʼ���汾��Ϊ 0
        mDiffNoteValues = new ContentValues();  // ��ʼ������ʼ�ֵ
        mDataList = new ArrayList<SqlData>();  // ��ʼ�������б�
    }
/*
 * ���캯�������ڴ����ݿ��α���رʼǶ���
 * �ù��캯����ʼ���ʼǶ���ĸ������ԣ�������Ϊ�Ǵ���������
 * @param context Ӧ�ó��������ġ�
 * @param c ���ݿ��αꡣ
 */
    public SqlNote(Context context, Cursor c) {
        mContext = context;  // ����Ӧ�ó���������
        mContentResolver = context.getContentResolver();  // ��ȡ���ݽ�����
        mIsCreate = false;  // ����Ϊ�Ǵ�������
        loadFromCursor(c);  // �����ݿ��α���رʼǶ���
        mDataList = new ArrayList<SqlData>();  // ��ʼ�������б�
        if (mType == Notes.TYPE_NOTE)  // ����ʼ�����Ϊ��ͨ�ʼ�
            loadDataContent();  // ���رʼǵ���������
        mDiffNoteValues = new ContentValues();  // ��ʼ������ʼ�ֵ
    }
/*
 *  ���캯�������ڴ����ݿ��м���ָ�� ID �ıʼǶ���
 * �ù��캯����ʼ���ʼǶ���ĸ������ԣ�������Ϊ�Ǵ���������
 * @param context Ӧ�ó��������ġ�
 * @param id �ʼ� ID��
 */
    public SqlNote(Context context, long id) {
        mContext = context;  // ����Ӧ�ó���������
        mContentResolver = context.getContentResolver();  // ��ȡ���ݽ�����
        mIsCreate = false;  // ����Ϊ�Ǵ�������
        loadFromCursor(id);  // �����ݿ��м���ָ�� ID �ıʼǶ���
        mDataList = new ArrayList<SqlData>();  // ��ʼ�������б�
        if (mType == Notes.TYPE_NOTE)  // ����ʼ�����Ϊ��ͨ�ʼ�
            loadDataContent();  // ���رʼǵ���������
        mDiffNoteValues = new ContentValues();  // ��ʼ������ʼ�ֵ

    }
/*
 * �����ݿ��м���ָ�� ID �ıʼǶ���
 * �÷��������ݿ��в�ѯָ�� ID �ıʼ����ݣ������� `loadFromCursor(Cursor c)` �������رʼǶ���
 * @param id �ʼ� ID��
 */
    private void loadFromCursor(long id) {
        Cursor c = null;
        try {
        	  // ��ѯָ�� ID �ıʼ�����
            c = mContentResolver.query(Notes.CONTENT_NOTE_URI, PROJECTION_NOTE, "(_id=?)",
                    new String[] {
                        String.valueOf(id)
                    }, null);
            if (c != null) {
                c.moveToNext();  // �ƶ����α�ĵ�һ��
                loadFromCursor(c);  // ���رʼǶ���
            } else {
                Log.w(TAG, "loadFromCursor: cursor = null");  // ��¼������־���α�Ϊ��
            }
        } finally {
            if (c != null)
                c.close();  // �ر��α�
        }
    }
/*
 *  �����ݿ��α���رʼǶ���
 * �÷��������ݿ��α�����ȡ�ʼ����ݣ�����ʼ���ʼǶ���ĸ������ԡ�
 * @param c ���ݿ��αꡣ
 */
    private void loadFromCursor(Cursor c) {
        mId = c.getLong(ID_COLUMN);  // ��ȡ�ʼ� ID
        mAlertDate = c.getLong(ALERTED_DATE_COLUMN);  // ��ȡ��������
        mBgColorId = c.getInt(BG_COLOR_ID_COLUMN);  // ��ȡ������ɫ ID
        mCreatedDate = c.getLong(CREATED_DATE_COLUMN);  // ��ȡ��������
        mHasAttachment = c.getInt(HAS_ATTACHMENT_COLUMN);  // ��ȡ�Ƿ��и���
        mModifiedDate = c.getLong(MODIFIED_DATE_COLUMN);  // ��ȡ�޸�����
        mParentId = c.getLong(PARENT_ID_COLUMN);  // ��ȡ���ʼ� ID
        mSnippet = c.getString(SNIPPET_COLUMN);  // ��ȡ�ʼ�ժҪ
        mType = c.getInt(TYPE_COLUMN);  // ��ȡ�ʼ�����
        mWidgetId = c.getInt(WIDGET_ID_COLUMN);  // ��ȡС���� ID
        mWidgetType = c.getInt(WIDGET_TYPE_COLUMN);  // ��ȡС��������
        mVersion = c.getLong(VERSION_COLUMN);  // ��ȡ�汾��
    }
/*
 * ���رʼǵ��������ݡ�
 * �÷��������ݿ��в�ѯ��ʼ���ص��������ݣ���������ص������б��С�
 */
    private void loadDataContent() {
        Cursor c = null;
        mDataList.clear();  // ��������б�
        try {
        	 // ��ѯ��ʼ���ص���������
            c = mContentResolver.query(Notes.CONTENT_DATA_URI, SqlData.PROJECTION_DATA,
                    "(note_id=?)", new String[] {
                        String.valueOf(mId)
                    }, null);
            if (c != null) {
                if (c.getCount() == 0) {
                    Log.w(TAG, "it seems that the note has not data");  // ��¼������־���ʼ�û����������
                    return;
                }
                while (c.moveToNext()) {
                    SqlData data = new SqlData(mContext, c);  // ���α��м�������
                    mDataList.add(data);  // �������������ӵ������б���
                }
            } else {
                Log.w(TAG, "loadDataContent: cursor = null");  // ��¼������־���α�Ϊ��
            }
        } finally {
            if (c != null)
                c.close();  // �ر��α�
        }
    }
/*
 * �������ݵķ���������һ��JSONObject��Ϊ���������������е����ݸ��µ�ǰ��������ԡ�
 * @param js �����ʼ���Ϣ��JSONObject
 * @return ������óɹ�����true�����򷵻�false
 */
    public boolean setContent(JSONObject js) {
        try {
        	 // ��JSONObject�л�ȡ�ʼǵ�Ԫ����
            JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
            // ���ʼǵ�����
            if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) {
            	// �����ϵͳ�ļ��У������������ݣ���¼������־
                Log.w(TAG, "cannot set system folder");
            } else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) {
            	// ������ļ������ͣ���ֻ�ܸ���ժҪ������
                // for folder we can only update the snnipet and type
            	// ��ȡժҪ�����δ�ṩ��Ĭ��Ϊ���ַ���
                String snippet = note.has(NoteColumns.SNIPPET) ? note
                        .getString(NoteColumns.SNIPPET) : "";
                // ������½��ʼǻ�ժҪ�б仯�������ժҪ
                if (mIsCreate || !mSnippet.equals(snippet)) {
                    mDiffNoteValues.put(NoteColumns.SNIPPET, snippet);
                }
                mSnippet = snippet;
                // ��ȡ���ͣ����δ�ṩ��Ĭ��Ϊ��ͨ�ʼ�����
                int type = note.has(NoteColumns.TYPE) ? note.getInt(NoteColumns.TYPE)
                        : Notes.TYPE_NOTE;
                // ������½��ʼǻ������б仯�����������
                if (mIsCreate || mType != type) {
                    mDiffNoteValues.put(NoteColumns.TYPE, type);
                }
                mType = type;
            } else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_NOTE) {
            	// �������ͨ�ʼ����ͣ�����������������
            	// ��ȡ��������
                JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA);
                // ��ȡ�ʼ�ID�����δ�ṩ��Ĭ��Ϊ��ЧID
                long id = note.has(NoteColumns.ID) ? note.getLong(NoteColumns.ID) : INVALID_ID;
                // ������½��ʼǻ�ID�б仯�������ID
                if (mIsCreate || mId != id) {
                    mDiffNoteValues.put(NoteColumns.ID, id);
                }
                mId = id;
                // ��ȡ�������ڣ����δ�ṩ��Ĭ��Ϊ0
                long alertDate = note.has(NoteColumns.ALERTED_DATE) ? note
                        .getLong(NoteColumns.ALERTED_DATE) : 0;
                // ������½��ʼǻ����������б仯���������������
                if (mIsCreate || mAlertDate != alertDate) {
                    mDiffNoteValues.put(NoteColumns.ALERTED_DATE, alertDate);
                }
                mAlertDate = alertDate;
                // ��ȡ������ɫID�����δ�ṩ��Ĭ��ΪĬ�ϱ�����ɫID
                int bgColorId = note.has(NoteColumns.BG_COLOR_ID) ? note
                        .getInt(NoteColumns.BG_COLOR_ID) : ResourceParser.getDefaultBgId(mContext);
                // ������½��ʼǻ򱳾���ɫID�б仯������±�����ɫID
                if (mIsCreate || mBgColorId != bgColorId) {
                    mDiffNoteValues.put(NoteColumns.BG_COLOR_ID, bgColorId);
                }
                mBgColorId = bgColorId;
                // ��ȡ�������ڣ����δ�ṩ��Ĭ��Ϊ��ǰʱ��
                long createDate = note.has(NoteColumns.CREATED_DATE) ? note
                        .getLong(NoteColumns.CREATED_DATE) : System.currentTimeMillis();
                // ������½��ʼǻ򴴽������б仯������´�������
                if (mIsCreate || mCreatedDate != createDate) {
                    mDiffNoteValues.put(NoteColumns.CREATED_DATE, createDate);
                }
                mCreatedDate = createDate;
                // ��ȡ�Ƿ��и��������δ�ṩ��Ĭ��Ϊ0���޸�����
                int hasAttachment = note.has(NoteColumns.HAS_ATTACHMENT) ? note
                        .getInt(NoteColumns.HAS_ATTACHMENT) : 0;
                // ������½��ʼǻ��Ƿ��и����б仯��������Ƿ��и���
                if (mIsCreate || mHasAttachment != hasAttachment) {
                    mDiffNoteValues.put(NoteColumns.HAS_ATTACHMENT, hasAttachment);
                }
                mHasAttachment = hasAttachment;
                // ��ȡ�޸����ڣ����δ�ṩ��Ĭ��Ϊ��ǰʱ��
                long modifiedDate = note.has(NoteColumns.MODIFIED_DATE) ? note
                        .getLong(NoteColumns.MODIFIED_DATE) : System.currentTimeMillis();
                // ������½��ʼǻ��޸������б仯��������޸�����
                if (mIsCreate || mModifiedDate != modifiedDate) {
                    mDiffNoteValues.put(NoteColumns.MODIFIED_DATE, modifiedDate);
                }
                mModifiedDate = modifiedDate;
                // ��ȡ��ID�����δ�ṩ��Ĭ��Ϊ0
                long parentId = note.has(NoteColumns.PARENT_ID) ? note
                        .getLong(NoteColumns.PARENT_ID) : 0;
                // ������½��ʼǻ�ID�б仯������¸�ID
                if (mIsCreate || mParentId != parentId) {
                    mDiffNoteValues.put(NoteColumns.PARENT_ID, parentId);
                }
                mParentId = parentId;
                // ��ȡժҪ�����δ�ṩ��Ĭ��Ϊ���ַ���
                String snippet = note.has(NoteColumns.SNIPPET) ? note
                        .getString(NoteColumns.SNIPPET) : "";
                // ������½��ʼǻ�ժҪ�б仯�������ժҪ
                if (mIsCreate || !mSnippet.equals(snippet)) {
                    mDiffNoteValues.put(NoteColumns.SNIPPET, snippet);
                }
                mSnippet = snippet;
                // ��ȡ���ͣ����δ�ṩ��Ĭ��Ϊ��ͨ�ʼ�����
                int type = note.has(NoteColumns.TYPE) ? note.getInt(NoteColumns.TYPE)
                        : Notes.TYPE_NOTE;
                // ������½��ʼǻ������б仯�����������
                if (mIsCreate || mType != type) {
                    mDiffNoteValues.put(NoteColumns.TYPE, type);
                }
                mType = type;
                // ��ȡС����ID�����δ�ṩ��Ĭ��Ϊ��ЧС����ID
                int widgetId = note.has(NoteColumns.WIDGET_ID) ? note.getInt(NoteColumns.WIDGET_ID)
                        : AppWidgetManager.INVALID_APPWIDGET_ID;
             // ������½��ʼǻ�С����ID�б仯�������С����ID
                if (mIsCreate || mWidgetId != widgetId) {
                    mDiffNoteValues.put(NoteColumns.WIDGET_ID, widgetId);
                }
                mWidgetId = widgetId;
                // ��ȡС�������ͣ����δ�ṩ��Ĭ��Ϊ��ЧС��������
                int widgetType = note.has(NoteColumns.WIDGET_TYPE) ? note
                        .getInt(NoteColumns.WIDGET_TYPE) : Notes.TYPE_WIDGET_INVALIDE;
                // ������½��ʼǻ�С���������б仯�������С��������
                if (mIsCreate || mWidgetType != widgetType) {
                    mDiffNoteValues.put(NoteColumns.WIDGET_TYPE, widgetType);
                }
                mWidgetType = widgetType;
                // ��ȡԭʼ��ID�����δ�ṩ��Ĭ��Ϊ0
                long originParent = note.has(NoteColumns.ORIGIN_PARENT_ID) ? note
                        .getLong(NoteColumns.ORIGIN_PARENT_ID) : 0;
                // ������½��ʼǻ�ԭʼ��ID�б仯�������ԭʼ��ID
                if (mIsCreate || mOriginParent != originParent) {
                    mDiffNoteValues.put(NoteColumns.ORIGIN_PARENT_ID, originParent);
                }
                mOriginParent = originParent;
                // �����������飬���������б�
                for (int i = 0; i < dataArray.length(); i++) {
                    JSONObject data = dataArray.getJSONObject(i);
                    SqlData sqlData = null;
                    // ������ݶ������ID���������������б��в���ƥ���SqlData����
                    if (data.has(DataColumns.ID)) {
                        long dataId = data.getLong(DataColumns.ID);
                        for (SqlData temp : mDataList) {
                            if (dataId == temp.getId()) {
                                sqlData = temp;
                            }
                        }
                    }
                 // ���δ�ҵ�ƥ���SqlData�����򴴽�һ���µ�SqlData�������ӵ������б���
                    if (sqlData == null) {
                        sqlData = new SqlData(mContext);
                        mDataList.add(sqlData);
                    }
                 // ����SqlData���������
                    sqlData.setContent(data);
                }
            }
        } catch (JSONException e) {
        	 // ����JSON�쳣����¼������־
            Log.e(TAG, e.toString());
            e.printStackTrace();
            return false;
        }
        return true;
    }
/*
 * ��ȡ��ǰ��������ݣ��������װΪһ��JSONObject���ء�
 * @return ������ǰ�������ݵ�JSONObject��������������򷵻�null
 */
    public JSONObject getContent() {
        try {
        	// ����һ���µ�JSONObject���ڴ洢����
            JSONObject js = new JSONObject();
            // �����ǰ������δ�����ݿ��д�������¼������־������null
            if (mIsCreate) {
                Log.e(TAG, "it seems that we haven't created this in database yet");
                return null;
            }
            // ����һ���µ�JSONObject���ڴ洢�ʼ���Ϣ
            JSONObject note = new JSONObject();
            // �����ǰ��������ͨ�ʼ�����
            if (mType == Notes.TYPE_NOTE) {
            	// ���ʼǵĸ����������ӵ�note JSONObject��
                note.put(NoteColumns.ID, mId);
                note.put(NoteColumns.ALERTED_DATE, mAlertDate);
                note.put(NoteColumns.BG_COLOR_ID, mBgColorId);
                note.put(NoteColumns.CREATED_DATE, mCreatedDate);
                note.put(NoteColumns.HAS_ATTACHMENT, mHasAttachment);
                note.put(NoteColumns.MODIFIED_DATE, mModifiedDate);
                note.put(NoteColumns.PARENT_ID, mParentId);
                note.put(NoteColumns.SNIPPET, mSnippet);
                note.put(NoteColumns.TYPE, mType);
                note.put(NoteColumns.WIDGET_ID, mWidgetId);
                note.put(NoteColumns.WIDGET_TYPE, mWidgetType);
                note.put(NoteColumns.ORIGIN_PARENT_ID, mOriginParent);
                // ��note JSONObject���ӵ�js JSONObject��
                js.put(GTaskStringUtils.META_HEAD_NOTE, note);
                // ����һ���µ�JSONArray���ڴ洢�����б�
                JSONArray dataArray = new JSONArray();
                // ���������б�����ÿ��SqlData������������ӵ�dataArray��
                for (SqlData sqlData : mDataList) {
                    JSONObject data = sqlData.getContent();
                    if (data != null) {
                        dataArray.put(data);
                    }
                }
                // ��dataArray���ӵ�js JSONObject��
                js.put(GTaskStringUtils.META_HEAD_DATA, dataArray);
            } else if (mType == Notes.TYPE_FOLDER || mType == Notes.TYPE_SYSTEM) {
            	// �����ǰ�������ļ��л�ϵͳ�ļ�������
            	// ���ʼǵ�ID�����ͺ�ժҪ���ӵ�note JSONObject��
                note.put(NoteColumns.ID, mId);
                note.put(NoteColumns.TYPE, mType);
                note.put(NoteColumns.SNIPPET, mSnippet);
                // ��note JSONObject���ӵ�js JSONObject��
                js.put(GTaskStringUtils.META_HEAD_NOTE, note);
            }
            // ���ذ������ݵ�JSONObject
            return js;
        } catch (JSONException e) {
        	// ����JSON�쳣����¼������־
            Log.e(TAG, e.toString());
            e.printStackTrace();
        }
        return null;
    }
/*
 * ���ø�ID�����������ӵ�����ֵ�С�
 * @param id �µĸ�ID
 */
    public void setParentId(long id) {
        mParentId = id;
        mDiffNoteValues.put(NoteColumns.PARENT_ID, id);
    }
/*
 * ����Google����ID�����������ӵ�����ֵ�С�
 * @param gid �µ�Google����ID
 */
    public void setGtaskId(String gid) {
        mDiffNoteValues.put(NoteColumns.GTASK_ID, gid);
    }
/*
 * ����ͬ��ID�����������ӵ�����ֵ�С�
 * @param syncId �µ�ͬ��ID
 */
    public void setSyncId(long syncId) {
        mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId);
    }
/*
 * ���ñ����޸ı�־����������Ϊ0��
 */
    public void resetLocalModified() {
        mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0);
    }
/*
 * ��ȡ��ǰ�ʼǵ�ID��
 * @return ��ǰ�ʼǵ�ID
 */
    public long getId() {
        return mId;
    }
/*
 * ��ȡ��ǰ�ʼǵĸ�ID��
 * @return ��ǰ�ʼǵĸ�ID
 */
    public long getParentId() {
        return mParentId;
    }
/*
 * ��ȡ��ǰ�ʼǵ�ժҪ��
 * @return ��ǰ�ʼǵ�ժҪ
 */
    public String getSnippet() {
        return mSnippet;
    }
/*
 * �жϵ�ǰ�ʼ��Ƿ�Ϊ��ͨ�ʼ����͡�
 * @return �����ǰ�ʼ�����ͨ�ʼ����ͣ�����true�����򷵻�false
 */
    public boolean isNoteType() {
        return mType == Notes.TYPE_NOTE;
    }
/*
 * �ύ���ģ�����ǰ����IJ���ֵ���浽���ݿ��С�
 * @param validateVersion �Ƿ���֤�汾��
 */
    public void commit(boolean validateVersion) {
    	 // ������½��ʼ�
        if (mIsCreate) {
        	// ���ID��Ч�Ҳ���ֵ�а���ID�����Ƴ�ID
            if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) {
                mDiffNoteValues.remove(NoteColumns.ID);
            }
            // ����ʼǵ����ݿ⣬����ȡ������URI
            Uri uri = mContentResolver.insert(Notes.CONTENT_NOTE_URI, mDiffNoteValues);
            try {
            	 // ��URI����ȡ�²���ʼǵ�ID
                mId = Long.valueOf(uri.getPathSegments().get(1));
            } catch (NumberFormatException e) {
            	 // �����ȡIDʧ�ܣ���¼������־���׳��쳣
                Log.e(TAG, "Get note id error :" + e.toString());
                throw new ActionFailureException("create note failed");
            }
            // ���IDΪ0����ʾ����ʧ�ܣ��׳��쳣
            if (mId == 0) {
                throw new IllegalStateException("Create thread id failed");
            }
            // �����ǰ�ʼ�����ͨ�ʼ�����
            if (mType == Notes.TYPE_NOTE) {
            	 // ���������б����ύÿ��SqlData����ĸ���
                for (SqlData sqlData : mDataList) {
                    sqlData.commit(mId, false, -1);
                }
            }
        } else {
        	 // ��������½��ʼ�
        	// ���ID��Ч�Ҳ��Ǹ��ļ��л�ͨ����¼�ļ��У���¼������־���׳��쳣
            if (mId <= 0 && mId != Notes.ID_ROOT_FOLDER && mId != Notes.ID_CALL_RECORD_FOLDER) {
                Log.e(TAG, "No such note");
                throw new IllegalStateException("Try to update note with invalid id");
            }
            // �������ֵ���и�������
            if (mDiffNoteValues.size() > 0) {
            	// ���Ӱ汾��
                mVersion ++;
                int result = 0;
                // �������֤�汾��
                if (!validateVersion) {
                	// ���±ʼ�����
                    result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "("
                            + NoteColumns.ID + "=?)", new String[] {
                        String.valueOf(mId)
                    });
                } else {
                	 // �����֤�汾�ţ����±ʼ����ݲ����汾��
                    result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "("
                            + NoteColumns.ID + "=?) AND (" + NoteColumns.VERSION + "<=?)",
                            new String[] {
                                    String.valueOf(mId), String.valueOf(mVersion)
                            });
                }
                // ���û�и������ݣ���¼������־
                if (result == 0) {
                    Log.w(TAG, "there is no update. maybe user updates note when syncing");
                }
            }
            // �����ǰ�ʼ�����ͨ�ʼ�����
            if (mType == Notes.TYPE_NOTE) {
            	 // ���������б����ύÿ��SqlData����ĸ���
                for (SqlData sqlData : mDataList) {
                    sqlData.commit(mId, validateVersion, mVersion);
                }
            }
        }

        // refresh local info
        // ˢ�±�����Ϣ
        loadFromCursor(mId);
        if (mType == Notes.TYPE_NOTE)
            loadDataContent();
        // ��ղ���ֵ������Ϊ���½�״̬
        mDiffNoteValues.clear();
        mIsCreate = false;
    }
}