KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > geinuke > bizlogic > TopicBL


1
2  /*
3  -- GeiNuke --
4 Copyright (c) 2005 by Roberto Sidoti [geinuke@users.sourceforge.net]
5  http://www.hostingjava.it/-geinuke/
6
7 This file is part of GeiNuke.
8
9     GeiNuke is free software; you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 2 of the License, or
12     (at your option) any later version.
13
14     GeiNuke is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17     GNU General Public License for more details.
18
19     You should have received a copy of the GNU General Public License
20     along with GeiNuke; if not, write to the Free Software
21     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */

23 package com.geinuke.bizlogic;
24
25 import java.sql.SQLException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import com.geinuke.dao.TopicDAO;
28 import com.geinuke.middle.ICommentBL;
29 import com.geinuke.middle.ITopicBL;
30 import com.geinuke.middle.IUserBL;
31 import com.geinuke.middle.MiddleFactory;
32 import com.geinuke.util.CountArrayList;
33 import com.geinuke.util.TextUtil;
34 import com.geinuke.util.collection.CachedBL;
35 import com.geinuke.util.collection.ForumTopicComparator;
36 import com.geinuke.util.collection.MagicCollections;
37 import com.geinuke.vo.CommentVO;
38 import com.geinuke.vo.NewsVO;
39 import com.geinuke.vo.TopicVO;
40
41
42 public class TopicBL extends CachedBL implements ITopicBL {
43     
44     protected static ITopicBL instance=null;
45     
46     public TopicBL(){
47         super();
48     }
49     
50     public void delForumTopicByTId(int tid)throws BLException{
51         ftopicCache.clear();
52         try{
53             
54             ICommentBL cbl=(ICommentBL)MiddleFactory.getBL("ICommentBL");
55             
56             cbl.delForumCommentsByXId(tid);
57             TopicDAO dao=new TopicDAO();
58             dao.delForumTopicByTId(tid);
59         }catch(SQLException JavaDoc sqle){
60             throw new DBException(sqle.getMessage());
61         }catch(Throwable JavaDoc t){
62             throw new BLException(t.getMessage());
63         }
64     
65     }
66     
67     public void delForumTopicsByFid(int fid)throws BLException{
68         ftopicCache.clear();
69         try{
70             ICommentBL cbl=(ICommentBL)MiddleFactory.getBL("ICommentBL");
71             
72             cbl.delForumCommentsByFId(fid);
73             
74             TopicDAO dao=new TopicDAO();
75             dao.delForumTopicsByFId(fid);
76         }catch(SQLException JavaDoc sqle){
77             throw new DBException(sqle.getMessage());
78         }catch(Throwable JavaDoc t){
79             throw new BLException(t.getMessage());
80         }
81     
82     }
83     
84     
85     public void insTopic(TopicVO to)throws BLException{
86         
87         try{
88             TopicDAO dao=new TopicDAO();
89             dao.insertTopic(to);
90             
91         }catch(SQLException JavaDoc sqle){
92             throw new DBException(sqle.getMessage());
93         }catch(Throwable JavaDoc t){
94             throw new BLException(t.getMessage());
95         }
96         
97     }
98     
99     public void insForumTopic(TopicVO to,int fid)throws BLException{
100         ftopicCache.clear();
101         try{
102             TopicDAO dao=new TopicDAO();
103             dao.insertForumTopic(to);
104             dao.insertTopics_Forums(to.getTId(),fid);
105         }catch(SQLException JavaDoc sqle){
106             throw new DBException(sqle.getMessage());
107         }catch(Throwable JavaDoc t){
108             throw new BLException(t.getMessage());
109         }
110         
111     }
112     
113     public void updateForumTopic(TopicVO to)throws BLException{
114         ftopicCache.clear();
115         try{
116             TopicDAO dao=new TopicDAO();
117             dao.updateForumTopic(to);
118         
119         }catch(SQLException JavaDoc sqle){
120             throw new DBException(sqle.getMessage());
121         }catch(Throwable JavaDoc t){
122             throw new BLException(t.getMessage());
123         }
124         
125     }
126     
127     
128
129     public TopicVO getTopic(int id) throws BLException{
130         //news topic
131
TopicVO mod=null;
132         try{
133             TopicDAO dao=new TopicDAO();
134             mod=dao.getTopicById(id);
135         }catch(SQLException JavaDoc sqle){
136             throw new DBException(sqle.getMessage());
137         }catch(Throwable JavaDoc t){
138             throw new BLException(t.getMessage());
139         }
140         return mod;
141     }
142     public TopicVO getLastTopicByFId(int id) throws BLException{
143         TopicVO mod=null;
144         if(ftopicCache.containsKey("TopicBL.getLastTopicByFId-"+id)){
145             mod=(TopicVO)ftopicCache.get("TopicBL.getLastTopicByFId-"+id);
146             return mod;
147         }
148         
149         try{
150             TopicDAO dao=new TopicDAO();
151             mod=dao.getLastTopicByFId(id);
152             this.fillTopic(mod);
153         }catch(SQLException JavaDoc sqle){
154             throw new DBException(sqle.getMessage());
155         }catch(Throwable JavaDoc t){
156             throw new BLException(t.getMessage());
157         }
158         if(mod!=null)
159             ftopicCache.put("TopicBL.getLastTopicByFId-"+id,mod,7200);
160         return mod;
161     }
162     
163     public TopicVO getForumTopicByTId(int id) throws BLException{
164         TopicVO mod=null;
165         if(ftopicCache.containsKey("TopicBL.getForumTopicByTId-"+id)){
166             mod=(TopicVO)ftopicCache.get("TopicBL.getForumTopicByTId-"+id);
167             return mod;
168         }
169         try{
170             TopicDAO dao=new TopicDAO();
171             mod=dao.getTopicById(id);
172             this.fillTopic(mod);
173         }catch(SQLException JavaDoc sqle){
174             throw new DBException(sqle.getMessage());
175         }catch(Throwable JavaDoc t){
176             throw new BLException(t.getMessage());
177         }
178         if(mod!=null)
179             ftopicCache.put("TopicBL.getForumTopicByTId-"+id,mod,7200);
180         return mod;
181     }
182     
183     public void setLastModTime(TopicVO t)throws BLException{
184         ICommentBL cbl=(ICommentBL)MiddleFactory.getBL("ICommentBL");
185         
186         CommentVO com=null;
187         com=cbl.getLastForumCommentByTId(t.getTId());
188         if(com==null){
189             t.setLastModTime(t.getCreationTime());
190         }else{
191             t.setLastModTime(com.getTime() );
192         }
193     }
194     
195     public TopicVO fillTopic(TopicVO t) throws BLException{
196         if(t==null)
197             return t;
198         //TopicVO mod=null;
199
//if(ftopicCache.containsKey("TopicBL.fillTopic-"+t.getTId())){
200
// mod=(TopicVO)ftopicCache.get("TopicBL.fillTopic-"+t.getTId());
201
// return mod;
202
//}
203

204         IUserBL ubl=(IUserBL)MiddleFactory.getBL("IUserBL");
205         ICommentBL cbl=(ICommentBL)MiddleFactory.getBL("ICommentBL");
206         t.setComments(cbl.getForumCommentsByXId(t.getTId()));
207         t.setAuthor(ubl.getUserByID(t.getELink()));
208         this.setLastModTime(t);
209         //ftopicCache.put("TopicBL.fillTopic-"+t.getTId(),t,7200);
210
return t;
211     }
212     
213     public TopicVO getTopicByName(String JavaDoc name) throws BLException{
214         //admin topic no forum topic
215
TopicVO mod=null;
216         try{
217             TopicDAO dao=new TopicDAO();
218             mod=dao.getTopicByName(name);
219         }catch(SQLException JavaDoc sqle){
220             throw new DBException(sqle.getMessage());
221         }catch(Throwable JavaDoc t){
222             throw new BLException(t.getMessage());
223         }
224         return mod;
225     }
226     
227     public TopicVO getTopicByImage(String JavaDoc name) throws BLException{
228         TopicVO mod=null;
229         try{
230             TopicDAO dao=new TopicDAO();
231             mod=dao.getTopicByImage(name);
232         }catch(SQLException JavaDoc sqle){
233             throw new DBException(sqle.getMessage());
234         }catch(Throwable JavaDoc t){
235             throw new BLException(t.getMessage());
236         }
237         return mod;
238     }
239     
240     public ArrayList JavaDoc getAllTopics(String JavaDoc type) throws BLException{
241         ArrayList JavaDoc list=null;
242         try{
243             TopicDAO dao=new TopicDAO();
244             list=dao.getAllTopics(type);
245             
246         }catch(SQLException JavaDoc sqle){
247             throw new DBException(sqle.getMessage());
248         }catch(Throwable JavaDoc t){
249             throw new BLException(t.getMessage());
250         }
251         return list;
252     }
253     
254     public ArrayList JavaDoc getTopicsByNews(NewsVO n) throws BLException{
255         ArrayList JavaDoc list=null;
256         try{
257             TopicDAO dao=new TopicDAO();
258             list=dao.getTopicsByNews(n);
259             
260         }catch(SQLException JavaDoc sqle){
261             throw new DBException(sqle.getMessage());
262         }catch(Throwable JavaDoc t){
263             throw new BLException(t.getMessage());
264         }
265         return list;
266     }
267     
268     public CountArrayList getTopicsByFId(int fid) throws BLException{
269         CountArrayList list=null;
270         if(ftopicCache.containsKey("TopicBL.getTopicsByFId-"+fid)){
271             list=(CountArrayList)ftopicCache.get("TopicBL.getTopicsByFId-"+fid);
272             return list;
273         }
274         try{
275             TopicDAO dao=new TopicDAO();
276             list=dao.getTopicsByFId(fid);
277             
278         }catch(SQLException JavaDoc sqle){
279             throw new DBException(sqle.getMessage());
280         }catch(Throwable JavaDoc t){
281             throw new BLException(t.getMessage());
282         }
283         if(list!=null)
284             ftopicCache.put("TopicBL.getTopicsByFId-"+fid,list,7200);
285         return list;
286     }
287     
288     public CountArrayList getForumTopicsByText(String JavaDoc text) throws BLException{
289         CountArrayList list=null;
290         if(ftopicCache.containsKey("TopicBL.getForumTopicsByTex-"+text)){
291             list=(CountArrayList)ftopicCache.get("TopicBL.getForumTopicsByTex-"+text);
292             return list;
293         }
294         ArrayList JavaDoc ll=null,aux=null;
295         ll=new ArrayList JavaDoc();
296         String JavaDoc [] wordsA=TextUtil.tokenize(text,",-;. ");
297         
298         try{
299             TopicDAO dao=new TopicDAO();
300             for(int i=0;i<wordsA.length;i++){
301                 list=dao.getForumTopicsByText(wordsA[i]);
302                 aux=new ArrayList JavaDoc(list);
303                 ll.addAll(aux);
304                 
305             }
306             
307             ll=MagicCollections.semplifyAndSort(ll,new ForumTopicComparator());
308             
309         }catch(SQLException JavaDoc sqle){
310             throw new DBException(sqle.getMessage());
311         }catch(Throwable JavaDoc t){
312             throw new BLException(t.getMessage());
313         }
314         list=new CountArrayList(ll);
315         
316         if(list!=null)
317             ftopicCache.put("TopicBL.getForumTopicsByTex-"+text,list,7200);
318         return list;
319     }
320     
321 }
Popular Tags