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 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+")";
53 returnList = DatabaseMedia.getInstance().selectByWhereClause(subselect,-1);
54 } catch (Exception e) {
55 theLog.printDebugInfo("-- get media failed " + e.toString());
62 public void setMedia(String contentId, String[] mediaId) {
63 if (contentId == null){
66 if (mediaId==null || mediaId[0]==null) {
69 //first delete all row with content_id=contentId
70 String sql = "delete from "+ theTable +" where content_id=" + contentId;
72 Connection con=null;Statement stmt=null;
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");
81 freeConnection(con,stmt);
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] + ")";
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");
97 freeConnection(con,stmt);
102 public void addMedia(String contentId, String mediaId) {
103 if (contentId == null && mediaId == null) {
107 Connection con=null;Statement stmt=null;
110 String sql = "insert into "+ theTable +" (content_id,media_id) values ("
111 + contentId + "," + mediaId + ")";
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");
120 freeConnection(con,stmt);
124 public void setMedia(String contentId, String mediaId) {
125 if (contentId == null && mediaId == null) {
128 //first delete all row with content_id=contentId
129 String sql = "delete from "+ theTable +" where content_id=" + contentId;
131 Connection con=null;Statement stmt=null;
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");
140 freeConnection(con,stmt);
144 //first delete all row with content_id=contentId
146 sql = "insert into "+ theTable +" (content_id,media_id) values ("
147 + contentId + "," + mediaId + ")";
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");
156 freeConnection(con,stmt);
160 public void deleteByContentId(String contentId) {
161 if (contentId == null) {
162 //theLog.printDebugInfo("-- delete topics failed -- no content id");
165 //delete all row with content_id=contentId
166 String sql = "delete from "+ theTable +" where content_id=" + contentId;
168 Connection con=null;Statement stmt=null;
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 ");
177 freeConnection(con,stmt);
181 public void deleteByMediaId(String mediaId) {
182 if (mediaId == null) {
183 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
186 //delete all row with content_id=contentId
187 String sql = "delete from "+ theTable +" where media_id=" + mediaId;
189 Connection con=null;Statement stmt=null;
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 ");
199 freeConnection(con,stmt);
203 public void delete(String contentId, String mediaId) {
204 if (mediaId == null || contentId==null) {
205 theLog.printDebugInfo("-- delete media failed -- missing parameter");
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;
211 Connection con=null;Statement stmt=null;
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 ");
221 freeConnection(con,stmt);
226 public EntityList getContent(EntityMedia media) {
227 EntityList returnList=null;
229 String id = media.getId();
230 String select = "select content_id from " + theTable + " where media_id=" + id;
232 // execute select statement
233 Connection con=null;Statement stmt=null;
235 con = getPooledCon();
236 // should be a preparedStatement because is faster
237 stmt = con.createStatement();
238 ResultSet rs = executeSql(stmt,select);
240 String mediaSelect= "id IN (";
243 if (first==false) mediaSelect+=",";
244 mediaSelect += rs.getString(1);
249 returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,-1);
252 catch (Exception e) {theLog.printDebugInfo("-- get content failed");}
253 finally { freeConnection(con,stmt);}
259 * Returns a EntityList with all content-objects having a relation to a media
262 public EntityList getContent() {
263 EntityList returnList=null;
265 String select = "select distinct content_id from " + theTable;
266 // execute select statement
267 Connection con=null;Statement stmt=null;
269 con = getPooledCon();
270 // should be a preparedStatement because is faster
271 stmt = con.createStatement();
272 ResultSet rs = executeSql(stmt,select);
274 String mediaSelect= "id IN (";
277 if (first==false) mediaSelect+=",";
278 mediaSelect += rs.getString(1);
283 returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,"webdb_lastchange desc");
286 catch (Exception e) {theLog.printDebugInfo("-- get content failed");}
287 finally { freeConnection(con,stmt);}