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 mir.entity.EntityList;
34 import mir.log.LoggerWrapper;
35 import mir.storage.Database;
36 import mir.storage.DatabaseFailure;
37 import mircoders.entity.EntityContent;
38 import mircoders.entity.EntityTopics;
40 import java.sql.Connection;
41 import java.sql.ResultSet;
42 import java.sql.Statement;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.Iterator;
46 import java.util.List;
49 * <b>This class implements the 1-n-relation between
54 public class DatabaseContentToTopics extends Database {
56 private static DatabaseContentToTopics instance;
58 public synchronized static DatabaseContentToTopics getInstance() {
59 if (instance == null) {
60 instance = new DatabaseContentToTopics();
65 private DatabaseContentToTopics() {
68 logger = new LoggerWrapper("Database.ContentToTopics");
69 mainTable="content_x_topic";
70 entityClass = mir.entity.GenericEntity.class;
74 * This class return an EntityList of Topics
76 public EntityList getTopics(EntityContent content) {
77 EntityList returnList=null;
78 if (content != null) {
80 String id = content.getId();
82 ArrayList extraTables = new ArrayList();
83 extraTables.add(mainTable+" cxt");
84 returnList = DatabaseTopics.getInstance()
85 .selectByWhereClauseWithExtraTables("t",extraTables,
86 "t.id=cxt.topic_id and cxt.content_id="+id );
89 logger.error("-- get topics failed " + e.toString());
96 * Returns a List of String-Objects from a content-id.
98 public List getTopicsOfContent(String contentId)
99 throws DatabaseFailure {
100 ArrayList returnList = new ArrayList();
102 if (contentId != null) {
103 String sql = "select topic_id from " + mainTable + " where content_id=" + contentId;
104 Connection con=null;Statement stmt=null;
106 con = obtainConnection();
108 // should be a preparedStatement because is faster
109 stmt = con.createStatement();
110 ResultSet rs = executeSql(stmt,sql);
113 returnList.add(Integer.toString(rs.getInt("topic_id")));
117 catch (Exception e) {
118 logger.error("DatabaseContentToTopics.getTopicsOfContent: " + e.getMessage());
121 freeConnection(con,stmt);
127 private String getIdListExpression(List aList) {
130 Iterator i = aList.iterator();
132 while (i.hasNext()) {
133 result = result + i.next().toString();
135 result = result + ", ";
140 public void setTopics(String anArticleId, String [] aTopics) throws DatabaseFailure {
142 setTopics(anArticleId, (List) null);
144 setTopics(anArticleId, Arrays.asList(aTopics));
147 public void setTopics(String anArticleId, List aTopics) throws DatabaseFailure {
148 List newTopics = new ArrayList();
150 Iterator i = aTopics.iterator();
152 while (i.hasNext()) {
153 newTopics.add(new Integer(Integer.parseInt((String) i.next())));
157 List currentTopics = getTopicsOfContent(anArticleId);
158 logger.debug("New topics = " + newTopics.toString());
159 logger.debug("Current topics = " + currentTopics.toString());
160 List topicsToDelete = new ArrayList(currentTopics);
161 topicsToDelete.removeAll(newTopics);
162 List topicsToAdd = new ArrayList(newTopics);
163 topicsToAdd.removeAll(currentTopics);
164 logger.debug("to delete = " + topicsToDelete.toString());
165 logger.debug("to add = " + topicsToAdd.toString());
168 if (!topicsToDelete.isEmpty()) {
170 "delete from " + mainTable + " " +
171 "where content_id=" + anArticleId +
172 " and topic_id in (" + getIdListExpression(topicsToDelete) + ")";
174 Connection connection=null;
175 Statement statement=null;
177 connection = obtainConnection();
178 statement = connection.createStatement();
179 executeUpdate(statement, sql);
181 catch (Exception e) {
182 logger.error("-- deleting topics failed");
186 freeConnection(connection, statement);
188 catch (Throwable t) {
193 Iterator i = topicsToAdd.iterator();
194 while (i.hasNext()) {
195 Integer topicId = (Integer) i.next();
197 "insert into " + mainTable + " (content_id, topic_id) "+
198 "values (" + anArticleId + "," + topicId + ")";
199 Connection connection=null;
200 Statement statement=null;
202 connection = obtainConnection();
203 // should be a preparedStatement because is faster
204 statement = connection.createStatement();
205 executeUpdate(statement, sql);
207 catch (Exception e) {
208 logger.error("-- adding topics failed");
212 freeConnection(connection, statement);
214 catch (Throwable t) {
220 public void deleteByContentId(String contentId)
221 throws DatabaseFailure {
222 if (contentId == null) {
223 //theLog.printDebugInfo("-- delete topics failed -- no content id");
226 //delete all row with content_id=contentId
227 String sql = "delete from "+ mainTable +" where content_id=" + contentId;
229 Connection con=null;Statement stmt=null;
231 con = obtainConnection();
232 // should be a preparedStatement because is faster
233 stmt = con.createStatement();
234 executeSql(stmt,sql);
235 } catch (Exception e) {
236 //theLog.printDebugInfo("-- delete topics failed ");
238 freeConnection(con,stmt);
242 public void deleteByTopicId(String topicId)
243 throws DatabaseFailure {
244 if (topicId == null) {
245 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
248 //delete all row with content_id=contentId
249 String sql = "delete from "+ mainTable +" where topic_id=" + topicId;
251 Connection con=null;Statement stmt=null;
253 con = obtainConnection();
254 // should be a preparedStatement because is faster
255 stmt = con.createStatement();
256 executeSql(stmt,sql);
258 catch (Exception e) {
259 logger.error("-- delete topics failed ");
262 freeConnection(con,stmt);
267 * Returns list of Content for a specific topic
270 * @throws DatabaseFailure
272 public EntityList getContent(EntityTopics topic)
273 throws DatabaseFailure {
274 EntityList returnList=null;
276 String id = topic.getId();
278 ArrayList extraTables = new ArrayList();
279 extraTables.add(mainTable+" cxt");
280 returnList = DatabaseContent.getInstance()
281 .selectByWhereClauseWithExtraTables("c",extraTables,
282 "c.id=cxt.content_id and cxt.topic_id="+id );
284 catch (Exception e) {
285 logger.error("-- get content failed");