1 package mircoders.storage;
8 import freemarker.template.*;
14 import mircoders.entity.*;
17 * <b>This class implements the 1-n-relation between
22 public class DatabaseContentToTopics extends Database implements StorageObject{
24 private static DatabaseContentToTopics instance;
26 // the following *has* to be sychronized cause this static method
27 // could get preemted and we could end up with 2 instances of DatabaseFoo.
28 // see the "Singletons with needles and thread" article at JavaWorld -mh
29 public synchronized static DatabaseContentToTopics getInstance()
30 throws StorageObjectException {
31 if (instance == null) {
32 instance = new DatabaseContentToTopics();
33 instance.myselfDatabase = instance;
38 private DatabaseContentToTopics()
39 throws StorageObjectException {
42 this.hasTimestamp = false;
43 this.theTable="content_x_topic";
44 try { this.theEntityClass = Class.forName("mir.entity.GenericEntity"); }
45 catch (Exception e) { throw new StorageObjectException(e.toString()); }
50 * This class return an EntityList of Topics
51 * @param EntityContent content
54 public EntityList getTopics(EntityContent content) {
55 EntityList returnList=null;
56 if (content != null) {
57 // get all to_topic from content_x_topic
58 String id = content.getId();
59 String subselect = "id in (select topic_id from " + theTable + " where content_id=" + id+")";
62 returnList = DatabaseTopics.getInstance().selectByWhereClause(subselect,-1);
63 } catch (Exception e) {
64 theLog.printDebugInfo("-- get topics failed " + e.toString());
71 * Returns a ArrayList of Integer-Objects from a content-id.
74 public ArrayList getTopicsOfContent(String contentId)
75 throws StorageObjectException {
76 ArrayList returnList = new ArrayList();
77 if (contentId != null) {
78 String sql = "select topic_id from " + theTable + " where content_id=" + contentId;
79 Connection con=null;Statement stmt=null;
82 // should be a preparedStatement because is faster
83 stmt = con.createStatement();
84 ResultSet rs = executeSql(stmt,sql);
87 returnList.add(new Integer(rs.getInt("topic_id")));
90 } catch (Exception e) {
91 theLog.printError(e.toString());
92 theLog.printError("-- get topicsofcontent failed");
94 freeConnection(con,stmt);
103 public void setTopics(String contentId, String[] topicId)
104 throws StorageObjectException {
105 if (contentId == null){
108 if (topicId==null || topicId[0]==null) {
111 //first check which topics this article has
112 Collection hasTopics = getTopicsOfContent(contentId);
113 Collection toSet = new ArrayList();
114 Collection toDelete = new ArrayList();
116 if(hasTopics!=null && hasTopics.size()>0){
117 //now we check if there are new topics and copy them to an array.
118 for(int i = 0; i< topicId.length;i++){
121 for(Iterator it=hasTopics.iterator();it.hasNext();){
122 Integer topic = (Integer)it.next();
123 if(topicId[i].equals(topic.toString())){
130 toSet.add(topicId[i]);
131 theLog.printDebugInfo("to set: "+ topicId[i]);
134 //now we check if we have to delete topics
135 for(Iterator it=hasTopics.iterator();it.hasNext();){
138 Integer topic = (Integer)it.next();
139 for(int i = 0; i< topicId.length;i++){
140 if(topicId[i].equals(topic.toString())){
147 toDelete.add(topic.toString());
148 theLog.printDebugInfo("to delete: "+ topic.toString());
152 //all the topics has to be set, so we copy all to the array
153 for (int i = 0; i < topicId.length; i++){
154 toSet.add(topicId[i]);
158 //first delete all row with content_id=contentId
159 String sql = "delete from "+ theTable +" where content_id=" + contentId
160 + " and topic_id in (";
162 for(Iterator it = toDelete.iterator(); it.hasNext();){
168 sql+= (String)it.next();
171 Connection con=null;Statement stmt=null;
173 con = getPooledCon();
174 // should be a preparedStatement because is faster
175 stmt = con.createStatement();
176 int rs = executeUpdate(stmt,sql);
177 } catch (Exception e) {
178 theLog.printDebugInfo("-- deleting topics failed");
180 freeConnection(con,stmt);
184 //first delete all row with content_id=contentId
185 for (Iterator it = toSet.iterator(); it.hasNext();) {
186 sql = "insert into "+ theTable +" (content_id,topic_id) values ("
187 + contentId + "," + (String)it.next() + ")";
189 con = getPooledCon();
190 // should be a preparedStatement because is faster
191 stmt = con.createStatement();
192 int rs = executeUpdate(stmt,sql);
193 } catch (Exception e) {
194 theLog.printDebugInfo("-- set topics failed -- insert laenge topicId" + topicId.length);
196 freeConnection(con,stmt);
201 public void deleteByContentId(String contentId)
202 throws StorageObjectException {
203 if (contentId == null) {
204 //theLog.printDebugInfo("-- delete topics failed -- no content id");
207 //delete all row with content_id=contentId
208 String sql = "delete from "+ theTable +" where content_id=" + contentId;
210 Connection con=null;Statement stmt=null;
212 con = getPooledCon();
213 // should be a preparedStatement because is faster
214 stmt = con.createStatement();
215 ResultSet rs = executeSql(stmt,sql);
216 } catch (Exception e) {
217 //theLog.printDebugInfo("-- delete topics failed ");
219 freeConnection(con,stmt);
223 public void deleteByTopicId(String topicId)
224 throws StorageObjectException {
225 if (topicId == null) {
226 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
229 //delete all row with content_id=contentId
230 String sql = "delete from "+ theTable +" where topic_id=" + topicId;
232 Connection con=null;Statement stmt=null;
234 con = getPooledCon();
235 // should be a preparedStatement because is faster
236 stmt = con.createStatement();
237 ResultSet rs = executeSql(stmt,sql);
238 } catch (Exception e) {
239 theLog.printDebugInfo("-- delete topics failed ");
241 freeConnection(con,stmt);
246 public EntityList getContent(EntityTopics topic)
247 throws StorageObjectException {
248 EntityList returnList=null;
250 String id = topic.getId();
251 String select = "select content_id from " + theTable + " where topic_id=" + id;
253 // execute select statement
254 Connection con=null;Statement stmt=null;
256 con = getPooledCon();
257 // should be a preparedStatement because is faster
258 stmt = con.createStatement();
259 ResultSet rs = executeSql(stmt,select);
261 String topicSelect= "id IN (";
264 if (first==false) topicSelect+=",";
265 topicSelect += rs.getString(1);
270 returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1);
273 catch (Exception e) {theLog.printDebugInfo("-- get contetn failed");}
274 finally { freeConnection(con,stmt);}