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.Arrays;
39 import java.util.Iterator;
40 import java.util.List;
41 import java.util.Vector;
43 import mir.entity.EntityList;
44 import mir.log.LoggerWrapper;
45 import mir.storage.Database;
46 import mir.storage.StorageObject;
47 import mir.storage.StorageObjectFailure;
48 import mircoders.entity.EntityContent;
49 import mircoders.entity.EntityTopics;
52 * <b>This class implements the 1-n-relation between
57 public class DatabaseContentToTopics extends Database implements StorageObject{
59 private static DatabaseContentToTopics instance;
61 public static DatabaseContentToTopics getInstance() {
62 if (instance == null) {
63 synchronized (DatabaseContentToTopics.class) {
64 if (instance == null) {
65 instance = new DatabaseContentToTopics();
66 instance.myselfDatabase = instance;
73 private DatabaseContentToTopics() {
76 logger = new LoggerWrapper("Database.ContentToTopics");
79 theTable="content_x_topic";
80 theEntityClass = mir.entity.GenericEntity.class;
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 List 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);
137 private String getIdListExpression(List aList) {
140 Iterator i = aList.iterator();
142 while (i.hasNext()) {
143 result = result + i.next().toString();
145 result = result + ", ";
150 public void setTopics(String anArticleId, String [] aTopics) throws StorageObjectFailure {
152 setTopics(anArticleId, (List) null);
154 setTopics(anArticleId, Arrays.asList(aTopics));
157 public void setTopics(String anArticleId, List aTopics) throws StorageObjectFailure {
158 List newTopics = new Vector();
160 Iterator i = aTopics.iterator();
162 while (i.hasNext()) {
163 newTopics.add(new Integer(Integer.parseInt((String) i.next())));
167 List currentTopics = getTopicsOfContent(anArticleId);
168 logger.debug("New topics = " + newTopics.toString());
169 logger.debug("Current topics = " + currentTopics.toString());
170 List topicsToDelete = new Vector(currentTopics);
171 topicsToDelete.removeAll(newTopics);
172 List topicsToAdd = new Vector(newTopics);
173 topicsToAdd.removeAll(currentTopics);
174 logger.debug("to delete = " + topicsToDelete.toString());
175 logger.debug("to add = " + topicsToAdd.toString());
178 if (!topicsToDelete.isEmpty()) {
180 "delete from " + theTable + " " +
181 "where content_id=" + anArticleId +
182 " and topic_id in (" + getIdListExpression(topicsToDelete) + ")";
184 Connection connection=null;
185 Statement statement=null;
187 connection = getPooledCon();
188 statement = connection.createStatement();
189 int rs = executeUpdate(statement, sql);
191 catch (Exception e) {
192 logger.error("-- deleting topics failed");
196 freeConnection(connection, statement);
198 catch (Throwable t) {
203 Iterator i = topicsToAdd.iterator();
204 while (i.hasNext()) {
205 Integer topicId = (Integer) i.next();
207 "insert into " + theTable + " (content_id, topic_id) "+
208 "values (" + anArticleId + "," + topicId + ")";
209 Connection connection=null;
210 Statement statement=null;
212 connection = getPooledCon();
213 // should be a preparedStatement because is faster
214 statement = connection.createStatement();
215 int rs = executeUpdate(statement, sql);
217 catch (Exception e) {
218 logger.error("-- adding topics failed");
222 freeConnection(connection, statement);
224 catch (Throwable t) {
230 public void deleteByContentId(String contentId)
231 throws StorageObjectFailure {
232 if (contentId == null) {
233 //theLog.printDebugInfo("-- delete topics failed -- no content id");
236 //delete all row with content_id=contentId
237 String sql = "delete from "+ theTable +" where content_id=" + contentId;
239 Connection con=null;Statement stmt=null;
241 con = getPooledCon();
242 // should be a preparedStatement because is faster
243 stmt = con.createStatement();
244 ResultSet rs = executeSql(stmt,sql);
245 } catch (Exception e) {
246 //theLog.printDebugInfo("-- delete topics failed ");
248 freeConnection(con,stmt);
252 public void deleteByTopicId(String topicId)
253 throws StorageObjectFailure {
254 if (topicId == null) {
255 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
258 //delete all row with content_id=contentId
259 String sql = "delete from "+ theTable +" where topic_id=" + topicId;
261 Connection con=null;Statement stmt=null;
263 con = getPooledCon();
264 // should be a preparedStatement because is faster
265 stmt = con.createStatement();
266 ResultSet rs = executeSql(stmt,sql);
268 catch (Exception e) {
269 logger.error("-- delete topics failed ");
272 freeConnection(con,stmt);
277 public EntityList getContent(EntityTopics topic)
278 throws StorageObjectFailure {
279 EntityList returnList=null;
281 String id = topic.getId();
282 String select = "select content_id from " + theTable + " where topic_id=" + id;
284 // execute select statement
285 Connection con=null;Statement stmt=null;
287 con = getPooledCon();
288 // should be a preparedStatement because is faster
289 stmt = con.createStatement();
290 ResultSet rs = executeSql(stmt,select);
292 String topicSelect= "id IN (";
295 if (first==false) topicSelect+=",";
296 topicSelect += rs.getString(1);
301 returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1);
304 catch (Exception e) {
305 logger.error("-- get contetn failed");
307 finally { freeConnection(con,stmt);}