1 | /* |
---|
2 | * Copyright (C) 2013 DASISH |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or |
---|
5 | * modify it under the terms of the GNU General Public License |
---|
6 | * as published by the Free Software Foundation; either version 2 |
---|
7 | * of the License, or (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
17 | */ |
---|
18 | package eu.dasish.annotation.backend.dao.impl; |
---|
19 | |
---|
20 | import eu.dasish.annotation.backend.NotInDataBaseException; |
---|
21 | import eu.dasish.annotation.backend.dao.NotebookDao; |
---|
22 | import eu.dasish.annotation.schema.Notebook; |
---|
23 | import eu.dasish.annotation.schema.NotebookInfo; |
---|
24 | import eu.dasish.annotation.schema.Access; |
---|
25 | import java.sql.ResultSet; |
---|
26 | import java.sql.SQLException; |
---|
27 | import java.util.HashMap; |
---|
28 | import java.util.List; |
---|
29 | import java.util.Map; |
---|
30 | import java.util.UUID; |
---|
31 | import javax.sql.DataSource; |
---|
32 | import org.springframework.jdbc.core.RowMapper; |
---|
33 | import org.slf4j.Logger; |
---|
34 | import org.slf4j.LoggerFactory; |
---|
35 | |
---|
36 | /** |
---|
37 | * Created on : Jun 14, 2013, 3:27:04 PM |
---|
38 | * |
---|
39 | * @author Peter Withers <peter.withers@mpi.nl> |
---|
40 | */ |
---|
41 | // TODO: not updated fully yet. |
---|
42 | public class JdbcNotebookDao extends JdbcResourceDao implements NotebookDao { |
---|
43 | |
---|
44 | private final Logger loggerNotebookDao = LoggerFactory.getLogger(JdbcNotebookDao.class); |
---|
45 | |
---|
46 | public JdbcNotebookDao(DataSource dataSource) { |
---|
47 | setDataSource(dataSource); |
---|
48 | internalIdName = notebook_id; |
---|
49 | resourceTableName = notebookTableName; |
---|
50 | } |
---|
51 | |
---|
52 | /// GETTERS ///////// |
---|
53 | //////////////////////////////////////////////// |
---|
54 | //////////////////////////////////////////////// |
---|
55 | @Override |
---|
56 | public Number getOwner(Number notebookID){ |
---|
57 | StringBuilder sql = new StringBuilder("SELECT "); |
---|
58 | sql.append(owner_id).append(" FROM ").append(notebookTableName).append(" WHERE "). |
---|
59 | append(notebook_id).append(" = ?"); |
---|
60 | List<Number> result = this.loggedQuery(sql.toString(), ownerIDRowMapper, notebookID); |
---|
61 | return result.get(0); |
---|
62 | |
---|
63 | } |
---|
64 | |
---|
65 | ////////////////////////////////////////////////// |
---|
66 | @Override |
---|
67 | public List<Map<Number, String>> getPermissions(Number notebookID) { |
---|
68 | |
---|
69 | StringBuilder sql = new StringBuilder("SELECT "); |
---|
70 | sql.append(principal_id).append(",").append(access).append(" FROM ").append(notebookPermissionsTableName).append(" WHERE ").append(notebook_id).append(" = ?"); |
---|
71 | return this.loggedQuery(sql.toString(), principalsAccesssRowMapper, notebookID); |
---|
72 | } |
---|
73 | |
---|
74 | ///////////// |
---|
75 | @Override |
---|
76 | public List<Number> getNotebookIDs(Number principalID, Access access) { |
---|
77 | Map<String, Object> params = new HashMap<String, Object>(); |
---|
78 | params.put("principalID", principalID); |
---|
79 | params.put("accessMode", access.value()); |
---|
80 | StringBuilder sql = new StringBuilder("SELECT "); |
---|
81 | sql.append(notebook_id).append(" FROM ").append(notebookPermissionsTableName).append(" WHERE "). |
---|
82 | append(principal_id).append(" = :principalID AND ").append(this.access).append(" = :accessMode"); |
---|
83 | return this.loggedQuery(sql.toString(), internalIDRowMapper, params); |
---|
84 | } |
---|
85 | |
---|
86 | //////////////////////////////////////////////// |
---|
87 | @Override |
---|
88 | public List<Number> getNotebookIDsOwnedBy(Number principalID) { |
---|
89 | |
---|
90 | StringBuilder sql = new StringBuilder("SELECT "); |
---|
91 | sql.append(notebook_id).append(" FROM ").append(notebookTableName).append(" WHERE "). |
---|
92 | append(owner_id).append(" = ?"); |
---|
93 | return this.loggedQuery(sql.toString(), internalIDRowMapper, principalID); |
---|
94 | } |
---|
95 | |
---|
96 | @Override |
---|
97 | public NotebookInfo getNotebookInfoWithoutOwner(Number notebookID){ |
---|
98 | StringBuilder sql = new StringBuilder("SELECT "); |
---|
99 | sql.append(external_id).append(" , ").append(title). |
---|
100 | append(" FROM ").append(notebookTableName).append(" WHERE "). |
---|
101 | append(notebook_id).append(" = :notebookID"); |
---|
102 | List<NotebookInfo> result = this.loggedQuery(sql.toString(), notebookInfoRowMapper, notebookID); |
---|
103 | return result.get(0); |
---|
104 | } |
---|
105 | private final RowMapper<NotebookInfo> notebookInfoRowMapper = new RowMapper<NotebookInfo>() { |
---|
106 | @Override |
---|
107 | public NotebookInfo mapRow(ResultSet rs, int rowNumber) throws SQLException { |
---|
108 | NotebookInfo notebookInfo = new NotebookInfo(); |
---|
109 | notebookInfo.setRef(externalIDtoURI(rs.getString(external_id))); |
---|
110 | notebookInfo.setTitle(rs.getString(title)); |
---|
111 | return notebookInfo; |
---|
112 | } |
---|
113 | }; |
---|
114 | |
---|
115 | @Override |
---|
116 | public Notebook getNotebookWithoutAnnotationsAndAccesssAndOwner(Number notebookID){ |
---|
117 | StringBuilder sql = new StringBuilder("SELECT "); |
---|
118 | sql.append(external_id).append(" , ").append(title).append(" , ").append(last_modified). |
---|
119 | append(" FROM ").append(notebookTableName).append(" WHERE "). |
---|
120 | append(notebook_id).append(" = :notebookID"); |
---|
121 | List<Notebook> result = this.loggedQuery(sql.toString(), notebookRowMapper, notebookID); |
---|
122 | return result.get(0); |
---|
123 | } |
---|
124 | private final RowMapper<Notebook> notebookRowMapper = new RowMapper<Notebook>() { |
---|
125 | @Override |
---|
126 | public Notebook mapRow(ResultSet rs, int rowNumber) throws SQLException { |
---|
127 | Notebook notebook = new Notebook(); |
---|
128 | notebook.setTitle(rs.getString(title)); |
---|
129 | notebook.setLastModified(timeStampToXMLGregorianCalendar(rs.getString(last_modified))); |
---|
130 | notebook.setURI(externalIDtoURI(rs.getString(external_id))); |
---|
131 | return notebook; |
---|
132 | } |
---|
133 | }; |
---|
134 | |
---|
135 | /** |
---|
136 | * |
---|
137 | * UPDATERS |
---|
138 | * |
---|
139 | * |
---|
140 | */ |
---|
141 | /** |
---|
142 | * |
---|
143 | * @param notebookID |
---|
144 | * @return true if updated, false otherwise. Logs the reason if the notebook |
---|
145 | * is not updated. |
---|
146 | */ |
---|
147 | @Override |
---|
148 | public boolean updateNotebookMetadata(Number notebookID, String title, Number ownerID) { |
---|
149 | |
---|
150 | |
---|
151 | Map<String, Object> params = new HashMap<String, Object>(); |
---|
152 | params.put("notebookID", notebookID); |
---|
153 | params.put("title", title); |
---|
154 | params.put("owner", ownerID); |
---|
155 | |
---|
156 | StringBuilder sql = new StringBuilder("UPDATE "); |
---|
157 | sql.append(notebookTableName).append(" SET "). |
---|
158 | append(last_modified).append("= default,"). |
---|
159 | append(this.title).append("= :title, "). |
---|
160 | append(owner_id).append("= :owner"). |
---|
161 | append(" WHERE ").append(notebook_id).append("= :notebookID"); |
---|
162 | int affectedRows = this.loggedUpdate(sql.toString(), params); |
---|
163 | if (affectedRows <= 0) { |
---|
164 | logger.info("For some reason no rows in the table notebooks were updated. "); |
---|
165 | return false; |
---|
166 | } else { |
---|
167 | if (affectedRows > 1) { |
---|
168 | logger.info("For some reason more than 1 row in the table notebooks were updated. that's strange."); |
---|
169 | return true; |
---|
170 | } else { |
---|
171 | return true; |
---|
172 | } |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | @Override |
---|
177 | public boolean setOwner(Number notebookID, Number ownerID) { |
---|
178 | |
---|
179 | Map<String, Object> params = new HashMap<String, Object>(); |
---|
180 | params.put("notebookID", notebookID); |
---|
181 | params.put("ownerID", ownerID); |
---|
182 | |
---|
183 | StringBuilder sql = new StringBuilder("UPDATE "); |
---|
184 | sql.append(notebookTableName).append(" SET "). |
---|
185 | append(last_modified).append("= default,"). |
---|
186 | append(owner_id).append("= :ownerID"). |
---|
187 | append(" WHERE ").append(notebook_id).append("= :notebookID"); |
---|
188 | int affectedRows = this.loggedUpdate(sql.toString(), params); |
---|
189 | if (affectedRows <= 0) { |
---|
190 | logger.info("For some reason no rows in the table notebooks were updated. "); |
---|
191 | return false; |
---|
192 | } else { |
---|
193 | if (affectedRows > 1) { |
---|
194 | logger.info("For some reason more than 1 row in the table notebooks were updated. that's strange."); |
---|
195 | return true; |
---|
196 | } else { |
---|
197 | return true; |
---|
198 | } |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | @Override |
---|
203 | public boolean updatePrincipalAccessForNotebook(Number notebookID, Number principalID, Access access) { |
---|
204 | |
---|
205 | Map<String, Object> params = new HashMap<String, Object>(); |
---|
206 | params.put("notebookID", notebookID); |
---|
207 | params.put("principalID", principalID); |
---|
208 | params.put("access", access.value()); |
---|
209 | |
---|
210 | StringBuilder sql = new StringBuilder("UPDATE "); |
---|
211 | sql.append(notebookPermissionsTableName).append(" SET "). |
---|
212 | append(this.access).append("= :access "). |
---|
213 | append(" WHERE ").append(notebook_id).append("= :notebookID AND "). |
---|
214 | append(principal_id).append("= :principalID"); |
---|
215 | int affectedRows = this.loggedUpdate(sql.toString(), params); |
---|
216 | if (affectedRows <= 0) { |
---|
217 | logger.info("For some reason no rows in the table notebooks-accesss were updated. "); |
---|
218 | return false; |
---|
219 | } else { |
---|
220 | if (affectedRows > 1) { |
---|
221 | logger.info("For some reason more than 1 row in the table notebooks-accesss were updated. that's strange."); |
---|
222 | return true; |
---|
223 | } else { |
---|
224 | return true; |
---|
225 | } |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | /** |
---|
230 | * |
---|
231 | * ADDERS |
---|
232 | * |
---|
233 | * |
---|
234 | */ |
---|
235 | @Override |
---|
236 | public Number createNotebookWithoutAccesssAndAnnotations(Notebook notebook, Number ownerID) throws NotInDataBaseException { |
---|
237 | |
---|
238 | UUID externalID = UUID.randomUUID(); |
---|
239 | Map<String, Object> params = new HashMap<String, Object>(); |
---|
240 | params.put("externalId", externalID.toString()); |
---|
241 | params.put("owner", ownerID); |
---|
242 | params.put("title", notebook.getTitle()); |
---|
243 | |
---|
244 | StringBuilder sql = new StringBuilder("INSERT INTO "); |
---|
245 | sql.append(notebookTableName).append("(").append(external_id).append(",").append(owner_id); |
---|
246 | sql.append(",").append(title). |
---|
247 | append(" ) VALUES (:externalId, :owner, :title)"); |
---|
248 | int affectedRows = this.loggedUpdate(sql.toString(), params); |
---|
249 | return getInternalID(externalID); |
---|
250 | } |
---|
251 | |
---|
252 | @Override |
---|
253 | public boolean addAnnotationToNotebook(Number notebookID, Number annotationID) { |
---|
254 | |
---|
255 | Map<String, Object> params = new HashMap<String, Object>(); |
---|
256 | params.put("notebookID", notebookID); |
---|
257 | params.put("annotationID", annotationID); |
---|
258 | |
---|
259 | StringBuilder sql = new StringBuilder("INSERT INTO "); |
---|
260 | sql.append(notebooksAnnotationsTableName).append("(").append(notebook_id).append(",").append(annotation_id); |
---|
261 | sql.append(" ) VALUES (:notebookID, :annotationID)"); |
---|
262 | int affectedRows = this.loggedUpdate(sql.toString(), params); |
---|
263 | return (affectedRows > 0); |
---|
264 | } |
---|
265 | |
---|
266 | @Override |
---|
267 | public boolean addAccessToNotebook(Number notebookID, Number principalID, Access access) { |
---|
268 | |
---|
269 | Map<String, Object> params = new HashMap<String, Object>(); |
---|
270 | params.put("notebookID", notebookID); |
---|
271 | params.put("principalID", principalID); |
---|
272 | params.put("access", access.value()); |
---|
273 | |
---|
274 | StringBuilder sql = new StringBuilder("INSERT INTO "); |
---|
275 | sql.append(notebookPermissionsTableName).append("(").append(notebook_id).append(",").append(principal_id); |
---|
276 | sql.append(",").append(this.access). |
---|
277 | append(" ) VALUES (:notebookID, :principalID, :access)"); |
---|
278 | int affectedRows = this.loggedUpdate(sql.toString(), params); |
---|
279 | return (affectedRows > 0); |
---|
280 | } |
---|
281 | |
---|
282 | /** |
---|
283 | * |
---|
284 | * DELETERS |
---|
285 | * |
---|
286 | * |
---|
287 | */ |
---|
288 | @Override |
---|
289 | public boolean deleteAnnotationFromNotebook(Number notebookID, Number annotationID) { |
---|
290 | |
---|
291 | Map<String, Number> params = new HashMap(); |
---|
292 | params.put("notebookID", notebookID); |
---|
293 | params.put("annotationID", annotationID); |
---|
294 | StringBuilder sql = new StringBuilder("DELETE FROM "); |
---|
295 | sql.append(notebooksAnnotationsTableName).append(" WHERE ").append(notebook_id).append(" = :notebookID AND "). |
---|
296 | append(annotation_id).append(" = :annotationID"); |
---|
297 | int affectedRows = this.loggedUpdate(sql.toString(), params); |
---|
298 | return (affectedRows > 0); |
---|
299 | |
---|
300 | } |
---|
301 | |
---|
302 | @Override |
---|
303 | public boolean deleteNotebookPrincipalAccess(Number notebookID, Number principalID) { |
---|
304 | |
---|
305 | Map<String, Number> params = new HashMap(); |
---|
306 | params.put("notebookID", notebookID); |
---|
307 | params.put("principalID", principalID); |
---|
308 | StringBuilder sqlAccesss = new StringBuilder("DELETE FROM "); |
---|
309 | sqlAccesss.append(notebookPermissionsTableName).append(" WHERE ").append(notebook_id).append(" = :notebookID AND "). |
---|
310 | append(principal_id).append(" = :principalID"); |
---|
311 | int affectedRows = this.loggedUpdate(sqlAccesss.toString(), params); |
---|
312 | return (affectedRows > 0); |
---|
313 | |
---|
314 | } |
---|
315 | |
---|
316 | @Override |
---|
317 | public boolean deleteAllAnnotationsFromNotebook(Number notebookID) { |
---|
318 | StringBuilder sql = new StringBuilder("DELETE FROM "); |
---|
319 | sql.append(notebooksAnnotationsTableName).append(" WHERE ").append(notebook_id).append(" = ? "); |
---|
320 | int affectedRows = this.loggedUpdate(sql.toString(), notebookID); |
---|
321 | return (affectedRows > 0); |
---|
322 | |
---|
323 | } |
---|
324 | |
---|
325 | @Override |
---|
326 | public boolean deleteAllAccesssForNotebook(Number notebookID) { |
---|
327 | |
---|
328 | StringBuilder sqlAccesss = new StringBuilder("DELETE FROM "); |
---|
329 | sqlAccesss.append(notebookPermissionsTableName).append(" WHERE ").append(notebook_id).append(" = ? "); |
---|
330 | int affectedRows = this.loggedUpdate(sqlAccesss.toString(), notebookID); |
---|
331 | return (affectedRows > 0); |
---|
332 | |
---|
333 | } |
---|
334 | |
---|
335 | @Override |
---|
336 | public boolean deleteNotebook(Number notebookID) { |
---|
337 | StringBuilder sql = new StringBuilder("DELETE FROM "); |
---|
338 | sql.append(notebookTableName).append(" WHERE ").append(notebook_id).append(" = ? "); |
---|
339 | int affectedRows = this.loggedUpdate(sql.toString(), notebookID); |
---|
340 | return (affectedRows > 0); |
---|
341 | |
---|
342 | } |
---|
343 | } |
---|