8fb8a9eacf6cd0256c999cf7973499be332bd556
[mir.git] / source / mircoders / storage / DatabaseContentToTopics.java
1 package mircoders.storage;
2
3 import java.lang.*;
4 import java.sql.*;
5 import java.io.*;
6 import java.util.*;
7
8 import freemarker.template.*;
9
10 import mir.storage.*;
11 import mir.entity.*;
12 import mir.misc.*;
13
14 import mircoders.entity.*;
15
16 /**
17  * <b>This class implements the 1-n-relation between
18  * content and topic
19  *
20  */
21
22 public class DatabaseContentToTopics extends Database implements StorageObject{
23
24   private static DatabaseContentToTopics instance;
25
26   public static DatabaseContentToTopics getInstance()
27     throws StorageObjectException {
28     if (instance == null) {
29       instance = new DatabaseContentToTopics();
30       instance.myselfDatabase = instance;
31     }
32     return instance;
33   }
34
35   private DatabaseContentToTopics()
36     throws StorageObjectException {
37
38     super();
39     this.hasTimestamp = false;
40     this.theTable="content_x_topic";
41     try { this.theEntityClass = Class.forName("mir.entity.GenericEntity"); }
42     catch (Exception e) { throw new StorageObjectException(e.toString()); }
43
44   }
45
46   /**
47    * This class return an EntityList of Topics
48    * @param EntityContent content
49    * @returns EntityList
50    */
51   public EntityList getTopics(EntityContent content) {
52     EntityList returnList=null;
53     if (content != null) {
54       // get all to_topic from content_x_topic
55       String id = content.getId();
56       String subselect = "id in (select topic_id from " + theTable + " where content_id=" + id+")";
57
58       try {
59         returnList = DatabaseTopics.getInstance().selectByWhereClause(subselect,-1);
60       } catch (Exception e) {
61         theLog.printDebugInfo("-- get topics failed " + e.toString());
62       }
63     }
64     return returnList;
65   }
66
67   /**
68    * Returns a ArrayList of Integer-Objects from a content-id.
69    * @returns ArrayList
70    */
71   public ArrayList getTopicsOfContent(String contentId)
72     throws StorageObjectException {
73     ArrayList returnList = new ArrayList();
74     if (contentId != null) {
75       String sql = "select topic_id from " + theTable + " where content_id=" + contentId;
76       Connection con=null;Statement stmt=null;
77       try {
78         con = getPooledCon();
79         // should be a preparedStatement because is faster
80         stmt = con.createStatement();
81         ResultSet rs = executeSql(stmt,sql);
82         if(rs!=null){
83           while(rs.next()){
84             returnList.add(new Integer(rs.getInt("topic_id")));
85           }
86         }
87       } catch (Exception e) {
88         theLog.printError(e.toString());
89         theLog.printError("-- get topicsofcontent failed");
90       } finally {
91         freeConnection(con,stmt);
92       }
93     }
94     return returnList;
95   }
96
97   /**
98    * Set new topics
99    */
100   public void setTopics(String contentId, String[] topicId)
101     throws StorageObjectException {
102     if (contentId == null){
103       return;
104     }
105     if (topicId==null || topicId[0]==null) {
106       return;
107     }
108     //first check which topics this article has
109     Collection hasTopics = getTopicsOfContent(contentId);
110     Collection toSet = new ArrayList();
111     Collection toDelete = new ArrayList();
112
113     if(hasTopics!=null && hasTopics.size()>0){
114       //now we check if there are new topics and copy them to an array.
115       for(int i = 0; i< topicId.length;i++){
116         boolean set=false;
117         int whichTopic = 0;
118         for(Iterator it=hasTopics.iterator();it.hasNext();){
119           Integer topic = (Integer)it.next();
120           if(topicId[i].equals(topic.toString())){
121             set=true;
122           } else {
123             whichTopic = i;
124           }
125         }
126         if(set==false){
127           toSet.add(topicId[i]);
128           theLog.printDebugInfo("to set: "+ topicId[i]);
129         }
130       }
131       //now we check if we have to delete topics
132       for(Iterator it=hasTopics.iterator();it.hasNext();){
133         boolean delete=true;
134         int whichTopic = 0;
135         Integer topic = (Integer)it.next();
136         for(int i = 0; i< topicId.length;i++){
137           if(topicId[i].equals(topic.toString())){
138             delete=false;
139           } else {
140             whichTopic = i;
141           }
142         }
143         if(delete==true){
144           toDelete.add(topic.toString());
145           theLog.printDebugInfo("to delete: "+ topic.toString());
146         }
147       }
148     } else {
149       //all the topics has to be set, so we copy all to the array
150                         for (int i = 0; i < topicId.length; i++){
151                                 toSet.add(topicId[i]);
152                         }
153     }
154
155     //first delete all row with content_id=contentId
156     String sql = "delete from "+ theTable +" where content_id=" + contentId
157                 + " and topic_id in (";
158     boolean first=false;
159     for(Iterator it = toDelete.iterator(); it.hasNext();){
160       if(first==false){
161         first=true;
162       } else {
163         sql+=",";
164       }
165       sql+= (String)it.next();
166     }
167     sql+=")";
168     Connection con=null;Statement stmt=null;
169     try {
170       con = getPooledCon();
171       // should be a preparedStatement because is faster
172       stmt = con.createStatement();
173       int rs = executeUpdate(stmt,sql);
174     } catch (Exception e) {
175       theLog.printDebugInfo("-- deleting topics failed");
176     } finally {
177       freeConnection(con,stmt);
178     }
179
180     //now insert
181     //first delete all row with content_id=contentId
182     for (Iterator it = toSet.iterator(); it.hasNext();) {
183       sql = "insert into "+ theTable +" (content_id,topic_id) values ("
184             + contentId + "," + (String)it.next() + ")";
185       try {
186         con = getPooledCon();
187         // should be a preparedStatement because is faster
188         stmt = con.createStatement();
189         int rs = executeUpdate(stmt,sql);
190       } catch (Exception e) {
191         theLog.printDebugInfo("-- set topics failed -- insert laenge topicId" + topicId.length);
192       } finally {
193         freeConnection(con,stmt);
194       }
195     }
196   }
197
198   public void deleteByContentId(String contentId)
199     throws StorageObjectException {
200     if (contentId == null) {
201       //theLog.printDebugInfo("-- delete topics failed -- no content id");
202       return;
203     }
204     //delete all row with content_id=contentId
205     String sql = "delete from "+ theTable +" where content_id=" + contentId;
206
207     Connection con=null;Statement stmt=null;
208     try {
209       con = getPooledCon();
210       // should be a preparedStatement because is faster
211       stmt = con.createStatement();
212       ResultSet rs = executeSql(stmt,sql);
213     } catch (Exception e) {
214       //theLog.printDebugInfo("-- delete topics failed  ");
215     } finally {
216       freeConnection(con,stmt);
217     }
218   }
219
220   public void deleteByTopicId(String topicId)
221     throws StorageObjectException {
222     if (topicId == null) {
223       //theLog.printDebugInfo("-- delete topics failed -- no topic id");
224       return;
225     }
226     //delete all row with content_id=contentId
227     String sql = "delete from "+ theTable +" where topic_id=" + topicId;
228
229     Connection con=null;Statement stmt=null;
230     try {
231       con = getPooledCon();
232       // should be a preparedStatement because is faster
233       stmt = con.createStatement();
234       ResultSet rs = executeSql(stmt,sql);
235     } catch (Exception e) {
236       theLog.printDebugInfo("-- delete topics failed ");
237     } finally {
238       freeConnection(con,stmt);
239     }
240   }
241
242
243   public EntityList getContent(EntityTopics topic)
244     throws StorageObjectException {
245     EntityList returnList=null;
246     if (topic != null) {
247       String id = topic.getId();
248       String select = "select content_id from " + theTable + " where topic_id=" + id;
249
250       // execute select statement
251       Connection con=null;Statement stmt=null;
252       try {
253         con = getPooledCon();
254         // should be a preparedStatement because is faster
255         stmt = con.createStatement();
256         ResultSet rs = executeSql(stmt,select);
257         if (rs!=null) {
258           String topicSelect= "id IN (";
259           boolean first=true;
260           while (rs.next()) {
261             if (first==false) topicSelect+=",";
262             topicSelect += rs.getString(1);
263             first=false;
264           }
265           topicSelect+=")";
266           if (first==false)
267             returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1);
268         }
269       }
270       catch (Exception e) {theLog.printDebugInfo("-- get contetn failed");}
271       finally { freeConnection(con,stmt);}
272     }
273     return returnList;
274   }
275 }