1 2 23 package com.geinuke.bizlogic; 24 25 import java.sql.SQLException ; 26 import java.util.ArrayList ; 27 import java.util.GregorianCalendar ; 28 import java.util.HashMap ; 29 import java.util.List ; 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 getAllNews() throws BLException { 57 ArrayList ll=null; 58 try{ 59 NewsDAO dao=new NewsDAO(); 60 List list=dao.getAllNews(); 61 62 ll=new ArrayList (list); 63 }catch(SQLException sqle){ 64 throw new DBException(sqle.getMessage()); 65 }catch(Throwable t){ 66 throw new BLException(t.getMessage()); 67 } 68 69 return ll; 70 } 71 72 public ArrayList getEnabledNews() throws BLException { 73 ArrayList ll=null; 74 try{ 75 NewsDAO dao=new NewsDAO(); 76 List list=dao.getEnabledNews(); 77 78 ll=new ArrayList (list); 79 }catch(SQLException sqle){ 80 throw new DBException(sqle.getMessage()); 81 }catch(Throwable t){ 82 throw new BLException(t.getMessage()); 83 } 84 85 return ll; 86 } 87 88 89 public ArrayList getNewsByTId(int tid,boolean full) throws BLException { 90 ArrayList ll=null,aux=null; 91 NewsVO n=null; 92 try{ 93 NewsDAO dao=new NewsDAO(); 94 List list=dao.getNewsByTopic(tid); 95 96 ll=new ArrayList (list); 97 }catch(SQLException sqle){ 98 throw new DBException(sqle.getMessage()); 99 }catch(Throwable t){ 100 throw new BLException(t.getMessage()); 101 } 102 if(full){ 103 aux=new ArrayList (); 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 getNewsByTitleOrText(String text) throws BLException { 123 ArrayList ll=null,aux=null; 124 ll=new ArrayList (); 125 String [] 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 list=dao.getNewsByTitleOrText(wordsA[i]); 131 aux=new ArrayList (list); 132 ll.addAll(aux); 133 } 134 ll=MagicCollections.semplifyAndSort(ll,new NewsComparator()); 135 136 }catch(SQLException sqle){ 137 throw new DBException(sqle.getMessage()); 138 }catch(Throwable t){ 139 throw new BLException(t.getMessage()); 140 } 141 142 return ll; 143 } 144 145 public ArrayList getLimitedNewsByTId(int tid,int max,boolean full) throws BLException { 146 ArrayList ll=null; 147 ll=this.getNewsByTId(tid,full); 148 if(ll!=null && ll.size()>max){ 149 ll=new ArrayList ( ll.subList(0,max-1) ); 150 } 151 return ll; 152 } 153 154 public ArrayList getEnabledNewsByPeriod(Long [] bounds) throws BLException { 155 ArrayList ll=null; 156 ArrayList 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 list=dao.getEnabledNewsByPeriod(bounds); 166 NewsVO n=null; 167 ll=new ArrayList (list); 168 169 aux=new ArrayList (); 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 sqle){ 178 throw new DBException(sqle.getMessage()); 179 }catch(Throwable t){ 180 throw new BLException(t.getMessage()); 181 } 182 183 return ll; 184 } 185 186 public ArrayList getEnabledNewsArchive() throws BLException { 187 ArrayList ll=null; 188 ArrayList list=this.getEnabledNews(); 189 NewsVO n=null; 190 ll=new ArrayList (); 191 for(int i=list.size()-1;i>=0;i--){ 192 n=(NewsVO)list.get(i); 193 String 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 getEnabledFullNews(int qt) throws BLException { 201 ArrayList ll=null; 202 ArrayList 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 list=dao.getEnabledNews(); 212 if(list.size()>qt) 213 list=list.subList(0,qt-1); 214 ll=new ArrayList (list); 215 aux=new ArrayList (); 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 sqle){ 226 throw new DBException(sqle.getMessage()); 227 }catch(Throwable 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 sqle){ 245 throw new DBException(sqle.getMessage()); 246 }catch(Throwable 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 sqle){ 256 throw new DBException(sqle.getMessage()); 257 }catch(Throwable 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 cats=cBL.getCategoriesByNews(n); 274 n.getCategories().addAll(cats); 275 ArrayList topics=tBL.getTopicsByNews(n); 276 n.getTopics().addAll(topics); 277 n.setComments(coBL.getNewsCommentsTreeByXId(n.getNId())); 278 279 }catch(SQLException sqle){ 280 throw new DBException(sqle.getMessage()); 281 }catch(Throwable 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 getDisabledNews() throws BLException { 299 ArrayList ll=null; 300 try{ 301 NewsDAO dao=new NewsDAO(); 302 List list=dao.getDisabledNews(); 303 304 ll=new ArrayList (list); 305 }catch(SQLException sqle){ 306 throw new DBException(sqle.getMessage()); 307 }catch(Throwable t){ 308 throw new BLException(t.getMessage()); 309 } 310 311 return ll; 312 } 313 314 public HashMap getAutorList(ArrayList list) throws BLException { 315 HashMap map=null; 316 map=new HashMap (); 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 sqle){ 340 throw new DBException(sqle.getMessage()); 341 }catch(Throwable 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 sqle){ 356 throw new DBException(sqle.getMessage()); 357 }catch(Throwable t){ 358 throw new BLException(t.getMessage()); 359 } 360 361 } 362 363 } 364 | Popular Tags |