2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
32 package mircoders.storage;
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;
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;
49 * <b>This class implements the 1-n-relation between
54 public class DatabaseContentToTopics extends Database implements StorageObject{
56 private static DatabaseContentToTopics instance;
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;
70 private DatabaseContentToTopics()
71 throws StorageObjectFailure {
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); }
82 * This class return an EntityList of Topics
83 * @param EntityContent content
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+")";
94 returnList = DatabaseTopics.getInstance().selectByWhereClause(subselect,-1);
95 } catch (Exception e) {
96 theLog.printDebugInfo("-- get topics failed " + e.toString());
103 * Returns a ArrayList of Integer-Objects from a content-id.
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;
113 con = getPooledCon();
114 // should be a preparedStatement because is faster
115 stmt = con.createStatement();
116 ResultSet rs = executeSql(stmt,sql);
119 returnList.add(new Integer(rs.getInt("topic_id")));
122 } catch (Exception e) {
123 theLog.printError(e.toString());
124 theLog.printError("-- get topicsofcontent failed");
126 freeConnection(con,stmt);
135 public void setTopics(String contentId, String[] topicId)
136 throws StorageObjectFailure {
137 if (contentId == null){
140 if (topicId==null || topicId[0]==null) {
143 //first check which topics this article has
144 Collection hasTopics = getTopicsOfContent(contentId);
145 Collection toSet = new ArrayList();
146 Collection toDelete = new ArrayList();
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++){
153 for(Iterator it=hasTopics.iterator();it.hasNext();){
154 Integer topic = (Integer)it.next();
155 if(topicId[i].equals(topic.toString())){
162 toSet.add(topicId[i]);
163 theLog.printDebugInfo("to set: "+ topicId[i]);
166 //now we check if we have to delete topics
167 for(Iterator it=hasTopics.iterator();it.hasNext();){
170 Integer topic = (Integer)it.next();
171 for(int i = 0; i< topicId.length;i++){
172 if(topicId[i].equals(topic.toString())){
179 toDelete.add(topic.toString());
180 theLog.printDebugInfo("to delete: "+ topic.toString());
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]);
190 //first delete all row with content_id=contentId
191 String sql = "delete from "+ theTable +" where content_id=" + contentId
192 + " and topic_id in (";
194 for(Iterator it = toDelete.iterator(); it.hasNext();){
200 sql+= (String)it.next();
203 Connection con=null;Statement stmt=null;
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");
212 freeConnection(con,stmt);
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() + ")";
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);
228 freeConnection(con,stmt);
233 public void deleteByContentId(String contentId)
234 throws StorageObjectFailure {
235 if (contentId == null) {
236 //theLog.printDebugInfo("-- delete topics failed -- no content id");
239 //delete all row with content_id=contentId
240 String sql = "delete from "+ theTable +" where content_id=" + contentId;
242 Connection con=null;Statement stmt=null;
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 ");
251 freeConnection(con,stmt);
255 public void deleteByTopicId(String topicId)
256 throws StorageObjectFailure {
257 if (topicId == null) {
258 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
261 //delete all row with content_id=contentId
262 String sql = "delete from "+ theTable +" where topic_id=" + topicId;
264 Connection con=null;Statement stmt=null;
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 ");
273 freeConnection(con,stmt);
278 public EntityList getContent(EntityTopics topic)
279 throws StorageObjectFailure {
280 EntityList returnList=null;
282 String id = topic.getId();
283 String select = "select content_id from " + theTable + " where topic_id=" + id;
285 // execute select statement
286 Connection con=null;Statement stmt=null;
288 con = getPooledCon();
289 // should be a preparedStatement because is faster
290 stmt = con.createStatement();
291 ResultSet rs = executeSql(stmt,select);
293 String topicSelect= "id IN (";
296 if (first==false) topicSelect+=",";
297 topicSelect += rs.getString(1);
302 returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1);
305 catch (Exception e) {theLog.printDebugInfo("-- get contetn failed");}
306 finally { freeConnection(con,stmt);}