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;
\r
34 import java.sql.ResultSet;
\r
35 import java.sql.Statement;
\r
36 import java.util.ArrayList;
\r
37 import java.util.Arrays;
\r
38 import java.util.Iterator;
\r
39 import java.util.List;
\r
40 import java.util.Vector;
\r
42 import mir.entity.EntityList;
\r
43 import mir.log.LoggerWrapper;
\r
44 import mir.storage.Database;
\r
45 import mir.storage.StorageObject;
\r
46 import mir.storage.StorageObjectFailure;
\r
47 import mircoders.entity.EntityContent;
\r
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 synchronized static DatabaseContentToTopics getInstance() {
61 if (instance == null) {
62 instance = new DatabaseContentToTopics();
67 private DatabaseContentToTopics() {
70 logger = new LoggerWrapper("Database.ContentToTopics");
73 theTable="content_x_topic";
74 theEntityClass = mir.entity.GenericEntity.class;
78 * This class return an EntityList of Topics
79 * @param EntityContent content
82 public EntityList getTopics(EntityContent content) {
83 EntityList returnList=null;
84 if (content != null) {
85 // get all to_topic from content_x_topic
86 String id = content.getId();
87 String subselect = "id in (select topic_id from " + theTable + " where content_id=" + id+")";
90 returnList = DatabaseTopics.getInstance().selectByWhereClause(subselect,-1);
93 logger.error("-- get topics failed " + e.toString());
100 * Returns a ArrayList of Integer-Objects from a content-id.
103 public List getTopicsOfContent(String contentId)
104 throws StorageObjectFailure {
105 ArrayList returnList = new ArrayList();
107 if (contentId != null) {
108 String sql = "select topic_id from " + theTable + " where content_id=" + contentId;
109 Connection con=null;Statement stmt=null;
111 con = getPooledCon();
112 // should be a preparedStatement because is faster
113 stmt = con.createStatement();
114 ResultSet rs = executeSql(stmt,sql);
117 returnList.add(new Integer(rs.getInt("topic_id")));
121 catch (Exception e) {
122 logger.error("DatabaseContentToTopics.getTopicsOfContent: " + e.getMessage());
125 freeConnection(con,stmt);
131 private String getIdListExpression(List aList) {
134 Iterator i = aList.iterator();
136 while (i.hasNext()) {
137 result = result + i.next().toString();
139 result = result + ", ";
144 public void setTopics(String anArticleId, String [] aTopics) throws StorageObjectFailure {
146 setTopics(anArticleId, (List) null);
148 setTopics(anArticleId, Arrays.asList(aTopics));
151 public void setTopics(String anArticleId, List aTopics) throws StorageObjectFailure {
152 List newTopics = new Vector();
154 Iterator i = aTopics.iterator();
156 while (i.hasNext()) {
157 newTopics.add(new Integer(Integer.parseInt((String) i.next())));
161 List currentTopics = getTopicsOfContent(anArticleId);
162 logger.debug("New topics = " + newTopics.toString());
163 logger.debug("Current topics = " + currentTopics.toString());
164 List topicsToDelete = new Vector(currentTopics);
165 topicsToDelete.removeAll(newTopics);
166 List topicsToAdd = new Vector(newTopics);
167 topicsToAdd.removeAll(currentTopics);
168 logger.debug("to delete = " + topicsToDelete.toString());
169 logger.debug("to add = " + topicsToAdd.toString());
172 if (!topicsToDelete.isEmpty()) {
174 "delete from " + theTable + " " +
175 "where content_id=" + anArticleId +
176 " and topic_id in (" + getIdListExpression(topicsToDelete) + ")";
178 Connection connection=null;
179 Statement statement=null;
181 connection = getPooledCon();
182 statement = connection.createStatement();
183 int rs = executeUpdate(statement, sql);
185 catch (Exception e) {
186 logger.error("-- deleting topics failed");
190 freeConnection(connection, statement);
192 catch (Throwable t) {
197 Iterator i = topicsToAdd.iterator();
198 while (i.hasNext()) {
199 Integer topicId = (Integer) i.next();
201 "insert into " + theTable + " (content_id, topic_id) "+
202 "values (" + anArticleId + "," + topicId + ")";
203 Connection connection=null;
204 Statement statement=null;
206 connection = getPooledCon();
207 // should be a preparedStatement because is faster
208 statement = connection.createStatement();
209 int rs = executeUpdate(statement, sql);
211 catch (Exception e) {
212 logger.error("-- adding topics failed");
216 freeConnection(connection, statement);
218 catch (Throwable t) {
224 public void deleteByContentId(String contentId)
225 throws StorageObjectFailure {
226 if (contentId == null) {
227 //theLog.printDebugInfo("-- delete topics failed -- no content id");
230 //delete all row with content_id=contentId
231 String sql = "delete from "+ theTable +" where content_id=" + contentId;
233 Connection con=null;Statement stmt=null;
235 con = getPooledCon();
236 // should be a preparedStatement because is faster
237 stmt = con.createStatement();
238 ResultSet rs = executeSql(stmt,sql);
239 } catch (Exception e) {
240 //theLog.printDebugInfo("-- delete topics failed ");
242 freeConnection(con,stmt);
246 public void deleteByTopicId(String topicId)
247 throws StorageObjectFailure {
248 if (topicId == null) {
249 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
252 //delete all row with content_id=contentId
253 String sql = "delete from "+ theTable +" where topic_id=" + topicId;
255 Connection con=null;Statement stmt=null;
257 con = getPooledCon();
258 // should be a preparedStatement because is faster
259 stmt = con.createStatement();
260 ResultSet rs = executeSql(stmt,sql);
262 catch (Exception e) {
263 logger.error("-- delete topics failed ");
266 freeConnection(con,stmt);
271 public EntityList getContent(EntityTopics topic)
272 throws StorageObjectFailure {
273 EntityList returnList=null;
275 String id = topic.getId();
276 String select = "select content_id from " + theTable + " where topic_id=" + id;
278 // execute select statement
279 Connection con=null;Statement stmt=null;
281 con = getPooledCon();
282 // should be a preparedStatement because is faster
283 stmt = con.createStatement();
284 ResultSet rs = executeSql(stmt,select);
286 String topicSelect= "id IN (";
289 if (first==false) topicSelect+=",";
290 topicSelect += rs.getString(1);
295 returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1);
298 catch (Exception e) {
299 logger.error("-- get contetn failed");
301 finally { freeConnection(con,stmt);}