1 package org.javabb.transaction; 2 3 import java.util.ArrayList ; 4 import java.util.Date ; 5 import java.util.HashMap ; 6 import java.util.List ; 7 import java.util.Map ; 8 9 import org.javabb.component.VelocityTemplate; 10 import org.javabb.dao.entity.ITopicDAO; 11 import org.javabb.infra.Configuration; 12 import org.javabb.infra.ConfigurationFactory; 13 import org.javabb.infra.Constants; 14 import org.javabb.infra.Email; 15 import org.javabb.infra.Paging; 16 import org.javabb.infra.UserContext; 17 import org.javabb.infra.Utils; 18 import org.javabb.vo.Forum; 19 import org.javabb.vo.Topic; 20 21 36 37 46 public class TopicTransaction extends Transaction { 47 51 private ITopicDAO _topicDAO; 52 53 private PostTransaction postTransaction; 54 55 private ForumTransaction forumTransaction; 56 57 private UserTransaction _userTransaction; 58 59 60 64 public void setTopicDAO(ITopicDAO topicDAO) { 65 this._topicDAO = topicDAO; 66 } 67 68 72 public void setPostTransaction(PostTransaction postTransaction) { 73 this.postTransaction = postTransaction; 74 } 75 76 80 public void setForumTransaction(ForumTransaction forumTransaction) { 81 this.forumTransaction = forumTransaction; 82 } 83 84 87 public void setUserTransaction(UserTransaction userTransaction) { 88 _userTransaction = userTransaction; 89 } 90 91 94 public List findAll() { 95 return _topicDAO.findAll(); 96 } 97 98 103 public List getLastTopicsByLastPosts(Long forumId, int pageNumber) { 104 105 int itemsPerPage = ConfigurationFactory.getConf().topicsPage.intValue(); 107 long topicCount = _topicDAO.countTopicsByForum(forumId, new Integer (0)); 108 int totalPages = Paging.getNroPages(itemsPerPage, topicCount); 109 Paging.setPageList(pageNumber, totalPages); 110 111 List lstTopics = null; 112 List arrTopics = new ArrayList (); 113 Topic topic = null; 114 115 lstTopics = _topicDAO.findByForum(forumId, new Integer (2)); 117 putArrayTopics(lstTopics, arrTopics); 118 lstTopics = null; 119 120 lstTopics = _topicDAO.findByForum(forumId, new Integer (1)); 122 putArrayTopics(lstTopics, arrTopics); 123 lstTopics = null; 124 125 lstTopics = _topicDAO.findByForum(forumId, new Integer (0), pageNumber, 127 itemsPerPage); 128 putArrayTopics(lstTopics, arrTopics); 129 lstTopics = null; 130 131 return arrTopics; 132 } 133 134 private void putArrayTopics(List fromArrTopics, List toArrTopics) { 135 if (!fromArrTopics.isEmpty()) { 136 for (int i = 0; i < fromArrTopics.size(); i++) { 137 Topic topic = (Topic) fromArrTopics.get(i); 138 topic.setPagesPerTopic(postTransaction.findPagesByTopic(topic)); 139 toArrTopics.add(topic); 140 } 141 } 142 } 143 144 150 public Topic loadTopicForVisualization(Long id) { 151 Topic topic = _topicDAO.load(id); 152 topic.setVisualizacoes(new Integer ( 153 (topic.getVisualizacoes().intValue() + 1))); 154 return topic; 155 } 156 157 160 public int findIdLastTopic() { 161 Topic t = _topicDAO.findLastTopic(); 162 int i = -1; 163 164 if (t != null) { 165 i = t.getIdTopic().intValue(); 166 } 167 168 return i; 169 } 170 171 174 public void updateDatePostTopic(Long topicId) { 175 Topic t = _topicDAO.load(topicId); 176 t.setLastPostDate(new Date ()); 177 } 179 180 183 public void sumNumberReplysByTopic(Long topicId) { 184 Topic t = _topicDAO.load(topicId); 185 186 int resp = t.getRespostas().intValue(); 187 resp++; 188 t.setRespostas(new Integer (resp)); 189 } 192 193 196 public void subNumberReplysByTopic(Long lng) { 197 Topic t = _topicDAO.load(lng); 198 199 int resp = t.getRespostas().intValue(); 200 resp--; 201 t.setRespostas(new Integer (resp)); 202 } 205 206 210 public Integer findCountOfTopicsByForum(Forum forum) { 211 Integer number = _topicDAO.findCountOfTopicsByForum(forum); 212 213 if (number == null) { 214 number = new Integer (0); 215 } 216 217 return number; 218 } 219 220 223 public void lockTopic(Topic topic) { 224 _topicDAO.lockTopic(topic, new Integer (1)); 225 } 226 227 230 public void unlockTopic(Topic topic) { 231 _topicDAO.lockTopic(topic, new Integer (0)); 232 } 233 234 238 public void moveTopic(Topic topic, Long idForumDest, String message, 239 String fFrom_i18n, String fTo_i18n, String topic_i18n) 240 throws Exception { 241 242 topic = _topicDAO.load(topic.getId()); 243 244 String fromForumName = topic.getForum().getNome(); 245 Long fromForumId = topic.getForum().getIdForum(); 246 247 _topicDAO.moveTopic(topic, idForumDest); 248 249 Forum forumDest = forumTransaction.loadForum(idForumDest); 250 251 if (message != null && !"".equals(message)) { 253 Configuration conf = new Configuration(); 254 String mailUser = topic.getUser().getEmail(); 255 256 message = message.replaceAll("\n", "<br>"); 257 258 Map mailMap = new HashMap (); 259 mailMap.put("conf", conf); 260 mailMap.put("messageBody", message); 261 mailMap.put("topic", topic_i18n); 262 mailMap.put("topicName", topic.getTitleTopic()); 263 mailMap.put("topicId", topic.getIdTopic()); 264 mailMap.put("forumFrom", fFrom_i18n); 265 mailMap.put("forumFromId", fromForumId); 266 mailMap.put("forumFromName", fromForumName); 267 mailMap.put("forumTo", fTo_i18n); 268 mailMap.put("forumToId", forumDest.getIdForum()); 269 mailMap.put("forumToName", forumDest.getNome()); 270 271 message = VelocityTemplate.makeTemplate(mailMap, 272 Constants.moveTopicMailTemplate); 273 274 Email.sendMail(conf.adminMail, mailUser, conf.forumName, message, 275 true); 276 } 277 278 forumTransaction.refreshForum(idForumDest); 279 forumTransaction.refreshForum(topic.getForum().getId()); 280 } 281 282 286 public Topic loadTopic(Long id) { 287 return _topicDAO.load(id); 288 } 289 290 294 public Long createTopic(Topic topic) { 295 296 topic.getUser().setId(UserContext.getContext().getUser().getId()); 297 topic.setVisualizacoes(new Integer (0)); 298 topic.setRespostas(new Integer (0)); 299 topic.setTitleTopic(Utils.replaceHTML(topic.getTitleTopic())); 300 topic.setLastPostDate(new Date ()); 301 302 if (topic.getNotifyMe() == null) { 303 topic.setNotifyMe(new Integer (0)); 304 } 305 306 if (UserContext.getContext().getUser().getAdmin().intValue() != 1) { 307 topic.setTopicModel(new Integer (0)); 308 } 309 310 topic.setDataTopico(new Date ()); 311 return _topicDAO.create(topic); 312 } 313 314 317 public void deleteTopic(Long topicId) { 318 _topicDAO.delete(topicId); 319 } 320 321 public void updateTopic(Topic topic) { 322 if (topic.getNotifyMe() == null) { 323 topic.setNotifyMe(new Integer (0)); 324 } 325 _topicDAO.update(topic); 326 } 327 328 332 public List listUnreadTopics(int pageNumber) { 333 334 Date lastVisitTimestamp = UserContext.getContext() 336 .getLastVisitTimestamp(); 337 338 int itemsPerPage = ConfigurationFactory.getConf().topicsPage.intValue(); 340 long topicCount = _topicDAO.countPostedAfter(lastVisitTimestamp); 341 int totalPages = Paging.getNroPages(itemsPerPage, topicCount); 342 Paging.setPageList(pageNumber, totalPages); 343 344 List items = _topicDAO.findPostedAfter(lastVisitTimestamp, pageNumber, 345 itemsPerPage); 346 347 List topics = new ArrayList (); 348 for (int i = 0; i < items.size(); i++) { 349 Topic topic = (Topic) items.get(i); 350 topic.setPagesPerTopic(postTransaction.findPagesByTopic(topic)); 351 topics.add(topic); 352 } 353 items = null; 354 355 return topics; 356 } 357 358 365 public int isWatchTopic(Long topicId, Long userId) { 366 List watchTopics = _topicDAO.wathTopicByTopicUser(userId, topicId); 367 if (!watchTopics.isEmpty()) { 368 watchTopics = null; 369 return 1; 370 } else { 371 return 0; 372 } 373 } 374 375 381 public void insertWatchTopicUser(Long topicId, Long userId) { 382 if(isWatchTopic(topicId, userId) == 0){ 383 _topicDAO.insertWatchTopicUser(topicId, userId); 384 } 385 } 386 387 393 public void deleteWatchTopicUser(Long topicId, Long userId) { 394 _topicDAO.deleteWatchTopicUser(topicId, userId); 395 } 396 397 403 public List watchTopicsByUserId(Long userId){ 404 return _topicDAO.wathTopicByUser(userId); 405 } 406 407 public List favoriteTopicsByUserId(Long userId){ 408 return _topicDAO.favoriteTopicByUser(userId); 409 } 410 411 417 public List watchTopicsByTopic(Long topicId){ 418 return _topicDAO.wathTopicByTopic(topicId); 419 } 420 421 public List favoriteTopicsByTopic(Long topicId){ 422 return _topicDAO.favoriteTopicByTopic(topicId); 423 } 424 425 public List favoriteTopics(){ 426 return _topicDAO.favoriteTopics(); 427 } 428 429 public List findLastTopics(){ 430 return _topicDAO.findLastTopics(new Configuration().getTopicsPage() 431 .intValue()); 432 } 433 434 435 public int isFavoriteTopic(Long topicId, Long userId) { 436 List favoriteTopics = _topicDAO.favoriteTopicByTopicUser(userId, topicId); 437 if (!favoriteTopics.isEmpty()) { 438 favoriteTopics = null; 439 return 1; 440 } else { 441 return 0; 442 } 443 } 444 }
| Popular Tags
|