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.log.LoggerWrapper;
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 public static DatabaseContentToTopics getInstance() {
60 if (instance == null) {
61 synchronized (DatabaseContentToTopics.class) {
62 if (instance == null) {
63 instance = new DatabaseContentToTopics();
64 instance.myselfDatabase = instance;
71 private DatabaseContentToTopics() {
74 logger = new LoggerWrapper("Database.ContentToTopics");
77 theTable="content_x_topic";
78 theEntityClass = mir.entity.GenericEntity.class;
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);
97 logger.error("-- get topics failed " + e.toString());
104 * Returns a ArrayList of Integer-Objects from a content-id.
107 public ArrayList getTopicsOfContent(String contentId)
108 throws StorageObjectFailure {
109 ArrayList returnList = new ArrayList();
111 if (contentId != null) {
112 String sql = "select topic_id from " + theTable + " where content_id=" + contentId;
113 Connection con=null;Statement stmt=null;
115 con = getPooledCon();
116 // should be a preparedStatement because is faster
117 stmt = con.createStatement();
118 ResultSet rs = executeSql(stmt,sql);
121 returnList.add(new Integer(rs.getInt("topic_id")));
125 catch (Exception e) {
126 logger.error("DatabaseContentToTopics.getTopicsOfContent: " + e.getMessage());
129 freeConnection(con,stmt);
138 public void setTopics(String contentId, String[] topicId)
139 throws StorageObjectFailure {
140 if (contentId == null){
143 if (topicId==null || topicId[0]==null) {
146 //first check which topics this article has
147 Collection hasTopics = getTopicsOfContent(contentId);
148 Collection toSet = new ArrayList();
149 Collection toDelete = new ArrayList();
151 if(hasTopics!=null && hasTopics.size()>0){
152 //now we check if there are new topics and copy them to an array.
153 for(int i = 0; i< topicId.length;i++){
156 for(Iterator it=hasTopics.iterator();it.hasNext();){
157 Integer topic = (Integer)it.next();
158 if(topicId[i].equals(topic.toString())){
165 toSet.add(topicId[i]);
166 logger.debug("to set: "+ topicId[i]);
169 //now we check if we have to delete topics
170 for(Iterator it=hasTopics.iterator();it.hasNext();){
173 Integer topic = (Integer)it.next();
174 for(int i = 0; i< topicId.length;i++){
175 if(topicId[i].equals(topic.toString())){
182 toDelete.add(topic.toString());
183 logger.debug("to delete: "+ topic.toString());
187 //all the topics has to be set, so we copy all to the array
188 for (int i = 0; i < topicId.length; i++){
189 toSet.add(topicId[i]);
193 //first delete all row with content_id=contentId
194 String sql = "delete from "+ theTable +" where content_id=" + contentId
195 + " and topic_id in (";
197 for(Iterator it = toDelete.iterator(); it.hasNext();){
203 sql+= (String)it.next();
206 Connection con=null;Statement stmt=null;
208 con = getPooledCon();
209 // should be a preparedStatement because is faster
210 stmt = con.createStatement();
211 int rs = executeUpdate(stmt,sql);
212 } catch (Exception e) {
213 logger.error("-- deleting topics failed");
215 freeConnection(con,stmt);
219 //first delete all row with content_id=contentId
220 for (Iterator it = toSet.iterator(); it.hasNext();) {
221 sql = "insert into "+ theTable +" (content_id,topic_id) values ("
222 + contentId + "," + (String)it.next() + ")";
224 con = getPooledCon();
225 // should be a preparedStatement because is faster
226 stmt = con.createStatement();
227 int rs = executeUpdate(stmt,sql);
229 catch (Exception e) {
230 logger.error("-- set topics failed -- insert laenge topicId" + topicId.length);
232 freeConnection(con,stmt);
237 public void deleteByContentId(String contentId)
238 throws StorageObjectFailure {
239 if (contentId == null) {
240 //theLog.printDebugInfo("-- delete topics failed -- no content id");
243 //delete all row with content_id=contentId
244 String sql = "delete from "+ theTable +" where content_id=" + contentId;
246 Connection con=null;Statement stmt=null;
248 con = getPooledCon();
249 // should be a preparedStatement because is faster
250 stmt = con.createStatement();
251 ResultSet rs = executeSql(stmt,sql);
252 } catch (Exception e) {
253 //theLog.printDebugInfo("-- delete topics failed ");
255 freeConnection(con,stmt);
259 public void deleteByTopicId(String topicId)
260 throws StorageObjectFailure {
261 if (topicId == null) {
262 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
265 //delete all row with content_id=contentId
266 String sql = "delete from "+ theTable +" where topic_id=" + topicId;
268 Connection con=null;Statement stmt=null;
270 con = getPooledCon();
271 // should be a preparedStatement because is faster
272 stmt = con.createStatement();
273 ResultSet rs = executeSql(stmt,sql);
275 catch (Exception e) {
276 logger.error("-- delete topics failed ");
279 freeConnection(con,stmt);
284 public EntityList getContent(EntityTopics topic)
285 throws StorageObjectFailure {
286 EntityList returnList=null;
288 String id = topic.getId();
289 String select = "select content_id from " + theTable + " where topic_id=" + id;
291 // execute select statement
292 Connection con=null;Statement stmt=null;
294 con = getPooledCon();
295 // should be a preparedStatement because is faster
296 stmt = con.createStatement();
297 ResultSet rs = executeSql(stmt,select);
299 String topicSelect= "id IN (";
302 if (first==false) topicSelect+=",";
303 topicSelect += rs.getString(1);
308 returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1);
311 catch (Exception e) {
312 logger.error("-- get contetn failed");
314 finally { freeConnection(con,stmt);}