1-n-content-media, tomcat-session-tracking without cookies, and more
[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   public EntityList getMedia(EntityContent content) {
45     EntityList returnList=null;
46     if (content != null) {
47       // get all to_topic from media_x_topic
48       String id = content.getId();
49       //this is not supported by mysql
50       String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
51
52       try {
53         returnList = DatabaseMedia.getInstance().selectByWhereClause(subselect,-1);
54       } catch (Exception e) {
55         theLog.printDebugInfo("-- get media failed " + e.toString());
56       }
57     }
58     return returnList;
59   }
60
61
62   public void setMedia(String contentId, String[] mediaId) {
63     if (contentId == null){
64       return;
65     }
66     if (mediaId==null || mediaId[0]==null) {
67       return;
68     }
69     //first delete all row with content_id=contentId
70     String sql = "delete from "+ theTable +" where content_id=" + contentId;
71   
72     Connection con=null;Statement stmt=null;
73     try {
74       con = getPooledCon();
75       // should be a preparedStatement because is faster
76       stmt = con.createStatement();
77       ResultSet rs = executeSql(stmt,sql);
78     } catch (Exception e) {
79       theLog.printDebugInfo("-- set media failed -- delete");
80     } finally {
81       freeConnection(con,stmt);
82     }
83   
84     //now insert
85     //first delete all row with content_id=contentId
86     for (int i=0;i<mediaId.length;i++) {
87       sql = "insert into "+ theTable +" (content_id,media_id) values ("
88             + contentId + "," + mediaId[i] + ")";
89       try {
90         con = getPooledCon();
91         // should be a preparedStatement because is faster
92         stmt = con.createStatement();
93         int rs = executeUpdate(stmt,sql);
94       } catch (Exception e) {
95         theLog.printDebugInfo("-- set topics failed -- insert");
96       } finally {
97         freeConnection(con,stmt);
98       }
99     }
100   }
101
102   public void addMedia(String contentId, String mediaId) {
103     if (contentId == null && mediaId == null) {
104       return;
105     }
106     
107     Connection con=null;Statement stmt=null;
108     //now insert
109     
110     String sql = "insert into "+ theTable +" (content_id,media_id) values ("
111           + contentId + "," + mediaId + ")";
112     try {
113       con = getPooledCon();
114       // should be a preparedStatement because is faster
115       stmt = con.createStatement();
116       int rs = executeUpdate(stmt,sql);
117     } catch (Exception e) {
118       theLog.printDebugInfo("-- add media failed -- insert");
119     } finally {
120       freeConnection(con,stmt);
121     }
122   }
123         
124   public void setMedia(String contentId, String mediaId) {
125     if (contentId == null && mediaId == null) {
126       return;
127     }
128     //first delete all row with content_id=contentId
129     String sql = "delete from "+ theTable +" where content_id=" + contentId;
130
131     Connection con=null;Statement stmt=null;
132     try {
133       con = getPooledCon();
134       // should be a preparedStatement because is faster
135       stmt = con.createStatement();
136       int rs = executeUpdate(stmt,sql);
137     } catch (Exception e) {
138       theLog.printDebugInfo("-- set media failed -- delete");
139     } finally {
140       freeConnection(con,stmt);
141     }
142
143     //now insert
144     //first delete all row with content_id=contentId
145
146     sql = "insert into "+ theTable +" (content_id,media_id) values ("
147           + contentId + "," + mediaId + ")";
148     try {
149       con = getPooledCon();
150       // should be a preparedStatement because is faster
151       stmt = con.createStatement();
152       int rs = executeUpdate(stmt,sql);
153     } catch (Exception e) {
154       theLog.printDebugInfo("-- set media failed -- insert");
155     } finally {
156       freeConnection(con,stmt);
157     }
158   }
159
160   public void deleteByContentId(String contentId) {
161     if (contentId == null) {
162       //theLog.printDebugInfo("-- delete topics failed -- no content id");
163       return;
164     }
165     //delete all row with content_id=contentId
166     String sql = "delete from "+ theTable +" where content_id=" + contentId;
167
168     Connection con=null;Statement stmt=null;
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("-- delete topics failed  ");
176     } finally {
177       freeConnection(con,stmt);
178     }
179   }
180
181   public void deleteByMediaId(String mediaId) {
182     if (mediaId == null) {
183       //theLog.printDebugInfo("-- delete topics failed -- no topic id");
184       return;
185     }
186     //delete all row with content_id=contentId
187     String sql = "delete from "+ theTable +" where media_id=" + mediaId;
188
189     Connection con=null;Statement stmt=null;
190     try {
191       con = getPooledCon();
192       // should be a preparedStatement because is faster
193       stmt = con.createStatement();
194       int rs = executeUpdate(stmt,sql);
195       theLog.printDebugInfo("-- delete media success ");
196     } catch (Exception e) {
197       theLog.printDebugInfo("-- delete media failed ");
198     } finally {
199       freeConnection(con,stmt);
200     }
201   }
202         
203         public void delete(String contentId, String mediaId) {
204     if (mediaId == null || contentId==null) {
205       theLog.printDebugInfo("-- delete media failed -- missing parameter");
206       return;
207     }
208     //delete all row with content_id=contentId and media_id=mediaId
209     String sql = "delete from "+ theTable +" where media_id=" + mediaId + " and content_id= "+contentId;
210
211     Connection con=null;Statement stmt=null;
212     try {
213       con = getPooledCon();
214       // should be a preparedStatement because is faster
215       stmt = con.createStatement();
216       int rs = executeUpdate(stmt,sql);
217       theLog.printDebugInfo("-- delete content_x_media success ");
218     } catch (Exception e) {
219       theLog.printDebugInfo("-- delete content_x_media failed ");
220     } finally {
221       freeConnection(con,stmt);
222     }
223   }
224
225
226   public EntityList getContent(EntityMedia media) {
227     EntityList returnList=null;
228     if (media != null) {
229       String id = media.getId();
230       String select = "select content_id from " + theTable + " where media_id=" + id;
231
232       // execute select statement
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         ResultSet rs = executeSql(stmt,select);
239         if (rs!=null) {
240           String mediaSelect= "id IN (";
241           boolean first=true;
242           while (rs.next()) {
243             if (first==false) mediaSelect+=",";
244             mediaSelect += rs.getString(1);
245             first=false;
246           }
247           mediaSelect+=")";
248           if (first==false)
249             returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,-1);
250         }
251       }
252       catch (Exception e) {theLog.printDebugInfo("-- get content failed");}
253       finally { freeConnection(con,stmt);}
254     }
255     return returnList;
256   }
257         
258 /**
259  * Returns a EntityList with all content-objects having a relation to a media
260  */
261         
262 public EntityList getContent() {
263     EntityList returnList=null;
264     
265     String select = "select distinct content_id from " + theTable;
266                 // execute select statement
267                 Connection con=null;Statement stmt=null;
268                 try {
269                         con = getPooledCon();
270                         // should be a preparedStatement because is faster
271                         stmt = con.createStatement();
272                         ResultSet rs = executeSql(stmt,select);
273                         if (rs!=null) {
274                                 String mediaSelect= "id IN (";
275                                 boolean first=true;
276                                 while (rs.next()) {
277                                         if (first==false) mediaSelect+=",";
278                                         mediaSelect += rs.getString(1);
279                                         first=false;
280                                 }
281                                 mediaSelect+=")";
282                                 if (first==false)
283                                         returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,"webdb_lastchange desc");
284                         }
285                 }
286                 catch (Exception e) {theLog.printDebugInfo("-- get content failed");}
287                 finally { freeConnection(con,stmt);}
288
289                 return returnList;
290   }
291
292 }