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";
41 try { this.theEntityClass = Class.forName("mir.entity.GenericEntity"); }
42 catch (Exception e) { throw new StorageObjectException(e.toString()); }
46 * get all the media-files belonging to a content entity
49 public EntityList getMedia(EntityContent content)
50 throws StorageObjectException {
51 EntityList returnList=null;
52 if (content != null) {
53 // get all to_topic from media_x_topic
54 String id = content.getId();
55 //this is not supported by mysql
56 String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
59 returnList = DatabaseMedia.getInstance().selectByWhereClause(subselect,-1);
60 } catch (Exception e) {
61 theLog.printDebugInfo("-- get media failed " + e.toString());
62 throw new StorageObjectException("-- get media failed " + e.toString());
68 public boolean hasMedia(EntityContent content)
69 throws StorageObjectException {
70 String wc = "content_id="+content.getId();
71 if( content != null) {
73 if(selectByWhereClause(wc,-1).size() == 0)
77 } catch (Exception e) {
78 theLog.printError("-- hasMedia failed " + e.toString());
79 throw new StorageObjectException("-- hasMedia failed " + e.toString());
82 theLog.printError("-- hasMedia failed: content is NULL");
83 throw new StorageObjectException("-- hasMedia failed: content is NULL");
91 * get all the audio belonging to a content entity
94 public EntityList getAudio(EntityContent content)
95 throws StorageObjectException {
96 EntityList returnList=null;
97 if (content != null) {
98 // get all to_topic from media_x_topic
99 String id = content.getId();
100 //this is not supported by mysql
101 String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
104 returnList = DatabaseAudio.getInstance().selectByWhereClause(subselect,-1);
105 } catch (Exception e) {
106 theLog.printDebugInfo("-- get audio failed " + e.toString());
107 throw new StorageObjectException("-- get audio failed " + e.toString());
114 * get all the video belonging to a content entity
117 public EntityList getVideo(EntityContent content)
118 throws StorageObjectException {
119 EntityList returnList=null;
120 if (content != null) {
121 // get all to_topic from media_x_topic
122 String id = content.getId();
123 //this is not supported by mysql
124 String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
127 returnList = DatabaseVideo.getInstance().selectByWhereClause(subselect,-1);
128 } catch (Exception e) {
129 theLog.printDebugInfo("-- get video failed " + e.toString());
130 throw new StorageObjectException("-- get video failed " + e.toString());
137 * get all the images belonging to a content entity
140 public EntityList getImages(EntityContent content)
141 throws StorageObjectException {
142 EntityList returnList=null;
143 if (content != null) {
144 // get all to_topic from media_x_topic
145 String id = content.getId();
146 //this is not supported by mysql
147 String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
150 returnList = DatabaseImages.getInstance().selectByWhereClause(subselect,-1);
151 } catch (Exception e) {
152 theLog.printDebugInfo("-- get images failed " + e.toString());
153 throw new StorageObjectException("-- get images failed " + e.toString());
161 * get all the uploaded/other Media belonging to a content entity
164 public EntityList getOther(EntityContent content)
165 throws StorageObjectException
167 /** @todo this should only fetch published media / rk */
169 EntityList returnList=null;
170 if (content != null) {
171 // get all to_topic from media_x_topic
172 String id = content.getId();
173 //this is not supported by mysql
174 String subselect = "id in (select media_id from " + theTable +
175 " where content_id=" + id+")";
178 returnList = DatabaseOther.getInstance().selectByWhereClause(subselect,
180 } catch (Exception e) {
182 theLog.printDebugInfo("-- get Other failed " + e.toString());
183 throw new StorageObjectException("-- get Other failed "
191 * get all the uploaded/other Media belonging to a content entity
194 public EntityList getUploadedMedia(EntityContent content)
195 throws StorageObjectException
197 /** @todo this should only fetch published media / rk */
199 EntityList returnList=null;
200 if (content != null) {
201 // get all to_topic from media_x_topic
202 String id = content.getId();
203 //this is not supported by mysql
204 String subselect = "id in (select media_id from " + theTable +
205 " where content_id=" + id+")";
208 returnList = DatabaseUploadedMedia.getInstance().selectByWhereClause(subselect,
210 } catch (Exception e) {
212 theLog.printDebugInfo("-- get uploadedMedia failed " + e.toString());
213 throw new StorageObjectException("-- get uploadedMedia failed "
221 public void setMedia(String contentId, String[] mediaId)
222 throws StorageObjectException {
223 if (contentId == null){
226 if (mediaId==null || mediaId[0]==null) {
229 //first delete all row with content_id=contentId
230 String sql = "delete from "+ theTable +" where content_id=" + contentId;
232 Connection con=null;Statement stmt=null;
234 con = getPooledCon();
235 // should be a preparedStatement because is faster
236 stmt = con.createStatement();
237 ResultSet rs = executeSql(stmt,sql);
238 } catch (Exception e) {
239 theLog.printDebugInfo("-- set media failed -- delete");
240 throw new StorageObjectException("-- set media failed -- delete"+e.toString());
242 freeConnection(con,stmt);
246 //first delete all row with content_id=contentId
247 for (int i=0;i<mediaId.length;i++) {
248 sql = "insert into "+ theTable +" (content_id,media_id) values ("
249 + contentId + "," + mediaId[i] + ")";
251 con = getPooledCon();
252 // should be a preparedStatement because is faster
253 stmt = con.createStatement();
254 int rs = executeUpdate(stmt,sql);
255 } catch (Exception e) {
256 theLog.printDebugInfo("-- set topics failed -- insert");
257 throw new StorageObjectException("-- set topics failed -- insert "+e.toString());
259 freeConnection(con,stmt);
264 public void addMedia(String contentId, String mediaId)
265 throws StorageObjectException {
266 if (contentId == null && mediaId == null) {
270 Connection con=null;Statement stmt=null;
273 String sql = "insert into "+ theTable +" (content_id,media_id) values ("
274 + contentId + "," + mediaId + ")";
276 con = getPooledCon();
277 // should be a preparedStatement because is faster
278 stmt = con.createStatement();
279 int rs = executeUpdate(stmt,sql);
280 } catch (Exception e) {
281 theLog.printDebugInfo("-- add media failed -- insert");
282 throw new StorageObjectException("-- add media failed -- insert "
285 freeConnection(con,stmt);
289 public void setMedia(String contentId, String mediaId)
290 throws StorageObjectException {
291 if (contentId == null && mediaId == null) {
294 //first delete all row with content_id=contentId
295 String sql = "delete from "+ theTable +" where content_id=" + contentId;
297 Connection con=null;Statement stmt=null;
299 con = getPooledCon();
300 // should be a preparedStatement because is faster
301 stmt = con.createStatement();
302 int rs = executeUpdate(stmt,sql);
303 } catch (Exception e) {
304 theLog.printDebugInfo("-- set media failed -- delete");
305 throw new StorageObjectException("-- set media failed -- delete "
308 freeConnection(con,stmt);
312 //first delete all row with content_id=contentId
314 sql = "insert into "+ theTable +" (content_id,media_id) values ("
315 + contentId + "," + mediaId + ")";
317 con = getPooledCon();
318 // should be a preparedStatement because is faster
319 stmt = con.createStatement();
320 int rs = executeUpdate(stmt,sql);
321 } catch (Exception e) {
322 theLog.printDebugInfo("-- set media failed -- insert");
323 throw new StorageObjectException("-- set media failed -- insert "
326 freeConnection(con,stmt);
330 public void deleteByContentId(String contentId)
331 throws StorageObjectException {
332 if (contentId == null) {
333 //theLog.printDebugInfo("-- delete topics failed -- no content id");
336 //delete all row with content_id=contentId
337 String sql = "delete from "+ theTable +" where content_id=" + contentId;
339 Connection con=null;Statement stmt=null;
341 con = getPooledCon();
342 // should be a preparedStatement because is faster
343 stmt = con.createStatement();
344 int rs = executeUpdate(stmt,sql);
345 } catch (Exception e) {
346 theLog.printDebugInfo("-- delete by contentId failed ");
347 throw new StorageObjectException("-- delete by content id failed -- delete "
350 freeConnection(con,stmt);
354 public void deleteByMediaId(String mediaId)
355 throws StorageObjectException {
356 if (mediaId == null) {
357 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
360 //delete all row with content_id=contentId
361 String sql = "delete from "+ theTable +" where media_id=" + mediaId;
363 Connection con=null;Statement stmt=null;
365 con = getPooledCon();
366 // should be a preparedStatement because is faster
367 stmt = con.createStatement();
368 int rs = executeUpdate(stmt,sql);
369 theLog.printDebugInfo("-- delete media success ");
370 } catch (Exception e) {
371 theLog.printDebugInfo("-- delete media failed ");
372 throw new StorageObjectException("-- delete by media id failed -- "
375 freeConnection(con,stmt);
379 public void delete(String contentId, String mediaId)
380 throws StorageObjectException {
381 if (mediaId == null || contentId==null) {
382 theLog.printDebugInfo("-- delete media failed -- missing parameter");
385 //delete all row with content_id=contentId and media_id=mediaId
386 String sql = "delete from "+ theTable +" where media_id=" + mediaId + " and content_id= "+contentId;
388 Connection con=null;Statement stmt=null;
390 con = getPooledCon();
391 // should be a preparedStatement because is faster
392 stmt = con.createStatement();
393 int rs = executeUpdate(stmt,sql);
394 theLog.printDebugInfo("-- delete content_x_media success ");
395 } catch (Exception e) {
396 theLog.printDebugInfo("-- delete content_x_media failed ");
397 throw new StorageObjectException("-- delete content_x_media failed -- "
400 freeConnection(con,stmt);
405 public EntityList getContent(EntityMedia media)
406 throws StorageObjectException {
407 EntityList returnList=null;
409 String id = media.getId();
410 String select = "select content_id from " + theTable + " where media_id=" + id;
412 // execute select statement
413 Connection con=null;Statement stmt=null;
415 con = getPooledCon();
416 // should be a preparedStatement because is faster
417 stmt = con.createStatement();
418 ResultSet rs = executeSql(stmt,select);
420 String mediaSelect= "id IN (";
423 if (first==false) mediaSelect+=",";
424 mediaSelect += rs.getString(1);
429 returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,-1);
432 catch (Exception e) {
433 theLog.printDebugInfo("-- get content failed");
434 throw new StorageObjectException("-- get content failed -- "
437 finally { freeConnection(con,stmt);}
443 * Returns a EntityList with all content-objects having a relation to a media
446 public EntityList getContent()
447 throws StorageObjectException {
448 EntityList returnList=null;
450 String select = "select distinct content_id from " + theTable;
451 // execute select statement
452 Connection con=null;Statement stmt=null;
454 con = getPooledCon();
455 // should be a preparedStatement because is faster
456 stmt = con.createStatement();
457 ResultSet rs = executeSql(stmt,select);
459 String mediaSelect= "id IN (";
462 if (first==false) mediaSelect+=",";
463 mediaSelect += rs.getString(1);
468 returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,"webdb_lastchange desc");
471 catch (Exception e) {
472 theLog.printDebugInfo("-- get content failed");
473 throw new StorageObjectException("-- get content failed -- "
476 finally { freeConnection(con,stmt);}