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