d00fa861973ee497bdbc751134c49f9b7df90f9b
[mir.git] / source / mircoders / storage / DatabaseContentToTopics.java
1 package mircoders.storage;
2
3 import java.lang.*;
4 import java.sql.*;
5 import java.io.*;
6 import java.util.*;
7
8 import freemarker.template.*;
9
10 import mir.storage.*;
11 import mir.entity.*;
12 import mir.misc.*;
13
14 import mircoders.entity.*;
15
16 /**
17  * <b>This class implements the 1-n-relation between
18  * content and topic
19  *
20  */
21
22 public class DatabaseContentToTopics extends Database implements StorageObject{
23
24   private static DatabaseContentToTopics instance;
25
26   public static DatabaseContentToTopics getInstance()
27     throws StorageObjectException {
28     if (instance == null) {
29       instance = new DatabaseContentToTopics();
30       instance.myselfDatabase = instance;
31     }
32     return instance;
33   }
34
35   private DatabaseContentToTopics()
36     throws StorageObjectException {
37
38     super();
39     this.hasTimestamp = false;
40     this.theTable="content_x_topic";
41   }
42
43   /**
44    * This class return an EntityList of Topics
45    * @param EntityContent content
46    * @returns EntityList
47    */
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+")";
54
55       try {
56         returnList = DatabaseTopics.getInstance().selectByWhereClause(subselect,-1);
57       } catch (Exception e) {
58         theLog.printDebugInfo("-- get topics failed " + e.toString());
59       }
60     }
61     return returnList;
62   }
63   
64   /**
65    * Returns a ArrayList of Integer-Objects from a content-id.
66    * @returns ArrayList
67    */
68   public ArrayList getTopicsOfContent(String contentId) {
69     ArrayList returnList = new ArrayList();
70     if (contentId != null) {
71       String sql = "select topic_id from " + theTable + " where content_id=" + contentId;
72       Connection con=null;Statement stmt=null;
73       try {
74         con = getPooledCon();
75         // should be a preparedStatement because is faster
76         stmt = con.createStatement();
77         ResultSet rs = executeSql(stmt,sql);
78         if(rs!=null){
79           while(rs.next()){
80             returnList.add(new Integer(rs.getInt("topic_id")));
81           }
82         }
83       } catch (Exception e) {
84         theLog.printError(e.toString());
85         theLog.printError("-- get topicsofcontent failed");
86       } finally {
87         freeConnection(con,stmt);
88       }
89     }
90     return returnList;
91   }
92
93   /**
94    * Set new topics
95    */
96   public void setTopics(String contentId, String[] topicId) {
97     if (contentId == null){
98       return;
99     }
100     if (topicId==null || topicId[0]==null) {
101       return;
102     }
103     //first check which topics this article has
104     ArrayList hasTopics = getTopicsOfContent(contentId);
105     ArrayList toSet = new ArrayList();
106     ArrayList toDelete = new ArrayList();
107     
108     if(hasTopics!=null && hasTopics.size()>0){
109       //now we check if there are new topics and copy them to an array.
110       for(int i = 0; i< topicId.length;i++){
111         boolean set=false;
112         int whichTopic = 0;
113         for(Iterator it=hasTopics.iterator();it.hasNext();){
114           Integer topic = (Integer)it.next();
115           if(topicId[i].equals(topic.toString())){
116             set=true;
117           } else {
118             whichTopic = i;
119           }
120         }
121         if(set==false){
122           toSet.add(topicId[i]);
123           theLog.printDebugInfo("to set: "+ topicId[i]);
124         }
125       }
126       //now we check if we have to delete topics
127       for(Iterator it=hasTopics.iterator();it.hasNext();){
128         boolean delete=true;
129         int whichTopic = 0;
130         Integer topic = (Integer)it.next();
131         for(int i = 0; i< topicId.length;i++){
132           if(topicId[i].equals(topic.toString())){
133             delete=false;
134           } else {
135             whichTopic = i;
136           }
137         }
138         if(delete==true){
139           toDelete.add(topic.toString());
140           theLog.printDebugInfo("to delete: "+ topic.toString());
141         }
142       }
143     } else {
144       //all the topics has to be set, so we copy all to the array
145       toSet=(ArrayList)Arrays.asList(topicId);
146     }
147     
148     //first delete all row with content_id=contentId
149     String sql = "delete from "+ theTable +" where content_id=" + contentId
150                 + " and topic_id in (";
151     boolean first=false;
152     for(Iterator it = toDelete.iterator(); it.hasNext();){
153       if(first==false){
154         first=true;
155       } else {
156         sql+=",";
157       }
158       sql+= (String)it.next();
159     }
160     sql+=")";
161     Connection con=null;Statement stmt=null;
162     try {
163       con = getPooledCon();
164       // should be a preparedStatement because is faster
165       stmt = con.createStatement();
166       int rs = executeUpdate(stmt,sql);
167     } catch (Exception e) {
168       theLog.printDebugInfo("-- deleting topics failed");
169     } finally {
170       freeConnection(con,stmt);
171     }
172
173     //now insert
174     //first delete all row with content_id=contentId
175     for (Iterator it = toSet.iterator(); it.hasNext();) {
176       sql = "insert into "+ theTable +" (content_id,topic_id) values ("
177             + contentId + "," + (String)it.next() + ")";
178       try {
179         con = getPooledCon();
180         // should be a preparedStatement because is faster
181         stmt = con.createStatement();
182         int rs = executeUpdate(stmt,sql);
183       } catch (Exception e) {
184         theLog.printDebugInfo("-- set topics failed -- insert laenge topicId" + topicId.length);
185       } finally {
186         freeConnection(con,stmt);
187       }
188     }
189   }
190
191   public void deleteByContentId(String contentId) {
192     if (contentId == null) {
193       //theLog.printDebugInfo("-- delete topics failed -- no content id");
194       return;
195     }
196     //delete all row with content_id=contentId
197     String sql = "delete from "+ theTable +" where content_id=" + contentId;
198
199     Connection con=null;Statement stmt=null;
200     try {
201       con = getPooledCon();
202       // should be a preparedStatement because is faster
203       stmt = con.createStatement();
204       ResultSet rs = executeSql(stmt,sql);
205     } catch (Exception e) {
206       //theLog.printDebugInfo("-- delete topics failed  ");
207     } finally {
208       freeConnection(con,stmt);
209     }
210   }
211
212   public void deleteByTopicId(String topicId) {
213     if (topicId == null) {
214       //theLog.printDebugInfo("-- delete topics failed -- no topic id");
215       return;
216     }
217     //delete all row with content_id=contentId
218     String sql = "delete from "+ theTable +" where topic_id=" + topicId;
219
220     Connection con=null;Statement stmt=null;
221     try {
222       con = getPooledCon();
223       // should be a preparedStatement because is faster
224       stmt = con.createStatement();
225       ResultSet rs = executeSql(stmt,sql);
226     } catch (Exception e) {
227       theLog.printDebugInfo("-- delete topics failed ");
228     } finally {
229       freeConnection(con,stmt);
230     }
231   }
232
233
234   public EntityList getContent(EntityTopics topic) {
235     EntityList returnList=null;
236     if (topic != null) {
237       String id = topic.getId();
238       String select = "select content_id from " + theTable + " where topic_id=" + id;
239
240       // execute select statement
241       Connection con=null;Statement stmt=null;
242       try {
243         con = getPooledCon();
244         // should be a preparedStatement because is faster
245         stmt = con.createStatement();
246         ResultSet rs = executeSql(stmt,select);
247         if (rs!=null) {
248           String topicSelect= "id IN (";
249           boolean first=true;
250           while (rs.next()) {
251             if (first==false) topicSelect+=",";
252             topicSelect += rs.getString(1);
253             first=false;
254           }
255           topicSelect+=")";
256           if (first==false)
257             returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1);
258         }
259       }
260       catch (Exception e) {theLog.printDebugInfo("-- get contetn failed");}
261       finally { freeConnection(con,stmt);}
262     }
263     return returnList;
264   }
265 }