Initial revision
[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 webdb.storage.*;
11 import webdb.entity.*;
12 import webdb.misc.*;
13
14 import mir.entity.*;
15
16 /**
17  * <b>Diese Klasse implementiert die Datenbankverbindung zur MetaObjekt-Tabelle
18  *
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 {
42                         // this.theEntityClass = Class.forName("mir.entity.EntityGruppen");
43                 } catch (Exception e) {
44                         throw new StorageObjectException(e.toString());
45                 }
46         }
47
48
49         public EntityList getTopics(EntityContent content) {
50                 EntityList returnList=null;
51                 if (content != null) {
52                         // get all to_topic from content_x_topic
53                         String id = content.getId();
54                         String subselect = "id in (select topic_id from " + theTable + " where content_id=" + id+")";
55
56                         try {
57                                 returnList = DatabaseTopics.getInstance().selectByWhereClause(subselect,-1);
58                         } catch (Exception e) {
59                                 theLog.printDebugInfo("-- get topics failed " + e.toString());
60                         }
61                 }
62                 return returnList;
63         }
64
65
66         public void setTopics(EntityContent content, ArrayList topicId) {
67                 if (content == null && topicId == null) {
68                         return;
69                 }
70                 String contentId = content.getId();
71                 //first delete all row with content_id=contentId
72                 String sql = "delete from "+ theTable +" where content_id=" + contentId;
73
74                 Connection con=null;Statement stmt=null;
75                 try {
76                         con = getPooledCon();
77                         // should be a preparedStatement because is faster
78                         stmt = con.createStatement();
79                         ResultSet rs = executeSql(stmt,sql);
80                 } catch (Exception e) {
81                         theLog.printDebugInfo("-- set topics failed -- delete");
82                 } finally {
83                         freeConnection(con,stmt);
84                 }
85
86                 //now insert
87                 //first delete all row with content_id=contentId
88
89                 for (Iterator i = topicId.listIterator(); i.hasNext();) {
90                         sql = "insert into "+ theTable +" (content_id,topic_id) values ("
91                                                 + contentId + "," + i.next().toString() + ")";
92                         try {
93                                 con = getPooledCon();
94                                 // should be a preparedStatement because is faster
95                                 stmt = con.createStatement();
96                                 ResultSet rs = executeSql(stmt,sql);
97                         } catch (Exception e) {
98                                 theLog.printDebugInfo("-- set topics failed -- insert");
99                         } finally {
100                                 freeConnection(con,stmt);
101                         }
102                 }
103         }
104
105
106         public void setTopics(String contentId, String topicId) {
107                 if (contentId == null && topicId == null) {
108                         return;
109                 }
110                 //first delete all row with content_id=contentId
111                 String sql = "delete from "+ theTable +" where content_id=" + contentId;
112
113                 Connection con=null;Statement stmt=null;
114                 try {
115                         con = getPooledCon();
116                         // should be a preparedStatement because is faster
117                         stmt = con.createStatement();
118                         int rs = executeUpdate(stmt,sql);
119                 } catch (Exception e) {
120                         theLog.printDebugInfo("-- set topics failed -- delete");
121                 } finally {
122                         freeConnection(con,stmt);
123                 }
124
125                 //now insert
126                 //first delete all row with content_id=contentId
127
128                 sql = "insert into "+ theTable +" (content_id,topic_id) values ("
129                                         + contentId + "," + topicId + ")";
130                 try {
131                         con = getPooledCon();
132                         // should be a preparedStatement because is faster
133                         stmt = con.createStatement();
134                         int rs = executeUpdate(stmt,sql);
135                 } catch (Exception e) {
136                         theLog.printDebugInfo("-- set topics failed -- insert");
137                 } finally {
138                         freeConnection(con,stmt);
139                 }
140         }
141
142         public void deleteByContentId(String contentId) {
143                 if (contentId == null) {
144                         //theLog.printDebugInfo("-- delete topics failed -- no content id");
145                         return;
146                 }
147                 //delete all row with content_id=contentId
148                 String sql = "delete from "+ theTable +" where content_id=" + contentId;
149
150                 Connection con=null;Statement stmt=null;
151                 try {
152                         con = getPooledCon();
153                         // should be a preparedStatement because is faster
154                         stmt = con.createStatement();
155                         ResultSet rs = executeSql(stmt,sql);
156                 } catch (Exception e) {
157                         //theLog.printDebugInfo("-- delete topics failed  ");
158                 } finally {
159                         freeConnection(con,stmt);
160                 }
161         }
162
163         public void deleteByTopicId(String topicId) {
164                 if (topicId == null) {
165                         //theLog.printDebugInfo("-- delete topics failed -- no topic id");
166                         return;
167                 }
168                 //delete all row with content_id=contentId
169                 String sql = "delete from "+ theTable +" where topic_id=" + topicId;
170
171                 Connection con=null;Statement stmt=null;
172                 try {
173                         con = getPooledCon();
174                         // should be a preparedStatement because is faster
175                         stmt = con.createStatement();
176                         ResultSet rs = executeSql(stmt,sql);
177                 } catch (Exception e) {
178                         theLog.printDebugInfo("-- delete topics failed ");
179                 } finally {
180                         freeConnection(con,stmt);
181                 }
182         }
183
184
185         public EntityList getContent(EntityTopics topic) {
186                 EntityList returnList=null;
187                 if (topic != null) {
188                         String id = topic.getId();
189                         String select = "select content_id from " + theTable + " where topic_id=" + id;
190
191                         // execute select statement
192                         Connection con=null;Statement stmt=null;
193                         try {
194                                 con = getPooledCon();
195                                 // should be a preparedStatement because is faster
196                                 stmt = con.createStatement();
197                                 ResultSet rs = executeSql(stmt,select);
198                                 if (rs!=null) {
199                                         String topicSelect= "id IN (";
200                                         boolean first=true;
201                                         while (rs.next()) {
202                                                 if (first==false) topicSelect+=",";
203                                                 topicSelect += rs.getString(1);
204                                                 first=false;
205                                         }
206                                         topicSelect+=")";
207                                         if (first==false)
208                                                 returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1);
209                                 }
210                         }
211                         catch (Exception e) {theLog.printDebugInfo("-- get contetn failed");}
212                         finally { freeConnection(con,stmt);}
213                 }
214                 return returnList;
215         }
216
217 }