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 public static DatabaseContentToTopics getInstance()
27 throws StorageObjectException {
28 if (instance == null) {
29 instance = new DatabaseContentToTopics();
30 instance.myselfDatabase = instance;
35 private DatabaseContentToTopics()
36 throws StorageObjectException {
39 this.hasTimestamp = false;
40 this.theTable="content_x_topic";
41 try { this.theEntityClass = Class.forName("mir.entity.GenericEntity"); }
42 catch (Exception e) { throw new StorageObjectException(e.toString()); }
47 * This class return an EntityList of Topics
48 * @param EntityContent content
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+")";
59 returnList = DatabaseTopics.getInstance().selectByWhereClause(subselect,-1);
60 } catch (Exception e) {
61 theLog.printDebugInfo("-- get topics failed " + e.toString());
68 * Returns a ArrayList of Integer-Objects from a content-id.
71 public ArrayList getTopicsOfContent(String contentId)
72 throws StorageObjectException {
73 ArrayList returnList = new ArrayList();
74 if (contentId != null) {
75 String sql = "select topic_id from " + theTable + " where content_id=" + contentId;
76 Connection con=null;Statement stmt=null;
79 // should be a preparedStatement because is faster
80 stmt = con.createStatement();
81 ResultSet rs = executeSql(stmt,sql);
84 returnList.add(new Integer(rs.getInt("topic_id")));
87 } catch (Exception e) {
88 theLog.printError(e.toString());
89 theLog.printError("-- get topicsofcontent failed");
91 freeConnection(con,stmt);
100 public void setTopics(String contentId, String[] topicId)
101 throws StorageObjectException {
102 if (contentId == null){
105 if (topicId==null || topicId[0]==null) {
108 //first check which topics this article has
109 Collection hasTopics = getTopicsOfContent(contentId);
110 Collection toSet = new ArrayList();
111 Collection toDelete = new ArrayList();
113 if(hasTopics!=null && hasTopics.size()>0){
114 //now we check if there are new topics and copy them to an array.
115 for(int i = 0; i< topicId.length;i++){
118 for(Iterator it=hasTopics.iterator();it.hasNext();){
119 Integer topic = (Integer)it.next();
120 if(topicId[i].equals(topic.toString())){
127 toSet.add(topicId[i]);
128 theLog.printDebugInfo("to set: "+ topicId[i]);
131 //now we check if we have to delete topics
132 for(Iterator it=hasTopics.iterator();it.hasNext();){
135 Integer topic = (Integer)it.next();
136 for(int i = 0; i< topicId.length;i++){
137 if(topicId[i].equals(topic.toString())){
144 toDelete.add(topic.toString());
145 theLog.printDebugInfo("to delete: "+ topic.toString());
149 //all the topics has to be set, so we copy all to the array
150 for (int i = 0; i < topicId.length; i++){
151 toSet.add(topicId[i]);
155 //first delete all row with content_id=contentId
156 String sql = "delete from "+ theTable +" where content_id=" + contentId
157 + " and topic_id in (";
159 for(Iterator it = toDelete.iterator(); it.hasNext();){
165 sql+= (String)it.next();
168 Connection con=null;Statement stmt=null;
170 con = getPooledCon();
171 // should be a preparedStatement because is faster
172 stmt = con.createStatement();
173 int rs = executeUpdate(stmt,sql);
174 } catch (Exception e) {
175 theLog.printDebugInfo("-- deleting topics failed");
177 freeConnection(con,stmt);
181 //first delete all row with content_id=contentId
182 for (Iterator it = toSet.iterator(); it.hasNext();) {
183 sql = "insert into "+ theTable +" (content_id,topic_id) values ("
184 + contentId + "," + (String)it.next() + ")";
186 con = getPooledCon();
187 // should be a preparedStatement because is faster
188 stmt = con.createStatement();
189 int rs = executeUpdate(stmt,sql);
190 } catch (Exception e) {
191 theLog.printDebugInfo("-- set topics failed -- insert laenge topicId" + topicId.length);
193 freeConnection(con,stmt);
198 public void deleteByContentId(String contentId)
199 throws StorageObjectException {
200 if (contentId == null) {
201 //theLog.printDebugInfo("-- delete topics failed -- no content id");
204 //delete all row with content_id=contentId
205 String sql = "delete from "+ theTable +" where content_id=" + contentId;
207 Connection con=null;Statement stmt=null;
209 con = getPooledCon();
210 // should be a preparedStatement because is faster
211 stmt = con.createStatement();
212 ResultSet rs = executeSql(stmt,sql);
213 } catch (Exception e) {
214 //theLog.printDebugInfo("-- delete topics failed ");
216 freeConnection(con,stmt);
220 public void deleteByTopicId(String topicId)
221 throws StorageObjectException {
222 if (topicId == null) {
223 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
226 //delete all row with content_id=contentId
227 String sql = "delete from "+ theTable +" where topic_id=" + topicId;
229 Connection con=null;Statement stmt=null;
231 con = getPooledCon();
232 // should be a preparedStatement because is faster
233 stmt = con.createStatement();
234 ResultSet rs = executeSql(stmt,sql);
235 } catch (Exception e) {
236 theLog.printDebugInfo("-- delete topics failed ");
238 freeConnection(con,stmt);
243 public EntityList getContent(EntityTopics topic)
244 throws StorageObjectException {
245 EntityList returnList=null;
247 String id = topic.getId();
248 String select = "select content_id from " + theTable + " where topic_id=" + id;
250 // execute select statement
251 Connection con=null;Statement stmt=null;
253 con = getPooledCon();
254 // should be a preparedStatement because is faster
255 stmt = con.createStatement();
256 ResultSet rs = executeSql(stmt,select);
258 String topicSelect= "id IN (";
261 if (first==false) topicSelect+=",";
262 topicSelect += rs.getString(1);
267 returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1);
270 catch (Exception e) {theLog.printDebugInfo("-- get contetn failed");}
271 finally { freeConnection(con,stmt);}