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.NotInDataBaseException; |
---|
21 | import eu.dasish.annotation.backend.dao.CachedRepresentationDao; |
---|
22 | import eu.dasish.annotation.schema.CachedRepresentationInfo; |
---|
23 | import java.io.IOException; |
---|
24 | import java.io.InputStream; |
---|
25 | import java.lang.String; |
---|
26 | import java.sql.ResultSet; |
---|
27 | import java.sql.SQLException; |
---|
28 | import java.util.HashMap; |
---|
29 | import java.util.List; |
---|
30 | import java.util.Map; |
---|
31 | import java.util.UUID; |
---|
32 | import javax.sql.DataSource; |
---|
33 | import org.springframework.jdbc.core.RowMapper; |
---|
34 | import org.apache.commons.io.IOUtils; |
---|
35 | import org.slf4j.Logger; |
---|
36 | import org.slf4j.LoggerFactory; |
---|
37 | |
---|
38 | /** |
---|
39 | * |
---|
40 | * @author olhsha |
---|
41 | */ |
---|
42 | public class JdbcCachedRepresentationDao extends JdbcResourceDao implements CachedRepresentationDao { |
---|
43 | |
---|
44 | private final Logger loggerCachedDao = LoggerFactory.getLogger(JdbcCachedRepresentationDao.class); |
---|
45 | |
---|
46 | public JdbcCachedRepresentationDao(DataSource dataSource) { |
---|
47 | setDataSource(dataSource); |
---|
48 | internalIdName = cached_representation_id; |
---|
49 | resourceTableName = cachedRepresentationTableName; |
---|
50 | } |
---|
51 | |
---|
52 | @Override |
---|
53 | public void setServiceURI(String serviceURI) { |
---|
54 | _serviceURI = serviceURI; |
---|
55 | } |
---|
56 | |
---|
57 | /////////////////////////// GETTERS //////////////////////////////////////// |
---|
58 | @Override |
---|
59 | public CachedRepresentationInfo getCachedRepresentationInfo(Number internalID) { |
---|
60 | StringBuilder sql = new StringBuilder("SELECT "); |
---|
61 | sql.append(cachedRepresentationStar).append(" FROM ").append(cachedRepresentationTableName).append(" WHERE ").append(cached_representation_id).append("= ? LIMIT 1"); |
---|
62 | List<CachedRepresentationInfo> result = this.loggedQuery(sql.toString(), cachedRepresentationRowMapper, internalID); |
---|
63 | return result.get(0); |
---|
64 | } |
---|
65 | private final RowMapper<CachedRepresentationInfo> cachedRepresentationRowMapper = new RowMapper<CachedRepresentationInfo>() { |
---|
66 | @Override |
---|
67 | public CachedRepresentationInfo mapRow(ResultSet rs, int rowNumber) throws SQLException { |
---|
68 | CachedRepresentationInfo result = new CachedRepresentationInfo(); |
---|
69 | result.setMimeType(rs.getString(mime_type)); |
---|
70 | result.setURI(externalIDtoURI(rs.getString(external_id))); |
---|
71 | result.setTool(rs.getString(tool)); |
---|
72 | result.setType(rs.getString(type_)); |
---|
73 | return result; |
---|
74 | } |
---|
75 | }; |
---|
76 | |
---|
77 | /////////////////////////// GETTERS //////////////////////////////////////// |
---|
78 | @Override |
---|
79 | public InputStream getCachedRepresentationBlob(Number internalID) { |
---|
80 | StringBuilder sql = new StringBuilder("SELECT "); |
---|
81 | sql.append(file_).append(" FROM ").append(cachedRepresentationTableName).append(" WHERE ").append(cached_representation_id).append("= ? LIMIT 1"); |
---|
82 | List<InputStream> result = this.loggedQuery(sql.toString(), cachedRepresentationBlobRowMapper, internalID); |
---|
83 | return result.get(0); |
---|
84 | } |
---|
85 | private final RowMapper<InputStream> cachedRepresentationBlobRowMapper = new RowMapper<InputStream>() { |
---|
86 | @Override |
---|
87 | public InputStream mapRow(ResultSet rs, int rowNumber) throws SQLException { |
---|
88 | return rs.getBinaryStream(file_); |
---|
89 | } |
---|
90 | }; |
---|
91 | |
---|
92 | ///////////////////////////////////////// |
---|
93 | @Override |
---|
94 | public List<Number> getCachedRepresentationsForTarget(Number targetID) { |
---|
95 | |
---|
96 | String sql = "SELECT " + cached_representation_id + " FROM " + targetsCachedRepresentationsTableName + " WHERE " + target_id + " = ?"; |
---|
97 | return this.loggedQuery(sql, internalIDRowMapper, targetID); |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | |
---|
102 | // Map<String, Object> params = new HashMap<String, Object>(); |
---|
103 | // |
---|
104 | // params.put("annotationID", annotationID); |
---|
105 | // params.put("principalID", principalID); |
---|
106 | // params.put("accessString", access.value()); |
---|
107 | // StringBuilder sql = new StringBuilder("UPDATE "); |
---|
108 | // sql.append(permissionsTableName).append(" SET "). |
---|
109 | // append(this.access).append("= :accessString"). |
---|
110 | // append(" WHERE ").append(annotation_id).append("= :annotationID"). |
---|
111 | // append(" AND ").append(principal_id).append("= :principalID"); |
---|
112 | // return this.loggedUpdate(sql.toString(), params); |
---|
113 | |
---|
114 | //// UPDATERS /// |
---|
115 | |
---|
116 | @Override |
---|
117 | public int updateCachedRepresentationMetadata(Number internalID, CachedRepresentationInfo cachedInfo){ |
---|
118 | Map<String, Object> params = new HashMap<String, Object>(); |
---|
119 | params.put("internalID", internalID); |
---|
120 | params.put("mimetype", cachedInfo.getMimeType()); |
---|
121 | params.put("tool", cachedInfo.getTool()); |
---|
122 | params.put("type", cachedInfo.getType()); |
---|
123 | StringBuilder sql = new StringBuilder("UPDATE "); |
---|
124 | sql.append(cachedRepresentationTableName).append(" SET "). |
---|
125 | append(mime_type).append("= :mimetype").append(","). |
---|
126 | append(type_).append("= :type").append(","). |
---|
127 | append(tool).append("= :tool"). |
---|
128 | append(" WHERE ").append(cached_representation_id).append("= :internalID"); |
---|
129 | return this.loggedUpdate(sql.toString(), params); |
---|
130 | } |
---|
131 | |
---|
132 | @Override |
---|
133 | public int updateCachedRepresentationBlob(Number internalID, InputStream cachedBlob) throws IOException{ |
---|
134 | Map<String, Object> params = new HashMap<String, Object>(); |
---|
135 | params.put("internalID", internalID); |
---|
136 | params.put("blob", IOUtils.toByteArray(cachedBlob)); |
---|
137 | StringBuilder sql = new StringBuilder("UPDATE "); |
---|
138 | sql.append(cachedRepresentationTableName).append(" SET "). |
---|
139 | append(file_).append("= :blob"). |
---|
140 | append(" WHERE ").append(cached_representation_id).append("= :internalID"); |
---|
141 | return this.loggedUpdate(sql.toString(), params); |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | |
---|
146 | |
---|
147 | |
---|
148 | //////////////////////// ADDERS /////////////////////////////// |
---|
149 | @Override |
---|
150 | public Number addCachedRepresentation(CachedRepresentationInfo cachedInfo, InputStream streamCached) throws NotInDataBaseException, IOException { |
---|
151 | |
---|
152 | UUID externalIdentifier = UUID.randomUUID(); |
---|
153 | Map<String, Object> params = new HashMap<String, Object>(); |
---|
154 | params.put("externalId", externalIdentifier.toString()); |
---|
155 | params.put("mime_type", cachedInfo.getMimeType()); |
---|
156 | params.put("tool", cachedInfo.getTool()); |
---|
157 | params.put("type", cachedInfo.getType()); |
---|
158 | params.put("blob", IOUtils.toByteArray(streamCached)); |
---|
159 | StringBuilder sql = new StringBuilder("INSERT INTO "); |
---|
160 | sql.append(cachedRepresentationTableName).append("(").append(external_id).append(",").append(mime_type).append(",").append(tool).append(",").append(type_).append(",").append(file_).append(" ) VALUES (:externalId, :mime_type, :tool, :type, :blob)"); |
---|
161 | final int affectedRows = this.loggedUpdate(sql.toString(), params); |
---|
162 | return getInternalID(externalIdentifier); |
---|
163 | } |
---|
164 | |
---|
165 | /////////////////////// DELETERS ////////////////////////////////////////////// |
---|
166 | @Override |
---|
167 | public int deleteCachedRepresentation(Number internalID) { |
---|
168 | |
---|
169 | if (internalID == null) { |
---|
170 | loggerCachedDao.debug("internalID: " + nullArgument); |
---|
171 | return 0; |
---|
172 | } |
---|
173 | |
---|
174 | StringBuilder sql = new StringBuilder("DELETE FROM "); |
---|
175 | sql.append(cachedRepresentationTableName).append(" WHERE ").append(cached_representation_id).append(" = ?"); |
---|
176 | return this.loggedUpdate(sql.toString(), internalID); |
---|
177 | } |
---|
178 | } |
---|