KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javabb > transaction > TopicTransaction


1 package org.javabb.transaction;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Date JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
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 /*
22  * Copyright 2004 JavaFree.org
23  *
24  * Licensed under the Apache License, Version 2.0 (the "License");
25  * you may not use this file except in compliance with the License.
26  * You may obtain a copy of the License at
27  *
28  * http://www.apache.org/licenses/LICENSE-2.0
29  *
30  * Unless required by applicable law or agreed to in writing, software
31  * distributed under the License is distributed on an "AS IS" BASIS,
32  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33  * See the License for the specific language governing permissions and
34  * limitations under the License.
35  */

36
37 /**
38  * $Id: TopicTransaction.java,v 1.37.8.5 2006/08/08 18:49:02 daltoncamargo Exp $
39  *
40  * @author Dalton Camargo - <a HREF="mailto:dalton@javabb.org">dalton@javabb.org
41  * </a> <br>
42  * @author Lucas Frare Teixeira - <a
43  * HREF="mailto:lucas@javabb.org">lucas@javabb.org </a> <br>
44  * @author Ronald Tetsuo Miura
45  */

46 public class TopicTransaction extends Transaction {
47     /**
48      * Sobrepõe o atributo dao definido na superclasse Este atributo está
49      * sobreposto para poder adquirir os métodos específicos desta entidade
50      */

51     private ITopicDAO _topicDAO;
52
53     private PostTransaction postTransaction;
54
55     private ForumTransaction forumTransaction;
56
57     private UserTransaction _userTransaction;
58
59    
60     /**
61      * @param topicDAO
62      * the new topicDAO value
63      */

64     public void setTopicDAO(ITopicDAO topicDAO) {
65         this._topicDAO = topicDAO;
66     }
67
68     /**
69      * @param postTransaction
70      * the new postTransaction value
71      */

72     public void setPostTransaction(PostTransaction postTransaction) {
73         this.postTransaction = postTransaction;
74     }
75
76     /**
77      * @param forumTransaction
78      * The forumTransaction to set.
79      */

80     public void setForumTransaction(ForumTransaction forumTransaction) {
81         this.forumTransaction = forumTransaction;
82     }
83
84     /**
85      * @param userTransaction
86      */

87     public void setUserTransaction(UserTransaction userTransaction) {
88         _userTransaction = userTransaction;
89     }
90     
91     /**
92      * @return list
93      */

94     public List JavaDoc findAll() {
95         return _topicDAO.findAll();
96     }
97
98     /**
99      * @param forumId
100      * @param pageNumber
101      * @return list
102      */

103     public List JavaDoc getLastTopicsByLastPosts(Long JavaDoc forumId, int pageNumber) {
104
105         // PAGING ** Obtendo informações
106
int itemsPerPage = ConfigurationFactory.getConf().topicsPage.intValue();
107         long topicCount = _topicDAO.countTopicsByForum(forumId, new Integer JavaDoc(0));
108         int totalPages = Paging.getNroPages(itemsPerPage, topicCount);
109         Paging.setPageList(pageNumber, totalPages);
110
111         List JavaDoc lstTopics = null;
112         List JavaDoc arrTopics = new ArrayList JavaDoc();
113         Topic topic = null;
114
115         //Stick
116
lstTopics = _topicDAO.findByForum(forumId, new Integer JavaDoc(2));
117         putArrayTopics(lstTopics, arrTopics);
118         lstTopics = null;
119
120         //Fixed
121
lstTopics = _topicDAO.findByForum(forumId, new Integer JavaDoc(1));
122         putArrayTopics(lstTopics, arrTopics);
123         lstTopics = null;
124
125         //Normal
126
lstTopics = _topicDAO.findByForum(forumId, new Integer JavaDoc(0), pageNumber,
127                 itemsPerPage);
128         putArrayTopics(lstTopics, arrTopics);
129         lstTopics = null;
130
131         return arrTopics;
132     }
133
134     private void putArrayTopics(List JavaDoc fromArrTopics, List JavaDoc 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     /**
145      * Adds the visualization counter, and returns the Topic.
146      *
147      * @param id
148      * @return topic
149      */

150     public Topic loadTopicForVisualization(Long JavaDoc id) {
151         Topic topic = _topicDAO.load(id);
152         topic.setVisualizacoes(new Integer JavaDoc(
153                 (topic.getVisualizacoes().intValue() + 1)));
154         return topic;
155     }
156
157     /**
158      * @return id
159      */

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     /**
172      * @param topicId
173      */

174     public void updateDatePostTopic(Long JavaDoc topicId) {
175         Topic t = _topicDAO.load(topicId);
176         t.setLastPostDate(new Date JavaDoc());
177         // topicDAO.update(t);
178
}
179
180     /**
181      * @param topicId
182      */

183     public void sumNumberReplysByTopic(Long JavaDoc topicId) {
184         Topic t = _topicDAO.load(topicId);
185
186         int resp = t.getRespostas().intValue();
187         resp++;
188         t.setRespostas(new Integer JavaDoc(resp));
189         // topicDAO.update(t);
190
// topicDAO.subOrSumMsgTopic(t, new Long(resp));
191
}
192
193     /**
194      * @param lng
195      */

196     public void subNumberReplysByTopic(Long JavaDoc lng) {
197         Topic t = _topicDAO.load(lng);
198
199         int resp = t.getRespostas().intValue();
200         resp--;
201         t.setRespostas(new Integer JavaDoc(resp));
202         // topicDAO.update(t);
203
// topicDAO.subOrSumMsgTopic(t, new Long(resp));
204
}
205
206     /**
207      * @param forum
208      * @return count
209      */

210     public Integer JavaDoc findCountOfTopicsByForum(Forum forum) {
211         Integer JavaDoc number = _topicDAO.findCountOfTopicsByForum(forum);
212
213         if (number == null) {
214             number = new Integer JavaDoc(0);
215         }
216
217         return number;
218     }
219
220     /**
221      * @param topic
222      */

223     public void lockTopic(Topic topic) {
224         _topicDAO.lockTopic(topic, new Integer JavaDoc(1));
225     }
226
227     /**
228      * @param topic
229      */

230     public void unlockTopic(Topic topic) {
231         _topicDAO.lockTopic(topic, new Integer JavaDoc(0));
232     }
233
234     /**
235      * @param topic
236      * @param idForumDest
237      */

238     public void moveTopic(Topic topic, Long JavaDoc idForumDest, String JavaDoc message,
239             String JavaDoc fFrom_i18n, String JavaDoc fTo_i18n, String JavaDoc topic_i18n)
240             throws Exception JavaDoc {
241
242         topic = _topicDAO.load(topic.getId());
243
244         String JavaDoc fromForumName = topic.getForum().getNome();
245         Long JavaDoc fromForumId = topic.getForum().getIdForum();
246
247         _topicDAO.moveTopic(topic, idForumDest);
248
249         Forum forumDest = forumTransaction.loadForum(idForumDest);
250
251         //Send mail to user
252
if (message != null && !"".equals(message)) {
253             Configuration conf = new Configuration();
254             String JavaDoc mailUser = topic.getUser().getEmail();
255
256             message = message.replaceAll("\n", "<br>");
257
258             Map JavaDoc mailMap = new HashMap JavaDoc();
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     /**
283      * @param id
284      * @return topic
285      */

286     public Topic loadTopic(Long JavaDoc id) {
287         return _topicDAO.load(id);
288     }
289
290     /**
291      * @param topic
292      * @return topic id
293      */

294     public Long JavaDoc createTopic(Topic topic) {
295
296         topic.getUser().setId(UserContext.getContext().getUser().getId());
297         topic.setVisualizacoes(new Integer JavaDoc(0));
298         topic.setRespostas(new Integer JavaDoc(0));
299         topic.setTitleTopic(Utils.replaceHTML(topic.getTitleTopic()));
300         topic.setLastPostDate(new Date JavaDoc());
301
302         if (topic.getNotifyMe() == null) {
303             topic.setNotifyMe(new Integer JavaDoc(0));
304         }
305
306         if (UserContext.getContext().getUser().getAdmin().intValue() != 1) {
307             topic.setTopicModel(new Integer JavaDoc(0));
308         }
309
310         topic.setDataTopico(new Date JavaDoc());
311         return _topicDAO.create(topic);
312     }
313
314     /**
315      * @param topicId
316      */

317     public void deleteTopic(Long JavaDoc topicId) {
318         _topicDAO.delete(topicId);
319     }
320
321     public void updateTopic(Topic topic) {
322         if (topic.getNotifyMe() == null) {
323             topic.setNotifyMe(new Integer JavaDoc(0));
324         }
325         _topicDAO.update(topic);
326     }
327
328     /**
329      * @param pageNumber
330      * @return unread posts list
331      */

332     public List JavaDoc listUnreadTopics(int pageNumber) {
333
334         //_userTransaction.updateVisitTimestamp();
335
Date JavaDoc lastVisitTimestamp = UserContext.getContext()
336                 .getLastVisitTimestamp();
337
338         // PAGING ** Obtendo informações
339
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 JavaDoc items = _topicDAO.findPostedAfter(lastVisitTimestamp, pageNumber,
345                 itemsPerPage);
346
347         List JavaDoc topics = new ArrayList JavaDoc();
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     /**
359      * Verify if the user has watch topic
360      *
361      * @param topicId
362      * @param userId
363      * @return 0 to false and 1 to true
364      */

365     public int isWatchTopic(Long JavaDoc topicId, Long JavaDoc userId) {
366         List JavaDoc watchTopics = _topicDAO.wathTopicByTopicUser(userId, topicId);
367         if (!watchTopics.isEmpty()) {
368             watchTopics = null;
369             return 1;
370         } else {
371             return 0;
372         }
373     }
374
375     /**
376      * Insert a new User watch topic
377      *
378      * @param topicId
379      * @param userId
380      */

381     public void insertWatchTopicUser(Long JavaDoc topicId, Long JavaDoc userId) {
382         if(isWatchTopic(topicId, userId) == 0){
383             _topicDAO.insertWatchTopicUser(topicId, userId);
384         }
385     }
386
387     /**
388      * Delete user watch topic
389      *
390      * @param topicId
391      * @param userId
392      */

393     public void deleteWatchTopicUser(Long JavaDoc topicId, Long JavaDoc userId) {
394         _topicDAO.deleteWatchTopicUser(topicId, userId);
395     }
396     
397     /**
398      * List all watch topics by UserId
399      *
400      * @param userId
401      * @return
402      */

403     public List JavaDoc watchTopicsByUserId(Long JavaDoc userId){
404         return _topicDAO.wathTopicByUser(userId);
405     }
406     
407     public List JavaDoc favoriteTopicsByUserId(Long JavaDoc userId){
408         return _topicDAO.favoriteTopicByUser(userId);
409     }
410     
411     /**
412      * List all watch topics by TopicId
413      *
414      * @param topicId
415      * @return
416      */

417     public List JavaDoc watchTopicsByTopic(Long JavaDoc topicId){
418         return _topicDAO.wathTopicByTopic(topicId);
419     }
420     
421     public List JavaDoc favoriteTopicsByTopic(Long JavaDoc topicId){
422         return _topicDAO.favoriteTopicByTopic(topicId);
423     }
424
425     public List JavaDoc favoriteTopics(){
426         return _topicDAO.favoriteTopics();
427     }
428     
429     public List JavaDoc findLastTopics(){
430         return _topicDAO.findLastTopics(new Configuration().getTopicsPage()
431                 .intValue());
432     }
433
434     
435     public int isFavoriteTopic(Long JavaDoc topicId, Long JavaDoc userId) {
436         List JavaDoc 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