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