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 EntityList returnList=null;
49 if (content != null) {
50 // get all to_topic from media_x_topic
51 String id = content.getId();
52 //this is not supported by mysql
53 String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
56 returnList = DatabaseMedia.getInstance().selectByWhereClause(subselect,-1);
57 } catch (Exception e) {
58 theLog.printDebugInfo("-- get media failed " + e.toString());
65 * get all the images belonging to a content entity
68 public EntityList getImages(EntityContent content) {
69 EntityList returnList=null;
70 if (content != null) {
71 // get all to_topic from media_x_topic
72 String id = content.getId();
73 //this is not supported by mysql
74 String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
77 returnList = DatabaseImages.getInstance().selectByWhereClause(subselect,-1);
78 } catch (Exception e) {
79 theLog.printDebugInfo("-- get images failed " + e.toString());
87 * get all the uploaded Media belonging to a content entity
90 public EntityList getUploadedMedia(EntityContent content) {
91 EntityList returnList=null;
92 if (content != null) {
93 // get all to_topic from media_x_topic
94 String id = content.getId();
95 //this is not supported by mysql
96 String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
99 returnList = DatabaseMedia.getInstance().selectByWhereClause(subselect,-1);
100 } catch (Exception e) {
101 theLog.printDebugInfo("-- get uploadedMedia failed " + e.toString());
108 public void setMedia(String contentId, String[] mediaId)
109 throws StorageObjectException {
110 if (contentId == null){
113 if (mediaId==null || mediaId[0]==null) {
116 //first delete all row with content_id=contentId
117 String sql = "delete from "+ theTable +" where content_id=" + contentId;
119 Connection con=null;Statement stmt=null;
121 con = getPooledCon();
122 // should be a preparedStatement because is faster
123 stmt = con.createStatement();
124 ResultSet rs = executeSql(stmt,sql);
125 } catch (Exception e) {
126 theLog.printDebugInfo("-- set media failed -- delete");
128 freeConnection(con,stmt);
132 //first delete all row with content_id=contentId
133 for (int i=0;i<mediaId.length;i++) {
134 sql = "insert into "+ theTable +" (content_id,media_id) values ("
135 + contentId + "," + mediaId[i] + ")";
137 con = getPooledCon();
138 // should be a preparedStatement because is faster
139 stmt = con.createStatement();
140 int rs = executeUpdate(stmt,sql);
141 } catch (Exception e) {
142 theLog.printDebugInfo("-- set topics failed -- insert");
144 freeConnection(con,stmt);
149 public void addMedia(String contentId, String mediaId)
150 throws StorageObjectException {
151 if (contentId == null && mediaId == null) {
155 Connection con=null;Statement stmt=null;
158 String sql = "insert into "+ theTable +" (content_id,media_id) values ("
159 + contentId + "," + mediaId + ")";
161 con = getPooledCon();
162 // should be a preparedStatement because is faster
163 stmt = con.createStatement();
164 int rs = executeUpdate(stmt,sql);
165 } catch (Exception e) {
166 theLog.printDebugInfo("-- add media failed -- insert");
168 freeConnection(con,stmt);
172 public void setMedia(String contentId, String mediaId)
173 throws StorageObjectException {
174 if (contentId == null && mediaId == null) {
177 //first delete all row with content_id=contentId
178 String sql = "delete from "+ theTable +" where content_id=" + contentId;
180 Connection con=null;Statement stmt=null;
182 con = getPooledCon();
183 // should be a preparedStatement because is faster
184 stmt = con.createStatement();
185 int rs = executeUpdate(stmt,sql);
186 } catch (Exception e) {
187 theLog.printDebugInfo("-- set media failed -- delete");
189 freeConnection(con,stmt);
193 //first delete all row with content_id=contentId
195 sql = "insert into "+ theTable +" (content_id,media_id) values ("
196 + contentId + "," + mediaId + ")";
198 con = getPooledCon();
199 // should be a preparedStatement because is faster
200 stmt = con.createStatement();
201 int rs = executeUpdate(stmt,sql);
202 } catch (Exception e) {
203 theLog.printDebugInfo("-- set media failed -- insert");
205 freeConnection(con,stmt);
209 public void deleteByContentId(String contentId)
210 throws StorageObjectException {
211 if (contentId == null) {
212 //theLog.printDebugInfo("-- delete topics failed -- no content id");
215 //delete all row with content_id=contentId
216 String sql = "delete from "+ theTable +" where content_id=" + contentId;
218 Connection con=null;Statement stmt=null;
220 con = getPooledCon();
221 // should be a preparedStatement because is faster
222 stmt = con.createStatement();
223 int rs = executeUpdate(stmt,sql);
224 } catch (Exception e) {
225 //theLog.printDebugInfo("-- delete topics failed ");
227 freeConnection(con,stmt);
231 public void deleteByMediaId(String mediaId)
232 throws StorageObjectException {
233 if (mediaId == null) {
234 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
237 //delete all row with content_id=contentId
238 String sql = "delete from "+ theTable +" where media_id=" + mediaId;
240 Connection con=null;Statement stmt=null;
242 con = getPooledCon();
243 // should be a preparedStatement because is faster
244 stmt = con.createStatement();
245 int rs = executeUpdate(stmt,sql);
246 theLog.printDebugInfo("-- delete media success ");
247 } catch (Exception e) {
248 theLog.printDebugInfo("-- delete media failed ");
250 freeConnection(con,stmt);
254 public void delete(String contentId, String mediaId)
255 throws StorageObjectException {
256 if (mediaId == null || contentId==null) {
257 theLog.printDebugInfo("-- delete media failed -- missing parameter");
260 //delete all row with content_id=contentId and media_id=mediaId
261 String sql = "delete from "+ theTable +" where media_id=" + mediaId + " and content_id= "+contentId;
263 Connection con=null;Statement stmt=null;
265 con = getPooledCon();
266 // should be a preparedStatement because is faster
267 stmt = con.createStatement();
268 int rs = executeUpdate(stmt,sql);
269 theLog.printDebugInfo("-- delete content_x_media success ");
270 } catch (Exception e) {
271 theLog.printDebugInfo("-- delete content_x_media failed ");
273 freeConnection(con,stmt);
278 public EntityList getContent(EntityMedia media)
279 throws StorageObjectException {
280 EntityList returnList=null;
282 String id = media.getId();
283 String select = "select content_id from " + theTable + " where media_id=" + id;
285 // execute select statement
286 Connection con=null;Statement stmt=null;
288 con = getPooledCon();
289 // should be a preparedStatement because is faster
290 stmt = con.createStatement();
291 ResultSet rs = executeSql(stmt,select);
293 String mediaSelect= "id IN (";
296 if (first==false) mediaSelect+=",";
297 mediaSelect += rs.getString(1);
302 returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,-1);
305 catch (Exception e) {theLog.printDebugInfo("-- get content failed");}
306 finally { freeConnection(con,stmt);}
312 * Returns a EntityList with all content-objects having a relation to a media
315 public EntityList getContent()
316 throws StorageObjectException {
317 EntityList returnList=null;
319 String select = "select distinct content_id from " + theTable;
320 // execute select statement
321 Connection con=null;Statement stmt=null;
323 con = getPooledCon();
324 // should be a preparedStatement because is faster
325 stmt = con.createStatement();
326 ResultSet rs = executeSql(stmt,select);
328 String mediaSelect= "id IN (";
331 if (first==false) mediaSelect+=",";
332 mediaSelect += rs.getString(1);
337 returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,"webdb_lastchange desc");
340 catch (Exception e) {theLog.printDebugInfo("-- get content failed");}
341 finally { freeConnection(con,stmt);}