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 DatabaseContentToTopics extends Database implements StorageObject{
24 private static DatabaseContentToTopics instance;
26 public static DatabaseContentToTopics getInstance()
27 throws StorageObjectException {
28 if (instance == null) {
29 instance = new DatabaseContentToTopics();
30 instance.myselfDatabase = instance;
35 private DatabaseContentToTopics()
36 throws StorageObjectException {
39 this.hasTimestamp = false;
40 this.theTable="content_x_topic";
42 this.theEntityClass = Class.forName("mircoders.entity.EntityGruppen");
43 } catch (Exception e) {
44 throw new StorageObjectException(e.toString());
49 public EntityList getTopics(EntityContent content) {
50 EntityList returnList=null;
51 if (content != null) {
52 // get all to_topic from content_x_topic
53 String id = content.getId();
54 String subselect = "id in (select topic_id from " + theTable + " where content_id=" + id+")";
57 returnList = DatabaseTopics.getInstance().selectByWhereClause(subselect,-1);
58 } catch (Exception e) {
59 theLog.printDebugInfo("-- get topics failed " + e.toString());
66 public void setTopics(EntityContent content, ArrayList topicId) {
67 if (content == null && topicId == null) {
70 String contentId = content.getId();
71 //first delete all row with content_id=contentId
72 String sql = "delete from "+ theTable +" where content_id=" + contentId;
74 Connection con=null;Statement stmt=null;
77 // should be a preparedStatement because is faster
78 stmt = con.createStatement();
79 ResultSet rs = executeSql(stmt,sql);
80 } catch (Exception e) {
81 theLog.printDebugInfo("-- set topics failed -- delete");
83 freeConnection(con,stmt);
87 //first delete all row with content_id=contentId
89 for (Iterator i = topicId.listIterator(); i.hasNext();) {
90 sql = "insert into "+ theTable +" (content_id,topic_id) values ("
91 + contentId + "," + i.next().toString() + ")";
94 // should be a preparedStatement because is faster
95 stmt = con.createStatement();
96 ResultSet rs = executeSql(stmt,sql);
97 } catch (Exception e) {
98 theLog.printDebugInfo("-- set topics failed -- insert");
100 freeConnection(con,stmt);
106 public void setTopics(String contentId, String topicId) {
107 if (contentId == null && topicId == null) {
110 //first delete all row with content_id=contentId
111 String sql = "delete from "+ theTable +" where content_id=" + contentId;
113 Connection con=null;Statement stmt=null;
115 con = getPooledCon();
116 // should be a preparedStatement because is faster
117 stmt = con.createStatement();
118 int rs = executeUpdate(stmt,sql);
119 } catch (Exception e) {
120 theLog.printDebugInfo("-- set topics failed -- delete");
122 freeConnection(con,stmt);
126 //first delete all row with content_id=contentId
128 sql = "insert into "+ theTable +" (content_id,topic_id) values ("
129 + contentId + "," + topicId + ")";
131 con = getPooledCon();
132 // should be a preparedStatement because is faster
133 stmt = con.createStatement();
134 int rs = executeUpdate(stmt,sql);
135 } catch (Exception e) {
136 theLog.printDebugInfo("-- set topics failed -- insert");
138 freeConnection(con,stmt);
142 public void deleteByContentId(String contentId) {
143 if (contentId == null) {
144 //theLog.printDebugInfo("-- delete topics failed -- no content id");
147 //delete all row with content_id=contentId
148 String sql = "delete from "+ theTable +" where content_id=" + contentId;
150 Connection con=null;Statement stmt=null;
152 con = getPooledCon();
153 // should be a preparedStatement because is faster
154 stmt = con.createStatement();
155 ResultSet rs = executeSql(stmt,sql);
156 } catch (Exception e) {
157 //theLog.printDebugInfo("-- delete topics failed ");
159 freeConnection(con,stmt);
163 public void deleteByTopicId(String topicId) {
164 if (topicId == null) {
165 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
168 //delete all row with content_id=contentId
169 String sql = "delete from "+ theTable +" where topic_id=" + topicId;
171 Connection con=null;Statement stmt=null;
173 con = getPooledCon();
174 // should be a preparedStatement because is faster
175 stmt = con.createStatement();
176 ResultSet rs = executeSql(stmt,sql);
177 } catch (Exception e) {
178 theLog.printDebugInfo("-- delete topics failed ");
180 freeConnection(con,stmt);
185 public EntityList getContent(EntityTopics topic) {
186 EntityList returnList=null;
188 String id = topic.getId();
189 String select = "select content_id from " + theTable + " where topic_id=" + id;
191 // execute select statement
192 Connection con=null;Statement stmt=null;
194 con = getPooledCon();
195 // should be a preparedStatement because is faster
196 stmt = con.createStatement();
197 ResultSet rs = executeSql(stmt,select);
199 String topicSelect= "id IN (";
202 if (first==false) topicSelect+=",";
203 topicSelect += rs.getString(1);
208 returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1);
211 catch (Exception e) {theLog.printDebugInfo("-- get contetn failed");}
212 finally { freeConnection(con,stmt);}