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.log.LoggerWrapper;
42 import mir.entity.EntityList;
43 import mir.storage.Database;
44 import mir.storage.StorageObject;
45 import mir.storage.StorageObjectFailure;
46 import mircoders.entity.EntityContent;
47 import mircoders.entity.EntityTopics;
50 * <b>This class implements the 1-n-relation between
55 public class DatabaseContentToTopics extends Database implements StorageObject{
57 private static DatabaseContentToTopics instance;
59 // the following *has* to be sychronized cause this static method
60 // could get preemted and we could end up with 2 instances of DatabaseFoo.
61 // see the "Singletons with needles and thread" article at JavaWorld -mh
62 public synchronized static DatabaseContentToTopics getInstance()
63 throws StorageObjectFailure {
64 if (instance == null) {
65 instance = new DatabaseContentToTopics();
66 instance.myselfDatabase = instance;
71 private DatabaseContentToTopics() throws StorageObjectFailure {
74 logger = new LoggerWrapper("Database.ContentToTopics");
77 theTable="content_x_topic";
78 try { this.theEntityClass = Class.forName("mir.entity.GenericEntity"); }
79 catch (Exception e) { throw new StorageObjectFailure(e); }
84 * This class return an EntityList of Topics
85 * @param EntityContent content
88 public EntityList getTopics(EntityContent content) {
89 EntityList returnList=null;
90 if (content != null) {
91 // get all to_topic from content_x_topic
92 String id = content.getId();
93 String subselect = "id in (select topic_id from " + theTable + " where content_id=" + id+")";
96 returnList = DatabaseTopics.getInstance().selectByWhereClause(subselect,-1);
99 logger.error("-- get topics failed " + e.toString());
106 * Returns a ArrayList of Integer-Objects from a content-id.
109 public ArrayList getTopicsOfContent(String contentId)
110 throws StorageObjectFailure {
111 ArrayList returnList = new ArrayList();
113 if (contentId != null) {
114 String sql = "select topic_id from " + theTable + " where content_id=" + contentId;
115 Connection con=null;Statement stmt=null;
117 con = getPooledCon();
118 // should be a preparedStatement because is faster
119 stmt = con.createStatement();
120 ResultSet rs = executeSql(stmt,sql);
123 returnList.add(new Integer(rs.getInt("topic_id")));
127 catch (Exception e) {
128 logger.error("DatabaseContentToTopics.getTopicsOfContent: " + e.getMessage());
131 freeConnection(con,stmt);
140 public void setTopics(String contentId, String[] topicId)
141 throws StorageObjectFailure {
142 if (contentId == null){
145 if (topicId==null || topicId[0]==null) {
148 //first check which topics this article has
149 Collection hasTopics = getTopicsOfContent(contentId);
150 Collection toSet = new ArrayList();
151 Collection toDelete = new ArrayList();
153 if(hasTopics!=null && hasTopics.size()>0){
154 //now we check if there are new topics and copy them to an array.
155 for(int i = 0; i< topicId.length;i++){
158 for(Iterator it=hasTopics.iterator();it.hasNext();){
159 Integer topic = (Integer)it.next();
160 if(topicId[i].equals(topic.toString())){
167 toSet.add(topicId[i]);
168 logger.debug("to set: "+ topicId[i]);
171 //now we check if we have to delete topics
172 for(Iterator it=hasTopics.iterator();it.hasNext();){
175 Integer topic = (Integer)it.next();
176 for(int i = 0; i< topicId.length;i++){
177 if(topicId[i].equals(topic.toString())){
184 toDelete.add(topic.toString());
185 logger.debug("to delete: "+ topic.toString());
189 //all the topics has to be set, so we copy all to the array
190 for (int i = 0; i < topicId.length; i++){
191 toSet.add(topicId[i]);
195 //first delete all row with content_id=contentId
196 String sql = "delete from "+ theTable +" where content_id=" + contentId
197 + " and topic_id in (";
199 for(Iterator it = toDelete.iterator(); it.hasNext();){
205 sql+= (String)it.next();
208 Connection con=null;Statement stmt=null;
210 con = getPooledCon();
211 // should be a preparedStatement because is faster
212 stmt = con.createStatement();
213 int rs = executeUpdate(stmt,sql);
214 } catch (Exception e) {
215 logger.error("-- deleting topics failed");
217 freeConnection(con,stmt);
221 //first delete all row with content_id=contentId
222 for (Iterator it = toSet.iterator(); it.hasNext();) {
223 sql = "insert into "+ theTable +" (content_id,topic_id) values ("
224 + contentId + "," + (String)it.next() + ")";
226 con = getPooledCon();
227 // should be a preparedStatement because is faster
228 stmt = con.createStatement();
229 int rs = executeUpdate(stmt,sql);
231 catch (Exception e) {
232 logger.error("-- set topics failed -- insert laenge topicId" + topicId.length);
234 freeConnection(con,stmt);
239 public void deleteByContentId(String contentId)
240 throws StorageObjectFailure {
241 if (contentId == null) {
242 //theLog.printDebugInfo("-- delete topics failed -- no content id");
245 //delete all row with content_id=contentId
246 String sql = "delete from "+ theTable +" where content_id=" + contentId;
248 Connection con=null;Statement stmt=null;
250 con = getPooledCon();
251 // should be a preparedStatement because is faster
252 stmt = con.createStatement();
253 ResultSet rs = executeSql(stmt,sql);
254 } catch (Exception e) {
255 //theLog.printDebugInfo("-- delete topics failed ");
257 freeConnection(con,stmt);
261 public void deleteByTopicId(String topicId)
262 throws StorageObjectFailure {
263 if (topicId == null) {
264 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
267 //delete all row with content_id=contentId
268 String sql = "delete from "+ theTable +" where topic_id=" + topicId;
270 Connection con=null;Statement stmt=null;
272 con = getPooledCon();
273 // should be a preparedStatement because is faster
274 stmt = con.createStatement();
275 ResultSet rs = executeSql(stmt,sql);
277 catch (Exception e) {
278 logger.error("-- delete topics failed ");
281 freeConnection(con,stmt);
286 public EntityList getContent(EntityTopics topic)
287 throws StorageObjectFailure {
288 EntityList returnList=null;
290 String id = topic.getId();
291 String select = "select content_id from " + theTable + " where topic_id=" + id;
293 // execute select statement
294 Connection con=null;Statement stmt=null;
296 con = getPooledCon();
297 // should be a preparedStatement because is faster
298 stmt = con.createStatement();
299 ResultSet rs = executeSql(stmt,select);
301 String topicSelect= "id IN (";
304 if (first==false) topicSelect+=",";
305 topicSelect += rs.getString(1);
310 returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1);
313 catch (Exception e) {
314 logger.error("-- get contetn failed");
316 finally { freeConnection(con,stmt);}