OSDN Git Service

Add logic to display stacks in notebook tree
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / sql / SharedNotebookTable.java
1 /*\r
2  * This file is part of NeverNote \r
3  * Copyright 2009 Randy Baumgarte\r
4  * \r
5  * This file may be licensed under the terms of of the\r
6  * GNU General Public License Version 2 (the ``GPL'').\r
7  *\r
8  * Software distributed under the License is distributed\r
9  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either\r
10  * express or implied. See the GPL for the specific language\r
11  * governing rights and limitations.\r
12  *\r
13  * You should have received a copy of the GPL along with this\r
14  * program. If not, go to http://www.gnu.org/licenses/gpl.html\r
15  * or write to the Free Software Foundation, Inc.,\r
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
17  *\r
18 */\r
19 \r
20 \r
21 package cx.fbn.nevernote.sql;\r
22 \r
23 import java.text.DateFormat;\r
24 import java.text.ParseException;\r
25 import java.text.SimpleDateFormat;\r
26 import java.util.ArrayList;\r
27 import java.util.List;\r
28 \r
29 import com.evernote.edam.type.SharedNotebook;\r
30 \r
31 import cx.fbn.nevernote.sql.driver.NSqlQuery;\r
32 import cx.fbn.nevernote.utilities.ApplicationLogger;\r
33 \r
34 public class SharedNotebookTable {\r
35         \r
36         private final ApplicationLogger                 logger;\r
37         DatabaseConnection                                                      db;\r
38         \r
39         // Constructor\r
40         public SharedNotebookTable(ApplicationLogger l, DatabaseConnection d) {\r
41                 logger = l;\r
42                 db = d;\r
43         }\r
44         // Create the table\r
45         public void createTable() {\r
46                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
47         logger.log(logger.HIGH, "Creating table SharedNotebook...");\r
48         if (!query.exec("Create table SharedNotebook (id long primary key, " +\r
49                         "userid Integer, " +\r
50                         "notebookGuid VarChar, "+\r
51                         "email VarChar, "+\r
52                         "notebookModifiable boolean, " +\r
53                         "requireLogin boolean, "+\r
54                         "serviceCreated timestamp, "+\r
55                         "shareKey VarChar,  "+\r
56                         "username VarChar, "+\r
57                         "icon blob, " +\r
58                         "isDirty boolean)"))                            \r
59                 logger.log(logger.HIGH, "Table SharedNotebook creation FAILED!!!");   \r
60         }\r
61         // Drop the table\r
62         public void dropTable() {\r
63                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
64                 query.exec("Drop table SharedNotebook");\r
65         }\r
66         // Save an individual notebook\r
67         public void addNotebook(SharedNotebook tempNotebook, boolean isDirty) {\r
68                 boolean check;\r
69                 \r
70                 SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");\r
71         NSqlQuery query = new NSqlQuery(db.getConnection());\r
72                 check = query.prepare("Insert Into SharedNotebook (id, userid, notebookGuid, email,  "\r
73                                 +"notebookModifiable, requireLogin, serviceCreated, shareKey, username, isDirty) "   \r
74                                 + " Values("\r
75                                 +":id, :userid, :notebookGuid, :email, "\r
76                                 +":notebookModifiable, :requireLogin, :serviceCreated, "\r
77                                 +":shareKey, :username, :isDirty)");\r
78                 query.bindValue(":id", tempNotebook.getId());\r
79                 query.bindValue(":userid", tempNotebook.getUserId());\r
80                 query.bindValue(":notebookGuid", tempNotebook.getNotebookGuid());\r
81                 query.bindValue(":email", tempNotebook.getEmail());\r
82                 query.bindValue(":notebookModifiable", tempNotebook.isNotebookModifiable());\r
83                 query.bindValue(":requireLogin", tempNotebook.isRequireLogin());\r
84 \r
85                 StringBuilder serviceCreated = new StringBuilder(simple.format(tempNotebook.getServiceCreated()));                      \r
86                 query.bindValue(":serviceCreated", serviceCreated.toString());\r
87                 \r
88                 query.bindValue(":shareKey", tempNotebook.getShareKey());\r
89                 query.bindValue(":username", tempNotebook.getUsername());\r
90                 \r
91                 if (isDirty)\r
92                         query.bindValue(":isDirty", true);\r
93                 else\r
94                         query.bindValue(":isDirty", false);\r
95 \r
96                 check = query.exec();\r
97                 if (!check) {\r
98                         logger.log(logger.MEDIUM, "SharedNotebook Table insert failed.");\r
99                         logger.log(logger.MEDIUM, query.lastError().toString());\r
100                 }\r
101         }\r
102         // Check if a notebook exists\r
103         public boolean exists(long id) {\r
104         NSqlQuery query = new NSqlQuery(db.getConnection());\r
105         boolean check = query.prepare("Select id from sharednotebook where id=:id");\r
106         query.bindValue(":id", id);\r
107                 check = query.exec();\r
108                 if (!check) {\r
109                         logger.log(logger.MEDIUM, "SharedNotebook Table exists check failed.");\r
110                         logger.log(logger.MEDIUM, query.lastError().toString());\r
111                 }\r
112                 if (query.next())\r
113                         return true;\r
114                 return false;\r
115         }\r
116         // Delete the notebook based on a id\r
117         public void expungeNotebook(long id, boolean needsSync) {\r
118                 boolean check;\r
119         NSqlQuery query = new NSqlQuery(db.getConnection());\r
120 \r
121         check = query.prepare("delete from SharedNotebook "\r
122                                 +"where id=:id");\r
123                 if (!check) {\r
124                         logger.log(logger.EXTREME, "SharedNotebook SQL delete prepare has failed.");\r
125                         logger.log(logger.EXTREME, query.lastError().toString());\r
126                 }\r
127                 query.bindValue(":id", id);\r
128                 check = query.exec();\r
129                 if (!check) \r
130                         logger.log(logger.MEDIUM, "SharedNotebook delete failed.");\r
131                 \r
132                 // Signal the parent that work needs to be done\r
133                 if  (needsSync) {\r
134                         DeletedTable deletedTable = new DeletedTable(logger, db);\r
135                         deletedTable.addDeletedItem(new Long(id).toString(), "SharedNotebook");\r
136                 }\r
137         }\r
138         // Update a notebook\r
139         public void updateNotebook(SharedNotebook tempNotebook, boolean isDirty) {\r
140                 boolean check;\r
141                 if (!exists(tempNotebook.getId())) {\r
142                         addNotebook(tempNotebook, isDirty);\r
143                         return;\r
144                 }\r
145                 \r
146                 SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");\r
147                 \r
148         NSqlQuery query = new NSqlQuery(db.getConnection());\r
149         check = query.prepare("Update SharedNotebook set id=:id, userid=:userid, notebookGuid=:notebook, "\r
150                         + "email=:email, notebookModifiable=:mod, requireLogin=:rlogin, serviceCreated=:created, "\r
151                         + "shareKey=:shareKey, username=:username, isDirty=:isdirty");\r
152                 query.bindValue(":id", tempNotebook.getId());\r
153                 query.bindValue(":userid", tempNotebook.getUserId());\r
154                 query.bindValue(":notebook", tempNotebook.getNotebookGuid());\r
155                 query.bindValue(":email", tempNotebook.getEmail());\r
156                 query.bindValue(":mod", tempNotebook.isNotebookModifiable());\r
157                 query.bindValue(":rlogin", tempNotebook.isRequireLogin());\r
158                 query.bindValue(":shareKey", tempNotebook.getShareKey());\r
159                 query.bindValue(":username", tempNotebook.getUsername());\r
160 \r
161                 StringBuilder serviceCreated = new StringBuilder(simple.format(tempNotebook.getServiceCreated()));                                              \r
162                 query.bindValue(":serviceCreated", serviceCreated.toString());\r
163                 \r
164                 query.bindValue(":isDirty", isDirty);\r
165                 \r
166                 check = query.exec();\r
167                 if (!check) {\r
168                         logger.log(logger.MEDIUM, "SharedNotebook Table update failed.");\r
169                         logger.log(logger.MEDIUM, query.lastError().toString());\r
170                 }\r
171         }\r
172         // Load notebooks from the database\r
173         public List<SharedNotebook> getAll() {\r
174                 SharedNotebook tempNotebook;\r
175                 List<SharedNotebook> index = new ArrayList<SharedNotebook>();\r
176                 boolean check;\r
177                                         \r
178         NSqlQuery query = new NSqlQuery(db.getConnection());\r
179                                         \r
180                 check = query.exec("Select id, userid, notebookGuid, email, notebookModifiable, requireLogin, " +\r
181                                 "serviceCreated, "+\r
182                                 "shareKey, username from SharedNotebook");\r
183                 if (!check)\r
184                         logger.log(logger.EXTREME, "Notebook SQL retrieve has failed.");\r
185                 while (query.next()) {\r
186                         tempNotebook = new SharedNotebook();\r
187                         tempNotebook.setId(query.valueLong(0));\r
188                         tempNotebook.setUserId(query.valueInteger(1));\r
189                         tempNotebook.setNotebookGuid(query.valueString(2));\r
190                         tempNotebook.setEmail(query.valueString(3));\r
191                         tempNotebook.setNotebookModifiable(query.valueBoolean(4,false));\r
192                         tempNotebook.setRequireLogin(query.valueBoolean(5,true));\r
193                         DateFormat indfm = null;\r
194                         try {\r
195                                 indfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");\r
196                         } catch (Exception e) { }\r
197                         try {\r
198                                 tempNotebook.setServiceCreated(indfm.parse(query.valueString(6)).getTime());\r
199                         } catch (ParseException e) {\r
200                                 e.printStackTrace();\r
201                         }\r
202                         tempNotebook.setShareKey(query.valueString(7));\r
203                         tempNotebook.setUsername(query.valueString(8));\r
204 \r
205                         index.add(tempNotebook); \r
206                 }       \r
207                 return index;\r
208         }                       \r
209 \r
210         // does a record exist?\r
211         public String findNotebookByName(String newname) {\r
212                 \r
213                 NSqlQuery query = new NSqlQuery(db.getConnection());\r
214                 \r
215                 query.prepare("Select guid from sharednotebook where name=:newname");\r
216                 query.bindValue(":newname", newname);\r
217                 if (!query.exec())\r
218                         logger.log(logger.EXTREME, "notebook SQL retrieve has failed.");\r
219                 String val = null;\r
220                 if (query.next())\r
221                         val = query.valueString(0);\r
222                 return val;\r
223         }\r
224 \r
225 \r
226 }\r
227 \r