1 2 23 package com.geinuke.bizlogic; 24 25 import java.sql.SQLException ; 26 import java.util.ArrayList ; 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 sqle){ 59 throw new DBException(sqle.getMessage()); 60 }catch(Throwable 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 sqle){ 73 throw new DBException(sqle.getMessage()); 74 }catch(Throwable t){ 75 throw new BLException(t.getMessage()); 76 } 77 78 } 79 80 public ArrayList getBlogPostsByUID(int uid) throws BLException{ 81 ArrayList list=null; 82 try{ 83 BlogDAO dao=new BlogDAO(); 84 list=dao.getBlogPostsByUID(uid); 85 this.fillBlogPosts(list); 86 }catch(SQLException sqle){ 87 throw new DBException(sqle.getMessage()); 88 }catch(Throwable t){ 89 throw new BLException(t.getMessage()); 90 } 91 return list; 92 } 93 94 95 96 public ArrayList getBlogPostsByText(String words) throws BLException{ 97 ArrayList list=null,aux=null; 98 String [] wordsA=TextUtil.tokenize(words,",-;. "); 99 list=new ArrayList (); 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 sqle){ 110 throw new DBException(sqle.getMessage()); 111 }catch(Throwable t){ 112 throw new BLException(t.getMessage()); 113 } 114 return list; 115 } 116 117 public ArrayList getBlogPostsByTitle(String words) throws BLException{ 118 ArrayList list=null,aux=null; 119 String [] wordsA=TextUtil.tokenize(words,",-;. "); 120 list=new ArrayList (); 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 sqle){ 131 throw new DBException(sqle.getMessage()); 132 }catch(Throwable t){ 133 throw new BLException(t.getMessage()); 134 } 135 return list; 136 } 137 138 139 protected void fillBlogPost(BlogPostVO bp) throws Exception { 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 list) throws Exception { 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 } 156 } 157 158 public ArrayList getBlogPostsByTitleOrText(String words) throws BLException{ 159 ArrayList list=null,aux=null; 160 String [] wordsA=TextUtil.tokenize(words,",-;. "); 161 list=new ArrayList (); 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 sqle){ 172 throw new DBException(sqle.getMessage()); 173 }catch(Throwable t){ 174 throw new BLException(t.getMessage()); 175 } 176 return list; 177 } 178 179 public ArrayList getEmptyBlogAuthors() throws BLException{ 180 ArrayList list=null; 181 try{ 182 UserDAO dao=new UserDAO(); 183 list=dao.getBlogAuthors(); 184 185 }catch(SQLException sqle){ 186 throw new DBException(sqle.getMessage()); 187 }catch(Throwable t){ 188 throw new BLException(t.getMessage()); 189 } 190 return list; 191 } 192 193 public ArrayList getBlogAuthors() throws BLException{ 194 ArrayList 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 sqle){ 213 throw new DBException(sqle.getMessage()); 214 }catch(Throwable 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 }catch(SQLException sqle){ 228 throw new DBException(sqle.getMessage()); 229 }catch(Throwable 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 sqle){ 252 throw new DBException(sqle.getMessage()); 253 }catch(Throwable t){ 254 throw new BLException(t.getMessage()); 255 } 256 return mod; 257 } 258 259 260 261 } 262 | Popular Tags |