code cleaning, new config
[mir.git] / source / mircoders / storage / DatabaseContentToTopics.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package mircoders.storage;
33
34 import java.sql.Connection;
35 import java.sql.ResultSet;
36 import java.sql.Statement;
37 import java.util.ArrayList;
38 import java.util.Collection;
39 import java.util.Iterator;
40
41 import mir.entity.EntityList;
42 import mir.storage.Database;
43 import mir.storage.StorageObject;
44 import mir.storage.StorageObjectFailure;
45 import mircoders.entity.EntityContent;
46 import mircoders.entity.EntityTopics;
47
48 /**
49  * <b>This class implements the 1-n-relation between
50  * content and topic
51  *
52  */
53
54 public class DatabaseContentToTopics extends Database implements StorageObject{
55
56   private static DatabaseContentToTopics instance;
57
58   // the following *has* to be sychronized cause this static method
59   // could get preemted and we could end up with 2 instances of DatabaseFoo.
60   // see the "Singletons with needles and thread" article at JavaWorld -mh
61   public synchronized static DatabaseContentToTopics getInstance()
62     throws StorageObjectFailure {
63     if (instance == null) {
64       instance = new DatabaseContentToTopics();
65       instance.myselfDatabase = instance;
66     }
67     return instance;
68   }
69
70   private DatabaseContentToTopics()
71     throws StorageObjectFailure {
72
73     super();
74     this.hasTimestamp = false;
75     this.theTable="content_x_topic";
76     try { this.theEntityClass = Class.forName("mir.entity.GenericEntity"); }
77     catch (Exception e) { throw new StorageObjectFailure(e); }
78
79   }
80
81   /**
82    * This class return an EntityList of Topics
83    * @param EntityContent content
84    * @returns EntityList
85    */
86   public EntityList getTopics(EntityContent content) {
87     EntityList returnList=null;
88     if (content != null) {
89       // get all to_topic from content_x_topic
90       String id = content.getId();
91       String subselect = "id in (select topic_id from " + theTable + " where content_id=" + id+")";
92
93       try {
94         returnList = DatabaseTopics.getInstance().selectByWhereClause(subselect,-1);
95       } catch (Exception e) {
96         theLog.printDebugInfo("-- get topics failed " + e.toString());
97       }
98     }
99     return returnList;
100   }
101
102   /**
103    * Returns a ArrayList of Integer-Objects from a content-id.
104    * @returns ArrayList
105    */
106   public ArrayList getTopicsOfContent(String contentId)
107     throws StorageObjectFailure {
108     ArrayList returnList = new ArrayList();
109     if (contentId != null) {
110       String sql = "select topic_id from " + theTable + " where content_id=" + contentId;
111       Connection con=null;Statement stmt=null;
112       try {
113         con = getPooledCon();
114         // should be a preparedStatement because is faster
115         stmt = con.createStatement();
116         ResultSet rs = executeSql(stmt,sql);
117         if(rs!=null){
118           while(rs.next()){
119             returnList.add(new Integer(rs.getInt("topic_id")));
120           }
121         }
122       } catch (Exception e) {
123         theLog.printError(e.toString());
124         theLog.printError("-- get topicsofcontent failed");
125       } finally {
126         freeConnection(con,stmt);
127       }
128     }
129     return returnList;
130   }
131
132   /**
133    * Set new topics
134    */
135   public void setTopics(String contentId, String[] topicId)
136     throws StorageObjectFailure {
137     if (contentId == null){
138       return;
139     }
140     if (topicId==null || topicId[0]==null) {
141       return;
142     }
143     //first check which topics this article has
144     Collection hasTopics = getTopicsOfContent(contentId);
145     Collection toSet = new ArrayList();
146     Collection toDelete = new ArrayList();
147
148     if(hasTopics!=null && hasTopics.size()>0){
149       //now we check if there are new topics and copy them to an array.
150       for(int i = 0; i< topicId.length;i++){
151         boolean set=false;
152         int whichTopic = 0;
153         for(Iterator it=hasTopics.iterator();it.hasNext();){
154           Integer topic = (Integer)it.next();
155           if(topicId[i].equals(topic.toString())){
156             set=true;
157           } else {
158             whichTopic = i;
159           }
160         }
161         if(set==false){
162           toSet.add(topicId[i]);
163           theLog.printDebugInfo("to set: "+ topicId[i]);
164         }
165       }
166       //now we check if we have to delete topics
167       for(Iterator it=hasTopics.iterator();it.hasNext();){
168         boolean delete=true;
169         int whichTopic = 0;
170         Integer topic = (Integer)it.next();
171         for(int i = 0; i< topicId.length;i++){
172           if(topicId[i].equals(topic.toString())){
173             delete=false;
174           } else {
175             whichTopic = i;
176           }
177         }
178         if(delete==true){
179           toDelete.add(topic.toString());
180           theLog.printDebugInfo("to delete: "+ topic.toString());
181         }
182       }
183     } else {
184       //all the topics has to be set, so we copy all to the array
185                         for (int i = 0; i < topicId.length; i++){
186                                 toSet.add(topicId[i]);
187                         }
188     }
189
190     //first delete all row with content_id=contentId
191     String sql = "delete from "+ theTable +" where content_id=" + contentId
192                 + " and topic_id in (";
193     boolean first=false;
194     for(Iterator it = toDelete.iterator(); it.hasNext();){
195       if(first==false){
196         first=true;
197       } else {
198         sql+=",";
199       }
200       sql+= (String)it.next();
201     }
202     sql+=")";
203     Connection con=null;Statement stmt=null;
204     try {
205       con = getPooledCon();
206       // should be a preparedStatement because is faster
207       stmt = con.createStatement();
208       int rs = executeUpdate(stmt,sql);
209     } catch (Exception e) {
210       theLog.printDebugInfo("-- deleting topics failed");
211     } finally {
212       freeConnection(con,stmt);
213     }
214
215     //now insert
216     //first delete all row with content_id=contentId
217     for (Iterator it = toSet.iterator(); it.hasNext();) {
218       sql = "insert into "+ theTable +" (content_id,topic_id) values ("
219             + contentId + "," + (String)it.next() + ")";
220       try {
221         con = getPooledCon();
222         // should be a preparedStatement because is faster
223         stmt = con.createStatement();
224         int rs = executeUpdate(stmt,sql);
225       } catch (Exception e) {
226         theLog.printDebugInfo("-- set topics failed -- insert laenge topicId" + topicId.length);
227       } finally {
228         freeConnection(con,stmt);
229       }
230     }
231   }
232
233   public void deleteByContentId(String contentId)
234     throws StorageObjectFailure {
235     if (contentId == null) {
236       //theLog.printDebugInfo("-- delete topics failed -- no content id");
237       return;
238     }
239     //delete all row with content_id=contentId
240     String sql = "delete from "+ theTable +" where content_id=" + contentId;
241
242     Connection con=null;Statement stmt=null;
243     try {
244       con = getPooledCon();
245       // should be a preparedStatement because is faster
246       stmt = con.createStatement();
247       ResultSet rs = executeSql(stmt,sql);
248     } catch (Exception e) {
249       //theLog.printDebugInfo("-- delete topics failed  ");
250     } finally {
251       freeConnection(con,stmt);
252     }
253   }
254
255   public void deleteByTopicId(String topicId)
256     throws StorageObjectFailure {
257     if (topicId == null) {
258       //theLog.printDebugInfo("-- delete topics failed -- no topic id");
259       return;
260     }
261     //delete all row with content_id=contentId
262     String sql = "delete from "+ theTable +" where topic_id=" + topicId;
263
264     Connection con=null;Statement stmt=null;
265     try {
266       con = getPooledCon();
267       // should be a preparedStatement because is faster
268       stmt = con.createStatement();
269       ResultSet rs = executeSql(stmt,sql);
270     } catch (Exception e) {
271       theLog.printDebugInfo("-- delete topics failed ");
272     } finally {
273       freeConnection(con,stmt);
274     }
275   }
276
277
278   public EntityList getContent(EntityTopics topic)
279     throws StorageObjectFailure {
280     EntityList returnList=null;
281     if (topic != null) {
282       String id = topic.getId();
283       String select = "select content_id from " + theTable + " where topic_id=" + id;
284
285       // execute select statement
286       Connection con=null;Statement stmt=null;
287       try {
288         con = getPooledCon();
289         // should be a preparedStatement because is faster
290         stmt = con.createStatement();
291         ResultSet rs = executeSql(stmt,select);
292         if (rs!=null) {
293           String topicSelect= "id IN (";
294           boolean first=true;
295           while (rs.next()) {
296             if (first==false) topicSelect+=",";
297             topicSelect += rs.getString(1);
298             first=false;
299           }
300           topicSelect+=")";
301           if (first==false)
302             returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1);
303         }
304       }
305       catch (Exception e) {theLog.printDebugInfo("-- get contetn failed");}
306       finally { freeConnection(con,stmt);}
307     }
308     return returnList;
309   }
310 }