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 throws StorageObjectException {
50 EntityList returnList=null;
51 if (content != null) {
52 // get all to_topic from content_x_topic
53 String id = content.getId();
54 String subselect = "id in (select topic_id from " + theTable + " where content_id=" + id+")";
57 returnList = DatabaseTopics.getInstance().selectByWhereClause(subselect,-1);
58 } catch (Exception e) {
59 theLog.printDebugInfo("-- get topics failed " + e.toString());
60 throw new StorageObjectException("-- get topics failed " + e.toString());
67 * Returns a ArrayList of Integer-Objects from a content-id.
70 public ArrayList getTopicsOfContent(String contentId)
71 throws StorageObjectException {
72 ArrayList returnList = new ArrayList();
73 if (contentId != null) {
74 String sql = "select topic_id from " + theTable + " where content_id=" + contentId;
75 Connection con=null;Statement stmt=null;
78 // should be a preparedStatement because is faster
79 stmt = con.createStatement();
80 ResultSet rs = executeSql(stmt,sql);
83 returnList.add(new Integer(rs.getInt("topic_id")));
86 } catch (Exception e) {
87 theLog.printError(e.toString());
88 theLog.printError("-- get topicsofcontent failed");
89 throw new StorageObjectException("-- get topicsofcontent failed" + e.toString());
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 ArrayList hasTopics = getTopicsOfContent(contentId);
110 ArrayList toSet = new ArrayList();
111 ArrayList 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 toSet=(ArrayList)Arrays.asList(topicId);
153 //first delete all row with content_id=contentId
154 String sql = "delete from "+ theTable +" where content_id=" + contentId
155 + " and topic_id in (";
157 for(Iterator it = toDelete.iterator(); it.hasNext();){
163 sql+= (String)it.next();
166 Connection con=null;Statement stmt=null;
168 con = getPooledCon();
169 // should be a preparedStatement because is faster
170 stmt = con.createStatement();
171 int rs = executeUpdate(stmt,sql);
172 } catch (Exception e) {
173 theLog.printDebugInfo("-- deleting topics failed");
174 throw new StorageObjectException("-- deleting topics failed" + e.toString());
176 freeConnection(con,stmt);
180 //first delete all row with content_id=contentId
181 for (Iterator it = toSet.iterator(); it.hasNext();) {
182 sql = "insert into "+ theTable +" (content_id,topic_id) values ("
183 + contentId + "," + (String)it.next() + ")";
185 con = getPooledCon();
186 // should be a preparedStatement because is faster
187 stmt = con.createStatement();
188 int rs = executeUpdate(stmt,sql);
189 } catch (Exception e) {
190 theLog.printDebugInfo("-- set topics failed -- insert laenge topicId" + topicId.length);
191 throw new StorageObjectException("-- set topics failed" + e.toString());
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 by contentId failed ");
215 throw new StorageObjectException("-- delete topics by contentId failed" + e.toString());
217 freeConnection(con,stmt);
221 public void deleteByTopicId(String topicId)
222 throws StorageObjectException {
223 if (topicId == null) {
224 //theLog.printDebugInfo("-- delete topics failed -- no topic id");
227 //delete all row with content_id=contentId
228 String sql = "delete from "+ theTable +" where topic_id=" + topicId;
230 Connection con=null;Statement stmt=null;
232 con = getPooledCon();
233 // should be a preparedStatement because is faster
234 stmt = con.createStatement();
235 ResultSet rs = executeSql(stmt,sql);
236 } catch (Exception e) {
237 theLog.printDebugInfo("-- delete topics failed ");
238 throw new StorageObjectException("-- delete topics by topicId failed" + e.toString());
240 freeConnection(con,stmt);
245 public EntityList getContent(EntityTopics topic)
246 throws StorageObjectException {
247 EntityList returnList=null;
249 String id = topic.getId();
250 String select = "select content_id from " + theTable + " where topic_id=" + id;
252 // execute select statement
253 Connection con=null;Statement stmt=null;
255 con = getPooledCon();
256 // should be a preparedStatement because is faster
257 stmt = con.createStatement();
258 ResultSet rs = executeSql(stmt,select);
260 String topicSelect= "id IN (";
263 if (first==false) topicSelect+=",";
264 topicSelect += rs.getString(1);
269 returnList = DatabaseContent.getInstance().selectByWhereClause(topicSelect,-1);
272 catch (Exception e) {
273 theLog.printDebugInfo("-- get content failed");
274 throw new StorageObjectException("-- get content failed" + e.toString());
275 } finally { freeConnection(con,stmt);}