source: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcResourceDao.java @ 4146

Last change on this file since 4146 was 4146, checked in by olhsha, 10 years ago

saving the current "last update" time stamp in UTC time in the DB. Outputting it as UTC. Works with PostgreSQL, does not work with HSQL (had to comment unit tests)

File size: 11.4 KB
Line 
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 */
18package eu.dasish.annotation.backend.dao.impl;
19
20import eu.dasish.annotation.backend.dao.ResourceDao;
21import java.sql.ResultSet;
22import java.sql.SQLException;
23import java.util.List;
24import java.util.UUID;
25import javax.xml.datatype.DatatypeConfigurationException;
26import javax.xml.datatype.DatatypeFactory;
27import javax.xml.datatype.XMLGregorianCalendar;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30import org.springframework.jdbc.core.RowMapper;
31import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
32
33/**
34 *
35 * @author olhsha
36 */
37public class JdbcResourceDao extends SimpleJdbcDaoSupport implements ResourceDao {
38
39    // base string constants: reTarget table Names
40    final static protected String notebookTableName = "notebook";
41    final static protected String annotationTableName = "annotation";
42    final static protected String targetTableName = "target";
43    final static protected String cachedRepresentationTableName = "cached_representation";
44    final static protected String versionTableName = "version";
45    final static protected String principalTableName = "principal";
46    // joint tablenames
47    final static protected String notebooksAnnotationsTableName = "notebooks_annotations";
48    final static protected String permissionsTableName = "annotations_principals_permissions";
49    final static protected String annotationsTargetsTableName = "annotations_targets";
50    final static protected String targetsCachedRepresentationsTableName = "targets_cached_representations";
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 target_id = "target_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_text = "body_text";
59    final static protected String body_mimetype = "body_mimetype";
60    final static protected String title = "title";
61    final static protected String principal_id = "principal_id";
62    final static protected String last_modified = "last_modified";
63    final static protected String version = "version";
64    final static protected String permission = "permission_";
65    final static protected String link_uri = "link_uri";
66    final static protected String cached_representation_id = "cached_representation_id";
67    final static protected String sibling_Target_class = "sibling_Target_class";
68    final static protected String mime_type = "mime_type";
69    final static protected String tool = "tool";
70    final static protected String type_ = "type_";
71    final static protected String file_ = "file_";
72    final static protected String principal_name = "principal_name";
73    final static protected String e_mail = "e_mail";
74    final static protected String remote_id = "remote_id";
75    final static protected String is_xml = "is_xml";
76    final static protected String fragment_descriptor = "fragment_descriptor";
77    final static protected String fragment_descriptor_in_cached = "fragment_descriptor_in_cached";
78    // derived string constants: table+field names
79    final static protected String annotationStar = annotationTableName + ".*";
80    final static protected String annotationAnnotation_id = annotationTableName + "." + annotation_id;
81    final static protected String annotationExternal_id = annotationTableName + "." + external_id;
82    final static protected String notebookStar = notebookTableName + ".*";
83    final static protected String notebookNotebook_id = notebookTableName + "." + notebook_id;
84    final static protected String notebookTitle = notebookTableName + "." + title;
85    final static protected String notebookExternal_id = notebookTableName + "." + external_id;
86    final static protected String notebookOwner_id = notebookTableName + "." + owner_id;
87    final static protected String notebooksAnnotationsTableNameAnnotation_id = notebooksAnnotationsTableName + "." + annotation_id;
88    final static protected String principalPrincipal_id = principalTableName + "." + principal_id;
89    final static protected String principalExternal_id = principalTableName + "." + external_id;
90    final static protected String cachedRepresentationStar = cachedRepresentationTableName + ".*";
91    final static protected String TargetStar = targetTableName + ".*";
92    final static protected String principalStar = principalTableName + ".*";
93    ///////////////////////////////////////////////////
94    protected String internalIdName = null;
95    protected String resourceTableName = null;
96    protected String _serviceURI;
97    private static final Logger _logger = LoggerFactory.getLogger(JdbcResourceDao.class);
98
99    ////////
100    /////////////////// Class field SETTERS /////////////
101    @Override
102    public void setServiceURI(String serviceURI) {
103        _serviceURI = serviceURI;
104    }
105
106    //////////////////////////////////////////////////////////////////////////////////
107    @Override
108    public Number getInternalID(UUID externalId) {
109        if (externalId == null) {
110            return null;
111        }
112        StringBuilder sql = new StringBuilder("SELECT ");
113        sql.append(internalIdName).append(" FROM ").append(resourceTableName).append(" WHERE ").append(external_id).append("= ? LIMIT 1");
114        List<Number> sqlResult = getSimpleJdbcTemplate().query(sql.toString(), internalIDRowMapper, externalId.toString());
115        return (sqlResult.isEmpty() ? null : sqlResult.get(0));
116    }
117
118    /////////////////////////////////////////////
119    @Override
120    public UUID getExternalID(Number internalId) {
121        StringBuilder sql = new StringBuilder("SELECT ");
122        sql.append(external_id).append(" FROM ").append(resourceTableName).append(" WHERE ").append(internalIdName).append("= ? LIMIT 1");
123        List<UUID> sqlResult = getSimpleJdbcTemplate().query(sql.toString(), externalIDRowMapper, internalId);
124        return (sqlResult.isEmpty() ? null : sqlResult.get(0));
125    }
126
127    //////////////////////////////////////////////
128    @Override
129    public Number getInternalIDFromURI(String uri) {
130        String externalID = stringURItoExternalID(uri);
131        return getInternalID(UUID.fromString(externalID));
132    }
133
134    //////////////////////////////////////////////
135    @Override
136    public String getURIFromInternalID(Number internalID) {
137        return externalIDtoURI(getExternalID(internalID).toString());
138    }
139
140    /////////////////////////////////////////////////////
141    protected XMLGregorianCalendar retrieveTimeStamp(Number internalID) {
142        StringBuilder sqlTime = new StringBuilder("SELECT ");
143        sqlTime.append(last_modified).append(" FROM ").append(resourceTableName).append(" WHERE ").append(internalIdName).append("= ? LIMIT 1");
144        List<XMLGregorianCalendar> timeStamp = getSimpleJdbcTemplate().query(sqlTime.toString(), timeStampRowMapper, internalID);
145        if (timeStamp.isEmpty()) {
146            return null;
147        }
148        return timeStamp.get(0);
149    }
150    protected final RowMapper<XMLGregorianCalendar> timeStampRowMapper = new RowMapper<XMLGregorianCalendar>() {
151        @Override
152        public XMLGregorianCalendar mapRow(ResultSet rs, int rowNumber) throws SQLException {
153            return timeStampToXMLGregorianCalendar(rs);
154        }
155    };
156////////////////// ROW MAPPERS ///////////////////
157    protected final RowMapper<Number> internalIDRowMapper = new RowMapper<Number>() {
158        @Override
159        public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
160            int result = rs.getInt(internalIdName);
161            Number resultNumber = result;
162            return resultNumber;
163        }
164    };
165    protected final RowMapper<UUID> externalIDRowMapper = new RowMapper<UUID>() {
166        @Override
167        public UUID mapRow(ResultSet rs, int rowNumber) throws SQLException {
168            return (UUID.fromString(rs.getString(external_id)));
169        }
170    };
171    protected final RowMapper<Number> cachedIDRowMapper = new RowMapper<Number>() {
172        @Override
173        public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
174            return rs.getInt(cached_representation_id);
175        }
176    };
177    protected final RowMapper<Number> TargetIDRowMapper = new RowMapper<Number>() {
178        @Override
179        public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
180            return rs.getInt(target_id);
181        }
182    };
183    protected final RowMapper<Number> annotationIDRowMapper = new RowMapper<Number>() {
184        @Override
185        public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
186            return rs.getInt(annotation_id);
187        }
188    };
189    protected final RowMapper<Number> notebookIDRowMapper = new RowMapper<Number>() {
190        @Override
191        public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
192            return rs.getInt(notebook_id);
193        }
194    };
195    protected final RowMapper<Number> principalIDRowMapper = new RowMapper<Number>() {
196        @Override
197        public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
198            return rs.getInt(principal_id);
199        }
200    };
201    protected final RowMapper<Number> ownerIDRowMapper = new RowMapper<Number>() {
202        @Override
203        public Number mapRow(ResultSet rs, int rowNumber) throws SQLException {
204            return rs.getInt(owner_id);
205        }
206    };
207
208    @Override
209    public String externalIDtoURI(String externalID) {
210        if (_serviceURI != null) {
211            return _serviceURI + externalID;
212        } else {
213            return externalID;
214        }
215    }
216
217    @Override
218    public String stringURItoExternalID(String stringURI) {
219        return stringURI.substring(_serviceURI.length());
220    }
221
222    ////////////////////////////
223    protected <T> String makeListOfValues(List<T> vals) {
224
225        if (vals == null) {
226            return null;
227        }
228
229        if (vals.isEmpty()) {
230            return null;
231        }
232
233        String result = "(";
234        int length = vals.size();
235        for (int i = 0; i < length - 1; i++) {
236            result = result + vals.get(i).toString() + ", ";
237        }
238        result = result + vals.get(length - 1).toString() + ")";
239        return result;
240    }
241
242    /////////////////////////
243    protected XMLGregorianCalendar timeStampToXMLGregorianCalendar(ResultSet rs) {
244        try {
245            String ts = rs.getString(last_modified).replace(' ', 'T') + "Z";
246            try {
247                return DatatypeFactory.newInstance().newXMLGregorianCalendar(ts);
248            } catch (DatatypeConfigurationException dtce) {
249                _logger.error(" ", dtce);
250                return null;
251            }
252        } catch (SQLException sqle) {
253            _logger.error(" ", sqle);
254            return null;
255        }
256    }
257}
Note: See TracBrowser for help on using the repository browser.