media_id -> to_media
[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 to_media 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     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+")";
102
103       try {
104         returnList = DatabaseMedia.getInstance().selectByWhereClause(subselect,"id");
105       } catch (Exception e) {
106         theLog.printDebugInfo("-- get uploadedMedia failed " + e.toString());
107         throw new StorageObjectException("-- get uploadedMedia failed " + e.toString());
108       }
109     }
110     return returnList;
111   }
112
113
114   public void setMedia(String contentId, String[] mediaId)
115     throws StorageObjectException {
116     if (contentId == null){
117       return;
118     }
119     if (mediaId==null || mediaId[0]==null) {
120       return;
121     }
122     //first delete all row with content_id=contentId
123     String sql = "delete from "+ theTable +" where content_id=" + contentId;
124
125     Connection con=null;Statement stmt=null;
126     try {
127       con = getPooledCon();
128       // should be a preparedStatement because is faster
129       stmt = con.createStatement();
130       ResultSet rs = executeSql(stmt,sql);
131     } catch (Exception e) {
132       theLog.printDebugInfo("-- set media failed -- delete");
133       throw new StorageObjectException("-- set media failed -- delete"+e.toString());
134     } finally {
135       freeConnection(con,stmt);
136     }
137
138     //now insert
139     //first delete all row with content_id=contentId
140     for (int i=0;i<mediaId.length;i++) {
141       sql = "insert into "+ theTable +" (content_id,media_id) values ("
142             + contentId + "," + mediaId[i] + ")";
143       try {
144         con = getPooledCon();
145         // should be a preparedStatement because is faster
146         stmt = con.createStatement();
147         int rs = executeUpdate(stmt,sql);
148       } catch (Exception e) {
149         theLog.printDebugInfo("-- set topics failed -- insert");
150         throw new StorageObjectException("-- set topics failed -- insert "+e.toString());
151       } finally {
152         freeConnection(con,stmt);
153       }
154     }
155   }
156
157   public void addMedia(String contentId, String mediaId)
158     throws StorageObjectException {
159     if (contentId == null && mediaId == null) {
160       return;
161     }
162
163     Connection con=null;Statement stmt=null;
164     //now insert
165
166     String sql = "insert into "+ theTable +" (content_id,media_id) values ("
167           + contentId + "," + mediaId + ")";
168     try {
169       con = getPooledCon();
170       // should be a preparedStatement because is faster
171       stmt = con.createStatement();
172       int rs = executeUpdate(stmt,sql);
173     } catch (Exception e) {
174       theLog.printDebugInfo("-- add media failed -- insert");
175       throw new StorageObjectException("-- add media failed -- insert "
176         +e.toString());
177     } finally {
178       freeConnection(con,stmt);
179     }
180   }
181
182   public void setMedia(String contentId, String mediaId)
183     throws StorageObjectException {
184     if (contentId == null && mediaId == null) {
185       return;
186     }
187     //first delete all row with content_id=contentId
188     String sql = "delete from "+ theTable +" where content_id=" + contentId;
189
190     Connection con=null;Statement stmt=null;
191     try {
192       con = getPooledCon();
193       // should be a preparedStatement because is faster
194       stmt = con.createStatement();
195       int rs = executeUpdate(stmt,sql);
196     } catch (Exception e) {
197       theLog.printDebugInfo("-- set media failed -- delete");
198       throw new StorageObjectException("-- set media failed -- delete "
199         +e.toString());
200     } finally {
201       freeConnection(con,stmt);
202     }
203
204     //now insert
205     //first delete all row with content_id=contentId
206
207     sql = "insert into "+ theTable +" (content_id,media_id) values ("
208           + contentId + "," + mediaId + ")";
209     try {
210       con = getPooledCon();
211       // should be a preparedStatement because is faster
212       stmt = con.createStatement();
213       int rs = executeUpdate(stmt,sql);
214     } catch (Exception e) {
215       theLog.printDebugInfo("-- set media failed -- insert");
216       throw new StorageObjectException("-- set media failed -- insert "
217         +e.toString());
218     } finally {
219       freeConnection(con,stmt);
220     }
221   }
222
223   public void deleteByContentId(String contentId)
224     throws StorageObjectException {
225     if (contentId == null) {
226       //theLog.printDebugInfo("-- delete topics failed -- no content id");
227       return;
228     }
229     //delete all row with content_id=contentId
230     String sql = "delete from "+ theTable +" where content_id=" + contentId;
231
232     Connection con=null;Statement stmt=null;
233     try {
234       con = getPooledCon();
235       // should be a preparedStatement because is faster
236       stmt = con.createStatement();
237       int rs = executeUpdate(stmt,sql);
238     } catch (Exception e) {
239       theLog.printDebugInfo("-- delete by contentId failed  ");
240       throw new StorageObjectException("-- delete by content id failed -- delete "
241         +e.toString());
242     } finally {
243       freeConnection(con,stmt);
244     }
245   }
246
247   public void deleteByMediaId(String mediaId)
248     throws StorageObjectException {
249     if (mediaId == null) {
250       //theLog.printDebugInfo("-- delete topics failed -- no topic id");
251       return;
252     }
253     //delete all row with content_id=contentId
254     String sql = "delete from "+ theTable +" where media_id=" + mediaId;
255
256     Connection con=null;Statement stmt=null;
257     try {
258       con = getPooledCon();
259       // should be a preparedStatement because is faster
260       stmt = con.createStatement();
261       int rs = executeUpdate(stmt,sql);
262       theLog.printDebugInfo("-- delete media success ");
263     } catch (Exception e) {
264       theLog.printDebugInfo("-- delete media failed ");
265       throw new StorageObjectException("-- delete by media id failed -- "
266         +e.toString());
267     } finally {
268       freeConnection(con,stmt);
269     }
270   }
271
272   public void delete(String contentId, String mediaId)
273     throws StorageObjectException {
274     if (mediaId == null || contentId==null) {
275       theLog.printDebugInfo("-- delete media failed -- missing parameter");
276       return;
277     }
278     //delete all row with content_id=contentId and media_id=mediaId
279     String sql = "delete from "+ theTable +" where media_id=" + mediaId + " and content_id= "+contentId;
280
281     Connection con=null;Statement stmt=null;
282     try {
283       con = getPooledCon();
284       // should be a preparedStatement because is faster
285       stmt = con.createStatement();
286       int rs = executeUpdate(stmt,sql);
287       theLog.printDebugInfo("-- delete content_x_media success ");
288     } catch (Exception e) {
289       theLog.printDebugInfo("-- delete content_x_media failed ");
290       throw new StorageObjectException("-- delete content_x_media failed -- "
291         +e.toString());
292     } finally {
293       freeConnection(con,stmt);
294     }
295   }
296
297
298   public EntityList getContent(EntityMedia media)
299     throws StorageObjectException {
300     EntityList returnList=null;
301     if (media != null) {
302       String id = media.getId();
303       String select = "select content_id from " + theTable + " where media_id=" + id;
304
305       // execute select statement
306       Connection con=null;Statement stmt=null;
307       try {
308         con = getPooledCon();
309         // should be a preparedStatement because is faster
310         stmt = con.createStatement();
311         ResultSet rs = executeSql(stmt,select);
312         if (rs!=null) {
313           String mediaSelect= "id IN (";
314           boolean first=true;
315           while (rs.next()) {
316             if (first==false) mediaSelect+=",";
317             mediaSelect += rs.getString(1);
318             first=false;
319           }
320           mediaSelect+=")";
321           if (first==false)
322             returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,-1);
323         }
324       }
325       catch (Exception e) {
326         theLog.printDebugInfo("-- get content failed");
327         throw new StorageObjectException("-- get content failed -- "
328         +e.toString());
329       }
330       finally { freeConnection(con,stmt);}
331     }
332     return returnList;
333   }
334
335 /**
336  * Returns a EntityList with all content-objects having a relation to a media
337  */
338
339 public EntityList getContent()
340     throws StorageObjectException {
341     EntityList returnList=null;
342
343     String select = "select distinct content_id from " + theTable;
344     // execute select statement
345     Connection con=null;Statement stmt=null;
346     try {
347       con = getPooledCon();
348       // should be a preparedStatement because is faster
349       stmt = con.createStatement();
350       ResultSet rs = executeSql(stmt,select);
351       if (rs!=null) {
352         String mediaSelect= "id IN (";
353         boolean first=true;
354         while (rs.next()) {
355           if (first==false) mediaSelect+=",";
356           mediaSelect += rs.getString(1);
357           first=false;
358         }
359         mediaSelect+=")";
360         if (first==false)
361           returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,"webdb_lastchange desc");
362       }
363     }
364     catch (Exception e) {
365         theLog.printDebugInfo("-- get content failed");
366         throw new StorageObjectException("-- get content failed -- "
367         +e.toString());
368     }
369     finally { freeConnection(con,stmt);}
370
371     return returnList;
372   }
373
374 }