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