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 // the following *has* to be sychronized cause this static method
27 // could get preemted and we could end up with 2 instances of DatabaseFoo.
28 // see the "Singletons with needles and thread" article at JavaWorld -mh
29 public synchronized static DatabaseContentToMedia getInstance()
30 throws StorageObjectException {
31 if (instance == null) {
32 instance = new DatabaseContentToMedia();
33 instance.myselfDatabase = instance;
38 private DatabaseContentToMedia()
39 throws StorageObjectException {
42 this.hasTimestamp = false;
43 this.theTable="content_x_media";
44 try { this.theEntityClass = Class.forName("mir.entity.GenericEntity"); }
45 catch (Exception e) { throw new StorageObjectException(e.toString()); }
49 * get all the media-files belonging to a content entity
52 public EntityList getMedia(EntityContent content)
53 throws StorageObjectException {
54 EntityList returnList=null;
55 if (content != null) {
56 // get all to_topic from media_x_topic
57 String id = content.getId();
58 //this is not supported by mysql
59 String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
62 returnList = DatabaseMedia.getInstance().selectByWhereClause(subselect,-1);
63 } catch (Exception e) {
64 theLog.printDebugInfo("-- get media failed " + e.toString());
65 throw new StorageObjectException("-- get media failed " + e.toString());
71 public boolean hasMedia(EntityContent content)
72 throws StorageObjectException {
73 String wc = "content_id="+content.getId();
74 if( content != null) {
76 if(selectByWhereClause(wc,-1).size() == 0)
80 } catch (Exception e) {
81 theLog.printError("-- hasMedia failed " + e.toString());
82 throw new StorageObjectException("-- hasMedia failed " + e.toString());
85 theLog.printError("-- hasMedia failed: content is NULL");
86 throw new StorageObjectException("-- hasMedia failed: content is NULL");
94 * get all the audio belonging to a content entity
97 public EntityList getAudio(EntityContent content)
98 throws StorageObjectException {
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 = DatabaseAudio.getInstance().selectByWhereClause(subselect,-1);
108 } catch (Exception e) {
109 theLog.printDebugInfo("-- get audio failed " + e.toString());
110 throw new StorageObjectException("-- get audio failed " + e.toString());
117 * get all the video belonging to a content entity
120 public EntityList getVideo(EntityContent content)
121 throws StorageObjectException {
122 EntityList returnList=null;
123 if (content != null) {
124 // get all to_topic from media_x_topic
125 String id = content.getId();
126 //this is not supported by mysql
127 String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
130 returnList = DatabaseVideo.getInstance().selectByWhereClause(subselect,-1);
131 } catch (Exception e) {
132 theLog.printDebugInfo("-- get video failed " + e.toString());
133 throw new StorageObjectException("-- get video failed " + e.toString());
140 * get all the images belonging to a content entity
143 public EntityList getImages(EntityContent content)
144 throws StorageObjectException {
145 EntityList returnList=null;
146 if (content != null) {
147 // get all to_topic from media_x_topic
148 String id = content.getId();
149 //this is not supported by mysql
150 String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
153 returnList = DatabaseImages.getInstance().selectByWhereClause(subselect,-1);
154 } catch (Exception e) {
155 theLog.printDebugInfo("-- get images failed " + e.toString());
156 throw new StorageObjectException("-- get images failed " + e.toString());
164 * get all the uploaded/other Media belonging to a content entity
167 public EntityList getOther(EntityContent content)
168 throws StorageObjectException
170 /** @todo this should only fetch published media / rk */
172 EntityList returnList=null;
173 if (content != null) {
174 // get all to_topic from media_x_topic
175 String id = content.getId();
176 //this is not supported by mysql
177 String subselect = "id in (select media_id from " + theTable +
178 " where content_id=" + id+")";
181 returnList = DatabaseOther.getInstance().selectByWhereClause(subselect,
183 } catch (Exception e) {
185 theLog.printDebugInfo("-- get Other failed " + e.toString());
186 throw new StorageObjectException("-- get Other failed "
194 * get all the uploaded/other Media belonging to a content entity
197 public EntityList getUploadedMedia(EntityContent content)
198 throws StorageObjectException
200 /** @todo this should only fetch published media / rk */
202 EntityList returnList=null;
203 if (content != null) {
204 // get all to_topic from media_x_topic
205 String id = content.getId();
206 //this is not supported by mysql
207 String subselect = "id in (select media_id from " + theTable +
208 " where content_id=" + id+")";
211 returnList = DatabaseUploadedMedia.getInstance().selectByWhereClause(subselect,
213 } catch (Exception e) {
215 theLog.printDebugInfo("-- get uploadedMedia failed " + e.toString());
216 throw new StorageObjectException("-- get uploadedMedia failed "
224 public void setMedia(String contentId, String[] mediaId)
225 throws StorageObjectException {
226 if (contentId == null){
229 if (mediaId==null || mediaId[0]==null) {
232 //first delete all row with content_id=contentId
233 String sql = "delete from "+ theTable +" where content_id=" + contentId;
235 Connection con=null;Statement stmt=null;
237 con = getPooledCon();
238 // should be a preparedStatement because is faster
239 stmt = con.createStatement();
240 ResultSet rs = executeSql(stmt,sql);
241 } catch (Exception e) {
242 theLog.printDebugInfo("-- set media failed -- delete");
243 throw new StorageObjectException("-- set media failed -- delete"+e.toString());
245 freeConnection(con,stmt);
249 //first delete all row with content_id=contentId
250 for (int i=0;i<mediaId.length;i++) {
251 sql = "insert into "+ theTable +" (content_id,media_id) values ("
252 + contentId + "," + mediaId[i] + ")";
254 con = getPooledCon();
255 // should be a preparedStatement because is faster
256 stmt = con.createStatement();
257 int rs = executeUpdate(stmt,sql);
258 } catch (Exception e) {
259 theLog.printDebugInfo("-- set topics failed -- insert");
260 throw new StorageObjectException("-- set topics failed -- insert "+e.toString());
262 freeConnection(con,stmt);
267 public void addMedia(String contentId, String mediaId)
268 throws StorageObjectException {
269 if (contentId == null && mediaId == null) {
273 Connection con=null;Statement stmt=null;
276 String sql = "insert into "+ theTable +" (content_id,media_id) values ("
277 + contentId + "," + mediaId + ")";
279 con = getPooledCon();
280 // should be a preparedStatement because is faster
281 stmt = con.createStatement();
282 int rs = executeUpdate(stmt,sql);
283 } catch (Exception e) {
284 theLog.printDebugInfo("-- add media failed -- insert");
285 throw new StorageObjectException("-- add media failed -- insert "
288 freeConnection(con,stmt);
292 public void setMedia(String contentId, String mediaId)
293 throws StorageObjectException {
294 if (contentId == null && mediaId == null) {
297 //first delete all row with content_id=contentId
298 String sql = "delete from "+ theTable +" where content_id=" + contentId;
300 Connection con=null;Statement stmt=null;
302 con = getPooledCon();
303 // should be a preparedStatement because is faster
304 stmt = con.createStatement();
305 int rs = executeUpdate(stmt,sql);
306 } catch (Exception e) {
307 theLog.printDebugInfo("-- set media failed -- delete");
308 throw new StorageObjectException("-- set media failed -- delete "
311 freeConnection(con,stmt);
315 //first delete all row with content_id=contentId
317 sql = "insert into "+ theTable +" (content_id,media_id) values ("
318 + contentId + "," + mediaId + ")";
320 con = getPooledCon();
321 // should be a preparedStatement because is faster
322 stmt = con.createStatement();
323 int rs = executeUpdate(stmt,sql);
324 } catch (Exception e) {
325 theLog.printDebugInfo("-- set media failed -- insert");
326 throw new StorageObjectException("-- set media failed -- insert "
329 freeConnection(con,stmt);
333 public void deleteByContentId(String contentId)
334 throws StorageObjectException {
335 if (contentId == null) {
336 //theLog.printDebugInfo("-- delete topics failed -- no content id");
339 //delete all row with content_id=contentId
340 String sql = "delete from "+ theTable +" where content_id=" + contentId;
342 Connection con=null;Statement stmt=null;
344 con = getPooledCon();
345 // should be a preparedStatement because is faster
346 stmt = con.createStatement();
347 int rs = executeUpdate(stmt,sql);
348 } catch (Exception e) {
349 theLog.printDebugInfo("-- delete by contentId failed ");
350 throw new StorageObjectException("-- delete by content id failed -- delete "
353 freeConnection(con,stmt);
357 public void deleteByMediaId(String mediaId)
358 throws StorageObjectException {
359 if (mediaId == null) {
360 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
363 //delete all row with content_id=contentId
364 String sql = "delete from "+ theTable +" where media_id=" + mediaId;
366 Connection con=null;Statement stmt=null;
368 con = getPooledCon();
369 // should be a preparedStatement because is faster
370 stmt = con.createStatement();
371 int rs = executeUpdate(stmt,sql);
372 theLog.printDebugInfo("-- delete media success ");
373 } catch (Exception e) {
374 theLog.printDebugInfo("-- delete media failed ");
375 throw new StorageObjectException("-- delete by media id failed -- "
378 freeConnection(con,stmt);
382 public void delete(String contentId, String mediaId)
383 throws StorageObjectException {
384 if (mediaId == null || contentId==null) {
385 theLog.printDebugInfo("-- delete media failed -- missing parameter");
388 //delete all row with content_id=contentId and media_id=mediaId
389 String sql = "delete from "+ theTable +" where media_id=" + mediaId + " and content_id= "+contentId;
391 Connection con=null;Statement stmt=null;
393 con = getPooledCon();
394 // should be a preparedStatement because is faster
395 stmt = con.createStatement();
396 int rs = executeUpdate(stmt,sql);
397 theLog.printDebugInfo("-- delete content_x_media success ");
398 } catch (Exception e) {
399 theLog.printDebugInfo("-- delete content_x_media failed ");
400 throw new StorageObjectException("-- delete content_x_media failed -- "
403 freeConnection(con,stmt);
408 public EntityList getContent(EntityMedia media)
409 throws StorageObjectException {
410 EntityList returnList=null;
412 String id = media.getId();
413 String select = "select content_id from " + theTable + " where media_id=" + id;
415 // execute select statement
416 Connection con=null;Statement stmt=null;
418 con = getPooledCon();
419 // should be a preparedStatement because is faster
420 stmt = con.createStatement();
421 ResultSet rs = executeSql(stmt,select);
423 String mediaSelect= "id IN (";
426 if (first==false) mediaSelect+=",";
427 mediaSelect += rs.getString(1);
432 returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,-1);
435 catch (Exception e) {
436 theLog.printDebugInfo("-- get content failed");
437 throw new StorageObjectException("-- get content failed -- "
440 finally { freeConnection(con,stmt);}
446 * Returns a EntityList with all content-objects having a relation to a media
449 public EntityList getContent()
450 throws StorageObjectException {
451 EntityList returnList=null;
453 String select = "select distinct content_id from " + theTable;
454 // execute select statement
455 Connection con=null;Statement stmt=null;
457 con = getPooledCon();
458 // should be a preparedStatement because is faster
459 stmt = con.createStatement();
460 ResultSet rs = executeSql(stmt,select);
462 String mediaSelect= "id IN (";
465 if (first==false) mediaSelect+=",";
466 mediaSelect += rs.getString(1);
471 returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,"webdb_lastchange desc");
474 catch (Exception e) {
475 theLog.printDebugInfo("-- get content failed");
476 throw new StorageObjectException("-- get content failed -- "
479 finally { freeConnection(con,stmt);}