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 ResultSet rs = executeSql(stmt,sql);
94 } catch (Exception e) {
95 theLog.printDebugInfo("-- set topics failed -- insert");
97 freeConnection(con,stmt);
103 public void setMedia(String contentId, String mediaId) {
104 if (contentId == null && mediaId == null) {
107 //first delete all row with content_id=contentId
108 String sql = "delete from "+ theTable +" where content_id=" + contentId;
110 Connection con=null;Statement stmt=null;
112 con = getPooledCon();
113 // should be a preparedStatement because is faster
114 stmt = con.createStatement();
115 int rs = executeUpdate(stmt,sql);
116 } catch (Exception e) {
117 theLog.printDebugInfo("-- set media failed -- delete");
119 freeConnection(con,stmt);
123 //first delete all row with content_id=contentId
125 sql = "insert into "+ theTable +" (content_id,media_id) values ("
126 + contentId + "," + mediaId + ")";
128 con = getPooledCon();
129 // should be a preparedStatement because is faster
130 stmt = con.createStatement();
131 int rs = executeUpdate(stmt,sql);
132 } catch (Exception e) {
133 theLog.printDebugInfo("-- set media failed -- insert");
135 freeConnection(con,stmt);
139 public void deleteByContentId(String contentId) {
140 if (contentId == null) {
141 //theLog.printDebugInfo("-- delete topics failed -- no content id");
144 //delete all row with content_id=contentId
145 String sql = "delete from "+ theTable +" where content_id=" + contentId;
147 Connection con=null;Statement stmt=null;
149 con = getPooledCon();
150 // should be a preparedStatement because is faster
151 stmt = con.createStatement();
152 ResultSet rs = executeSql(stmt,sql);
153 } catch (Exception e) {
154 //theLog.printDebugInfo("-- delete topics failed ");
156 freeConnection(con,stmt);
160 public void deleteByMediaId(String mediaId) {
161 if (mediaId == null) {
162 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
165 //delete all row with content_id=contentId
166 String sql = "delete from "+ theTable +" where topic_id=" + mediaId;
168 Connection con=null;Statement stmt=null;
170 con = getPooledCon();
171 // should be a preparedStatement because is faster
172 stmt = con.createStatement();
173 ResultSet rs = executeSql(stmt,sql);
174 theLog.printDebugInfo("-- delete media success ");
175 } catch (Exception e) {
176 theLog.printDebugInfo("-- delete media failed ");
178 freeConnection(con,stmt);
183 public EntityList getContent(EntityMedia media) {
184 EntityList returnList=null;
186 String id = media.getId();
187 String select = "select content_id from " + theTable + " where media_id=" + id;
189 // execute select statement
190 Connection con=null;Statement stmt=null;
192 con = getPooledCon();
193 // should be a preparedStatement because is faster
194 stmt = con.createStatement();
195 ResultSet rs = executeSql(stmt,select);
197 String mediaSelect= "id IN (";
200 if (first==false) mediaSelect+=",";
201 mediaSelect += rs.getString(1);
206 returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,-1);
209 catch (Exception e) {theLog.printDebugInfo("-- get content failed");}
210 finally { freeConnection(con,stmt);}