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

Last change on this file since 3461 was 3461, checked in by olhsha, 11 years ago

replacing setURI(externa_id) with setURI(serviceURI_externalId). The implementation idea has been tested on getAnnotation method. Works. Now it must be implemented for get-method of the other resources for the other resources.

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