KickJava   Java API By Example, From Geeks To Geeks.

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


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
28 import com.geinuke.dao.BlogDAO;
29 import com.geinuke.dao.UserDAO;
30 import com.geinuke.middle.IBlogBL;
31 import com.geinuke.middle.ICommentBL;
32 import com.geinuke.middle.MiddleFactory;
33 import com.geinuke.util.TextUtil;
34 import com.geinuke.util.collection.BlogPostVOComparator;
35 import com.geinuke.util.collection.MagicCollections;
36 import com.geinuke.vo.BlogAuthorVO;
37 import com.geinuke.vo.BlogPostVO;
38 import com.geinuke.vo.CommentVO;
39
40
41
42
43 public class BlogBL implements IBlogBL {
44     protected static IBlogBL instance=null;
45     
46     public BlogBL(){
47         
48     }
49     
50     
51     
52     public void insBlogPost(BlogPostVO u)throws BLException{
53         
54         try{
55             BlogDAO dao=new BlogDAO();
56             dao.insertBlogPost(u);
57             
58         }catch(SQLException JavaDoc sqle){
59             throw new DBException(sqle.getMessage());
60         }catch(Throwable JavaDoc t){
61             throw new BLException(t.getMessage());
62         }
63         
64     }
65     
66     public void delBlogPost(int bid)throws BLException{
67         
68         try{
69             BlogDAO dao=new BlogDAO();
70             dao.deleteBlogPostByBID(bid);
71             
72         }catch(SQLException JavaDoc sqle){
73             throw new DBException(sqle.getMessage());
74         }catch(Throwable JavaDoc t){
75             throw new BLException(t.getMessage());
76         }
77         
78     }
79     
80     public ArrayList JavaDoc getBlogPostsByUID(int uid) throws BLException{
81         ArrayList JavaDoc list=null;
82         try{
83             BlogDAO dao=new BlogDAO();
84             list=dao.getBlogPostsByUID(uid);
85             this.fillBlogPosts(list);
86         }catch(SQLException JavaDoc sqle){
87             throw new DBException(sqle.getMessage());
88         }catch(Throwable JavaDoc t){
89             throw new BLException(t.getMessage());
90         }
91         return list;
92     }
93     
94     
95     
96     public ArrayList JavaDoc getBlogPostsByText(String JavaDoc words) throws BLException{
97         ArrayList JavaDoc list=null,aux=null;
98         String JavaDoc [] wordsA=TextUtil.tokenize(words,",-;. ");
99         list=new ArrayList JavaDoc();
100         
101         try{
102             BlogDAO dao=new BlogDAO();
103             for(int i=0;i<wordsA.length;i++){
104                 aux=dao.getBlogPostsByText(wordsA[i]);
105                 list.addAll(aux);
106             }
107             list=MagicCollections.semplifyAndSort(list,new BlogPostVOComparator());
108             this.fillBlogPosts(list);
109         }catch(SQLException JavaDoc sqle){
110             throw new DBException(sqle.getMessage());
111         }catch(Throwable JavaDoc t){
112             throw new BLException(t.getMessage());
113         }
114         return list;
115     }
116     
117     public ArrayList JavaDoc getBlogPostsByTitle(String JavaDoc words) throws BLException{
118         ArrayList JavaDoc list=null,aux=null;
119         String JavaDoc [] wordsA=TextUtil.tokenize(words,",-;. ");
120         list=new ArrayList JavaDoc();
121         
122         try{
123             BlogDAO dao=new BlogDAO();
124             for(int i=0;i<wordsA.length;i++){
125                 aux=dao.getBlogPostsByTitle(wordsA[i]);
126                 list.addAll(aux);
127             }
128             list=MagicCollections.semplifyAndSort(list,new BlogPostVOComparator());
129             this.fillBlogPosts(list);
130         }catch(SQLException JavaDoc sqle){
131             throw new DBException(sqle.getMessage());
132         }catch(Throwable JavaDoc t){
133             throw new BLException(t.getMessage());
134         }
135         return list;
136     }
137     
138     
139     protected void fillBlogPost(BlogPostVO bp) throws Exception JavaDoc{
140         
141         bp.setBlogAuthor(this.getBlogAuthorByBID(bp.getBid()));
142         
143         ICommentBL cbl=(ICommentBL)MiddleFactory.getBL("ICommentBL");
144         bp.setComments(cbl.getBlogCommentsTreeByXId(bp.getBid()));
145     }
146     
147     protected void fillBlogPosts(ArrayList JavaDoc list) throws Exception JavaDoc{
148         BlogPostVO bp=null;
149         
150         if(list!=null)
151             for(int i=0;i<list.size();i++){
152                 bp=(BlogPostVO)list.get(i);
153                 this.fillBlogPost(bp);
154                 //bp.setBlogAuthor(this.getBlogAuthorByBID(bp.getBid()));
155
}
156     }
157     
158     public ArrayList JavaDoc getBlogPostsByTitleOrText(String JavaDoc words) throws BLException{
159         ArrayList JavaDoc list=null,aux=null;
160         String JavaDoc [] wordsA=TextUtil.tokenize(words,",-;. ");
161         list=new ArrayList JavaDoc();
162         
163         try{
164             BlogDAO dao=new BlogDAO();
165             for(int i=0;i<wordsA.length;i++){
166                 aux=dao.getBlogPostsByTitleOrText(wordsA[i]);
167                 list.addAll(aux);
168             }
169             list=MagicCollections.semplifyAndSort(list,new BlogPostVOComparator());
170             this.fillBlogPosts(list);
171         }catch(SQLException JavaDoc sqle){
172             throw new DBException(sqle.getMessage());
173         }catch(Throwable JavaDoc t){
174             throw new BLException(t.getMessage());
175         }
176         return list;
177     }
178     
179     public ArrayList JavaDoc getEmptyBlogAuthors() throws BLException{
180         ArrayList JavaDoc list=null;
181         try{
182             UserDAO dao=new UserDAO();
183             list=dao.getBlogAuthors();
184             
185         }catch(SQLException JavaDoc sqle){
186             throw new DBException(sqle.getMessage());
187         }catch(Throwable JavaDoc t){
188             throw new BLException(t.getMessage());
189         }
190         return list;
191     }
192     
193     public ArrayList JavaDoc getBlogAuthors() throws BLException{
194         ArrayList JavaDoc list=null;
195         list=this.getEmptyBlogAuthors();
196         BlogAuthorVO ba=null;
197         if(list!=null)
198             for(int i=0;i<list.size();i++){
199                 ba=(BlogAuthorVO)list.get(i);
200                 ba.setLastPost(this.getLastBlogPostByUID(ba.getId()));
201             }
202         
203         return list;
204     }
205     
206     public BlogAuthorVO getBlogAuthorByBID(int bid)throws BLException{
207         BlogAuthorVO user=null;
208         user=new BlogAuthorVO();
209         try{
210             UserDAO dao=new UserDAO();
211             user=dao.getBlogAuthorByBID(bid);
212         }catch(SQLException JavaDoc sqle){
213             throw new DBException(sqle.getMessage());
214         }catch(Throwable JavaDoc t){
215             throw new BLException(t.getMessage());
216         }
217         return user;
218     }
219     
220     public BlogPostVO getBlogPostByBID(int bid) throws BLException{
221         BlogPostVO mod=null;
222         try{
223             BlogDAO dao=new BlogDAO();
224             mod=dao.getBlogPostByBID(bid);
225             this.fillBlogPost(mod);
226             //mod.setBlogAuthor(this.getBlogAuthorByBID(bid));
227
}catch(SQLException JavaDoc sqle){
228             throw new DBException(sqle.getMessage());
229         }catch(Throwable JavaDoc t){
230             throw new BLException(t.getMessage());
231         }
232         return mod;
233     }
234     
235     
236     public BlogPostVO getBlogPostByCommentCId(int cid)throws BLException {
237         BlogPostVO n=null;
238         CommentVO c=null;
239         ICommentBL cbl=(ICommentBL)MiddleFactory.getBL("ICommentBL");
240         c=cbl.getBlogCommentByCId(cid);
241         return this.getBlogPostByBID(c.getXId());
242         
243     }
244     
245     
246     public BlogPostVO getLastBlogPostByUID(int uid) throws BLException{
247         BlogPostVO mod=null;
248         try{
249             BlogDAO dao=new BlogDAO();
250             mod=dao.getLastBlogPostByUID(uid);
251         }catch(SQLException JavaDoc sqle){
252             throw new DBException(sqle.getMessage());
253         }catch(Throwable JavaDoc t){
254             throw new BLException(t.getMessage());
255         }
256         return mod;
257     }
258     
259     
260     
261 }
262
Popular Tags