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