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.dao.NotebookDao; |
---|
21 | import eu.dasish.annotation.schema.Notebook; |
---|
22 | import eu.dasish.annotation.schema.NotebookInfo; |
---|
23 | import eu.dasish.annotation.schema.Permission; |
---|
24 | import java.sql.ResultSet; |
---|
25 | import java.sql.SQLException; |
---|
26 | import java.util.HashMap; |
---|
27 | import java.util.List; |
---|
28 | import java.util.Map; |
---|
29 | import java.util.UUID; |
---|
30 | import javax.sql.DataSource; |
---|
31 | import org.springframework.jdbc.core.RowMapper; |
---|
32 | import org.slf4j.Logger; |
---|
33 | import org.slf4j.LoggerFactory; |
---|
34 | |
---|
35 | /** |
---|
36 | * Created on : Jun 14, 2013, 3:27:04 PM |
---|
37 | * |
---|
38 | * @author Peter Withers <peter.withers@mpi.nl> |
---|
39 | */ |
---|
40 | // TODO: not updated fully yet. |
---|
41 | public class JdbcNotebookDao extends JdbcResourceDao implements NotebookDao { |
---|
42 | |
---|
43 | private final Logger loggerNotebookDao = LoggerFactory.getLogger(JdbcNotebookDao.class); |
---|
44 | |
---|
45 | public JdbcNotebookDao(DataSource dataSource) { |
---|
46 | setDataSource(dataSource); |
---|
47 | internalIdName = notebook_id; |
---|
48 | resourceTableName = notebookTableName; |
---|
49 | } |
---|
50 | |
---|
51 | /// GETTERS ///////// |
---|
52 | //////////////////////////////////////////////// |
---|
53 | //////////////////////////////////////////////// |
---|
54 | @Override |
---|
55 | public Number getOwner(Number notebookID) { |
---|
56 | if (notebookID == null) { |
---|
57 | loggerNotebookDao.debug("notebookID: " + nullArgument); |
---|
58 | return null; |
---|
59 | } |
---|
60 | StringBuilder sql = new StringBuilder("SELECT "); |
---|
61 | sql.append(owner_id).append(" FROM ").append(notebookTableName).append(" WHERE "). |
---|
62 | append(notebook_id).append(" = ?"); |
---|
63 | List<Number> result = getSimpleJdbcTemplate().query(sql.toString(), ownerIDRowMapper, notebookID); |
---|
64 | if (result.isEmpty()) { |
---|
65 | return null; |
---|
66 | } else { |
---|
67 | return result.get(0); |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | ////////////////////////////////////////////////// |
---|
72 | @Override |
---|
73 | public List<Map<Number, String>> getPermissions(Number notebookID) { |
---|
74 | if (notebookID == null) { |
---|
75 | loggerNotebookDao.debug(nullArgument); |
---|
76 | return null; |
---|
77 | } |
---|
78 | StringBuilder sql = new StringBuilder("SELECT "); |
---|
79 | sql.append(principal_id).append(",").append(permission).append(" FROM ").append(notebookPermissionsTableName).append(" WHERE ").append(notebook_id).append(" = ?"); |
---|
80 | return getSimpleJdbcTemplate().query(sql.toString(), principalsPermissionsRowMapper, notebookID); |
---|
81 | } |
---|
82 | |
---|
83 | ///////////// |
---|
84 | |
---|
85 | @Override |
---|
86 | public List<Number> getNotebookIDs(Number principalID, Permission permission) { |
---|
87 | |
---|
88 | if (principalID == null) { |
---|
89 | loggerNotebookDao.debug("princiaplID: " + nullArgument); |
---|
90 | return null; |
---|
91 | } |
---|
92 | |
---|
93 | if (permission == null) { |
---|
94 | loggerNotebookDao.debug("permission: " + nullArgument); |
---|
95 | return null; |
---|
96 | } |
---|
97 | |
---|
98 | Map<String, Object> params = new HashMap<String, Object>(); |
---|
99 | params.put("principalID", principalID); |
---|
100 | params.put("accessMode", permission.value()); |
---|
101 | StringBuilder sql = new StringBuilder("SELECT "); |
---|
102 | sql.append(notebook_id).append(" FROM ").append(notebookPermissionsTableName).append(" WHERE "). |
---|
103 | append(principal_id).append(" = :principalID AND ").append(this.permission).append(" = :accessMode"); |
---|
104 | return getSimpleJdbcTemplate().query(sql.toString(), internalIDRowMapper, params); |
---|
105 | } |
---|
106 | |
---|
107 | //////////////////////////////////////////////// |
---|
108 | @Override |
---|
109 | public List<Number> getNotebookIDsOwnedBy(Number principalID) { |
---|
110 | |
---|
111 | if (principalID == null) { |
---|
112 | loggerNotebookDao.debug("principalID: " + nullArgument); |
---|
113 | return null; |
---|
114 | } |
---|
115 | |
---|
116 | StringBuilder sql = new StringBuilder("SELECT "); |
---|
117 | sql.append(notebook_id).append(" FROM ").append(notebookTableName).append(" WHERE "). |
---|
118 | append(owner_id).append(" = ?"); |
---|
119 | return getSimpleJdbcTemplate().query(sql.toString(), internalIDRowMapper, principalID); |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | |
---|
124 | @Override |
---|
125 | public NotebookInfo getNotebookInfoWithoutOwner(Number notebookID) { |
---|
126 | if (notebookID == null) { |
---|
127 | loggerNotebookDao.debug("notebookID: " + nullArgument); |
---|
128 | return null; |
---|
129 | } |
---|
130 | |
---|
131 | StringBuilder sql = new StringBuilder("SELECT "); |
---|
132 | sql.append(external_id).append(" , ").append(title). |
---|
133 | append(" FROM ").append(notebookTableName).append(" WHERE "). |
---|
134 | append(notebook_id).append(" = :notebookID"); |
---|
135 | List<NotebookInfo> result = getSimpleJdbcTemplate().query(sql.toString(), notebookInfoRowMapper, notebookID); |
---|
136 | return (!result.isEmpty() ? result.get(0) : null); |
---|
137 | } |
---|
138 | private final RowMapper<NotebookInfo> notebookInfoRowMapper = new RowMapper<NotebookInfo>() { |
---|
139 | @Override |
---|
140 | public NotebookInfo mapRow(ResultSet rs, int rowNumber) throws SQLException { |
---|
141 | NotebookInfo notebookInfo = new NotebookInfo(); |
---|
142 | notebookInfo.setRef(externalIDtoURI(rs.getString(external_id))); |
---|
143 | notebookInfo.setTitle(rs.getString(title)); |
---|
144 | return notebookInfo; |
---|
145 | } |
---|
146 | }; |
---|
147 | |
---|
148 | @Override |
---|
149 | public Notebook getNotebookWithoutAnnotationsAndPermissionsAndOwner(Number notebookID) { |
---|
150 | if (notebookID == null) { |
---|
151 | loggerNotebookDao.debug("notebookID: " + nullArgument); |
---|
152 | return null; |
---|
153 | } |
---|
154 | StringBuilder sql = new StringBuilder("SELECT "); |
---|
155 | sql.append(external_id).append(" , ").append(title).append(" , ").append(last_modified). |
---|
156 | append(" FROM ").append(notebookTableName).append(" WHERE "). |
---|
157 | append(notebook_id).append(" = :notebookID"); |
---|
158 | List<Notebook> result = getSimpleJdbcTemplate().query(sql.toString(), notebookRowMapper, notebookID); |
---|
159 | return (!result.isEmpty() ? result.get(0) : null); |
---|
160 | } |
---|
161 | private final RowMapper<Notebook> notebookRowMapper = new RowMapper<Notebook>() { |
---|
162 | @Override |
---|
163 | public Notebook mapRow(ResultSet rs, int rowNumber) throws SQLException { |
---|
164 | Notebook notebook = new Notebook(); |
---|
165 | notebook.setTitle(rs.getString(title)); |
---|
166 | notebook.setLastModified(timeStampToXMLGregorianCalendar(rs.getString(last_modified))); |
---|
167 | notebook.setURI(externalIDtoURI(rs.getString(external_id))); |
---|
168 | return notebook; |
---|
169 | } |
---|
170 | }; |
---|
171 | |
---|
172 | |
---|
173 | /** |
---|
174 | * |
---|
175 | * UPDATERS |
---|
176 | * |
---|
177 | * |
---|
178 | */ |
---|
179 | /** |
---|
180 | * |
---|
181 | * @param notebookID |
---|
182 | * @return true if updated, false otherwise. Logs the reason if the notebook |
---|
183 | * is not updated. |
---|
184 | */ |
---|
185 | @Override |
---|
186 | public boolean updateNotebookMetadata(Number notebookID, String title, Number ownerID) { |
---|
187 | |
---|
188 | if (notebookID == null) { |
---|
189 | loggerNotebookDao.debug("notebookID: " + nullArgument); |
---|
190 | return false; |
---|
191 | } |
---|
192 | |
---|
193 | if (title == null) { |
---|
194 | loggerNotebookDao.debug("title: " + nullArgument); |
---|
195 | return false; |
---|
196 | } |
---|
197 | |
---|
198 | if (ownerID == null) { |
---|
199 | loggerNotebookDao.debug("ownerID: " + nullArgument); |
---|
200 | return false; |
---|
201 | } |
---|
202 | |
---|
203 | Map<String, Object> params = new HashMap<String, Object>(); |
---|
204 | params.put("notebookID", notebookID); |
---|
205 | params.put("title", title); |
---|
206 | params.put("owner", ownerID); |
---|
207 | |
---|
208 | StringBuilder sql = new StringBuilder("UPDATE "); |
---|
209 | sql.append(notebookTableName).append(" SET "). |
---|
210 | append(last_modified).append("= default,"). |
---|
211 | append(this.title).append("= :title, "). |
---|
212 | append(owner_id).append("= :owner"). |
---|
213 | append(" WHERE ").append(notebook_id).append("= :notebookID"); |
---|
214 | int affectedRows = getSimpleJdbcTemplate().update(sql.toString(), params); |
---|
215 | if (affectedRows <= 0) { |
---|
216 | logger.info("For some reason no rows in the table notebooks were updated. "); |
---|
217 | return false; |
---|
218 | } else { |
---|
219 | if (affectedRows > 1) { |
---|
220 | logger.info("For some reason more than 1 row in the table notebooks were updated. that's strange."); |
---|
221 | return true; |
---|
222 | } else { |
---|
223 | return true; |
---|
224 | } |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | @Override |
---|
229 | public boolean setOwner(Number notebookID, Number ownerID) { |
---|
230 | |
---|
231 | if (notebookID == null) { |
---|
232 | loggerNotebookDao.debug("notebookID: " + nullArgument); |
---|
233 | return false; |
---|
234 | } |
---|
235 | |
---|
236 | if (ownerID == null) { |
---|
237 | loggerNotebookDao.debug("ownerID: " + nullArgument); |
---|
238 | return false; |
---|
239 | } |
---|
240 | |
---|
241 | Map<String, Object> params = new HashMap<String, Object>(); |
---|
242 | params.put("notebookID", notebookID); |
---|
243 | params.put("ownerID", ownerID); |
---|
244 | |
---|
245 | StringBuilder sql = new StringBuilder("UPDATE "); |
---|
246 | sql.append(notebookTableName).append(" SET "). |
---|
247 | append(last_modified).append("= default,"). |
---|
248 | append(owner_id).append("= :ownerID"). |
---|
249 | append(" WHERE ").append(notebook_id).append("= :notebookID"); |
---|
250 | int affectedRows = getSimpleJdbcTemplate().update(sql.toString(), params); |
---|
251 | if (affectedRows <= 0) { |
---|
252 | logger.info("For some reason no rows in the table notebooks were updated. "); |
---|
253 | return false; |
---|
254 | } else { |
---|
255 | if (affectedRows > 1) { |
---|
256 | logger.info("For some reason more than 1 row in the table notebooks were updated. that's strange."); |
---|
257 | return true; |
---|
258 | } else { |
---|
259 | return true; |
---|
260 | } |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | @Override |
---|
265 | public boolean updateUserPermissionForNotebook(Number notebookID, Number principalID, Permission permission) { |
---|
266 | |
---|
267 | if (notebookID == null) { |
---|
268 | loggerNotebookDao.debug("notebookID: " + nullArgument); |
---|
269 | return false; |
---|
270 | } |
---|
271 | |
---|
272 | if (principalID == null) { |
---|
273 | loggerNotebookDao.debug("principalID: " + nullArgument); |
---|
274 | return false; |
---|
275 | } |
---|
276 | |
---|
277 | if (permission == null) { |
---|
278 | loggerNotebookDao.debug("permission: " + nullArgument); |
---|
279 | return false; |
---|
280 | } |
---|
281 | |
---|
282 | |
---|
283 | Map<String, Object> params = new HashMap<String, Object>(); |
---|
284 | params.put("notebookID", notebookID); |
---|
285 | params.put("principalID", principalID); |
---|
286 | params.put("permission", permission.value()); |
---|
287 | |
---|
288 | StringBuilder sql = new StringBuilder("UPDATE "); |
---|
289 | sql.append(notebookPermissionsTableName).append(" SET "). |
---|
290 | append(this.permission).append("= :permission "). |
---|
291 | append(" WHERE ").append(notebook_id).append("= :notebookID AND "). |
---|
292 | append(principal_id).append("= :principalID"); |
---|
293 | int affectedRows = getSimpleJdbcTemplate().update(sql.toString(), params); |
---|
294 | if (affectedRows <= 0) { |
---|
295 | logger.info("For some reason no rows in the table notebooks-permissions were updated. "); |
---|
296 | return false; |
---|
297 | } else { |
---|
298 | if (affectedRows > 1) { |
---|
299 | logger.info("For some reason more than 1 row in the table notebooks-permissions were updated. that's strange."); |
---|
300 | return true; |
---|
301 | } else { |
---|
302 | return true; |
---|
303 | } |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | /** |
---|
308 | * |
---|
309 | * ADDERS |
---|
310 | * |
---|
311 | * |
---|
312 | */ |
---|
313 | @Override |
---|
314 | public Number createNotebookWithoutPermissionsAndAnnotations(Notebook notebook, Number ownerID) { |
---|
315 | if (notebook == null) { |
---|
316 | loggerNotebookDao.debug("notebook: " + nullArgument); |
---|
317 | return null; |
---|
318 | } |
---|
319 | |
---|
320 | if (ownerID == null) { |
---|
321 | loggerNotebookDao.debug("ownerID: " + nullArgument); |
---|
322 | return null; |
---|
323 | } |
---|
324 | |
---|
325 | UUID externalID = UUID.randomUUID(); |
---|
326 | Map<String, Object> params = new HashMap<String, Object>(); |
---|
327 | params.put("externalId", externalID.toString()); |
---|
328 | params.put("owner", ownerID); |
---|
329 | params.put("title", notebook.getTitle()); |
---|
330 | |
---|
331 | StringBuilder sql = new StringBuilder("INSERT INTO "); |
---|
332 | sql.append(notebookTableName).append("(").append(external_id).append(",").append(owner_id); |
---|
333 | sql.append(",").append(title). |
---|
334 | append(" ) VALUES (:externalId, :owner, :title)"); |
---|
335 | int affectedRows = getSimpleJdbcTemplate().update(sql.toString(), params); |
---|
336 | return ((affectedRows > 0) ? getInternalID(externalID) : null); |
---|
337 | } |
---|
338 | |
---|
339 | @Override |
---|
340 | public boolean addAnnotationToNotebook(Number notebookID, Number annotationID) { |
---|
341 | |
---|
342 | if (notebookID == null) { |
---|
343 | loggerNotebookDao.debug("notebookID: " + nullArgument); |
---|
344 | return false; |
---|
345 | } |
---|
346 | |
---|
347 | if (annotationID == null) { |
---|
348 | loggerNotebookDao.debug("annotationID: " + nullArgument); |
---|
349 | return false; |
---|
350 | } |
---|
351 | |
---|
352 | Map<String, Object> params = new HashMap<String, Object>(); |
---|
353 | params.put("notebookID", notebookID); |
---|
354 | params.put("annotationID", annotationID); |
---|
355 | |
---|
356 | StringBuilder sql = new StringBuilder("INSERT INTO "); |
---|
357 | sql.append(notebooksAnnotationsTableName).append("(").append(notebook_id).append(",").append(annotation_id); |
---|
358 | sql.append(" ) VALUES (:notebookID, :annotationID)"); |
---|
359 | int affectedRows = getSimpleJdbcTemplate().update(sql.toString(), params); |
---|
360 | return (affectedRows > 0); |
---|
361 | } |
---|
362 | |
---|
363 | @Override |
---|
364 | public boolean addPermissionToNotebook(Number notebookID, Number principalID, Permission permission) { |
---|
365 | if (notebookID == null) { |
---|
366 | loggerNotebookDao.debug("notebookID: " + nullArgument); |
---|
367 | return false; |
---|
368 | } |
---|
369 | |
---|
370 | if (principalID == null) { |
---|
371 | loggerNotebookDao.debug("principalID: " + nullArgument); |
---|
372 | return false; |
---|
373 | } |
---|
374 | |
---|
375 | if (permission == null) { |
---|
376 | loggerNotebookDao.debug("premission: " + nullArgument); |
---|
377 | return false; |
---|
378 | } |
---|
379 | |
---|
380 | Map<String, Object> params = new HashMap<String, Object>(); |
---|
381 | params.put("notebookID", notebookID); |
---|
382 | params.put("principalID", principalID); |
---|
383 | params.put("permission", permission.value()); |
---|
384 | |
---|
385 | StringBuilder sql = new StringBuilder("INSERT INTO "); |
---|
386 | sql.append(notebookPermissionsTableName).append("(").append(notebook_id).append(",").append(principal_id); |
---|
387 | sql.append(",").append(this.permission). |
---|
388 | append(" ) VALUES (:notebookID, :principalID, :permission)"); |
---|
389 | int affectedRows = getSimpleJdbcTemplate().update(sql.toString(), params); |
---|
390 | return (affectedRows > 0); |
---|
391 | } |
---|
392 | |
---|
393 | /** |
---|
394 | * |
---|
395 | * DELETERS |
---|
396 | * |
---|
397 | * |
---|
398 | */ |
---|
399 | @Override |
---|
400 | public boolean deleteAnnotationFromNotebook(Number notebookID, Number annotationID) { |
---|
401 | if (notebookID != null) { |
---|
402 | if (annotationID != null) { |
---|
403 | Map<String, Number> params = new HashMap(); |
---|
404 | params.put("notebookID", notebookID); |
---|
405 | params.put("annotationID", annotationID); |
---|
406 | StringBuilder sql = new StringBuilder("DELETE FROM "); |
---|
407 | sql.append(notebooksAnnotationsTableName).append(" WHERE ").append(notebook_id).append(" = :notebookID AND "). |
---|
408 | append(annotation_id).append(" = :annotationID"); |
---|
409 | int affectedRows = getSimpleJdbcTemplate().update(sql.toString(), params); |
---|
410 | return (affectedRows > 0); |
---|
411 | } else { |
---|
412 | loggerNotebookDao.debug("annotationID: " + nullArgument); |
---|
413 | return false; |
---|
414 | } |
---|
415 | } else { |
---|
416 | loggerNotebookDao.debug("notebookID: " + nullArgument); |
---|
417 | return false; |
---|
418 | } |
---|
419 | } |
---|
420 | |
---|
421 | @Override |
---|
422 | public boolean deleteNotebookPrincipalPermission(Number notebookID, Number principalID) { |
---|
423 | if (notebookID != null) { |
---|
424 | if (principalID != null) { |
---|
425 | Map<String, Number> params = new HashMap(); |
---|
426 | params.put("notebookID", notebookID); |
---|
427 | params.put("principalID", principalID); |
---|
428 | StringBuilder sqlPermissions = new StringBuilder("DELETE FROM "); |
---|
429 | sqlPermissions.append(notebookPermissionsTableName).append(" WHERE ").append(notebook_id).append(" = :notebookID AND "). |
---|
430 | append(principal_id).append(" = :principalID"); |
---|
431 | int affectedRows = getSimpleJdbcTemplate().update(sqlPermissions.toString(), params); |
---|
432 | return (affectedRows > 0); |
---|
433 | } else { |
---|
434 | loggerNotebookDao.debug("principalID: " + nullArgument); |
---|
435 | return false; |
---|
436 | } |
---|
437 | } else { |
---|
438 | loggerNotebookDao.debug("notebookID: " + nullArgument); |
---|
439 | return false; |
---|
440 | } |
---|
441 | } |
---|
442 | |
---|
443 | @Override |
---|
444 | public boolean deleteAllAnnotationsFromNotebook(Number notebookID) { |
---|
445 | if (notebookID != null) { |
---|
446 | StringBuilder sql = new StringBuilder("DELETE FROM "); |
---|
447 | sql.append(notebooksAnnotationsTableName).append(" WHERE ").append(notebook_id).append(" = ? "); |
---|
448 | int affectedRows = getSimpleJdbcTemplate().update(sql.toString(), notebookID); |
---|
449 | return (affectedRows > 0); |
---|
450 | } else { |
---|
451 | loggerNotebookDao.debug("notebookID: " + nullArgument); |
---|
452 | return false; |
---|
453 | } |
---|
454 | } |
---|
455 | |
---|
456 | @Override |
---|
457 | public boolean deleteAllPermissionsForNotebook(Number notebookID) { |
---|
458 | if (notebookID != null) { |
---|
459 | StringBuilder sqlPermissions = new StringBuilder("DELETE FROM "); |
---|
460 | sqlPermissions.append(notebookPermissionsTableName).append(" WHERE ").append(notebook_id).append(" = ? "); |
---|
461 | int affectedRows = getSimpleJdbcTemplate().update(sqlPermissions.toString(), notebookID); |
---|
462 | return (affectedRows > 0); |
---|
463 | } else { |
---|
464 | loggerNotebookDao.debug("notebookID: " + nullArgument); |
---|
465 | return false; |
---|
466 | } |
---|
467 | } |
---|
468 | |
---|
469 | @Override |
---|
470 | public boolean deleteNotebook(Number notebookID) { |
---|
471 | if (notebookID != null) { |
---|
472 | StringBuilder sql = new StringBuilder("DELETE FROM "); |
---|
473 | sql.append(notebookTableName).append(" WHERE ").append(notebook_id).append(" = ? "); |
---|
474 | int affectedRows = getSimpleJdbcTemplate().update(sql.toString(), notebookID); |
---|
475 | return (affectedRows > 0); |
---|
476 | } else { |
---|
477 | loggerNotebookDao.debug("notebookID: " + nullArgument); |
---|
478 | return false; |
---|
479 | } |
---|
480 | } |
---|
481 | } |
---|