KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.GregorianCalendar JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.List JavaDoc;
30
31 import com.geinuke.common.UserI;
32 import com.geinuke.dao.NewsDAO;
33 import com.geinuke.dao.UserDAO;
34 import com.geinuke.middle.ICategoryBL;
35 import com.geinuke.middle.ICommentBL;
36 import com.geinuke.middle.INewsBL;
37 import com.geinuke.middle.ISessionBL;
38 import com.geinuke.middle.ITopicBL;
39 import com.geinuke.middle.MiddleFactory;
40 import com.geinuke.util.TextUtil;
41 import com.geinuke.util.collection.MagicCollections;
42 import com.geinuke.util.collection.NewsComparator;
43 import com.geinuke.vo.CommentVO;
44 import com.geinuke.vo.NewsVO;
45 import com.geinuke.vo.UserVO;
46
47 public class NewsBL implements INewsBL {
48     protected static INewsBL instance=null;
49     
50     public NewsBL(){
51         
52     }
53     
54
55     
56     public ArrayList JavaDoc getAllNews() throws BLException {
57         ArrayList JavaDoc ll=null;
58         try{
59             NewsDAO dao=new NewsDAO();
60             List JavaDoc list=dao.getAllNews();
61             
62             ll=new ArrayList JavaDoc(list);
63         }catch(SQLException JavaDoc sqle){
64             throw new DBException(sqle.getMessage());
65         }catch(Throwable JavaDoc t){
66             throw new BLException(t.getMessage());
67         }
68         
69         return ll;
70     }
71     
72     public ArrayList JavaDoc getEnabledNews() throws BLException {
73         ArrayList JavaDoc ll=null;
74         try{
75             NewsDAO dao=new NewsDAO();
76             List JavaDoc list=dao.getEnabledNews();
77             
78             ll=new ArrayList JavaDoc(list);
79         }catch(SQLException JavaDoc sqle){
80             throw new DBException(sqle.getMessage());
81         }catch(Throwable JavaDoc t){
82             throw new BLException(t.getMessage());
83         }
84         
85         return ll;
86     }
87     
88     
89     public ArrayList JavaDoc getNewsByTId(int tid,boolean full) throws BLException {
90         ArrayList JavaDoc ll=null,aux=null;
91         NewsVO n=null;
92         try{
93             NewsDAO dao=new NewsDAO();
94             List JavaDoc list=dao.getNewsByTopic(tid);
95             
96             ll=new ArrayList JavaDoc(list);
97         }catch(SQLException JavaDoc sqle){
98             throw new DBException(sqle.getMessage());
99         }catch(Throwable JavaDoc t){
100             throw new BLException(t.getMessage());
101         }
102         if(full){
103             aux=new ArrayList JavaDoc();
104             for(int i=0;i<ll.size();i++){
105                 n=(NewsVO)ll.get(i);
106                 ITopicBL tbl=(ITopicBL)MiddleFactory.getBL("ITopicBL");
107                 ICategoryBL cbl=(ICategoryBL)MiddleFactory.getBL("ICategoryBL");
108                 ICommentBL ccbl=(ICommentBL)MiddleFactory.getBL("ICommentBL");
109                 
110                 n.setTopics( tbl.getTopicsByNews(n) );
111                 n.setCategories( cbl.getCategoriesByNews(n));
112                 n.setComments(ccbl.getNewsCommentsTreeByXId(n.getNId()));
113                 aux.add(n);
114             }
115         }else{
116             aux=ll;
117         }
118         
119         return aux;
120     }
121     
122     public ArrayList JavaDoc getNewsByTitleOrText(String JavaDoc text) throws BLException {
123         ArrayList JavaDoc ll=null,aux=null;
124         ll=new ArrayList JavaDoc();
125         String JavaDoc [] wordsA=TextUtil.tokenize(text,",-;. ");
126         NewsVO n=null;
127         try{
128             NewsDAO dao=new NewsDAO();
129             for(int i=0;i<wordsA.length;i++){
130                 List JavaDoc list=dao.getNewsByTitleOrText(wordsA[i]);
131                 aux=new ArrayList JavaDoc(list);
132                 ll.addAll(aux);
133             }
134             ll=MagicCollections.semplifyAndSort(ll,new NewsComparator());
135             
136         }catch(SQLException JavaDoc sqle){
137             throw new DBException(sqle.getMessage());
138         }catch(Throwable JavaDoc t){
139             throw new BLException(t.getMessage());
140         }
141                 
142         return ll;
143     }
144     
145     public ArrayList JavaDoc getLimitedNewsByTId(int tid,int max,boolean full) throws BLException {
146         ArrayList JavaDoc ll=null;
147         ll=this.getNewsByTId(tid,full);
148         if(ll!=null && ll.size()>max){
149             ll=new ArrayList JavaDoc( ll.subList(0,max-1) );
150         }
151         return ll;
152     }
153     
154     public ArrayList JavaDoc getEnabledNewsByPeriod(Long JavaDoc[] bounds) throws BLException {
155         ArrayList JavaDoc ll=null;
156         ArrayList JavaDoc aux=null;
157         try{
158             
159             ITopicBL tBL=(ITopicBL)MiddleFactory.getBL("ITopicBL");
160             ICategoryBL cBL=(ICategoryBL)MiddleFactory.getBL("ICategoryBL");
161             ICommentBL coBL=(ICommentBL)MiddleFactory.getBL("ICommentBL");
162             
163             
164             NewsDAO dao=new NewsDAO();
165             List JavaDoc list=dao.getEnabledNewsByPeriod(bounds);
166             NewsVO n=null;
167             ll=new ArrayList JavaDoc(list);
168             
169             aux=new ArrayList JavaDoc();
170             for(int i=0;i<ll.size();i++){
171                 n=(NewsVO)ll.get(i);
172                 n.setTopics( tBL.getTopicsByNews(n) );
173                 n.setCategories( cBL.getCategoriesByNews(n));
174                 n.setComments(coBL.getNewsCommentsTreeByXId(n.getNId()));
175                 aux.add(n);
176             }
177         }catch(SQLException JavaDoc sqle){
178             throw new DBException(sqle.getMessage());
179         }catch(Throwable JavaDoc t){
180             throw new BLException(t.getMessage());
181         }
182         
183         return ll;
184     }
185     
186     public ArrayList JavaDoc getEnabledNewsArchive() throws BLException {
187         ArrayList JavaDoc ll=null;
188         ArrayList JavaDoc list=this.getEnabledNews();
189         NewsVO n=null;
190         ll=new ArrayList JavaDoc();
191         for(int i=list.size()-1;i>=0;i--){
192             n=(NewsVO)list.get(i);
193             String JavaDoc aux=n.getDateTime().get(GregorianCalendar.MONTH)+"_"+n.getDateTime().get(GregorianCalendar.YEAR);
194             if(! ll.contains(aux))
195                 ll.add(0,aux);
196         }
197         return ll;
198     }
199     
200     public ArrayList JavaDoc getEnabledFullNews(int qt) throws BLException {
201         ArrayList JavaDoc ll=null;
202         ArrayList JavaDoc aux=null;
203         try{
204             NewsVO n=null;
205             
206             ITopicBL tBL=(ITopicBL)MiddleFactory.getBL("ITopicBL");
207             ICategoryBL cBL=(ICategoryBL)MiddleFactory.getBL("ICategoryBL");
208             ICommentBL coBL=(ICommentBL)MiddleFactory.getBL("ICommentBL");
209             
210             NewsDAO dao=new NewsDAO();
211             List JavaDoc list=dao.getEnabledNews();
212             if(list.size()>qt)
213                 list=list.subList(0,qt-1);
214             ll=new ArrayList JavaDoc(list);
215             aux=new ArrayList JavaDoc();
216             for(int i=0;i<ll.size();i++){
217                 n=(NewsVO)ll.get(i);
218                 n.setTopics( tBL.getTopicsByNews(n) );
219                 n.setCategories( cBL.getCategoriesByNews(n));
220                 n.setComments(coBL.getNewsCommentsTreeByXId(n.getNId()));
221                 aux.add(n);
222             }
223             
224             
225         }catch(SQLException JavaDoc sqle){
226             throw new DBException(sqle.getMessage());
227         }catch(Throwable JavaDoc t){
228             throw new BLException(t.getMessage());
229         }
230         
231         return aux;
232     }
233     
234     public void updateNews(NewsVO n)throws BLException{
235         try{
236             NewsDAO dao=new NewsDAO();
237             dao.updateNews(n);
238             if(n.getCategories()!=null){
239                 dao.updateNewsCategories(n);
240             }
241             if(n.getTopics()!=null){
242                 dao.updateNewsTopics(n);
243             }
244         }catch(SQLException JavaDoc sqle){
245             throw new DBException(sqle.getMessage());
246         }catch(Throwable JavaDoc t){
247             throw new BLException(t.getMessage());
248         }
249     }
250     
251     public void deleteNews(int nid)throws BLException{
252         try{
253             NewsDAO dao=new NewsDAO();
254             dao.deleteNews(nid);
255         }catch(SQLException JavaDoc sqle){
256             throw new DBException(sqle.getMessage());
257         }catch(Throwable JavaDoc t){
258             throw new BLException(t.getMessage());
259         }
260     }
261     
262     public NewsVO getNewsByNId(int id)throws BLException {
263         NewsVO n=null;
264         try{
265             
266             ITopicBL tBL=(ITopicBL)MiddleFactory.getBL("ITopicBL");
267             ICategoryBL cBL=(ICategoryBL)MiddleFactory.getBL("ICategoryBL");
268             ICommentBL coBL=(ICommentBL)MiddleFactory.getBL("ICommentBL");
269             
270             
271             NewsDAO dao=new NewsDAO();
272             n=dao.getNewsByNId(id);
273             ArrayList JavaDoc cats=cBL.getCategoriesByNews(n);
274             n.getCategories().addAll(cats);
275             ArrayList JavaDoc topics=tBL.getTopicsByNews(n);
276             n.getTopics().addAll(topics);
277             n.setComments(coBL.getNewsCommentsTreeByXId(n.getNId()));
278             
279         }catch(SQLException JavaDoc sqle){
280             throw new DBException(sqle.getMessage());
281         }catch(Throwable JavaDoc t){
282             throw new BLException(t.getMessage());
283         }
284         return n;
285     }
286     
287     public NewsVO getNewsByCommentCId(int cid)throws BLException {
288         NewsVO n=null;
289         CommentVO c=null;
290                 
291         ICommentBL coBL=(ICommentBL)MiddleFactory.getBL("ICommentBL");
292         c=coBL.getNewsCommentByCId(cid);
293         return this.getNewsByNId(c.getXId());
294         
295     }
296     
297     
298     public ArrayList JavaDoc getDisabledNews() throws BLException {
299         ArrayList JavaDoc ll=null;
300         try{
301             NewsDAO dao=new NewsDAO();
302             List JavaDoc list=dao.getDisabledNews();
303             
304             ll=new ArrayList JavaDoc(list);
305         }catch(SQLException JavaDoc sqle){
306             throw new DBException(sqle.getMessage());
307         }catch(Throwable JavaDoc t){
308             throw new BLException(t.getMessage());
309         }
310         
311         return ll;
312     }
313     
314     public HashMap JavaDoc getAutorList(ArrayList JavaDoc list) throws BLException {
315         HashMap JavaDoc map=null;
316         map=new HashMap JavaDoc();
317         try{
318             NewsDAO dao=new NewsDAO();
319             UserDAO uDao=new UserDAO();
320             int uid=-1;
321             UserVO wr=null;
322             for(int i=0;i<list.size();i++){
323                 uid=((NewsVO)list.get(i)).getUId();
324                 if(uid==UserI.ANONYMOUS_ID){
325                     wr=new UserVO();
326                     wr.setUName(UserI.ANONYMOUS);
327                     wr.setName(UserI.ANONYMOUS);
328                     map.put(uid+"",wr);
329                 }else if(! map.containsKey(""+uid) ){
330                     wr=uDao.getUserByID(uid);
331                     map.put(uid+"",wr);
332                 }
333                 
334             
335             }
336             
337             
338             
339         }catch(SQLException JavaDoc sqle){
340             throw new DBException(sqle.getMessage());
341         }catch(Throwable JavaDoc t){
342             throw new BLException(t.getMessage());
343         }
344         
345         return map;
346     }
347     
348     
349     public void insNews(NewsVO n)throws BLException{
350         
351         try{
352             NewsDAO dao=new NewsDAO();
353             dao.insertNews(n);
354             
355         }catch(SQLException JavaDoc sqle){
356             throw new DBException(sqle.getMessage());
357         }catch(Throwable JavaDoc t){
358             throw new BLException(t.getMessage());
359         }
360         
361     }
362     
363 }
364
Popular Tags