1 package mircoders.storage;
8 import freemarker.template.*;
14 import mircoders.entity.*;
17 * <b>Diese Klasse implementiert die Datenbankverbindung zur MetaObjekt-Tabelle
22 public class DatabaseContentToMedia extends Database implements StorageObject{
24 private static DatabaseContentToMedia instance;
26 public static DatabaseContentToMedia getInstance()
27 throws StorageObjectException {
28 if (instance == null) {
29 instance = new DatabaseContentToMedia();
30 instance.myselfDatabase = instance;
35 private DatabaseContentToMedia()
36 throws StorageObjectException {
39 this.hasTimestamp = false;
40 this.theTable="content_x_media";
44 * get all the media-files belonging to a content entity
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+")";
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());
67 * get all the images belonging to a content entity
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+")";
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());
91 * get all the uploaded Media belonging to a content entity
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+")";
104 returnList = DatabaseMedia.getInstance().selectByWhereClause(subselect,"id");
105 } catch (Exception e) {
107 theLog.printDebugInfo("-- get uploadedMedia failed " + e.toString());
108 throw new StorageObjectException("-- get uploadedMedia failed " + e.toString());
115 public void setMedia(String contentId, String[] mediaId)
116 throws StorageObjectException {
117 if (contentId == null){
120 if (mediaId==null || mediaId[0]==null) {
123 //first delete all row with content_id=contentId
124 String sql = "delete from "+ theTable +" where content_id=" + contentId;
126 Connection con=null;Statement stmt=null;
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());
136 freeConnection(con,stmt);
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] + ")";
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());
153 freeConnection(con,stmt);
158 public void addMedia(String contentId, String mediaId)
159 throws StorageObjectException {
160 if (contentId == null && mediaId == null) {
164 Connection con=null;Statement stmt=null;
167 String sql = "insert into "+ theTable +" (content_id,media_id) values ("
168 + contentId + "," + mediaId + ")";
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 "
179 freeConnection(con,stmt);
183 public void setMedia(String contentId, String mediaId)
184 throws StorageObjectException {
185 if (contentId == null && mediaId == null) {
188 //first delete all row with content_id=contentId
189 String sql = "delete from "+ theTable +" where content_id=" + contentId;
191 Connection con=null;Statement stmt=null;
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 "
202 freeConnection(con,stmt);
206 //first delete all row with content_id=contentId
208 sql = "insert into "+ theTable +" (content_id,media_id) values ("
209 + contentId + "," + mediaId + ")";
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 "
220 freeConnection(con,stmt);
224 public void deleteByContentId(String contentId)
225 throws StorageObjectException {
226 if (contentId == null) {
227 //theLog.printDebugInfo("-- delete topics failed -- no content id");
230 //delete all row with content_id=contentId
231 String sql = "delete from "+ theTable +" where content_id=" + contentId;
233 Connection con=null;Statement stmt=null;
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 "
244 freeConnection(con,stmt);
248 public void deleteByMediaId(String mediaId)
249 throws StorageObjectException {
250 if (mediaId == null) {
251 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
254 //delete all row with content_id=contentId
255 String sql = "delete from "+ theTable +" where media_id=" + mediaId;
257 Connection con=null;Statement stmt=null;
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 -- "
269 freeConnection(con,stmt);
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");
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;
282 Connection con=null;Statement stmt=null;
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 -- "
294 freeConnection(con,stmt);
299 public EntityList getContent(EntityMedia media)
300 throws StorageObjectException {
301 EntityList returnList=null;
303 String id = media.getId();
304 String select = "select content_id from " + theTable + " where media_id=" + id;
306 // execute select statement
307 Connection con=null;Statement stmt=null;
309 con = getPooledCon();
310 // should be a preparedStatement because is faster
311 stmt = con.createStatement();
312 ResultSet rs = executeSql(stmt,select);
314 String mediaSelect= "id IN (";
317 if (first==false) mediaSelect+=",";
318 mediaSelect += rs.getString(1);
323 returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,-1);
326 catch (Exception e) {
327 theLog.printDebugInfo("-- get content failed");
328 throw new StorageObjectException("-- get content failed -- "
331 finally { freeConnection(con,stmt);}
337 * Returns a EntityList with all content-objects having a relation to a media
340 public EntityList getContent()
341 throws StorageObjectException {
342 EntityList returnList=null;
344 String select = "select distinct content_id from " + theTable;
345 // execute select statement
346 Connection con=null;Statement stmt=null;
348 con = getPooledCon();
349 // should be a preparedStatement because is faster
350 stmt = con.createStatement();
351 ResultSet rs = executeSql(stmt,select);
353 String mediaSelect= "id IN (";
356 if (first==false) mediaSelect+=",";
357 mediaSelect += rs.getString(1);
362 returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,"webdb_lastchange desc");
365 catch (Exception e) {
366 theLog.printDebugInfo("-- get content failed");
367 throw new StorageObjectException("-- get content failed -- "
370 finally { freeConnection(con,stmt);}