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