2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with the com.oreilly.servlet library, any library
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24 * the above that use the same license as the above), and distribute linked
25 * combinations including the two. You must obey the GNU General Public
26 * License in all respects for all of the code used other than the above
27 * mentioned libraries. If you modify this file, you may extend this exception
28 * to your version of the file, but you are not obligated to do so. If you do
29 * not wish to do so, delete this exception statement from your version.
32 package mircoders.storage;
39 import freemarker.template.*;
45 import mircoders.entity.*;
48 * <b>Diese Klasse implementiert die Datenbankverbindung zur MetaObjekt-Tabelle
53 public class DatabaseContentToMedia extends Database implements StorageObject{
55 private static DatabaseContentToMedia instance;
57 // the following *has* to be sychronized cause this static method
58 // could get preemted and we could end up with 2 instances of DatabaseFoo.
59 // see the "Singletons with needles and thread" article at JavaWorld -mh
60 public synchronized static DatabaseContentToMedia getInstance()
61 throws StorageObjectException {
62 if (instance == null) {
63 instance = new DatabaseContentToMedia();
64 instance.myselfDatabase = instance;
69 private DatabaseContentToMedia()
70 throws StorageObjectException {
73 this.hasTimestamp = false;
74 this.theTable="content_x_media";
75 try { this.theEntityClass = Class.forName("mir.entity.GenericEntity"); }
76 catch (Exception e) { throw new StorageObjectException(e.toString()); }
80 * get all the media-files belonging to a content entity
83 public EntityList getMedia(EntityContent content)
84 throws StorageObjectException {
85 EntityList returnList=null;
86 if (content != null) {
87 // get all to_topic from media_x_topic
88 String id = content.getId();
89 //this is not supported by mysql
90 String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
93 // media should stay in uploaded order. this is especially important
94 // for photo stories which require a specific chronologic order.
95 // this is why we have the the second parameter "id"
96 returnList = DatabaseMedia.getInstance().selectByWhereClause(subselect,"id",-1);
97 } catch (Exception e) {
98 theLog.printDebugInfo("-- get media failed " + e.toString());
99 throw new StorageObjectException("-- get media failed " + e.toString());
105 public boolean hasMedia(EntityContent content)
106 throws StorageObjectException {
107 String wc = "content_id="+content.getId();
108 if( content != null) {
110 if(selectByWhereClause(wc,-1).size() == 0)
114 } catch (Exception e) {
115 theLog.printError("-- hasMedia failed " + e.toString());
116 throw new StorageObjectException("-- hasMedia failed " + e.toString());
119 theLog.printError("-- hasMedia failed: content is NULL");
120 throw new StorageObjectException("-- hasMedia failed: content is NULL");
128 * get all the audio belonging to a content entity
131 public EntityList getAudio(EntityContent content)
132 throws StorageObjectException {
133 EntityList returnList=null;
134 if (content != null) {
135 // get all to_topic from media_x_topic
136 String id = content.getId();
137 //this is not supported by mysql
138 String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
141 // media should stay in uploaded order. this is especially important
142 // for photo stories which require a specific chronologic order.
143 // this is why we have the the second parameter "id"
144 returnList = DatabaseAudio.getInstance().selectByWhereClause(subselect,"id",-1);
145 } catch (Exception e) {
146 theLog.printDebugInfo("-- get audio failed " + e.toString());
147 throw new StorageObjectException("-- get audio failed " + e.toString());
154 * get all the video belonging to a content entity
157 public EntityList getVideo(EntityContent content)
158 throws StorageObjectException {
159 EntityList returnList=null;
160 if (content != null) {
161 // get all to_topic from media_x_topic
162 String id = content.getId();
163 //this is not supported by mysql
164 String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
167 // media should stay in uploaded order. this is especially important
168 // for photo stories which require a specific chronologic order.
169 // this is why we have the the second parameter "id"
170 returnList = DatabaseVideo.getInstance().selectByWhereClause(subselect,"id",-1);
171 } catch (Exception e) {
172 theLog.printDebugInfo("-- get video failed " + e.toString());
173 throw new StorageObjectException("-- get video failed " + e.toString());
180 * get all the images belonging to a content entity
183 public EntityList getImages(EntityContent content)
184 throws StorageObjectException {
185 EntityList returnList=null;
186 if (content != null) {
187 // get all to_topic from media_x_topic
188 String id = content.getId();
189 //this is not supported by mysql
190 String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
193 // media should stay in uploaded order. this is especially important
194 // for photo stories which require a specific chronologic order.
195 // this is why we have the the second parameter "id"
196 returnList = DatabaseImages.getInstance().selectByWhereClause(subselect,"id",-1);
197 } catch (Exception e) {
198 theLog.printDebugInfo("-- get images failed " + e.toString());
199 throw new StorageObjectException("-- get images failed " + e.toString());
207 * get all the uploaded/other Media belonging to a content entity
210 public EntityList getOther(EntityContent content)
211 throws StorageObjectException
213 /** @todo this should only fetch published media / rk */
215 EntityList returnList=null;
216 if (content != null) {
217 // get all to_topic from media_x_topic
218 String id = content.getId();
219 //this is not supported by mysql
220 String subselect = "id in (select media_id from " + theTable +
221 " where content_id=" + id+")";
224 // media should stay in uploaded order. this is especially important
225 // for photo stories which require a specific chronologic order.
226 // this is why we have the the second parameter "id"
227 returnList = DatabaseOther.getInstance().selectByWhereClause(subselect,
229 } catch (Exception e) {
231 theLog.printDebugInfo("-- get Other failed " + e.toString());
232 throw new StorageObjectException("-- get Other failed "
240 * get all the uploaded/other Media belonging to a content entity
243 public EntityList getUploadedMedia(EntityContent content)
244 throws StorageObjectException
246 /** @todo this should only fetch published media / rk */
248 EntityList returnList=null;
249 if (content != null) {
250 // get all to_topic from media_x_topic
251 String id = content.getId();
252 //this is not supported by mysql
253 String subselect = "id in (select media_id from " + theTable +
254 " where content_id=" + id+")";
257 returnList = DatabaseUploadedMedia.getInstance().selectByWhereClause(subselect,
259 } catch (Exception e) {
261 theLog.printDebugInfo("-- get uploadedMedia failed " + e.toString());
262 throw new StorageObjectException("-- get uploadedMedia failed "
270 public void setMedia(String contentId, String[] mediaId)
271 throws StorageObjectException {
272 if (contentId == null){
275 if (mediaId==null || mediaId[0]==null) {
278 //first delete all row with content_id=contentId
279 String sql = "delete from "+ theTable +" where content_id=" + contentId;
281 Connection con=null;Statement stmt=null;
283 con = getPooledCon();
284 // should be a preparedStatement because is faster
285 stmt = con.createStatement();
286 ResultSet rs = executeSql(stmt,sql);
287 } catch (Exception e) {
288 theLog.printDebugInfo("-- set media failed -- delete");
289 throw new StorageObjectException("-- set media failed -- delete"+e.toString());
291 freeConnection(con,stmt);
295 //first delete all row with content_id=contentId
296 for (int i=0;i<mediaId.length;i++) {
297 sql = "insert into "+ theTable +" (content_id,media_id) values ("
298 + contentId + "," + mediaId[i] + ")";
300 con = getPooledCon();
301 // should be a preparedStatement because is faster
302 stmt = con.createStatement();
303 int rs = executeUpdate(stmt,sql);
304 } catch (Exception e) {
305 theLog.printDebugInfo("-- set topics failed -- insert");
306 throw new StorageObjectException("-- set topics failed -- insert "+e.toString());
308 freeConnection(con,stmt);
313 public void addMedia(String contentId, String mediaId)
314 throws StorageObjectException {
315 if (contentId == null && mediaId == null) {
319 Connection con=null;Statement stmt=null;
322 String sql = "insert into "+ theTable +" (content_id,media_id) values ("
323 + contentId + "," + mediaId + ")";
325 con = getPooledCon();
326 // should be a preparedStatement because is faster
327 stmt = con.createStatement();
328 int rs = executeUpdate(stmt,sql);
329 } catch (Exception e) {
330 theLog.printDebugInfo("-- add media failed -- insert");
331 throw new StorageObjectException("-- add media failed -- insert "
334 freeConnection(con,stmt);
338 public void setMedia(String contentId, String mediaId)
339 throws StorageObjectException {
340 if (contentId == null && mediaId == null) {
343 //first delete all row with content_id=contentId
344 String sql = "delete from "+ theTable +" where content_id=" + contentId;
346 Connection con=null;Statement stmt=null;
348 con = getPooledCon();
349 // should be a preparedStatement because is faster
350 stmt = con.createStatement();
351 int rs = executeUpdate(stmt,sql);
352 } catch (Exception e) {
353 theLog.printDebugInfo("-- set media failed -- delete");
354 throw new StorageObjectException("-- set media failed -- delete "
357 freeConnection(con,stmt);
361 //first delete all row with content_id=contentId
363 sql = "insert into "+ theTable +" (content_id,media_id) values ("
364 + contentId + "," + mediaId + ")";
366 con = getPooledCon();
367 // should be a preparedStatement because is faster
368 stmt = con.createStatement();
369 int rs = executeUpdate(stmt,sql);
370 } catch (Exception e) {
371 theLog.printDebugInfo("-- set media failed -- insert");
372 throw new StorageObjectException("-- set media failed -- insert "
375 freeConnection(con,stmt);
379 public void deleteByContentId(String contentId)
380 throws StorageObjectException {
381 if (contentId == null) {
382 //theLog.printDebugInfo("-- delete topics failed -- no content id");
385 //delete all row with content_id=contentId
386 String sql = "delete from "+ theTable +" where 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 } catch (Exception e) {
395 theLog.printDebugInfo("-- delete by contentId failed ");
396 throw new StorageObjectException("-- delete by content id failed -- delete "
399 freeConnection(con,stmt);
403 public void deleteByMediaId(String mediaId)
404 throws StorageObjectException {
405 if (mediaId == null) {
406 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
409 //delete all row with content_id=contentId
410 String sql = "delete from "+ theTable +" where media_id=" + mediaId;
412 Connection con=null;Statement stmt=null;
414 con = getPooledCon();
415 // should be a preparedStatement because is faster
416 stmt = con.createStatement();
417 int rs = executeUpdate(stmt,sql);
418 theLog.printDebugInfo("-- delete media success ");
419 } catch (Exception e) {
420 theLog.printDebugInfo("-- delete media failed ");
421 throw new StorageObjectException("-- delete by media id failed -- "
424 freeConnection(con,stmt);
428 public void delete(String contentId, String mediaId)
429 throws StorageObjectException {
430 if (mediaId == null || contentId==null) {
431 theLog.printDebugInfo("-- delete media failed -- missing parameter");
434 //delete all row with content_id=contentId and media_id=mediaId
435 String sql = "delete from "+ theTable +" where media_id=" + mediaId + " and content_id= "+contentId;
437 Connection con=null;Statement stmt=null;
439 con = getPooledCon();
440 // should be a preparedStatement because is faster
441 stmt = con.createStatement();
442 int rs = executeUpdate(stmt,sql);
443 theLog.printDebugInfo("-- delete content_x_media success ");
444 } catch (Exception e) {
445 theLog.printDebugInfo("-- delete content_x_media failed ");
446 throw new StorageObjectException("-- delete content_x_media failed -- "
449 freeConnection(con,stmt);
454 public EntityList getContent(EntityUploadedMedia media)
455 throws StorageObjectException {
456 EntityList returnList=null;
458 String id = media.getId();
459 String select = "select content_id from " + theTable + " where media_id=" + id;
461 // execute select statement
462 Connection con=null;Statement stmt=null;
464 con = getPooledCon();
465 // should be a preparedStatement because is faster
466 stmt = con.createStatement();
467 ResultSet rs = executeSql(stmt,select);
469 String mediaSelect= "id IN (";
472 if (first==false) mediaSelect+=",";
473 mediaSelect += rs.getString(1);
478 returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,-1);
481 catch (Exception e) {
482 theLog.printDebugInfo("-- get content failed");
483 throw new StorageObjectException("-- get content failed -- "
486 finally { freeConnection(con,stmt);}
492 * Returns a EntityList with all content-objects having a relation to a media
495 public EntityList getContent()
496 throws StorageObjectException {
497 EntityList returnList=null;
499 String select = "select distinct content_id from " + theTable;
500 // execute select statement
501 Connection con=null;Statement stmt=null;
503 con = getPooledCon();
504 // should be a preparedStatement because is faster
505 stmt = con.createStatement();
506 ResultSet rs = executeSql(stmt,select);
508 String mediaSelect= "id IN (";
511 if (first==false) mediaSelect+=",";
512 mediaSelect += rs.getString(1);
517 returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,"webdb_lastchange desc");
520 catch (Exception e) {
521 theLog.printDebugInfo("-- get content failed");
522 throw new StorageObjectException("-- get content failed -- "
525 finally { freeConnection(con,stmt);}