f88c157d5cc230e69d6f33276ac932817e2c821f
[mir.git] / source / mircoders / storage / DatabaseContentToMedia.java
1 /*
2  * Copyright (C) 2001, 2002 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
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.
10  *
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.
15  *
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
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with  any library licensed under the Apache Software License,
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23  * (or with modified versions of the above that use the same license as the above),
24  * and distribute linked combinations including the two.  You must obey the
25  * GNU General Public License in all respects for all of the code used other than
26  * the above mentioned libraries.  If you modify this file, you may extend this
27  * exception to your version of the file, but you are not obligated to do so.
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30
31 package mircoders.storage;
32
33 import java.sql.Connection;
34 import java.sql.ResultSet;
35 import java.sql.Statement;
36 import java.util.Vector;
37
38 import mir.entity.EntityList;
39 import mir.log.LoggerWrapper;
40 import mir.storage.Database;
41 import mir.storage.StorageObject;
42 import mir.storage.StorageObjectExc;
43 import mir.storage.StorageObjectFailure;
44 import mircoders.entity.EntityContent;
45 import mircoders.entity.EntityUploadedMedia;
46
47 /**
48  * <b>implements abstract DB connection to the content_x_media SQL table
49  *
50  * @author RK, mir-coders group
51  * @version $Id: DatabaseContentToMedia.java,v 1.19.2.3 2003/11/25 02:04:43 rk Exp $
52  *
53  */
54
55 public class DatabaseContentToMedia extends Database implements StorageObject{
56
57   private static DatabaseContentToMedia instance;
58
59   public synchronized static DatabaseContentToMedia getInstance() {
60     if (instance == null) {
61       instance = new DatabaseContentToMedia();
62     }
63     return instance;
64   }
65
66   private DatabaseContentToMedia() {
67     super();
68
69     logger = new LoggerWrapper("Database.ContentToMedia");
70     hasTimestamp = false;
71     theTable = "content_x_media";
72     theEntityClass = mir.entity.GenericEntity.class;
73   }
74
75   public boolean hasMedia(EntityContent content) throws StorageObjectFailure,
76       StorageObjectExc {
77     if (content != null) {
78       try {        
79         return (getSize("content_id=" + content.getId()) == 0) ? false:true;
80       }
81       catch (Exception e) {
82         logger.error("DatabaseContentToMedia.hasMedia: " + e.toString());
83         throw new StorageObjectFailure("DatabaseContentToMedia.hasMedia: " +
84                                        e.toString(), e);
85       }
86     }
87     else {
88       logger.error("DatabaseContentToMedia.hasMedia: content == null");
89       throw new StorageObjectExc(
90           "DatabaseContentToMedia.hasMedia: content == null");
91     }
92   }
93
94   private EntityList getMultiMediaForContent(StorageObject store, EntityContent content)
95     throws StorageObjectFailure {
96     
97     EntityList returnList = null;
98     if (content != null) {
99       String id = content.getId();
100       try {        
101         Vector extraTable = new Vector();
102         extraTable.add(theTable+" cxm");
103         // media should stay in uploaded order. this is especially important
104         // for photo stories which require a specific chronologic order.
105         // this is why we have the the second parameter "id"       
106         store.selectByWhereClause("m", extraTable, 
107           "m.id=cxm.media_id and cxm.content_id="+id, "id", -1, -1);
108                                         
109                         } catch (Exception e) {
110         logger.error("DatabaseContentToMedia.getMultiMediaForContent: " + e.toString());
111         throw new StorageObjectFailure("DatabaseContentToMedia.etMultiMediaForContent: " +
112                                      e.toString(), e);
113       }
114     }
115     return returnList;
116   }
117     
118   /**
119    * get all the audio belonging to a content entity
120    */
121   public EntityList getAudio(EntityContent content) throws StorageObjectFailure {
122     return getMultiMediaForContent(DatabaseAudio.getInstance(), content);
123   }
124
125   /**
126    * get all the video belonging to a content entity
127    */
128   public EntityList getVideo(EntityContent content) throws StorageObjectFailure {
129     return getMultiMediaForContent(DatabaseVideo.getInstance(), content);
130   }
131
132   /**
133    * get all the images belonging to a content entity
134    */
135   public EntityList getImages(EntityContent content) throws StorageObjectFailure {
136     return getMultiMediaForContent(DatabaseImages.getInstance(), content);
137   }
138
139   /**
140    * get all the uploaded/other Media belonging to a content entity
141    */
142   public EntityList getOther(EntityContent content) throws StorageObjectFailure {
143     return getMultiMediaForContent(DatabaseOther.getInstance(), content);
144   }
145
146   /**
147    * get all the uploaded/other Media belonging to a content entity
148    */
149   public EntityList getUploadedMedia(EntityContent content) throws StorageObjectFailure {
150     return getMultiMediaForContent(DatabaseUploadedMedia.getInstance(), content);
151   }
152
153
154   public void setMedia(String contentId, String[] mediaId) throws StorageObjectFailure {
155     if (contentId == null || mediaId == null || mediaId[0] == null) {
156       return;
157     }
158     //first delete all row with content_id=contentId
159     String sql = "delete from " + theTable + " where content_id=" + contentId;
160
161     Connection con = null;
162     Statement stmt = null;
163     try {
164       con = getPooledCon();
165       // should be a preparedStatement because is faster
166       stmt = con.createStatement();
167       ResultSet rs = executeSql(stmt, sql);
168     }
169     catch (Exception e) {
170       logger.error("-- set media failed -- delete");
171       throw new StorageObjectFailure("-- set media failed -- delete", e);
172     }
173     finally {
174       freeConnection(con, stmt);
175     }
176
177     //now insert
178     //first delete all row with content_id=contentId
179     for (int i = 0; i < mediaId.length; i++) {
180       sql = "insert into " + theTable + " (content_id,media_id) values ("
181           + contentId + "," + mediaId[i] + ")";
182       try {
183         con = getPooledCon();
184         // should be a preparedStatement because is faster
185         stmt = con.createStatement();
186         int rs = executeUpdate(stmt, sql);
187       }
188       catch (Exception e) {
189         logger.error("-- set topics failed -- insert");
190         throw new StorageObjectFailure("-- set topics failed -- insert ", e);
191       }
192       finally {
193         freeConnection(con, stmt);
194       }
195     }
196   }
197
198   public void addMedia(String contentId, String mediaId) throws
199       StorageObjectFailure {
200     if (contentId == null && mediaId == null) {
201       return;
202     }
203
204     Connection con = null;
205     Statement stmt = null;
206     //now insert
207
208     String sql = "insert into " + theTable + " (content_id,media_id) values ("
209         + contentId + "," + mediaId + ")";
210     try {
211       con = getPooledCon();
212       // should be a preparedStatement because is faster
213       stmt = con.createStatement();
214       int rs = executeUpdate(stmt, sql);
215     }
216     catch (Exception e) {
217       logger.error("-- add media failed -- insert");
218       throw new StorageObjectFailure("-- add media failed -- insert ", e);
219     }
220     finally {
221       freeConnection(con, stmt);
222     }
223   }
224
225   public void setMedia(String contentId, String mediaId) throws
226       StorageObjectFailure {
227     if (contentId == null && mediaId == null) {
228       return;
229     }
230     //first delete all row with content_id=contentId
231     String sql = "delete from " + theTable + " where content_id=" + contentId;
232
233     Connection con = null;
234     Statement stmt = null;
235     try {
236       con = getPooledCon();
237       // should be a preparedStatement because is faster
238       stmt = con.createStatement();
239       int rs = executeUpdate(stmt, sql);
240     }
241     catch (Exception e) {
242       logger.error("-- set media failed -- delete");
243       throw new StorageObjectFailure("-- set media failed -- delete ", e);
244     }
245     finally {
246       freeConnection(con, stmt);
247     }
248
249     //now insert
250     //first delete all row with content_id=contentId
251
252     sql = "insert into " + theTable + " (content_id,media_id) values ("
253         + contentId + "," + mediaId + ")";
254     try {
255       con = getPooledCon();
256       // should be a preparedStatement because is faster
257       stmt = con.createStatement();
258       int rs = executeUpdate(stmt, sql);
259     }
260     catch (Exception e) {
261       logger.error("-- set media failed -- insert");
262       throw new StorageObjectFailure("-- set media failed -- insert ", e);
263     }
264     finally {
265       freeConnection(con, stmt);
266     }
267   }
268
269   public void deleteByContentId(String contentId) throws StorageObjectFailure {
270     if (contentId == null) {
271       //theLog.printDebugInfo("-- delete topics failed -- no content id");
272       return;
273     }
274     //delete all row with content_id=contentId
275     String sql = "delete from " + theTable + " where content_id=" + contentId;
276
277     Connection con = null;
278     Statement stmt = null;
279     try {
280       con = getPooledCon();
281       // should be a preparedStatement because is faster
282       stmt = con.createStatement();
283       int rs = executeUpdate(stmt, sql);
284     }
285     catch (Exception e) {
286       logger.error("-- delete by contentId failed  ");
287       throw new StorageObjectFailure(
288           "-- delete by content id failed -- delete ", e);
289     }
290     finally {
291       freeConnection(con, stmt);
292     }
293   }
294
295   public void deleteByMediaId(String mediaId) throws StorageObjectFailure {
296   
297     if (mediaId == null) {
298       //theLog.printDebugInfo("-- delete topics failed -- no topic id");
299       return;
300     }
301     //delete all row with content_id=contentId
302     String sql = "delete from " + theTable + " where media_id=" + mediaId;
303
304     Connection con = null;
305     Statement stmt = null;
306     try {
307       con = getPooledCon();
308       // should be a preparedStatement because is faster
309       stmt = con.createStatement();
310       int rs = executeUpdate(stmt, sql);
311       logger.debug("-- delete media success ");
312     }
313     catch (Exception e) {
314       logger.error("-- delete media failed ");
315       throw new StorageObjectFailure("-- delete by media id failed -- ", e);
316     }
317     finally {
318       freeConnection(con, stmt);
319     }
320   }
321
322   public void delete(String contentId, String mediaId) throws
323       StorageObjectFailure {
324     if (mediaId == null || contentId == null) {
325       logger.debug("-- delete media failed -- missing parameter");
326       return;
327     }
328     //delete all row with content_id=contentId and media_id=mediaId
329     String sql = "delete from " + theTable + " where media_id=" + mediaId +
330         " and content_id= " + contentId;
331
332     Connection con = null;
333     Statement stmt = null;
334     try {
335       con = getPooledCon();
336       // should be a preparedStatement because is faster
337       stmt = con.createStatement();
338       int rs = executeUpdate(stmt, sql);
339       logger.debug("-- delete content_x_media success ");
340     }
341     catch (Exception e) {
342       logger.error("-- delete content_x_media failed ");
343       throw new StorageObjectFailure("-- delete content_x_media failed -- ", e);
344     }
345     finally {
346       freeConnection(con, stmt);
347     }
348   }
349
350   public EntityList getContent(EntityUploadedMedia media) throws
351       StorageObjectFailure {
352     EntityList returnList = null;
353     if (media != null) {
354 //    TODO rewrite as relational sql
355       String id = media.getId();
356       String select = "select content_id from " + theTable + " where media_id=" +
357           id;
358
359       // execute select statement
360       Connection con = null;
361       Statement stmt = null;
362       try {
363         con = getPooledCon();
364         // should be a preparedStatement because is faster
365         stmt = con.createStatement();
366         ResultSet rs = executeSql(stmt, select);
367         if (rs != null) {
368           String mediaSelect = "id IN (";
369           boolean first = true;
370           while (rs.next()) {
371             if (first == false)
372               mediaSelect += ",";
373             mediaSelect += rs.getString(1);
374             first = false;
375           }
376           mediaSelect += ")";
377           if (first == false)
378             returnList = DatabaseContent.getInstance().selectByWhereClause(
379                 mediaSelect, -1);
380         }
381       }
382       catch (Exception e) {
383         logger.error("-- get content failed");
384         throw new StorageObjectFailure("-- get content failed -- ", e);
385       }
386       finally {
387         freeConnection(con, stmt);
388       }
389     }
390     return returnList;
391   }
392
393   /**
394    * Returns a EntityList with all content-objects having a relation to a media
395    */
396
397   public EntityList getContent() throws StorageObjectFailure {
398     EntityList returnList = null;
399
400     String select = "select distinct content_id from " + theTable;
401     // execute select statement
402     Connection con = null;
403     Statement stmt = null;
404     try {
405 //    TODO rewrite as relational sql
406       con = getPooledCon();
407       // should be a preparedStatement because is faster
408       stmt = con.createStatement();
409       ResultSet rs = executeSql(stmt, select);
410       if (rs != null) {
411         String mediaSelect = "id IN (";
412         boolean first = true;
413         while (rs.next()) {
414           if (first == false)
415             mediaSelect += ",";
416           mediaSelect += rs.getString(1);
417           first = false;
418         }
419         mediaSelect += ")";
420         if (first == false)
421           returnList = DatabaseContent.getInstance().selectByWhereClause(
422               mediaSelect, "webdb_lastchange desc");
423       }
424     }
425     catch (Exception e) {
426       logger.error("-- get content failed");
427       throw new StorageObjectFailure("-- get content failed -- ", e);
428     }
429     finally {
430       freeConnection(con, stmt);
431     }
432
433     return returnList;
434   }
435
436 }