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";
44 * This class return an EntityList of Topics
45 * @param EntityContent content
48 public EntityList getTopics(EntityContent content) {
49 EntityList returnList=null;
50 if (content != null) {
51 // get all to_topic from content_x_topic
52 String id = content.getId();
53 String subselect = "id in (select topic_id from " + theTable + " where content_id=" + id+")";
56 returnList = DatabaseTopics.getInstance().selectByWhereClause(subselect,-1);
57 } catch (Exception e) {
58 theLog.printDebugInfo("-- get topics failed " + e.toString());
65 * Returns a ArrayList of Integer-Objects from a content-id.
68 public ArrayList getTopicsOfContent(String contentId) {
69 ArrayList returnList = new ArrayList();
70 if (contentId != null) {
71 String sql = "select topic_id from " + theTable + " where content_id=" + contentId;
72 Connection con=null;Statement stmt=null;
75 // should be a preparedStatement because is faster
76 stmt = con.createStatement();
77 ResultSet rs = executeSql(stmt,sql);
80 returnList.add(new Integer(rs.getInt("topic_id")));
83 } catch (Exception e) {
84 theLog.printError(e.toString());
85 theLog.printError("-- get topicsofcontent failed");
87 freeConnection(con,stmt);
96 public void setTopics(String contentId, String[] topicId) {
97 if (contentId == null){
100 if (topicId==null || topicId[0]==null) {
103 //first check which topics this article has
104 ArrayList hasTopics = getTopicsOfContent(contentId);
105 ArrayList toSet = new ArrayList();
106 ArrayList toDelete = new ArrayList();
108 if(hasTopics!=null && hasTopics.size()>0){
109 //now we check if there are new topics and copy them to an array.
110 for(int i = 0; i< topicId.length;i++){
113 for(Iterator it=hasTopics.iterator();it.hasNext();){
114 Integer topic = (Integer)it.next();
115 if(topicId[i].equals(topic.toString())){
122 toSet.add(topicId[i]);
123 theLog.printDebugInfo("to set: "+ topicId[i]);
126 //now we check if we have to delete topics
127 for(Iterator it=hasTopics.iterator();it.hasNext();){
130 Integer topic = (Integer)it.next();
131 for(int i = 0; i< topicId.length;i++){
132 if(topicId[i].equals(topic.toString())){
139 toDelete.add(topic.toString());
140 theLog.printDebugInfo("to delete: "+ topic.toString());
144 //all the topics has to be set, so we copy all to the array
145 toSet=(ArrayList)Arrays.asList(topicId);
148 //first delete all row with content_id=contentId
149 String sql = "delete from "+ theTable +" where content_id=" + contentId
150 + " and topic_id in (";
152 for(Iterator it = toDelete.iterator(); it.hasNext();){
158 sql+= (String)it.next();
161 Connection con=null;Statement stmt=null;
163 con = getPooledCon();
164 // should be a preparedStatement because is faster
165 stmt = con.createStatement();
166 int rs = executeUpdate(stmt,sql);
167 } catch (Exception e) {
168 theLog.printDebugInfo("-- deleting topics failed");
170 freeConnection(con,stmt);
174 //first delete all row with content_id=contentId
175 for (Iterator it = toSet.iterator(); it.hasNext();) {
176 sql = "insert into "+ theTable +" (content_id,topic_id) values ("
177 + contentId + "," + (String)it.next() + ")";
179 con = getPooledCon();
180 // should be a preparedStatement because is faster
181 stmt = con.createStatement();
182 int rs = executeUpdate(stmt,sql);
183 } catch (Exception e) {
184 theLog.printDebugInfo("-- set topics failed -- insert laenge topicId" + topicId.length);
186 freeConnection(con,stmt);
191 public void deleteByContentId(String contentId) {
192 if (contentId == null) {
193 //theLog.printDebugInfo("-- delete topics failed -- no content id");
196 //delete all row with content_id=contentId
197 String sql = "delete from "+ theTable +" where content_id=" + contentId;
199 Connection con=null;Statement stmt=null;
201 con = getPooledCon();
202 // should be a preparedStatement because is faster
203 stmt = con.createStatement();
204 ResultSet rs = executeSql(stmt,sql);
205 } catch (Exception e) {
206 //theLog.printDebugInfo("-- delete topics failed ");
208 freeConnection(con,stmt);
212 public void deleteByTopicId(String topicId) {
213 if (topicId == null) {
214 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
217 //delete all row with content_id=contentId
218 String sql = "delete from "+ theTable +" where topic_id=" + topicId;
220 Connection con=null;Statement stmt=null;
222 con = getPooledCon();
223 // should be a preparedStatement because is faster
224 stmt = con.createStatement();
225 ResultSet rs = executeSql(stmt,sql);
226 } catch (Exception e) {
227 theLog.printDebugInfo("-- delete topics failed ");
229 freeConnection(con,stmt);
234 public EntityList getContent(EntityTopics topic) {
235 EntityList returnList=null;
237 String id = topic.getId();
238 String select = "select content_id from " + theTable + " where topic_id=" + id;
240 // execute select statement
241 Connection con=null;Statement stmt=null;
243 con = getPooledCon();
244 // should be a preparedStatement because is faster
245 stmt = con.createStatement();
246 ResultSet rs = executeSql(stmt,select);
248 String topicSelect= "id IN (";
251 if (first==false) topicSelect+=",";
252 topicSelect += rs.getString(1);
257 returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1);
260 catch (Exception e) {theLog.printDebugInfo("-- get contetn failed");}
261 finally { freeConnection(con,stmt);}