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";
43 this.theEntityClass = Class.forName("mircoders.entity.EntityGruppen");
44 } catch (Exception e) {
45 throw new StorageObjectException(e.toString());
51 public EntityList getTopics(EntityContent content) {
52 EntityList returnList=null;
53 if (content != null) {
54 // get all to_topic from content_x_topic
55 String id = content.getId();
56 String subselect = "id in (select topic_id from " + theTable + " where content_id=" + id+")";
59 returnList = DatabaseTopics.getInstance().selectByWhereClause(subselect,-1);
60 } catch (Exception e) {
61 theLog.printDebugInfo("-- get topics failed " + e.toString());
68 public void setTopics(String contentId, String[] topicId) {
69 if (contentId == null && topicId == null) {
72 //first delete all row with content_id=contentId
73 String sql = "delete from "+ theTable +" where content_id=" + contentId;
75 Connection con=null;Statement stmt=null;
78 // should be a preparedStatement because is faster
79 stmt = con.createStatement();
80 ResultSet rs = executeSql(stmt,sql);
81 } catch (Exception e) {
82 theLog.printDebugInfo("-- set topics failed -- delete");
84 freeConnection(con,stmt);
88 //first delete all row with content_id=contentId
90 for (int i=0;i<topicId.length;i++) {
91 sql = "insert into "+ theTable +" (content_id,topic_id) values ("
92 + contentId + "," + topicId[i] + ")";
95 // should be a preparedStatement because is faster
96 stmt = con.createStatement();
97 ResultSet rs = executeSql(stmt,sql);
98 } catch (Exception e) {
99 theLog.printDebugInfo("-- set topics failed -- insert");
101 freeConnection(con,stmt);
107 public void setTopics(String contentId, String topicId) {
108 if (contentId == null && topicId == null) {
111 //first delete all row with content_id=contentId
112 String sql = "delete from "+ theTable +" where content_id=" + contentId;
114 Connection con=null;Statement stmt=null;
116 con = getPooledCon();
117 // should be a preparedStatement because is faster
118 stmt = con.createStatement();
119 int rs = executeUpdate(stmt,sql);
120 } catch (Exception e) {
121 theLog.printDebugInfo("-- set topics failed -- delete");
123 freeConnection(con,stmt);
127 //first delete all row with content_id=contentId
129 sql = "insert into "+ theTable +" (content_id,topic_id) values ("
130 + contentId + "," + topicId + ")";
132 con = getPooledCon();
133 // should be a preparedStatement because is faster
134 stmt = con.createStatement();
135 int rs = executeUpdate(stmt,sql);
136 } catch (Exception e) {
137 theLog.printDebugInfo("-- set topics failed -- insert");
139 freeConnection(con,stmt);
143 public void deleteByContentId(String contentId) {
144 if (contentId == null) {
145 //theLog.printDebugInfo("-- delete topics failed -- no content id");
148 //delete all row with content_id=contentId
149 String sql = "delete from "+ theTable +" where content_id=" + contentId;
151 Connection con=null;Statement stmt=null;
153 con = getPooledCon();
154 // should be a preparedStatement because is faster
155 stmt = con.createStatement();
156 ResultSet rs = executeSql(stmt,sql);
157 } catch (Exception e) {
158 //theLog.printDebugInfo("-- delete topics failed ");
160 freeConnection(con,stmt);
164 public void deleteByTopicId(String topicId) {
165 if (topicId == null) {
166 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
169 //delete all row with content_id=contentId
170 String sql = "delete from "+ theTable +" where topic_id=" + topicId;
172 Connection con=null;Statement stmt=null;
174 con = getPooledCon();
175 // should be a preparedStatement because is faster
176 stmt = con.createStatement();
177 ResultSet rs = executeSql(stmt,sql);
178 } catch (Exception e) {
179 theLog.printDebugInfo("-- delete topics failed ");
181 freeConnection(con,stmt);
186 public EntityList getContent(EntityTopics topic) {
187 EntityList returnList=null;
189 String id = topic.getId();
190 String select = "select content_id from " + theTable + " where topic_id=" + id;
192 // execute select statement
193 Connection con=null;Statement stmt=null;
195 con = getPooledCon();
196 // should be a preparedStatement because is faster
197 stmt = con.createStatement();
198 ResultSet rs = executeSql(stmt,select);
200 String topicSelect= "id IN (";
203 if (first==false) topicSelect+=",";
204 topicSelect += rs.getString(1);
209 returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1);
212 catch (Exception e) {theLog.printDebugInfo("-- get contetn failed");}
213 finally { freeConnection(con,stmt);}