import mircoders.entity.*;
/**
- * <b>Diese Klasse implementiert die Datenbankverbindung zur MetaObjekt-Tabelle
- *
+ * <b>This class implements the 1-n-relation between
+ * content and topic
*
*/
this.theTable="content_x_topic";
}
-
+ /**
+ * 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) {
+ 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;
+ }
-
+ /**
+ * Set new topics
+ */
public void setTopics(String contentId, String[] topicId) {
if (contentId == null){
return;
if (topicId==null || topicId[0]==null) {
return;
}
+ //first check which topics this article has
+ ArrayList hasTopics = getTopicsOfContent(contentId);
+ ArrayList toSet = new ArrayList();
+ ArrayList 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
+ toSet=(ArrayList)Arrays.asList(topicId);
+ }
+
//first delete all row with content_id=contentId
- String sql = "delete from "+ theTable +" where 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) {
if (contentId == null) {
//theLog.printDebugInfo("-- delete topics failed -- no content id");
}
return returnList;
}
-
}