first cut of merge of STABLE-pre1_0 into HEAD. I won't even guarantee that it
[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     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;
74       try {
75         con = getPooledCon();
76         // should be a preparedStatement because is faster
77         stmt = con.createStatement();
78         ResultSet rs = executeSql(stmt,sql);
79         if(rs!=null){
80           while(rs.next()){
81             returnList.add(new Integer(rs.getInt("topic_id")));
82           }
83         }
84       } catch (Exception e) {
85         theLog.printError(e.toString());
86         theLog.printError("-- get topicsofcontent failed");
87       } finally {
88         freeConnection(con,stmt);
89       }
90     }
91     return returnList;
92   }
93
94   /**
95    * Set new topics
96    */
97   public void setTopics(String contentId, String[] topicId)
98     throws StorageObjectException {
99     if (contentId == null){
100       return;
101     }
102     if (topicId==null || topicId[0]==null) {
103       return;
104     }
105     //first check which topics this article has
106     Collection hasTopics = getTopicsOfContent(contentId);
107     Collection toSet = new ArrayList();
108     Collection toDelete = new ArrayList();
109     
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++){
113         boolean set=false;
114         int whichTopic = 0;
115         for(Iterator it=hasTopics.iterator();it.hasNext();){
116           Integer topic = (Integer)it.next();
117           if(topicId[i].equals(topic.toString())){
118             set=true;
119           } else {
120             whichTopic = i;
121           }
122         }
123         if(set==false){
124           toSet.add(topicId[i]);
125           theLog.printDebugInfo("to set: "+ topicId[i]);
126         }
127       }
128       //now we check if we have to delete topics
129       for(Iterator it=hasTopics.iterator();it.hasNext();){
130         boolean delete=true;
131         int whichTopic = 0;
132         Integer topic = (Integer)it.next();
133         for(int i = 0; i< topicId.length;i++){
134           if(topicId[i].equals(topic.toString())){
135             delete=false;
136           } else {
137             whichTopic = i;
138           }
139         }
140         if(delete==true){
141           toDelete.add(topic.toString());
142           theLog.printDebugInfo("to delete: "+ topic.toString());
143         }
144       }
145     } else {
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]);
149                         }
150     }
151     
152     //first delete all row with content_id=contentId
153     String sql = "delete from "+ theTable +" where content_id=" + contentId
154                 + " and topic_id in (";
155     boolean first=false;
156     for(Iterator it = toDelete.iterator(); it.hasNext();){
157       if(first==false){
158         first=true;
159       } else {
160         sql+=",";
161       }
162       sql+= (String)it.next();
163     }
164     sql+=")";
165     Connection con=null;Statement stmt=null;
166     try {
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");
173     } finally {
174       freeConnection(con,stmt);
175     }
176
177     //now insert
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() + ")";
182       try {
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);
189       } finally {
190         freeConnection(con,stmt);
191       }
192     }
193   }
194
195   public void deleteByContentId(String contentId)
196     throws StorageObjectException {
197     if (contentId == null) {
198       //theLog.printDebugInfo("-- delete topics failed -- no content id");
199       return;
200     }
201     //delete all row with content_id=contentId
202     String sql = "delete from "+ theTable +" where content_id=" + contentId;
203
204     Connection con=null;Statement stmt=null;
205     try {
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  ");
212     } finally {
213       freeConnection(con,stmt);
214     }
215   }
216
217   public void deleteByTopicId(String topicId)
218     throws StorageObjectException {
219     if (topicId == null) {
220       //theLog.printDebugInfo("-- delete topics failed -- no topic id");
221       return;
222     }
223     //delete all row with content_id=contentId
224     String sql = "delete from "+ theTable +" where topic_id=" + topicId;
225
226     Connection con=null;Statement stmt=null;
227     try {
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 ");
234     } finally {
235       freeConnection(con,stmt);
236     }
237   }
238
239
240   public EntityList getContent(EntityTopics topic)
241     throws StorageObjectException {
242     EntityList returnList=null;
243     if (topic != null) {
244       String id = topic.getId();
245       String select = "select content_id from " + theTable + " where topic_id=" + id;
246
247       // execute select statement
248       Connection con=null;Statement stmt=null;
249       try {
250         con = getPooledCon();
251         // should be a preparedStatement because is faster
252         stmt = con.createStatement();
253         ResultSet rs = executeSql(stmt,select);
254         if (rs!=null) {
255           String topicSelect= "id IN (";
256           boolean first=true;
257           while (rs.next()) {
258             if (first==false) topicSelect+=",";
259             topicSelect += rs.getString(1);
260             first=false;
261           }
262           topicSelect+=")";
263           if (first==false)
264             returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1);
265         }
266       }
267       catch (Exception e) {theLog.printDebugInfo("-- get contetn failed");}
268       finally { freeConnection(con,stmt);}
269     }
270     return returnList;
271   }
272 }