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