OSDN Git Service

Evenoteの関連ノート取得を別スレッドで実行するよう試作。まだまだ問題あり。
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / threads / ENRelatedNotesRunner.java
1 package cx.fbn.nevernote.threads;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.concurrent.LinkedBlockingQueue;
6
7 import com.evernote.edam.error.EDAMNotFoundException;
8 import com.evernote.edam.error.EDAMSystemException;
9 import com.evernote.edam.error.EDAMUserException;
10 import com.evernote.edam.limits.Constants;
11 import com.evernote.edam.notestore.RelatedQuery;
12 import com.evernote.edam.notestore.RelatedResult;
13 import com.evernote.edam.notestore.RelatedResultSpec;
14 import com.evernote.edam.type.Note;
15 import com.evernote.thrift.TException;
16 import com.trolltech.qt.core.QMutex;
17 import com.trolltech.qt.core.QObject;
18
19 import cx.fbn.nevernote.signals.ENRelatedNotesSignal;
20
21 public class ENRelatedNotesRunner extends QObject implements Runnable{
22         private final SyncRunner syncRunner;
23 //      public volatile Signal0 getENRelatedNotesFinished;
24         public volatile ENRelatedNotesSignal ENRelatedNotesSignal;
25 //      private List<String> relatedNoteGuids;
26         public QMutex mutex;
27         private volatile boolean keepRunning;
28         private volatile LinkedBlockingQueue<String> guidQueue;
29         private volatile LinkedBlockingQueue<List<String>> resultQueue;
30         
31         public ENRelatedNotesRunner(SyncRunner syncRunner) {
32                 this.syncRunner = syncRunner;
33 //              this.relatedNoteGuids = new ArrayList<String>();
34 //              this.getENRelatedNotesFinished = new Signal0();
35                 this.ENRelatedNotesSignal = new ENRelatedNotesSignal();
36                 this.mutex = new QMutex();
37                 this.keepRunning = true;
38                 this.guidQueue = new LinkedBlockingQueue<String>();
39                 this.resultQueue = new LinkedBlockingQueue<List<String>>();
40         }
41
42         @Override
43         public void run() {
44                 thread().setPriority(Thread.MIN_PRIORITY);
45                 
46                 while (keepRunning) {
47                         try {
48                                 String guid = guidQueue.take();
49                                 List<String> relatedNoteGuids = new ArrayList<String>();
50                                 mutex.lock();
51                         
52                                 List<Note> relatedNotes = getENRelatedNotes(guid);
53                                 if (relatedNotes != null && !relatedNotes.isEmpty()) {
54                                         for (Note relatedNote : relatedNotes) {
55                                                 relatedNoteGuids.add(relatedNote.getGuid());
56                                         }
57                                 }
58                                 
59                                 resultQueue.offer(relatedNoteGuids);
60                                 ENRelatedNotesSignal.getENRelatedNotesFinished.emit();
61                                 mutex.unlock();
62                         } catch (InterruptedException e) {
63                                 // TODO 自動生成された catch ブロック
64                                 e.printStackTrace();
65                         }
66                 }
67         }
68         
69         private List<Note> getENRelatedNotes(String guid) {
70                 RelatedResult result = getENRelatedResult(guid);
71                 List<Note> relatedNotes = new ArrayList<Note>();
72                 
73                 if (result != null) {
74                         relatedNotes = result.getNotes();
75                 }
76                 
77                 return relatedNotes;
78         }
79         
80         private RelatedResult getENRelatedResult(String guid) {
81                 RelatedQuery rquery = new RelatedQuery();
82                 rquery.setNoteGuid(guid);
83                 RelatedResultSpec resultSpec = new RelatedResultSpec();
84                 resultSpec.setMaxNotes(Constants.EDAM_RELATED_MAX_NOTES);
85                 if (syncRunner != null && syncRunner.localNoteStore != null) {
86                         try {
87                                 RelatedResult result = syncRunner.localNoteStore.findRelated(syncRunner.authToken, rquery, resultSpec);
88                                 return result;
89                         } catch (EDAMUserException e) {
90                                 // TODO 自動生成された catch ブロック
91                                 e.printStackTrace();
92                         } catch (EDAMSystemException e) {
93                                 // TODO 自動生成された catch ブロック
94                                 e.printStackTrace();
95                         } catch (EDAMNotFoundException e) {
96                                 // TODO 自動生成された catch ブロック
97                                 e.printStackTrace();
98                         } catch (TException e) {
99                                 // TODO 自動生成された catch ブロック
100                                 e.printStackTrace();
101                         }
102                 }
103                 return null;
104         }
105
106         public boolean isKeepRunning() {
107                 return keepRunning;
108         }
109
110         public void setKeepRunning(boolean keepRunning) {
111                 this.keepRunning = keepRunning;
112         }
113         
114         public synchronized boolean addGuid(String guid) {
115                 if (guidQueue.offer(guid)) {
116                         return true;
117                 }
118                 
119                 return false;
120         }
121         
122         public synchronized List<String> getENRelatedNoteGuids() {
123                 try {
124                         return resultQueue.take();
125                 } catch (InterruptedException e) {
126                         // TODO 自動生成された catch ブロック
127                         e.printStackTrace();
128                 }
129                 
130                 return null;
131         }
132 }