1 package mircoders.storage;
8 import freemarker.template.*;
14 import mircoders.entity.*;
17 * <b>Diese Klasse implementiert die Datenbankverbindung zur MetaObjekt-Tabelle
22 public class DatabaseContentToMedia extends Database implements StorageObject{
24 private static DatabaseContentToMedia instance;
26 public static DatabaseContentToMedia getInstance()
27 throws StorageObjectException {
28 if (instance == null) {
29 instance = new DatabaseContentToMedia();
30 instance.myselfDatabase = instance;
35 private DatabaseContentToMedia()
36 throws StorageObjectException {
39 this.hasTimestamp = false;
40 this.theTable="content_x_media";
44 * get all the media-files belonging to a content entity
47 public EntityList getMedia(EntityContent content)
48 throws StorageObjectException {
49 EntityList returnList=null;
50 if (content != null) {
51 // get all to_topic from media_x_topic
52 String id = content.getId();
53 //this is not supported by mysql
54 String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
57 returnList = DatabaseMedia.getInstance().selectByWhereClause(subselect,-1);
58 } catch (Exception e) {
59 theLog.printDebugInfo("-- get media failed " + e.toString());
60 throw new StorageObjectException("-- get media failed " + e.toString());
67 * get all the images belonging to a content entity
70 public EntityList getImages(EntityContent content)
71 throws StorageObjectException {
72 EntityList returnList=null;
73 if (content != null) {
74 // get all to_topic from media_x_topic
75 String id = content.getId();
76 //this is not supported by mysql
77 String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
80 returnList = DatabaseImages.getInstance().selectByWhereClause(subselect,-1);
81 } catch (Exception e) {
82 theLog.printDebugInfo("-- get images failed " + e.toString());
83 throw new StorageObjectException("-- get images failed " + e.toString());
91 * get all the uploaded Media belonging to a content entity
94 public EntityList getUploadedMedia(EntityContent content)
95 throws StorageObjectException
97 /** @todo this should only fetch published media / rk */
99 EntityList returnList=null;
100 if (content != null) {
101 // get all to_topic from media_x_topic
102 String id = content.getId();
103 //this is not supported by mysql
104 String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
107 returnList = DatabaseMedia.getInstance().selectByWhereClause(subselect,"id");
108 } catch (Exception e) {
110 theLog.printDebugInfo("-- get uploadedMedia failed " + e.toString());
111 throw new StorageObjectException("-- get uploadedMedia failed " + e.toString());
118 public void setMedia(String contentId, String[] mediaId)
119 throws StorageObjectException {
120 if (contentId == null){
123 if (mediaId==null || mediaId[0]==null) {
126 //first delete all row with content_id=contentId
127 String sql = "delete from "+ theTable +" where content_id=" + contentId;
129 Connection con=null;Statement stmt=null;
131 con = getPooledCon();
132 // should be a preparedStatement because is faster
133 stmt = con.createStatement();
134 ResultSet rs = executeSql(stmt,sql);
135 } catch (Exception e) {
136 theLog.printDebugInfo("-- set media failed -- delete");
137 throw new StorageObjectException("-- set media failed -- delete"+e.toString());
139 freeConnection(con,stmt);
143 //first delete all row with content_id=contentId
144 for (int i=0;i<mediaId.length;i++) {
145 sql = "insert into "+ theTable +" (content_id,media_id) values ("
146 + contentId + "," + mediaId[i] + ")";
148 con = getPooledCon();
149 // should be a preparedStatement because is faster
150 stmt = con.createStatement();
151 int rs = executeUpdate(stmt,sql);
152 } catch (Exception e) {
153 theLog.printDebugInfo("-- set topics failed -- insert");
154 throw new StorageObjectException("-- set topics failed -- insert "+e.toString());
156 freeConnection(con,stmt);
161 public void addMedia(String contentId, String mediaId)
162 throws StorageObjectException {
163 if (contentId == null && mediaId == null) {
167 Connection con=null;Statement stmt=null;
170 String sql = "insert into "+ theTable +" (content_id,media_id) values ("
171 + contentId + "," + mediaId + ")";
173 con = getPooledCon();
174 // should be a preparedStatement because is faster
175 stmt = con.createStatement();
176 int rs = executeUpdate(stmt,sql);
177 } catch (Exception e) {
178 theLog.printDebugInfo("-- add media failed -- insert");
179 throw new StorageObjectException("-- add media failed -- insert "
182 freeConnection(con,stmt);
186 public void setMedia(String contentId, String mediaId)
187 throws StorageObjectException {
188 if (contentId == null && mediaId == null) {
191 //first delete all row with content_id=contentId
192 String sql = "delete from "+ theTable +" where content_id=" + contentId;
194 Connection con=null;Statement stmt=null;
196 con = getPooledCon();
197 // should be a preparedStatement because is faster
198 stmt = con.createStatement();
199 int rs = executeUpdate(stmt,sql);
200 } catch (Exception e) {
201 theLog.printDebugInfo("-- set media failed -- delete");
202 throw new StorageObjectException("-- set media failed -- delete "
205 freeConnection(con,stmt);
209 //first delete all row with content_id=contentId
211 sql = "insert into "+ theTable +" (content_id,media_id) values ("
212 + contentId + "," + mediaId + ")";
214 con = getPooledCon();
215 // should be a preparedStatement because is faster
216 stmt = con.createStatement();
217 int rs = executeUpdate(stmt,sql);
218 } catch (Exception e) {
219 theLog.printDebugInfo("-- set media failed -- insert");
220 throw new StorageObjectException("-- set media failed -- insert "
223 freeConnection(con,stmt);
227 public void deleteByContentId(String contentId)
228 throws StorageObjectException {
229 if (contentId == null) {
230 //theLog.printDebugInfo("-- delete topics failed -- no content id");
233 //delete all row with content_id=contentId
234 String sql = "delete from "+ theTable +" where content_id=" + contentId;
236 Connection con=null;Statement stmt=null;
238 con = getPooledCon();
239 // should be a preparedStatement because is faster
240 stmt = con.createStatement();
241 int rs = executeUpdate(stmt,sql);
242 } catch (Exception e) {
243 theLog.printDebugInfo("-- delete by contentId failed ");
244 throw new StorageObjectException("-- delete by content id failed -- delete "
247 freeConnection(con,stmt);
251 public void deleteByMediaId(String mediaId)
252 throws StorageObjectException {
253 if (mediaId == null) {
254 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
257 //delete all row with content_id=contentId
258 String sql = "delete from "+ theTable +" where media_id=" + mediaId;
260 Connection con=null;Statement stmt=null;
262 con = getPooledCon();
263 // should be a preparedStatement because is faster
264 stmt = con.createStatement();
265 int rs = executeUpdate(stmt,sql);
266 theLog.printDebugInfo("-- delete media success ");
267 } catch (Exception e) {
268 theLog.printDebugInfo("-- delete media failed ");
269 throw new StorageObjectException("-- delete by media id failed -- "
272 freeConnection(con,stmt);
276 public void delete(String contentId, String mediaId)
277 throws StorageObjectException {
278 if (mediaId == null || contentId==null) {
279 theLog.printDebugInfo("-- delete media failed -- missing parameter");
282 //delete all row with content_id=contentId and media_id=mediaId
283 String sql = "delete from "+ theTable +" where media_id=" + mediaId + " and content_id= "+contentId;
285 Connection con=null;Statement stmt=null;
287 con = getPooledCon();
288 // should be a preparedStatement because is faster
289 stmt = con.createStatement();
290 int rs = executeUpdate(stmt,sql);
291 theLog.printDebugInfo("-- delete content_x_media success ");
292 } catch (Exception e) {
293 theLog.printDebugInfo("-- delete content_x_media failed ");
294 throw new StorageObjectException("-- delete content_x_media failed -- "
297 freeConnection(con,stmt);
302 public EntityList getContent(EntityMedia media)
303 throws StorageObjectException {
304 EntityList returnList=null;
306 String id = media.getId();
307 String select = "select content_id from " + theTable + " where media_id=" + id;
309 // execute select statement
310 Connection con=null;Statement stmt=null;
312 con = getPooledCon();
313 // should be a preparedStatement because is faster
314 stmt = con.createStatement();
315 ResultSet rs = executeSql(stmt,select);
317 String mediaSelect= "id IN (";
320 if (first==false) mediaSelect+=",";
321 mediaSelect += rs.getString(1);
326 returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,-1);
329 catch (Exception e) {
330 theLog.printDebugInfo("-- get content failed");
331 throw new StorageObjectException("-- get content failed -- "
334 finally { freeConnection(con,stmt);}
340 * Returns a EntityList with all content-objects having a relation to a media
343 public EntityList getContent()
344 throws StorageObjectException {
345 EntityList returnList=null;
347 String select = "select distinct content_id from " + theTable;
348 // execute select statement
349 Connection con=null;Statement stmt=null;
351 con = getPooledCon();
352 // should be a preparedStatement because is faster
353 stmt = con.createStatement();
354 ResultSet rs = executeSql(stmt,select);
356 String mediaSelect= "id IN (";
359 if (first==false) mediaSelect+=",";
360 mediaSelect += rs.getString(1);
365 returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,"webdb_lastchange desc");
368 catch (Exception e) {
369 theLog.printDebugInfo("-- get content failed");
370 throw new StorageObjectException("-- get content failed -- "
373 finally { freeConnection(con,stmt);}