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) {
106 theLog.printDebugInfo("-- get uploadedMedia failed " + e.toString());
107 throw new StorageObjectException("-- get uploadedMedia failed " + e.toString());
114 public void setMedia(String contentId, String[] mediaId)
115 throws StorageObjectException {
116 if (contentId == null){
119 if (mediaId==null || mediaId[0]==null) {
122 //first delete all row with content_id=contentId
123 String sql = "delete from "+ theTable +" where content_id=" + contentId;
125 Connection con=null;Statement stmt=null;
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());
135 freeConnection(con,stmt);
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] + ")";
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());
152 freeConnection(con,stmt);
157 public void addMedia(String contentId, String mediaId)
158 throws StorageObjectException {
159 if (contentId == null && mediaId == null) {
163 Connection con=null;Statement stmt=null;
166 String sql = "insert into "+ theTable +" (content_id,media_id) values ("
167 + contentId + "," + mediaId + ")";
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 "
178 freeConnection(con,stmt);
182 public void setMedia(String contentId, String mediaId)
183 throws StorageObjectException {
184 if (contentId == null && mediaId == null) {
187 //first delete all row with content_id=contentId
188 String sql = "delete from "+ theTable +" where content_id=" + contentId;
190 Connection con=null;Statement stmt=null;
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 "
201 freeConnection(con,stmt);
205 //first delete all row with content_id=contentId
207 sql = "insert into "+ theTable +" (content_id,media_id) values ("
208 + contentId + "," + mediaId + ")";
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 "
219 freeConnection(con,stmt);
223 public void deleteByContentId(String contentId)
224 throws StorageObjectException {
225 if (contentId == null) {
226 //theLog.printDebugInfo("-- delete topics failed -- no content id");
229 //delete all row with content_id=contentId
230 String sql = "delete from "+ theTable +" where content_id=" + contentId;
232 Connection con=null;Statement stmt=null;
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 "
243 freeConnection(con,stmt);
247 public void deleteByMediaId(String mediaId)
248 throws StorageObjectException {
249 if (mediaId == null) {
250 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
253 //delete all row with content_id=contentId
254 String sql = "delete from "+ theTable +" where media_id=" + mediaId;
256 Connection con=null;Statement stmt=null;
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 -- "
268 freeConnection(con,stmt);
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");
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;
281 Connection con=null;Statement stmt=null;
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 -- "
293 freeConnection(con,stmt);
298 public EntityList getContent(EntityMedia media)
299 throws StorageObjectException {
300 EntityList returnList=null;
302 String id = media.getId();
303 String select = "select content_id from " + theTable + " where media_id=" + id;
305 // execute select statement
306 Connection con=null;Statement stmt=null;
308 con = getPooledCon();
309 // should be a preparedStatement because is faster
310 stmt = con.createStatement();
311 ResultSet rs = executeSql(stmt,select);
313 String mediaSelect= "id IN (";
316 if (first==false) mediaSelect+=",";
317 mediaSelect += rs.getString(1);
322 returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,-1);
325 catch (Exception e) {
326 theLog.printDebugInfo("-- get content failed");
327 throw new StorageObjectException("-- get content failed -- "
330 finally { freeConnection(con,stmt);}
336 * Returns a EntityList with all content-objects having a relation to a media
339 public EntityList getContent()
340 throws StorageObjectException {
341 EntityList returnList=null;
343 String select = "select distinct content_id from " + theTable;
344 // execute select statement
345 Connection con=null;Statement stmt=null;
347 con = getPooledCon();
348 // should be a preparedStatement because is faster
349 stmt = con.createStatement();
350 ResultSet rs = executeSql(stmt,select);
352 String mediaSelect= "id IN (";
355 if (first==false) mediaSelect+=",";
356 mediaSelect += rs.getString(1);
361 returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,"webdb_lastchange desc");
364 catch (Exception e) {
365 theLog.printDebugInfo("-- get content failed");
366 throw new StorageObjectException("-- get content failed -- "
369 finally { freeConnection(con,stmt);}