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());
66 public boolean hasMedia(EntityContent content)
67 throws StorageObjectException {
68 String wc = "content_id="+content.getId();
69 if( content != null) {
71 if(selectByWhereClause(wc,-1).size() == 0)
75 } catch (Exception e) {
76 theLog.printError("-- hasMedia failed " + e.toString());
77 throw new StorageObjectException("-- hasMedia failed " + e.toString());
80 theLog.printError("-- hasMedia failed: content is NULL");
81 throw new StorageObjectException("-- hasMedia failed: content is NULL");
89 * get all the audio belonging to a content entity
92 public EntityList getAudio(EntityContent content)
93 throws StorageObjectException {
94 EntityList returnList=null;
95 if (content != null) {
96 // get all to_topic from media_x_topic
97 String id = content.getId();
98 //this is not supported by mysql
99 String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
102 returnList = DatabaseAudio.getInstance().selectByWhereClause(subselect,-1);
103 } catch (Exception e) {
104 theLog.printDebugInfo("-- get audio failed " + e.toString());
105 throw new StorageObjectException("-- get audio failed " + e.toString());
112 * get all the video belonging to a content entity
115 public EntityList getVideo(EntityContent content)
116 throws StorageObjectException {
117 EntityList returnList=null;
118 if (content != null) {
119 // get all to_topic from media_x_topic
120 String id = content.getId();
121 //this is not supported by mysql
122 String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
125 returnList = DatabaseVideo.getInstance().selectByWhereClause(subselect,-1);
126 } catch (Exception e) {
127 theLog.printDebugInfo("-- get video failed " + e.toString());
128 throw new StorageObjectException("-- get video failed " + e.toString());
135 * get all the images belonging to a content entity
138 public EntityList getImages(EntityContent content)
139 throws StorageObjectException {
140 EntityList returnList=null;
141 if (content != null) {
142 // get all to_topic from media_x_topic
143 String id = content.getId();
144 //this is not supported by mysql
145 String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
148 returnList = DatabaseImages.getInstance().selectByWhereClause(subselect,-1);
149 } catch (Exception e) {
150 theLog.printDebugInfo("-- get images failed " + e.toString());
151 throw new StorageObjectException("-- get images failed " + e.toString());
159 * get all the uploaded/other Media belonging to a content entity
162 public EntityList getOther(EntityContent content)
163 throws StorageObjectException
165 /** @todo this should only fetch published media / rk */
167 EntityList returnList=null;
168 if (content != null) {
169 // get all to_topic from media_x_topic
170 String id = content.getId();
171 //this is not supported by mysql
172 String subselect = "id in (select media_id from " + theTable +
173 " where content_id=" + id+")";
176 returnList = DatabaseOther.getInstance().selectByWhereClause(subselect,
178 } catch (Exception e) {
180 theLog.printDebugInfo("-- get Other failed " + e.toString());
181 throw new StorageObjectException("-- get Other failed "
189 * get all the uploaded/other Media belonging to a content entity
192 public EntityList getUploadedMedia(EntityContent content)
193 throws StorageObjectException
195 /** @todo this should only fetch published media / rk */
197 EntityList returnList=null;
198 if (content != null) {
199 // get all to_topic from media_x_topic
200 String id = content.getId();
201 //this is not supported by mysql
202 String subselect = "id in (select media_id from " + theTable +
203 " where content_id=" + id+")";
206 returnList = DatabaseUploadedMedia.getInstance().selectByWhereClause(subselect,
208 } catch (Exception e) {
210 theLog.printDebugInfo("-- get uploadedMedia failed " + e.toString());
211 throw new StorageObjectException("-- get uploadedMedia failed "
219 public void setMedia(String contentId, String[] mediaId)
220 throws StorageObjectException {
221 if (contentId == null){
224 if (mediaId==null || mediaId[0]==null) {
227 //first delete all row with content_id=contentId
228 String sql = "delete from "+ theTable +" where content_id=" + contentId;
230 Connection con=null;Statement stmt=null;
232 con = getPooledCon();
233 // should be a preparedStatement because is faster
234 stmt = con.createStatement();
235 ResultSet rs = executeSql(stmt,sql);
236 } catch (Exception e) {
237 theLog.printDebugInfo("-- set media failed -- delete");
238 throw new StorageObjectException("-- set media failed -- delete"+e.toString());
240 freeConnection(con,stmt);
244 //first delete all row with content_id=contentId
245 for (int i=0;i<mediaId.length;i++) {
246 sql = "insert into "+ theTable +" (content_id,media_id) values ("
247 + contentId + "," + mediaId[i] + ")";
249 con = getPooledCon();
250 // should be a preparedStatement because is faster
251 stmt = con.createStatement();
252 int rs = executeUpdate(stmt,sql);
253 } catch (Exception e) {
254 theLog.printDebugInfo("-- set topics failed -- insert");
255 throw new StorageObjectException("-- set topics failed -- insert "+e.toString());
257 freeConnection(con,stmt);
262 public void addMedia(String contentId, String mediaId)
263 throws StorageObjectException {
264 if (contentId == null && mediaId == null) {
268 Connection con=null;Statement stmt=null;
271 String sql = "insert into "+ theTable +" (content_id,media_id) values ("
272 + contentId + "," + mediaId + ")";
274 con = getPooledCon();
275 // should be a preparedStatement because is faster
276 stmt = con.createStatement();
277 int rs = executeUpdate(stmt,sql);
278 } catch (Exception e) {
279 theLog.printDebugInfo("-- add media failed -- insert");
280 throw new StorageObjectException("-- add media failed -- insert "
283 freeConnection(con,stmt);
287 public void setMedia(String contentId, String mediaId)
288 throws StorageObjectException {
289 if (contentId == null && mediaId == null) {
292 //first delete all row with content_id=contentId
293 String sql = "delete from "+ theTable +" where content_id=" + contentId;
295 Connection con=null;Statement stmt=null;
297 con = getPooledCon();
298 // should be a preparedStatement because is faster
299 stmt = con.createStatement();
300 int rs = executeUpdate(stmt,sql);
301 } catch (Exception e) {
302 theLog.printDebugInfo("-- set media failed -- delete");
303 throw new StorageObjectException("-- set media failed -- delete "
306 freeConnection(con,stmt);
310 //first delete all row with content_id=contentId
312 sql = "insert into "+ theTable +" (content_id,media_id) values ("
313 + contentId + "," + mediaId + ")";
315 con = getPooledCon();
316 // should be a preparedStatement because is faster
317 stmt = con.createStatement();
318 int rs = executeUpdate(stmt,sql);
319 } catch (Exception e) {
320 theLog.printDebugInfo("-- set media failed -- insert");
321 throw new StorageObjectException("-- set media failed -- insert "
324 freeConnection(con,stmt);
328 public void deleteByContentId(String contentId)
329 throws StorageObjectException {
330 if (contentId == null) {
331 //theLog.printDebugInfo("-- delete topics failed -- no content id");
334 //delete all row with content_id=contentId
335 String sql = "delete from "+ theTable +" where content_id=" + contentId;
337 Connection con=null;Statement stmt=null;
339 con = getPooledCon();
340 // should be a preparedStatement because is faster
341 stmt = con.createStatement();
342 int rs = executeUpdate(stmt,sql);
343 } catch (Exception e) {
344 theLog.printDebugInfo("-- delete by contentId failed ");
345 throw new StorageObjectException("-- delete by content id failed -- delete "
348 freeConnection(con,stmt);
352 public void deleteByMediaId(String mediaId)
353 throws StorageObjectException {
354 if (mediaId == null) {
355 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
358 //delete all row with content_id=contentId
359 String sql = "delete from "+ theTable +" where media_id=" + mediaId;
361 Connection con=null;Statement stmt=null;
363 con = getPooledCon();
364 // should be a preparedStatement because is faster
365 stmt = con.createStatement();
366 int rs = executeUpdate(stmt,sql);
367 theLog.printDebugInfo("-- delete media success ");
368 } catch (Exception e) {
369 theLog.printDebugInfo("-- delete media failed ");
370 throw new StorageObjectException("-- delete by media id failed -- "
373 freeConnection(con,stmt);
377 public void delete(String contentId, String mediaId)
378 throws StorageObjectException {
379 if (mediaId == null || contentId==null) {
380 theLog.printDebugInfo("-- delete media failed -- missing parameter");
383 //delete all row with content_id=contentId and media_id=mediaId
384 String sql = "delete from "+ theTable +" where media_id=" + mediaId + " and content_id= "+contentId;
386 Connection con=null;Statement stmt=null;
388 con = getPooledCon();
389 // should be a preparedStatement because is faster
390 stmt = con.createStatement();
391 int rs = executeUpdate(stmt,sql);
392 theLog.printDebugInfo("-- delete content_x_media success ");
393 } catch (Exception e) {
394 theLog.printDebugInfo("-- delete content_x_media failed ");
395 throw new StorageObjectException("-- delete content_x_media failed -- "
398 freeConnection(con,stmt);
403 public EntityList getContent(EntityMedia media)
404 throws StorageObjectException {
405 EntityList returnList=null;
407 String id = media.getId();
408 String select = "select content_id from " + theTable + " where media_id=" + id;
410 // execute select statement
411 Connection con=null;Statement stmt=null;
413 con = getPooledCon();
414 // should be a preparedStatement because is faster
415 stmt = con.createStatement();
416 ResultSet rs = executeSql(stmt,select);
418 String mediaSelect= "id IN (";
421 if (first==false) mediaSelect+=",";
422 mediaSelect += rs.getString(1);
427 returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,-1);
430 catch (Exception e) {
431 theLog.printDebugInfo("-- get content failed");
432 throw new StorageObjectException("-- get content failed -- "
435 finally { freeConnection(con,stmt);}
441 * Returns a EntityList with all content-objects having a relation to a media
444 public EntityList getContent()
445 throws StorageObjectException {
446 EntityList returnList=null;
448 String select = "select distinct content_id from " + theTable;
449 // execute select statement
450 Connection con=null;Statement stmt=null;
452 con = getPooledCon();
453 // should be a preparedStatement because is faster
454 stmt = con.createStatement();
455 ResultSet rs = executeSql(stmt,select);
457 String mediaSelect= "id IN (";
460 if (first==false) mediaSelect+=",";
461 mediaSelect += rs.getString(1);
466 returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,"webdb_lastchange desc");
469 catch (Exception e) {
470 theLog.printDebugInfo("-- get content failed");
471 throw new StorageObjectException("-- get content failed -- "
474 finally { freeConnection(con,stmt);}