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

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

adding, updating, deleting a user (only for a user with admin rights)

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