X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmircoders%2Fstorage%2FDatabaseContentToTopics.java;h=f1d4633ba035b91a154825c8937e1a12a0511065;hb=95c2e3a74f492f6f61d4dbe8bb47996f43af0627;hp=1eb413740c13dc3b89ad824a3496d0f3d4388ecd;hpb=c6a2204d8d75293256fd17c07e54971d7672359a;p=mir.git diff --git a/source/mircoders/storage/DatabaseContentToTopics.java b/source/mircoders/storage/DatabaseContentToTopics.java index 1eb41374..f1d4633b 100755 --- a/source/mircoders/storage/DatabaseContentToTopics.java +++ b/source/mircoders/storage/DatabaseContentToTopics.java @@ -1,17 +1,49 @@ -package mircoders.storage; - -import java.lang.*; -import java.sql.*; -import java.io.*; -import java.util.*; +/* + * Copyright (C) 2001, 2002 The Mir-coders group + * + * This file is part of Mir. + * + * Mir is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Mir is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Mir; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * In addition, as a special exception, The Mir-coders gives permission to link + * the code of this program with any library licensed under the Apache Software License, + * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library + * (or with modified versions of the above that use the same license as the above), + * and distribute linked combinations including the two. You must obey the + * GNU General Public License in all respects for all of the code used other than + * the above mentioned libraries. If you modify this file, you may extend this + * exception to your version of the file, but you are not obligated to do so. + * If you do not wish to do so, delete this exception statement from your version. + */ -import freemarker.template.*; +package mircoders.storage; -import mir.storage.*; -import mir.entity.*; -import mir.misc.*; +import mir.entity.EntityList; +import mir.log.LoggerWrapper; +import mir.storage.Database; +import mir.storage.DatabaseFailure; +import mircoders.entity.EntityContent; +import mircoders.entity.EntityTopics; -import mircoders.entity.*; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; /** * This class implements the 1-n-relation between @@ -19,194 +51,187 @@ import mircoders.entity.*; * */ -public class DatabaseContentToTopics extends Database implements StorageObject{ +public class DatabaseContentToTopics extends Database { private static DatabaseContentToTopics instance; - public static DatabaseContentToTopics getInstance() - throws StorageObjectException { + public synchronized static DatabaseContentToTopics getInstance() { if (instance == null) { instance = new DatabaseContentToTopics(); - instance.myselfDatabase = instance; } return instance; } - private DatabaseContentToTopics() - throws StorageObjectException { - + private DatabaseContentToTopics() { super(); - this.hasTimestamp = false; - this.theTable="content_x_topic"; + + logger = new LoggerWrapper("Database.ContentToTopics"); + mainTable="content_x_topic"; + entityClass = mir.entity.GenericEntity.class; } /** * This class return an EntityList of Topics - * @param EntityContent content - * @returns EntityList */ public EntityList getTopics(EntityContent content) { EntityList returnList=null; if (content != null) { - // get all to_topic from content_x_topic - String id = content.getId(); - String subselect = "id in (select topic_id from " + theTable + " where content_id=" + id+")"; + String id = content.getId(); try { - returnList = DatabaseTopics.getInstance().selectByWhereClause(subselect,-1); - } catch (Exception e) { - theLog.printDebugInfo("-- get topics failed " + e.toString()); + ArrayList extraTables = new ArrayList(); + extraTables.add(mainTable+" cxt"); + returnList = DatabaseTopics.getInstance() + .selectByWhereClauseWithExtraTables("t",extraTables, + "t.id=cxt.topic_id and cxt.content_id="+id ); + } + catch (Exception e) { + logger.error("-- get topics failed " + e.toString()); } } return returnList; } - + /** - * Returns a ArrayList of Integer-Objects from a content-id. - * @returns ArrayList + * Returns a List of String-Objects from a content-id. */ - public ArrayList getTopicsOfContent(String contentId) - throws StorageObjectException { + public List getTopicsOfContent(String contentId) + throws DatabaseFailure { ArrayList returnList = new ArrayList(); + if (contentId != null) { - String sql = "select topic_id from " + theTable + " where content_id=" + contentId; + String sql = "select topic_id from " + mainTable + " where content_id=" + contentId; Connection con=null;Statement stmt=null; try { - con = getPooledCon(); + con = obtainConnection(); + // should be a preparedStatement because is faster stmt = con.createStatement(); ResultSet rs = executeSql(stmt,sql); if(rs!=null){ while(rs.next()){ - returnList.add(new Integer(rs.getInt("topic_id"))); + returnList.add(Integer.toString(rs.getInt("topic_id"))); } } - } catch (Exception e) { - theLog.printError(e.toString()); - theLog.printError("-- get topicsofcontent failed"); - } finally { + } + catch (Exception e) { + logger.error("DatabaseContentToTopics.getTopicsOfContent: " + e.getMessage()); + } + finally { freeConnection(con,stmt); } } return returnList; } - /** - * Set new topics - */ - public void setTopics(String contentId, String[] topicId) - throws StorageObjectException { - if (contentId == null){ - return; + private String getIdListExpression(List aList) { + String result = ""; + + Iterator i = aList.iterator(); + + while (i.hasNext()) { + result = result + i.next().toString(); + if (i.hasNext()) + result = result + ", "; } - if (topicId==null || topicId[0]==null) { - return; + return result; + } + + public void setTopics(String anArticleId, String [] aTopics) throws DatabaseFailure { + if (aTopics==null) + setTopics(anArticleId, (List) null); + else + setTopics(anArticleId, Arrays.asList(aTopics)); + } + + public void setTopics(String anArticleId, List aTopics) throws DatabaseFailure { + List newTopics = new ArrayList(); + if (aTopics!=null) { + Iterator i = aTopics.iterator(); + + while (i.hasNext()) { + newTopics.add(new Integer(Integer.parseInt((String) i.next()))); + } } - //first check which topics this article has - Collection hasTopics = getTopicsOfContent(contentId); - Collection toSet = new ArrayList(); - Collection toDelete = new ArrayList(); - - if(hasTopics!=null && hasTopics.size()>0){ - //now we check if there are new topics and copy them to an array. - for(int i = 0; i< topicId.length;i++){ - boolean set=false; - int whichTopic = 0; - for(Iterator it=hasTopics.iterator();it.hasNext();){ - Integer topic = (Integer)it.next(); - if(topicId[i].equals(topic.toString())){ - set=true; - } else { - whichTopic = i; - } - } - if(set==false){ - toSet.add(topicId[i]); - theLog.printDebugInfo("to set: "+ topicId[i]); - } + + List currentTopics = getTopicsOfContent(anArticleId); + logger.debug("New topics = " + newTopics.toString()); + logger.debug("Current topics = " + currentTopics.toString()); + List topicsToDelete = new ArrayList(currentTopics); + topicsToDelete.removeAll(newTopics); + List topicsToAdd = new ArrayList(newTopics); + topicsToAdd.removeAll(currentTopics); + logger.debug("to delete = " + topicsToDelete.toString()); + logger.debug("to add = " + topicsToAdd.toString()); + + + if (!topicsToDelete.isEmpty()) { + String sql = + "delete from " + mainTable + " " + + "where content_id=" + anArticleId + + " and topic_id in (" + getIdListExpression(topicsToDelete) + ")"; + + Connection connection=null; + Statement statement=null; + try { + connection = obtainConnection(); + statement = connection.createStatement(); + executeUpdate(statement, sql); } - //now we check if we have to delete topics - for(Iterator it=hasTopics.iterator();it.hasNext();){ - boolean delete=true; - int whichTopic = 0; - Integer topic = (Integer)it.next(); - for(int i = 0; i< topicId.length;i++){ - if(topicId[i].equals(topic.toString())){ - delete=false; - } else { - whichTopic = i; - } + catch (Exception e) { + logger.error("-- deleting topics failed"); + } + finally { + try { + freeConnection(connection, statement); } - if(delete==true){ - toDelete.add(topic.toString()); - theLog.printDebugInfo("to delete: "+ topic.toString()); + catch (Throwable t) { } } - } else { - //all the topics has to be set, so we copy all to the array - for (int i = 0; i < topicId.length; i++){ - toSet.add(topicId[i]); - } - } - - //first delete all row with content_id=contentId - String sql = "delete from "+ theTable +" where content_id=" + contentId - + " and topic_id in ("; - boolean first=false; - for(Iterator it = toDelete.iterator(); it.hasNext();){ - if(first==false){ - first=true; - } else { - sql+=","; - } - sql+= (String)it.next(); - } - sql+=")"; - Connection con=null;Statement stmt=null; - try { - con = getPooledCon(); - // should be a preparedStatement because is faster - stmt = con.createStatement(); - int rs = executeUpdate(stmt,sql); - } catch (Exception e) { - theLog.printDebugInfo("-- deleting topics failed"); - } finally { - freeConnection(con,stmt); } - //now insert - //first delete all row with content_id=contentId - for (Iterator it = toSet.iterator(); it.hasNext();) { - sql = "insert into "+ theTable +" (content_id,topic_id) values (" - + contentId + "," + (String)it.next() + ")"; + Iterator i = topicsToAdd.iterator(); + while (i.hasNext()) { + Integer topicId = (Integer) i.next(); + String sql = + "insert into " + mainTable + " (content_id, topic_id) "+ + "values (" + anArticleId + "," + topicId + ")"; + Connection connection=null; + Statement statement=null; try { - con = getPooledCon(); + connection = obtainConnection(); // should be a preparedStatement because is faster - stmt = con.createStatement(); - int rs = executeUpdate(stmt,sql); - } catch (Exception e) { - theLog.printDebugInfo("-- set topics failed -- insert laenge topicId" + topicId.length); - } finally { - freeConnection(con,stmt); + statement = connection.createStatement(); + executeUpdate(statement, sql); + } + catch (Exception e) { + logger.error("-- adding topics failed"); + } + finally { + try { + freeConnection(connection, statement); + } + catch (Throwable t) { + } } } } public void deleteByContentId(String contentId) - throws StorageObjectException { + throws DatabaseFailure { if (contentId == null) { //theLog.printDebugInfo("-- delete topics failed -- no content id"); return; } //delete all row with content_id=contentId - String sql = "delete from "+ theTable +" where content_id=" + contentId; + String sql = "delete from "+ mainTable +" where content_id=" + contentId; Connection con=null;Statement stmt=null; try { - con = getPooledCon(); + con = obtainConnection(); // should be a preparedStatement because is faster stmt = con.createStatement(); - ResultSet rs = executeSql(stmt,sql); + executeSql(stmt,sql); } catch (Exception e) { //theLog.printDebugInfo("-- delete topics failed "); } finally { @@ -215,57 +240,50 @@ public class DatabaseContentToTopics extends Database implements StorageObject{ } public void deleteByTopicId(String topicId) - throws StorageObjectException { + throws DatabaseFailure { if (topicId == null) { //theLog.printDebugInfo("-- delete topics failed -- no topic id"); return; } //delete all row with content_id=contentId - String sql = "delete from "+ theTable +" where topic_id=" + topicId; + String sql = "delete from "+ mainTable +" where topic_id=" + topicId; Connection con=null;Statement stmt=null; try { - con = getPooledCon(); + con = obtainConnection(); // should be a preparedStatement because is faster stmt = con.createStatement(); - ResultSet rs = executeSql(stmt,sql); - } catch (Exception e) { - theLog.printDebugInfo("-- delete topics failed "); - } finally { + executeSql(stmt,sql); + } + catch (Exception e) { + logger.error("-- delete topics failed "); + } + finally { freeConnection(con,stmt); } } - +/** + * Returns list of Content for a specific topic + * @param topic + * @return EntityList + * @throws DatabaseFailure + */ public EntityList getContent(EntityTopics topic) - throws StorageObjectException { + throws DatabaseFailure { EntityList returnList=null; if (topic != null) { String id = topic.getId(); - String select = "select content_id from " + theTable + " where topic_id=" + id; - - // execute select statement - Connection con=null;Statement stmt=null; try { - con = getPooledCon(); - // should be a preparedStatement because is faster - stmt = con.createStatement(); - ResultSet rs = executeSql(stmt,select); - if (rs!=null) { - String topicSelect= "id IN ("; - boolean first=true; - while (rs.next()) { - if (first==false) topicSelect+=","; - topicSelect += rs.getString(1); - first=false; - } - topicSelect+=")"; - if (first==false) - returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1); - } + ArrayList extraTables = new ArrayList(); + extraTables.add(mainTable+" cxt"); + returnList = DatabaseContent.getInstance() + .selectByWhereClauseWithExtraTables("c",extraTables, + "c.id=cxt.content_id and cxt.topic_id="+id ); + } + catch (Exception e) { + logger.error("-- get content failed"); } - catch (Exception e) {theLog.printDebugInfo("-- get contetn failed");} - finally { freeConnection(con,stmt);} } return returnList; }