moved logic to EntityUploadedMedia
[mir.git] / source / mircoders / storage / DatabaseContentToMedia.java
1 package mircoders.storage;
2
3 import java.lang.*;
4 import java.sql.*;
5 import java.io.*;
6 import java.util.*;
7
8 import freemarker.template.*;
9
10 import mir.storage.*;
11 import mir.entity.*;
12 import mir.misc.*;
13
14 import mircoders.entity.*;
15
16 /**
17  * <b>Diese Klasse implementiert die Datenbankverbindung zur MetaObjekt-Tabelle
18  *
19  *
20  */
21
22 public class DatabaseContentToMedia extends Database implements StorageObject{
23
24   private static DatabaseContentToMedia instance;
25
26   public static DatabaseContentToMedia getInstance()
27     throws StorageObjectException {
28     if (instance == null) {
29       instance = new DatabaseContentToMedia();
30       instance.myselfDatabase = instance;
31     }
32     return instance;
33   }
34
35   private DatabaseContentToMedia()
36     throws StorageObjectException {
37
38     super();
39     this.hasTimestamp = false;
40     this.theTable="content_x_media";
41   }
42
43   /**
44    * get all the media-files belonging to a content entity
45    *
46    */
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+")";
55
56       try {
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());
61       }
62     }
63     return returnList;
64   }
65
66   /**
67    * get all the images belonging to a content entity
68    *
69    */
70   public EntityList getImages(EntityContent content)
71     throws StorageObjectException {
72     EntityList returnList=null;
73     if (content != null) {
74       // get all to_topic from media_x_topic
75       String id = content.getId();
76       //this is not supported by mysql
77       String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
78
79       try {
80         returnList = DatabaseImages.getInstance().selectByWhereClause(subselect,-1);
81       } catch (Exception e) {
82         theLog.printDebugInfo("-- get images failed " + e.toString());
83         throw new StorageObjectException("-- get images failed " + e.toString());
84       }
85     }
86     return returnList;
87   }
88
89
90   /**
91    * get all the uploaded Media belonging to a content entity
92    *
93    */
94   public EntityList getUploadedMedia(EntityContent content)
95     throws StorageObjectException
96   {
97     /** @todo this should only fetch published media / rk */
98
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+")";
105
106       try {
107         returnList = DatabaseMedia.getInstance().selectByWhereClause(subselect,"id");
108       } catch (Exception e) {
109         e.printStackTrace();
110         theLog.printDebugInfo("-- get uploadedMedia failed " + e.toString());
111         throw new StorageObjectException("-- get uploadedMedia failed " + e.toString());
112       }
113     }
114     return returnList;
115   }
116
117
118   public void setMedia(String contentId, String[] mediaId)
119     throws StorageObjectException {
120     if (contentId == null){
121       return;
122     }
123     if (mediaId==null || mediaId[0]==null) {
124       return;
125     }
126     //first delete all row with content_id=contentId
127     String sql = "delete from "+ theTable +" where content_id=" + contentId;
128
129     Connection con=null;Statement stmt=null;
130     try {
131       con = getPooledCon();
132       // should be a preparedStatement because is faster
133       stmt = con.createStatement();
134       ResultSet rs = executeSql(stmt,sql);
135     } catch (Exception e) {
136       theLog.printDebugInfo("-- set media failed -- delete");
137       throw new StorageObjectException("-- set media failed -- delete"+e.toString());
138     } finally {
139       freeConnection(con,stmt);
140     }
141
142     //now insert
143     //first delete all row with content_id=contentId
144     for (int i=0;i<mediaId.length;i++) {
145       sql = "insert into "+ theTable +" (content_id,media_id) values ("
146             + contentId + "," + mediaId[i] + ")";
147       try {
148         con = getPooledCon();
149         // should be a preparedStatement because is faster
150         stmt = con.createStatement();
151         int rs = executeUpdate(stmt,sql);
152       } catch (Exception e) {
153         theLog.printDebugInfo("-- set topics failed -- insert");
154         throw new StorageObjectException("-- set topics failed -- insert "+e.toString());
155       } finally {
156         freeConnection(con,stmt);
157       }
158     }
159   }
160
161   public void addMedia(String contentId, String mediaId)
162     throws StorageObjectException {
163     if (contentId == null && mediaId == null) {
164       return;
165     }
166
167     Connection con=null;Statement stmt=null;
168     //now insert
169
170     String sql = "insert into "+ theTable +" (content_id,media_id) values ("
171           + contentId + "," + mediaId + ")";
172     try {
173       con = getPooledCon();
174       // should be a preparedStatement because is faster
175       stmt = con.createStatement();
176       int rs = executeUpdate(stmt,sql);
177     } catch (Exception e) {
178       theLog.printDebugInfo("-- add media failed -- insert");
179       throw new StorageObjectException("-- add media failed -- insert "
180         +e.toString());
181     } finally {
182       freeConnection(con,stmt);
183     }
184   }
185
186   public void setMedia(String contentId, String mediaId)
187     throws StorageObjectException {
188     if (contentId == null && mediaId == null) {
189       return;
190     }
191     //first delete all row with content_id=contentId
192     String sql = "delete from "+ theTable +" where content_id=" + contentId;
193
194     Connection con=null;Statement stmt=null;
195     try {
196       con = getPooledCon();
197       // should be a preparedStatement because is faster
198       stmt = con.createStatement();
199       int rs = executeUpdate(stmt,sql);
200     } catch (Exception e) {
201       theLog.printDebugInfo("-- set media failed -- delete");
202       throw new StorageObjectException("-- set media failed -- delete "
203         +e.toString());
204     } finally {
205       freeConnection(con,stmt);
206     }
207
208     //now insert
209     //first delete all row with content_id=contentId
210
211     sql = "insert into "+ theTable +" (content_id,media_id) values ("
212           + contentId + "," + mediaId + ")";
213     try {
214       con = getPooledCon();
215       // should be a preparedStatement because is faster
216       stmt = con.createStatement();
217       int rs = executeUpdate(stmt,sql);
218     } catch (Exception e) {
219       theLog.printDebugInfo("-- set media failed -- insert");
220       throw new StorageObjectException("-- set media failed -- insert "
221         +e.toString());
222     } finally {
223       freeConnection(con,stmt);
224     }
225   }
226
227   public void deleteByContentId(String contentId)
228     throws StorageObjectException {
229     if (contentId == null) {
230       //theLog.printDebugInfo("-- delete topics failed -- no content id");
231       return;
232     }
233     //delete all row with content_id=contentId
234     String sql = "delete from "+ theTable +" where content_id=" + contentId;
235
236     Connection con=null;Statement stmt=null;
237     try {
238       con = getPooledCon();
239       // should be a preparedStatement because is faster
240       stmt = con.createStatement();
241       int rs = executeUpdate(stmt,sql);
242     } catch (Exception e) {
243       theLog.printDebugInfo("-- delete by contentId failed  ");
244       throw new StorageObjectException("-- delete by content id failed -- delete "
245         +e.toString());
246     } finally {
247       freeConnection(con,stmt);
248     }
249   }
250
251   public void deleteByMediaId(String mediaId)
252     throws StorageObjectException {
253     if (mediaId == null) {
254       //theLog.printDebugInfo("-- delete topics failed -- no topic id");
255       return;
256     }
257     //delete all row with content_id=contentId
258     String sql = "delete from "+ theTable +" where media_id=" + mediaId;
259
260     Connection con=null;Statement stmt=null;
261     try {
262       con = getPooledCon();
263       // should be a preparedStatement because is faster
264       stmt = con.createStatement();
265       int rs = executeUpdate(stmt,sql);
266       theLog.printDebugInfo("-- delete media success ");
267     } catch (Exception e) {
268       theLog.printDebugInfo("-- delete media failed ");
269       throw new StorageObjectException("-- delete by media id failed -- "
270         +e.toString());
271     } finally {
272       freeConnection(con,stmt);
273     }
274   }
275
276   public void delete(String contentId, String mediaId)
277     throws StorageObjectException {
278     if (mediaId == null || contentId==null) {
279       theLog.printDebugInfo("-- delete media failed -- missing parameter");
280       return;
281     }
282     //delete all row with content_id=contentId and media_id=mediaId
283     String sql = "delete from "+ theTable +" where media_id=" + mediaId + " and content_id= "+contentId;
284
285     Connection con=null;Statement stmt=null;
286     try {
287       con = getPooledCon();
288       // should be a preparedStatement because is faster
289       stmt = con.createStatement();
290       int rs = executeUpdate(stmt,sql);
291       theLog.printDebugInfo("-- delete content_x_media success ");
292     } catch (Exception e) {
293       theLog.printDebugInfo("-- delete content_x_media failed ");
294       throw new StorageObjectException("-- delete content_x_media failed -- "
295         +e.toString());
296     } finally {
297       freeConnection(con,stmt);
298     }
299   }
300
301
302   public EntityList getContent(EntityMedia media)
303     throws StorageObjectException {
304     EntityList returnList=null;
305     if (media != null) {
306       String id = media.getId();
307       String select = "select content_id from " + theTable + " where media_id=" + id;
308
309       // execute select statement
310       Connection con=null;Statement stmt=null;
311       try {
312         con = getPooledCon();
313         // should be a preparedStatement because is faster
314         stmt = con.createStatement();
315         ResultSet rs = executeSql(stmt,select);
316         if (rs!=null) {
317           String mediaSelect= "id IN (";
318           boolean first=true;
319           while (rs.next()) {
320             if (first==false) mediaSelect+=",";
321             mediaSelect += rs.getString(1);
322             first=false;
323           }
324           mediaSelect+=")";
325           if (first==false)
326             returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,-1);
327         }
328       }
329       catch (Exception e) {
330         theLog.printDebugInfo("-- get content failed");
331         throw new StorageObjectException("-- get content failed -- "
332         +e.toString());
333       }
334       finally { freeConnection(con,stmt);}
335     }
336     return returnList;
337   }
338
339 /**
340  * Returns a EntityList with all content-objects having a relation to a media
341  */
342
343 public EntityList getContent()
344     throws StorageObjectException {
345     EntityList returnList=null;
346
347     String select = "select distinct content_id from " + theTable;
348     // execute select statement
349     Connection con=null;Statement stmt=null;
350     try {
351       con = getPooledCon();
352       // should be a preparedStatement because is faster
353       stmt = con.createStatement();
354       ResultSet rs = executeSql(stmt,select);
355       if (rs!=null) {
356         String mediaSelect= "id IN (";
357         boolean first=true;
358         while (rs.next()) {
359           if (first==false) mediaSelect+=",";
360           mediaSelect += rs.getString(1);
361           first=false;
362         }
363         mediaSelect+=")";
364         if (first==false)
365           returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,"webdb_lastchange desc");
366       }
367     }
368     catch (Exception e) {
369         theLog.printDebugInfo("-- get content failed");
370         throw new StorageObjectException("-- get content failed -- "
371         +e.toString());
372     }
373     finally { freeConnection(con,stmt);}
374
375     return returnList;
376   }
377
378 }