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 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.
31 package mircoders.storage;
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;
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;
51 * <b>This class implements the 1-n-relation between
56 public class DatabaseContentToTopics extends Database implements StorageObject{
58 private static DatabaseContentToTopics instance;
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;
72 private DatabaseContentToTopics() {
75 logger = new LoggerWrapper("Database.ContentToTopics");
78 theTable="content_x_topic";
79 theEntityClass = mir.entity.GenericEntity.class;
83 * This class return an EntityList of Topics
84 * @param EntityContent content
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+")";
95 returnList = DatabaseTopics.getInstance().selectByWhereClause(subselect,-1);
98 logger.error("-- get topics failed " + e.toString());
105 * Returns a ArrayList of Integer-Objects from a content-id.
108 public List getTopicsOfContent(String contentId)
109 throws StorageObjectFailure {
110 ArrayList returnList = new ArrayList();
112 if (contentId != null) {
113 String sql = "select topic_id from " + theTable + " where content_id=" + contentId;
114 Connection con=null;Statement stmt=null;
116 con = getPooledCon();
117 // should be a preparedStatement because is faster
118 stmt = con.createStatement();
119 ResultSet rs = executeSql(stmt,sql);
122 returnList.add(new Integer(rs.getInt("topic_id")));
126 catch (Exception e) {
127 logger.error("DatabaseContentToTopics.getTopicsOfContent: " + e.getMessage());
130 freeConnection(con,stmt);
136 private String getIdListExpression(List aList) {
139 Iterator i = aList.iterator();
141 while (i.hasNext()) {
142 result = result + i.next().toString();
144 result = result + ", ";
149 public void setTopics(String anArticleId, String [] aTopics) throws StorageObjectFailure {
151 setTopics(anArticleId, (List) null);
153 setTopics(anArticleId, Arrays.asList(aTopics));
156 public void setTopics(String anArticleId, List aTopics) throws StorageObjectFailure {
157 List newTopics = new Vector();
159 Iterator i = aTopics.iterator();
161 while (i.hasNext()) {
162 newTopics.add(new Integer(Integer.parseInt((String) i.next())));
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());
177 if (!topicsToDelete.isEmpty()) {
179 "delete from " + theTable + " " +
180 "where content_id=" + anArticleId +
181 " and topic_id in (" + getIdListExpression(topicsToDelete) + ")";
183 Connection connection=null;
184 Statement statement=null;
186 connection = getPooledCon();
187 statement = connection.createStatement();
188 int rs = executeUpdate(statement, sql);
190 catch (Exception e) {
191 logger.error("-- deleting topics failed");
195 freeConnection(connection, statement);
197 catch (Throwable t) {
202 Iterator i = topicsToAdd.iterator();
203 while (i.hasNext()) {
204 Integer topicId = (Integer) i.next();
206 "insert into " + theTable + " (content_id, topic_id) "+
207 "values (" + anArticleId + "," + topicId + ")";
208 Connection connection=null;
209 Statement statement=null;
211 connection = getPooledCon();
212 // should be a preparedStatement because is faster
213 statement = connection.createStatement();
214 int rs = executeUpdate(statement, sql);
216 catch (Exception e) {
217 logger.error("-- adding topics failed");
221 freeConnection(connection, statement);
223 catch (Throwable t) {
229 public void deleteByContentId(String contentId)
230 throws StorageObjectFailure {
231 if (contentId == null) {
232 //theLog.printDebugInfo("-- delete topics failed -- no content id");
235 //delete all row with content_id=contentId
236 String sql = "delete from "+ theTable +" where content_id=" + contentId;
238 Connection con=null;Statement stmt=null;
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 ");
247 freeConnection(con,stmt);
251 public void deleteByTopicId(String topicId)
252 throws StorageObjectFailure {
253 if (topicId == null) {
254 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
257 //delete all row with content_id=contentId
258 String sql = "delete from "+ theTable +" where topic_id=" + topicId;
260 Connection con=null;Statement stmt=null;
262 con = getPooledCon();
263 // should be a preparedStatement because is faster
264 stmt = con.createStatement();
265 ResultSet rs = executeSql(stmt,sql);
267 catch (Exception e) {
268 logger.error("-- delete topics failed ");
271 freeConnection(con,stmt);
276 public EntityList getContent(EntityTopics topic)
277 throws StorageObjectFailure {
278 EntityList returnList=null;
280 String id = topic.getId();
281 String select = "select content_id from " + theTable + " where topic_id=" + id;
283 // execute select statement
284 Connection con=null;Statement stmt=null;
286 con = getPooledCon();
287 // should be a preparedStatement because is faster
288 stmt = con.createStatement();
289 ResultSet rs = executeSql(stmt,select);
291 String topicSelect= "id IN (";
294 if (first==false) topicSelect+=",";
295 topicSelect += rs.getString(1);
300 returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1);
303 catch (Exception e) {
304 logger.error("-- get contetn failed");
306 finally { freeConnection(con,stmt);}