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){
72 if (topicId==null || topicId[0]==null) {
75 //first delete all row with content_id=contentId
76 String sql = "delete from "+ theTable +" where content_id=" + contentId;
78 Connection con=null;Statement stmt=null;
81 // should be a preparedStatement because is faster
82 stmt = con.createStatement();
83 ResultSet rs = executeSql(stmt,sql);
84 } catch (Exception e) {
85 theLog.printDebugInfo("-- set topics failed -- delete");
87 freeConnection(con,stmt);
91 //first delete all row with content_id=contentId
92 for (int i=0;i<topicId.length;i++) {
93 sql = "insert into "+ theTable +" (content_id,topic_id) values ("
94 + contentId + "," + topicId[i] + ")";
97 // should be a preparedStatement because is faster
98 stmt = con.createStatement();
99 ResultSet rs = executeSql(stmt,sql);
100 } catch (Exception e) {
101 theLog.printDebugInfo("-- set topics failed -- insert laenge topicId" + topicId.length);
103 freeConnection(con,stmt);
109 public void setTopics(String contentId, String topicId) {
110 if (contentId == null && topicId == null) {
113 //first delete all row with content_id=contentId
114 String sql = "delete from "+ theTable +" where content_id=" + contentId;
116 Connection con=null;Statement stmt=null;
118 con = getPooledCon();
119 // should be a preparedStatement because is faster
120 stmt = con.createStatement();
121 int rs = executeUpdate(stmt,sql);
122 } catch (Exception e) {
123 theLog.printDebugInfo("-- set topics failed -- delete");
125 freeConnection(con,stmt);
129 //first delete all row with content_id=contentId
131 sql = "insert into "+ theTable +" (content_id,topic_id) values ("
132 + contentId + "," + topicId + ")";
134 con = getPooledCon();
135 // should be a preparedStatement because is faster
136 stmt = con.createStatement();
137 int rs = executeUpdate(stmt,sql);
138 } catch (Exception e) {
139 theLog.printDebugInfo("-- set topics failed -- insert");
141 freeConnection(con,stmt);
145 public void deleteByContentId(String contentId) {
146 if (contentId == null) {
147 //theLog.printDebugInfo("-- delete topics failed -- no content id");
150 //delete all row with content_id=contentId
151 String sql = "delete from "+ theTable +" where content_id=" + contentId;
153 Connection con=null;Statement stmt=null;
155 con = getPooledCon();
156 // should be a preparedStatement because is faster
157 stmt = con.createStatement();
158 ResultSet rs = executeSql(stmt,sql);
159 } catch (Exception e) {
160 //theLog.printDebugInfo("-- delete topics failed ");
162 freeConnection(con,stmt);
166 public void deleteByTopicId(String topicId) {
167 if (topicId == null) {
168 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
171 //delete all row with content_id=contentId
172 String sql = "delete from "+ theTable +" where topic_id=" + topicId;
174 Connection con=null;Statement stmt=null;
176 con = getPooledCon();
177 // should be a preparedStatement because is faster
178 stmt = con.createStatement();
179 ResultSet rs = executeSql(stmt,sql);
180 } catch (Exception e) {
181 theLog.printDebugInfo("-- delete topics failed ");
183 freeConnection(con,stmt);
188 public EntityList getContent(EntityTopics topic) {
189 EntityList returnList=null;
191 String id = topic.getId();
192 String select = "select content_id from " + theTable + " where topic_id=" + id;
194 // execute select statement
195 Connection con=null;Statement stmt=null;
197 con = getPooledCon();
198 // should be a preparedStatement because is faster
199 stmt = con.createStatement();
200 ResultSet rs = executeSql(stmt,select);
202 String topicSelect= "id IN (";
205 if (first==false) topicSelect+=",";
206 topicSelect += rs.getString(1);
211 returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1);
214 catch (Exception e) {theLog.printDebugInfo("-- get contetn failed");}
215 finally { freeConnection(con,stmt);}