import mircoders.entity.*;
/**
- * <b>Diese Klasse implementiert die Datenbankverbindung zur MetaObjekt-Tabelle
- *
+ * <b>This class implements the 1-n-relation between
+ * content and topic
*
*/
private static DatabaseContentToTopics instance;
- public static DatabaseContentToTopics getInstance()
+ // the following *has* to be sychronized cause this static method
+ // could get preemted and we could end up with 2 instances of DatabaseFoo.
+ // see the "Singletons with needles and thread" article at JavaWorld -mh
+ public synchronized static DatabaseContentToTopics getInstance()
throws StorageObjectException {
if (instance == null) {
instance = new DatabaseContentToTopics();
super();
this.hasTimestamp = false;
this.theTable="content_x_topic";
- }
+ try { this.theEntityClass = Class.forName("mir.entity.GenericEntity"); }
+ catch (Exception e) { throw new StorageObjectException(e.toString()); }
+ }
+ /**
+ * This class return an EntityList of Topics
+ * @param EntityContent content
+ * @returns EntityList
+ */
public EntityList getTopics(EntityContent content) {
EntityList returnList=null;
if (content != null) {
return returnList;
}
+ /**
+ * Returns a ArrayList of Integer-Objects from a content-id.
+ * @returns ArrayList
+ */
+ public ArrayList getTopicsOfContent(String contentId)
+ throws StorageObjectException {
+ ArrayList returnList = new ArrayList();
+ if (contentId != null) {
+ String sql = "select topic_id from " + theTable + " where content_id=" + contentId;
+ Connection con=null;Statement stmt=null;
+ try {
+ con = getPooledCon();
+ // 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")));
+ }
+ }
+ } catch (Exception e) {
+ theLog.printError(e.toString());
+ theLog.printError("-- get topicsofcontent failed");
+ } finally {
+ freeConnection(con,stmt);
+ }
+ }
+ return returnList;
+ }
- public void setTopics(String contentId, String[] topicId) {
+ /**
+ * Set new topics
+ */
+ public void setTopics(String contentId, String[] topicId)
+ throws StorageObjectException {
if (contentId == null){
return;
}
if (topicId==null || topicId[0]==null) {
return;
}
- //first delete all row with content_id=contentId
- String sql = "delete from "+ theTable +" where content_id=" + contentId;
+ //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]);
+ }
+ }
+ //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;
+ }
+ }
+ if(delete==true){
+ toDelete.add(topic.toString());
+ theLog.printDebugInfo("to delete: "+ topic.toString());
+ }
+ }
+ } 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();
- ResultSet rs = executeSql(stmt,sql);
+ int rs = executeUpdate(stmt,sql);
} catch (Exception e) {
- theLog.printDebugInfo("-- set topics failed -- delete");
+ theLog.printDebugInfo("-- deleting topics failed");
} finally {
freeConnection(con,stmt);
}
//now insert
//first delete all row with content_id=contentId
- for (int i=0;i<topicId.length;i++) {
+ for (Iterator it = toSet.iterator(); it.hasNext();) {
sql = "insert into "+ theTable +" (content_id,topic_id) values ("
- + contentId + "," + topicId[i] + ")";
+ + contentId + "," + (String)it.next() + ")";
try {
con = getPooledCon();
// should be a preparedStatement because is faster
stmt = con.createStatement();
- ResultSet rs = executeSql(stmt,sql);
+ int rs = executeUpdate(stmt,sql);
} catch (Exception e) {
theLog.printDebugInfo("-- set topics failed -- insert laenge topicId" + topicId.length);
} finally {
}
}
-
- public void setTopics(String contentId, String topicId) {
- if (contentId == null && topicId == null) {
- return;
- }
- //first delete all row with content_id=contentId
- String sql = "delete from "+ theTable +" where content_id=" + contentId;
-
- 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("-- set topics failed -- delete");
- } finally {
- freeConnection(con,stmt);
- }
-
- //now insert
- //first delete all row with content_id=contentId
-
- sql = "insert into "+ theTable +" (content_id,topic_id) values ("
- + contentId + "," + topicId + ")";
- try {
- con = getPooledCon();
- // should be a preparedStatement because is faster
- stmt = con.createStatement();
- int rs = executeUpdate(stmt,sql);
- } catch (Exception e) {
- theLog.printDebugInfo("-- set topics failed -- insert");
- } finally {
- freeConnection(con,stmt);
- }
- }
-
- public void deleteByContentId(String contentId) {
+ public void deleteByContentId(String contentId)
+ throws StorageObjectException {
if (contentId == null) {
//theLog.printDebugInfo("-- delete topics failed -- no content id");
return;
}
}
- public void deleteByTopicId(String topicId) {
+ public void deleteByTopicId(String topicId)
+ throws StorageObjectException {
if (topicId == null) {
//theLog.printDebugInfo("-- delete topics failed -- no topic id");
return;
}
- public EntityList getContent(EntityTopics topic) {
+ public EntityList getContent(EntityTopics topic)
+ throws StorageObjectException {
EntityList returnList=null;
if (topic != null) {
String id = topic.getId();
}
return returnList;
}
-
}