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.Helpers; |
---|
21 | import eu.dasish.annotation.backend.dao.ResourceDao; |
---|
22 | import eu.dasish.annotation.backend.identifiers.DasishIdentifier; |
---|
23 | import java.sql.ResultSet; |
---|
24 | import java.sql.SQLException; |
---|
25 | import java.util.ArrayList; |
---|
26 | import java.util.List; |
---|
27 | import javax.xml.datatype.DatatypeConfigurationException; |
---|
28 | import javax.xml.datatype.XMLGregorianCalendar; |
---|
29 | import org.springframework.jdbc.core.RowMapper; |
---|
30 | import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport; |
---|
31 | |
---|
32 | /** |
---|
33 | * |
---|
34 | * @author olhsha |
---|
35 | */ |
---|
36 | public class JdbcResourceDao extends SimpleJdbcDaoSupport implements ResourceDao { |
---|
37 | |
---|
38 | // base string constants: resource table Names |
---|
39 | final static protected String notebookTableName = "notebook"; |
---|
40 | final static protected String annotationTableName = "annotation"; |
---|
41 | final static protected String sourceTableName = "target_source"; |
---|
42 | final static protected String cachedRepresentationTableName = "cached_representation_info"; |
---|
43 | final static protected String versionTableName = "version"; |
---|
44 | final static protected String principalTableName = "principal"; |
---|
45 | // joint tablenames |
---|
46 | final static protected String notebooksAnnotationsTableName = "notebooks_annotations"; |
---|
47 | final static protected String permissionsTableName = "annotations_principals_permissions"; |
---|
48 | final static protected String annotationsSourcesTableName = "annotations_target_sources"; |
---|
49 | final static protected String versionsCachedRepresentationsTableName = "versions_cached_representations"; |
---|
50 | final static protected String sourcesVersionsTableName = "sources_versions"; |
---|
51 | // base string constants: field Names |
---|
52 | final static protected String annotation_id = "annotation_id"; |
---|
53 | final static protected String notebook_id = "notebook_id"; |
---|
54 | final static protected String source_id = "source_id"; |
---|
55 | final static protected String external_id = "external_id"; |
---|
56 | final static protected String owner_id = "owner_id"; |
---|
57 | final static protected String headline = "headline"; |
---|
58 | final static protected String body_xml = "body_xml"; |
---|
59 | final static protected String title = "title"; |
---|
60 | final static protected String principal_id = "principal_id"; |
---|
61 | final static protected String time_stamp = "time_stamp"; |
---|
62 | final static protected String permission = "permission_"; |
---|
63 | final static protected String link_uri = "link_uri"; |
---|
64 | final static protected String version = "version"; |
---|
65 | final static protected String cached_representation_id = "cached_representation_id"; |
---|
66 | final static protected String version_id = "version_id"; |
---|
67 | final static protected String mime_type = "mime_type"; |
---|
68 | final static protected String tool = "tool"; |
---|
69 | final static protected String type_ = "type_"; |
---|
70 | final static protected String where_is_the_file = "where_is_the_file"; |
---|
71 | // derived string constants: table+field names |
---|
72 | final static protected String annotationStar = annotationTableName + ".*"; |
---|
73 | final static protected String annotationAnnotation_id = annotationTableName + "." + annotation_id; |
---|
74 | final static protected String annotationExternal_id = annotationTableName + "." + external_id; |
---|
75 | final static protected String notebookStar = notebookTableName + ".*"; |
---|
76 | final static protected String notebookNotebook_id = notebookTableName + "." + notebook_id; |
---|
77 | final static protected String notebookTitle = notebookTableName + "." + title; |
---|
78 | final static protected String notebookExternal_id = notebookTableName + "." + external_id; |
---|
79 | final static protected String notebookOwner_id = notebookTableName + "." + owner_id; |
---|
80 | final static protected String notebooksAnnotationsTableNameAnnotation_id = notebooksAnnotationsTableName + "." + annotation_id; |
---|
81 | final static protected String principalPrincipal_id = principalTableName + "." + principal_id; |
---|
82 | final static protected String principalExternal_id = principalTableName + "." + external_id; |
---|
83 | final static protected String cachedRepresentationStar = cachedRepresentationTableName + ".*"; |
---|
84 | final static protected String versionStar = versionTableName + ".*"; |
---|
85 | final static protected String sourceStar = sourceTableName + ".*"; |
---|
86 | /////////////////////////////////////////////////// |
---|
87 | protected String internalIdName = null; |
---|
88 | protected String resourceTableName = null; |
---|
89 | |
---|
90 | ////////////////////////////////////////////////////////////////////////////////// |
---|
91 | @Override |
---|
92 | public <T extends DasishIdentifier> Number getInternalID(T externalId) { |
---|
93 | if (externalId == null) { |
---|
94 | return null; |
---|
95 | } |
---|
96 | String sql = "SELECT " + internalIdName + " FROM " + resourceTableName + " WHERE " + external_id + "= ? LIMIT 1"; |
---|
97 | List<Number> sqlResult = getSimpleJdbcTemplate().query(sql, internalIDRowMapper, externalId.toString()); |
---|
98 | |
---|
99 | if (sqlResult == null) { |
---|
100 | return null; |
---|
101 | } |
---|
102 | if (sqlResult.isEmpty()) { |
---|
103 | return null; |
---|
104 | } |
---|
105 | |
---|
106 | Number result = sqlResult.get(0); |
---|
107 | return result; |
---|
108 | } |
---|
109 | protected final RowMapper<Number> internalIDRowMapper = new RowMapper<Number>() { |
---|
110 | @Override |
---|
111 | public Number mapRow(ResultSet rs, int rowNumber) throws SQLException { |
---|
112 | int result = rs.getInt(internalIdName); |
---|
113 | Number resultNumber = result; |
---|
114 | return resultNumber; |
---|
115 | } |
---|
116 | }; |
---|
117 | |
---|
118 | |
---|
119 | ///////////////////////////////////////////// |
---|
120 | protected String getExternalIdentifier(Number internalId) { |
---|
121 | if (internalId == null) { |
---|
122 | return null; |
---|
123 | } |
---|
124 | String sql = "SELECT " + external_id + " FROM " + resourceTableName + " WHERE " + internalIdName + "= ? LIMIT 1"; |
---|
125 | List<String> sqlResult = getSimpleJdbcTemplate().query(sql, externalIDRowMapper, internalId); |
---|
126 | |
---|
127 | if (sqlResult == null) { |
---|
128 | return null; |
---|
129 | } |
---|
130 | if (sqlResult.isEmpty()) { |
---|
131 | return null; |
---|
132 | } |
---|
133 | |
---|
134 | return (sqlResult.get(0)); |
---|
135 | } |
---|
136 | |
---|
137 | protected final RowMapper<String> externalIDRowMapper = new RowMapper<String>() { |
---|
138 | @Override |
---|
139 | public String mapRow(ResultSet rs, int rowNumber) throws SQLException { |
---|
140 | return (rs.getString(external_id)); |
---|
141 | } |
---|
142 | }; |
---|
143 | |
---|
144 | ///////////////////////////////////////////////////// |
---|
145 | protected XMLGregorianCalendar retrieveTimeStamp(Number internalID) { |
---|
146 | String sqlTime = "SELECT " + time_stamp + " FROM " + resourceTableName + " WHERE " + internalIdName + "= ? LIMIT 1"; |
---|
147 | List<XMLGregorianCalendar> timeStamp = getSimpleJdbcTemplate().query(sqlTime, timeStampRowMapper, internalID); |
---|
148 | if (timeStamp.isEmpty()) { |
---|
149 | return null; |
---|
150 | } |
---|
151 | return timeStamp.get(0); |
---|
152 | } |
---|
153 | protected final RowMapper<XMLGregorianCalendar> timeStampRowMapper = new RowMapper<XMLGregorianCalendar>() { |
---|
154 | @Override |
---|
155 | public XMLGregorianCalendar mapRow(ResultSet rs, int rowNumber) throws SQLException { |
---|
156 | try { |
---|
157 | XMLGregorianCalendar result = Helpers.setXMLGregorianCalendar(rs.getTimestamp(time_stamp)); |
---|
158 | return result; |
---|
159 | } catch (DatatypeConfigurationException e) { |
---|
160 | System.out.println(e); |
---|
161 | return null; |
---|
162 | } |
---|
163 | } |
---|
164 | }; |
---|
165 | |
---|
166 | protected <T> String makeListOfValues(List<T> vals) { |
---|
167 | |
---|
168 | if (vals == null) { |
---|
169 | return null; |
---|
170 | } |
---|
171 | |
---|
172 | if (vals.isEmpty()) { |
---|
173 | return null; |
---|
174 | } |
---|
175 | |
---|
176 | String result = "("; |
---|
177 | int length = vals.size(); |
---|
178 | for (int i = 0; i < length - 1; i++) { |
---|
179 | result = result + vals.get(i).toString() + ", "; |
---|
180 | } |
---|
181 | result = result + vals.get(length - 1).toString() + ")"; |
---|
182 | return result; |
---|
183 | } |
---|
184 | } |
---|