1 package mircoders.storage;
8 import freemarker.template.*;
14 import mircoders.entity.*;
17 * <b>This class implements the 1-n-relation between
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";
44 * This class return an EntityList of Topics
45 * @param EntityContent content
48 public EntityList getTopics(EntityContent content) {
49 EntityList returnList=null;
50 if (content != null) {
51 // get all to_topic from content_x_topic
52 String id = content.getId();
53 String subselect = "id in (select topic_id from " + theTable + " where content_id=" + id+")";
56 returnList = DatabaseTopics.getInstance().selectByWhereClause(subselect,-1);
57 } catch (Exception e) {
58 theLog.printDebugInfo("-- get topics failed " + e.toString());
65 * Returns a ArrayList of Integer-Objects from a content-id.
68 public ArrayList getTopicsOfContent(String contentId)
69 throws StorageObjectException {
70 ArrayList returnList = new ArrayList();
71 if (contentId != null) {
72 String sql = "select topic_id from " + theTable + " where content_id=" + contentId;
73 Connection con=null;Statement stmt=null;
76 // should be a preparedStatement because is faster
77 stmt = con.createStatement();
78 ResultSet rs = executeSql(stmt,sql);
81 returnList.add(new Integer(rs.getInt("topic_id")));
84 } catch (Exception e) {
85 theLog.printError(e.toString());
86 theLog.printError("-- get topicsofcontent failed");
88 freeConnection(con,stmt);
97 public void setTopics(String contentId, String[] topicId)
98 throws StorageObjectException {
99 if (contentId == null){
102 if (topicId==null || topicId[0]==null) {
105 //first check which topics this article has
106 Collection hasTopics = getTopicsOfContent(contentId);
107 Collection toSet = new ArrayList();
108 Collection toDelete = new ArrayList();
110 if(hasTopics!=null && hasTopics.size()>0){
111 //now we check if there are new topics and copy them to an array.
112 for(int i = 0; i< topicId.length;i++){
115 for(Iterator it=hasTopics.iterator();it.hasNext();){
116 Integer topic = (Integer)it.next();
117 if(topicId[i].equals(topic.toString())){
124 toSet.add(topicId[i]);
125 theLog.printDebugInfo("to set: "+ topicId[i]);
128 //now we check if we have to delete topics
129 for(Iterator it=hasTopics.iterator();it.hasNext();){
132 Integer topic = (Integer)it.next();
133 for(int i = 0; i< topicId.length;i++){
134 if(topicId[i].equals(topic.toString())){
141 toDelete.add(topic.toString());
142 theLog.printDebugInfo("to delete: "+ topic.toString());
146 //all the topics has to be set, so we copy all to the array
147 for (int i = 0; i < topicId.length; i++){
148 toSet.add(topicId[i]);
152 //first delete all row with content_id=contentId
153 String sql = "delete from "+ theTable +" where content_id=" + contentId
154 + " and topic_id in (";
156 for(Iterator it = toDelete.iterator(); it.hasNext();){
162 sql+= (String)it.next();
165 Connection con=null;Statement stmt=null;
167 con = getPooledCon();
168 // should be a preparedStatement because is faster
169 stmt = con.createStatement();
170 int rs = executeUpdate(stmt,sql);
171 } catch (Exception e) {
172 theLog.printDebugInfo("-- deleting topics failed");
174 freeConnection(con,stmt);
178 //first delete all row with content_id=contentId
179 for (Iterator it = toSet.iterator(); it.hasNext();) {
180 sql = "insert into "+ theTable +" (content_id,topic_id) values ("
181 + contentId + "," + (String)it.next() + ")";
183 con = getPooledCon();
184 // should be a preparedStatement because is faster
185 stmt = con.createStatement();
186 int rs = executeUpdate(stmt,sql);
187 } catch (Exception e) {
188 theLog.printDebugInfo("-- set topics failed -- insert laenge topicId" + topicId.length);
190 freeConnection(con,stmt);
195 public void deleteByContentId(String contentId)
196 throws StorageObjectException {
197 if (contentId == null) {
198 //theLog.printDebugInfo("-- delete topics failed -- no content id");
201 //delete all row with content_id=contentId
202 String sql = "delete from "+ theTable +" where content_id=" + contentId;
204 Connection con=null;Statement stmt=null;
206 con = getPooledCon();
207 // should be a preparedStatement because is faster
208 stmt = con.createStatement();
209 ResultSet rs = executeSql(stmt,sql);
210 } catch (Exception e) {
211 //theLog.printDebugInfo("-- delete topics failed ");
213 freeConnection(con,stmt);
217 public void deleteByTopicId(String topicId)
218 throws StorageObjectException {
219 if (topicId == null) {
220 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
223 //delete all row with content_id=contentId
224 String sql = "delete from "+ theTable +" where topic_id=" + topicId;
226 Connection con=null;Statement stmt=null;
228 con = getPooledCon();
229 // should be a preparedStatement because is faster
230 stmt = con.createStatement();
231 ResultSet rs = executeSql(stmt,sql);
232 } catch (Exception e) {
233 theLog.printDebugInfo("-- delete topics failed ");
235 freeConnection(con,stmt);
240 public EntityList getContent(EntityTopics topic)
241 throws StorageObjectException {
242 EntityList returnList=null;
244 String id = topic.getId();
245 String select = "select content_id from " + theTable + " where topic_id=" + id;
247 // execute select statement
248 Connection con=null;Statement stmt=null;
250 con = getPooledCon();
251 // should be a preparedStatement because is faster
252 stmt = con.createStatement();
253 ResultSet rs = executeSql(stmt,select);
255 String topicSelect= "id IN (";
258 if (first==false) topicSelect+=",";
259 topicSelect += rs.getString(1);
264 returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1);
267 catch (Exception e) {theLog.printDebugInfo("-- get contetn failed");}
268 finally { freeConnection(con,stmt);}