Organizing import, refresh the license (zapata deleted cos.jar)
[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  any library licensed under the Apache Software License, 
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library 
23  * (or with modified versions of the above that use the same license as the above), 
24  * and distribute linked combinations including the two.  You must obey the 
25  * GNU General Public License in all respects for all of the code used other than 
26  * the above mentioned libraries.  If you modify this file, you may extend this 
27  * exception to your version of the file, but you are not obligated to do so.  
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30
31 package mircoders.storage;
32
33 import java.sql.Connection;
34 import java.sql.ResultSet;
35 import java.sql.Statement;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.Iterator;
39 import java.util.List;
40 import java.util.Vector;
41
42 import mir.entity.EntityList;
43 import mir.log.LoggerWrapper;
44 import mir.storage.Database;
45 import mir.storage.StorageObject;
46 import mir.storage.StorageObjectFailure;
47 import mircoders.entity.EntityContent;
48 import mircoders.entity.EntityTopics;
49
50 /**
51  * <b>This class implements the 1-n-relation between
52  * content and topic
53  *
54  */
55
56 public class DatabaseContentToTopics extends Database implements StorageObject{
57
58   private static DatabaseContentToTopics instance;
59
60   public static DatabaseContentToTopics getInstance() {
61     if (instance == null) {
62       synchronized (DatabaseContentToTopics.class) {
63         if (instance == null) {
64           instance = new DatabaseContentToTopics();
65           instance.myselfDatabase = instance;
66         }
67       }
68     }
69     return instance;
70   }
71
72   private DatabaseContentToTopics() {
73     super();
74
75     logger = new LoggerWrapper("Database.ContentToTopics");
76
77     hasTimestamp = false;
78     theTable="content_x_topic";
79     theEntityClass = mir.entity.GenericEntity.class;
80   }
81
82   /**
83    * This class return an EntityList of Topics
84    * @param EntityContent content
85    * @returns EntityList
86    */
87   public EntityList getTopics(EntityContent content) {
88     EntityList returnList=null;
89     if (content != null) {
90       // get all to_topic from content_x_topic
91       String id = content.getId();
92       String subselect = "id in (select topic_id from " + theTable + " where content_id=" + id+")";
93
94       try {
95         returnList = DatabaseTopics.getInstance().selectByWhereClause(subselect,-1);
96       }
97       catch (Exception e) {
98         logger.error("-- get topics failed " + e.toString());
99       }
100     }
101     return returnList;
102   }
103
104   /**
105    * Returns a ArrayList of Integer-Objects from a content-id.
106    * @returns ArrayList
107    */
108   public List getTopicsOfContent(String contentId)
109     throws StorageObjectFailure {
110     ArrayList returnList = new ArrayList();
111
112     if (contentId != null) {
113       String sql = "select topic_id from " + theTable + " where content_id=" + contentId;
114       Connection con=null;Statement stmt=null;
115       try {
116         con = getPooledCon();
117         // should be a preparedStatement because is faster
118         stmt = con.createStatement();
119         ResultSet rs = executeSql(stmt,sql);
120         if(rs!=null){
121           while(rs.next()){
122             returnList.add(new Integer(rs.getInt("topic_id")));
123           }
124         }
125       }
126       catch (Exception e) {
127         logger.error("DatabaseContentToTopics.getTopicsOfContent: " + e.getMessage());
128       }
129       finally {
130         freeConnection(con,stmt);
131       }
132     }
133     return returnList;
134   }
135
136   private String getIdListExpression(List aList) {
137     String result = "";
138
139     Iterator i = aList.iterator();
140
141     while (i.hasNext()) {
142       result = result + i.next().toString();
143       if (i.hasNext())
144         result = result + ", ";
145     }
146     return result;
147   }
148
149   public void setTopics(String anArticleId, String [] aTopics) throws StorageObjectFailure {
150     if (aTopics==null)
151       setTopics(anArticleId, (List) null);
152     else
153       setTopics(anArticleId, Arrays.asList(aTopics));
154   }
155
156   public void setTopics(String anArticleId, List aTopics) throws StorageObjectFailure {
157     List newTopics = new Vector();
158     if (aTopics!=null) {
159       Iterator i = aTopics.iterator();
160
161       while (i.hasNext()) {
162         newTopics.add(new Integer(Integer.parseInt((String) i.next())));
163       }
164     }
165
166     List currentTopics = getTopicsOfContent(anArticleId);
167     logger.debug("New topics = " + newTopics.toString());
168     logger.debug("Current topics = " + currentTopics.toString());
169     List topicsToDelete = new Vector(currentTopics);
170     topicsToDelete.removeAll(newTopics);
171     List topicsToAdd = new Vector(newTopics);
172     topicsToAdd.removeAll(currentTopics);
173     logger.debug("to delete = " + topicsToDelete.toString());
174     logger.debug("to add = " + topicsToAdd.toString());
175
176
177     if (!topicsToDelete.isEmpty()) {
178       String sql =
179           "delete from " + theTable + " " +
180           "where content_id=" + anArticleId +
181           "        and topic_id in (" + getIdListExpression(topicsToDelete) + ")";
182
183       Connection connection=null;
184       Statement statement=null;
185       try {
186         connection = getPooledCon();
187         statement = connection.createStatement();
188         int rs = executeUpdate(statement, sql);
189       }
190       catch (Exception e) {
191         logger.error("-- deleting topics failed");
192       }
193       finally {
194         try {
195           freeConnection(connection, statement);
196         }
197         catch (Throwable t) {
198         }
199       }
200     }
201
202     Iterator i = topicsToAdd.iterator();
203     while (i.hasNext()) {
204       Integer topicId = (Integer) i.next();
205       String sql =
206           "insert into " + theTable + " (content_id, topic_id) "+
207           "values (" + anArticleId + "," + topicId + ")";
208       Connection connection=null;
209       Statement statement=null;
210       try {
211         connection = getPooledCon();
212         // should be a preparedStatement because is faster
213         statement = connection.createStatement();
214         int rs = executeUpdate(statement, sql);
215       }
216       catch (Exception e) {
217         logger.error("-- adding topics failed");
218       }
219       finally {
220         try {
221           freeConnection(connection, statement);
222         }
223         catch (Throwable t) {
224         }
225       }
226     }
227   }
228
229   public void deleteByContentId(String contentId)
230     throws StorageObjectFailure {
231     if (contentId == null) {
232       //theLog.printDebugInfo("-- delete topics failed -- no content id");
233       return;
234     }
235     //delete all row with content_id=contentId
236     String sql = "delete from "+ theTable +" where content_id=" + contentId;
237
238     Connection con=null;Statement stmt=null;
239     try {
240       con = getPooledCon();
241       // should be a preparedStatement because is faster
242       stmt = con.createStatement();
243       ResultSet rs = executeSql(stmt,sql);
244     } catch (Exception e) {
245       //theLog.printDebugInfo("-- delete topics failed  ");
246     } finally {
247       freeConnection(con,stmt);
248     }
249   }
250
251   public void deleteByTopicId(String topicId)
252     throws StorageObjectFailure {
253     if (topicId == null) {
254       //theLog.printDebugInfo("-- delete topics failed -- no topic id");
255       return;
256     }
257     //delete all row with content_id=contentId
258     String sql = "delete from "+ theTable +" where topic_id=" + topicId;
259
260     Connection con=null;Statement stmt=null;
261     try {
262       con = getPooledCon();
263       // should be a preparedStatement because is faster
264       stmt = con.createStatement();
265       ResultSet rs = executeSql(stmt,sql);
266     }
267     catch (Exception e) {
268       logger.error("-- delete topics failed ");
269     }
270     finally {
271       freeConnection(con,stmt);
272     }
273   }
274
275
276   public EntityList getContent(EntityTopics topic)
277     throws StorageObjectFailure {
278     EntityList returnList=null;
279     if (topic != null) {
280       String id = topic.getId();
281       String select = "select content_id from " + theTable + " where topic_id=" + id;
282
283       // execute select statement
284       Connection con=null;Statement stmt=null;
285       try {
286         con = getPooledCon();
287         // should be a preparedStatement because is faster
288         stmt = con.createStatement();
289         ResultSet rs = executeSql(stmt,select);
290         if (rs!=null) {
291           String topicSelect= "id IN (";
292           boolean first=true;
293           while (rs.next()) {
294             if (first==false) topicSelect+=",";
295             topicSelect += rs.getString(1);
296             first=false;
297           }
298           topicSelect+=")";
299           if (first==false)
300             returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1);
301         }
302       }
303       catch (Exception e) {
304         logger.error("-- get contetn failed");
305       }
306       finally { freeConnection(con,stmt);}
307     }
308     return returnList;
309   }
310 }