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