OSDN Git Service

FOO
[everclip/source.git] / workspace / EverClip / src / com / yuji / ec / utility / NoteUpdator.java
1 package com.yuji.ec.utility;
2
3 import java.util.List;
4
5 import android.content.Context;
6 import android.database.sqlite.SQLiteDatabase;
7
8 import com.evernote.edam.notestore.NoteList;
9 import com.yuji.ec.common.CommonUtil;
10 import com.yuji.ec.common.Constant;
11 import com.yuji.ec.db.DatabaseHelper;
12 import com.yuji.ec.db.Note;
13 import com.yuji.ec.db.NoteDao;
14 import com.yuji.ec.utility.EvernoteUtil.Error;
15
16 public class NoteUpdator {
17         private static NoteUpdator instance = null;
18         private static Object obj = new Object();
19
20         public static NoteUpdator getInstance() {
21                 if (instance == null) {
22                         synchronized (obj) {
23                                 if (instance == null) {
24                                         instance = new NoteUpdator();
25                                 }
26                         }
27                 }
28                 return instance;
29         }
30
31         private NoteUpdator() {
32
33         }
34
35         public void update(Context context, int noteItemType, String guid) {
36                 try {
37                         EvernoteUtil util = EvernoteUtil.getInstance();
38                         //String username = PasswordUtil.getUsername(context);
39                         //String password = PasswordUtil.getPassword(context);
40                         //if (!CommonUtil.isNull(username) && !CommonUtil.isNull(password)) {
41                         //      util.setConfig(username, password);
42                         //} else {
43                         //      return;
44                         //}
45
46                         List<NoteList> noteListList;
47                         switch (noteItemType) {
48                         case Constant.NOTE_ITEM_TYPE_NOTE_BOOK:
49                                 noteListList = util.getNoteListByNotebook(guid);
50                                 break;
51                         case Constant.NOTE_ITEM_TYPE_TAG:
52                                 noteListList = util.getNoteListByTag(guid);
53                                 break;
54                         default:
55                                 noteListList = util.getNoteList();
56                                 break;
57                         }
58
59                         if (util.getErrorCode() != Error.NONE) {
60                                 return;
61                         }
62
63                         List<Note> list = util.getNoteContentList(noteListList);
64                         if (list == null) {
65                                 // NOT_FOUND
66                                 return;
67                         }
68
69                         DatabaseHelper helper = DatabaseHelper.getInstance();
70                         SQLiteDatabase db = helper.getWritableDatabase();
71                         NoteDao dao = (NoteDao) NoteDao.getInstance();
72
73                         synchronized (obj) {
74                                 db.beginTransaction();
75                                 try {
76                                         dao.deleteNT();
77                                         for (Note note : list) {
78                                                 dao.addNT(note);
79                                         }
80                                         db.setTransactionSuccessful();
81                                 } finally {
82                                         db.endTransaction();
83                                 }
84                         }
85                 } catch (Exception e) {
86                         Debug.d(this, null, e);
87                 }
88         }
89 }