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(EntityContent content, ArrayList topicId) {
69 if (content == null && topicId == null) {
72 String contentId = content.getId();
73 //first delete all row with content_id=contentId
74 String sql = "delete from "+ theTable +" where content_id=" + contentId;
76 Connection con=null;Statement stmt=null;
79 // should be a preparedStatement because is faster
80 stmt = con.createStatement();
81 ResultSet rs = executeSql(stmt,sql);
82 } catch (Exception e) {
83 theLog.printDebugInfo("-- set topics failed -- delete");
85 freeConnection(con,stmt);
89 //first delete all row with content_id=contentId
91 for (Iterator i = topicId.listIterator(); i.hasNext();) {
92 sql = "insert into "+ theTable +" (content_id,topic_id) values ("
93 + contentId + "," + i.next().toString() + ")";
96 // should be a preparedStatement because is faster
97 stmt = con.createStatement();
98 ResultSet rs = executeSql(stmt,sql);
99 } catch (Exception e) {
100 theLog.printDebugInfo("-- set topics failed -- insert");
102 freeConnection(con,stmt);
108 public void setTopics(String contentId, String topicId) {
109 if (contentId == null && topicId == null) {
112 //first delete all row with content_id=contentId
113 String sql = "delete from "+ theTable +" where content_id=" + contentId;
115 Connection con=null;Statement stmt=null;
117 con = getPooledCon();
118 // should be a preparedStatement because is faster
119 stmt = con.createStatement();
120 int rs = executeUpdate(stmt,sql);
121 } catch (Exception e) {
122 theLog.printDebugInfo("-- set topics failed -- delete");
124 freeConnection(con,stmt);
128 //first delete all row with content_id=contentId
130 sql = "insert into "+ theTable +" (content_id,topic_id) values ("
131 + contentId + "," + topicId + ")";
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 topics failed -- insert");
140 freeConnection(con,stmt);
144 public void deleteByContentId(String contentId) {
145 if (contentId == null) {
146 //theLog.printDebugInfo("-- delete topics failed -- no content id");
149 //delete all row with content_id=contentId
150 String sql = "delete from "+ theTable +" where content_id=" + contentId;
152 Connection con=null;Statement stmt=null;
154 con = getPooledCon();
155 // should be a preparedStatement because is faster
156 stmt = con.createStatement();
157 ResultSet rs = executeSql(stmt,sql);
158 } catch (Exception e) {
159 //theLog.printDebugInfo("-- delete topics failed ");
161 freeConnection(con,stmt);
165 public void deleteByTopicId(String topicId) {
166 if (topicId == null) {
167 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
170 //delete all row with content_id=contentId
171 String sql = "delete from "+ theTable +" where topic_id=" + topicId;
173 Connection con=null;Statement stmt=null;
175 con = getPooledCon();
176 // should be a preparedStatement because is faster
177 stmt = con.createStatement();
178 ResultSet rs = executeSql(stmt,sql);
179 } catch (Exception e) {
180 theLog.printDebugInfo("-- delete topics failed ");
182 freeConnection(con,stmt);
187 public EntityList getContent(EntityTopics topic) {
188 EntityList returnList=null;
190 String id = topic.getId();
191 String select = "select content_id from " + theTable + " where topic_id=" + id;
193 // execute select statement
194 Connection con=null;Statement stmt=null;
196 con = getPooledCon();
197 // should be a preparedStatement because is faster
198 stmt = con.createStatement();
199 ResultSet rs = executeSql(stmt,select);
201 String topicSelect= "id IN (";
204 if (first==false) topicSelect+=",";
205 topicSelect += rs.getString(1);
210 returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1);
213 catch (Exception e) {theLog.printDebugInfo("-- get contetn failed");}
214 finally { freeConnection(con,stmt);}