KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.javabb.transaction;
2
3 import java.util.Collections JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.HashSet JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.Set JavaDoc;
10
11 import org.javabb.component.VelocityTemplate;
12 import org.javabb.dao.DAOConstants;
13 import org.javabb.dao.entity.IPostDAO;
14 import org.javabb.dao.entity.ITopicDAO;
15 import org.javabb.infra.Configuration;
16 import org.javabb.infra.ConfigurationFactory;
17 import org.javabb.infra.Constants;
18 import org.javabb.infra.Email;
19 import org.javabb.infra.Paging;
20 import org.javabb.infra.UserContext;
21 import org.javabb.lucene.index.Indexer;
22 import org.javabb.vo.AnswerNotify;
23 import org.javabb.vo.Forum;
24 import org.javabb.vo.Post;
25 import org.javabb.vo.Topic;
26 import org.javabb.vo.User;
27
28 /*
29  * Copyright 2004 JavaFree.org
30  *
31  * Licensed under the Apache License, Version 2.0 (the "License");
32  * you may not use this file except in compliance with the License.
33  * You may obtain a copy of the License at
34  *
35  * http://www.apache.org/licenses/LICENSE-2.0
36  *
37  * Unless required by applicable law or agreed to in writing, software
38  * distributed under the License is distributed on an "AS IS" BASIS,
39  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
40  * See the License for the specific language governing permissions and
41  * limitations under the License.
42  */

43
44 /**
45  * $Id: PostTransaction.java,v 1.34.2.2.6.4 2006/08/28 21:00:22 daltoncamargo Exp $
46  *
47  * @author Dalton Camargo - <a HREF="mailto:dalton@javabb.org">dalton@javabb.org
48  * </a> <br>
49  * @author Ronald Tetsuo Miura
50  */

51 public class PostTransaction extends Transaction {
52
53     private IPostDAO _postDAO;
54
55     private ITopicDAO _topicDAO;
56
57     private UserTransaction _userTransaction;
58
59     /**
60      * @param userTransaction
61      * the new userTransaction value
62      */

63     public void setUserTransaction(UserTransaction userTransaction) {
64         this._userTransaction = userTransaction;
65     }
66
67     /**
68      * @param postDAO
69      * the new postDAO value
70      */

71     public void setPostDAO(IPostDAO postDAO) {
72         this._postDAO = postDAO;
73     }
74
75     /**
76      * @param topicDAO
77      */

78     public void setTopicDAO(ITopicDAO topicDAO) {
79         this._topicDAO = topicDAO;
80     }
81     
82     private Indexer indexer;
83
84     public void setIndexer(Indexer indexer) {
85         this.indexer = indexer;
86     }
87     
88     // ///////////////////////////////////////////////////////////
89
// Business Methods
90
// ///////////////////////////////////////////////////////////
91

92     /**
93      * @param id
94      * @return post
95      */

96     public Post loadPost(Long JavaDoc id) {
97         return _postDAO.load(id);
98     }
99
100     /**
101      * Busca os posts pelo ID de um tópico
102      *
103      * @param topicId
104      * @param pageNumber
105      * @return - posts de um determinado tópico
106      */

107     public List JavaDoc findByTopic(Long JavaDoc topicId, int pageNumber) {
108
109         // PAGING ** Obtendo informações
110
int itemsPerPage = ConfigurationFactory.getConf().postsPage.intValue();
111         long postCountInTopic = _postDAO.countPostsByTopic(topicId);
112         int pageCount = Paging.getNroPages(itemsPerPage, postCountInTopic);
113         Paging.setPageList(pageNumber, pageCount);
114
115         return _postDAO.findByTopic(topicId, pageNumber, itemsPerPage);
116
117     }
118
119     /**
120      * @return id
121      */

122     public int findIdLastPost() {
123
124         Post p = _postDAO.findLastPost();
125         int i = -1;
126
127         if (p != null) {
128             i = p.getIdPost().intValue();
129         }
130
131         return i;
132
133     }
134
135     /**
136      * Busca o último post de um tópico
137      *
138      * @param topic
139      * @return post
140      */

141     public Post findbyTopicDesc(Topic topic) {
142
143         Post p = null;
144         List JavaDoc lst = _postDAO.findByTopicDesc(topic);
145
146         if (!lst.isEmpty()) {
147
148             p = (Post) lst.get(0);
149
150         }
151         return p;
152     }
153
154     public List JavaDoc findPagesByTopic(Topic topic) {
155         // PAGING
156
int rowsPerPage = ConfigurationFactory.getConf().postsPage.intValue();
157         long nroRecords = topic.getRespostas().intValue() + 1;
158         int totalPages = Paging.getNroPages(rowsPerPage, nroRecords);
159
160         return Paging.createQuickPaging(totalPages);
161     }
162
163     /**
164      * Busca o último post de um Fórum
165      *
166      * @param forum
167      * @return post
168      */

169     public Post findLastPostByForum(Forum forum) {
170
171         Post p = null;
172         List JavaDoc lst = _postDAO.findByForumDesc(forum);
173
174         if (!lst.isEmpty()) {
175
176             p = (Post) lst.get(0);
177
178         }
179
180         lst = null;
181
182         return p;
183
184     }
185
186     /**
187      * TODO METODO NAO SERA MAIS UTILIZADO NA LISTAGEM DA HOME, SO NA INCLUSAO
188      * DE UM NOVO POST (FORUMHIBERNATEDAO) Obtém a página do último post
189      *
190      * @param topic
191      * @return Numero da página do último post do forum
192      */

193     public Integer JavaDoc getPageOfLastPostByTopic(Topic topic) {
194
195         // PAGING ** Obtendo informações
196
int rowsPerPage = ConfigurationFactory.getConf().postsPage.intValue();
197         long nroRecords = _postDAO.countPostsByTopic(topic.getIdTopic());
198         int totalPages = Paging.getNroPages(rowsPerPage, nroRecords);
199
200         return new Integer JavaDoc(totalPages);
201
202     }
203
204     /**
205      * @param forum
206      * @return count
207      */

208     public Integer JavaDoc findCountOfPostsByForum(Forum forum) {
209         Integer JavaDoc number = _postDAO.findCountOfPostsByForum(forum);
210         if (number == null) {
211             number = new Integer JavaDoc(0);
212         }
213         return number;
214     }
215
216     /**
217      * Verifica se o usuário tem privilégios para excluir o post
218      *
219      * @param post
220      * @return result
221      */

222     public boolean canDeletePost(Post post) {
223
224         if (!UserContext.getContext().isAuthenticated()) {
225             return false;
226         }
227
228         User user = UserContext.getContext().getUser();
229
230         if (user.isAdministrator()) {
231             return true;
232         }
233         post = loadPost(post.getId());
234
235         return (post.getUser().getId().equals(user.getId()));
236     }
237
238     /**
239      * @param topic
240      */

241     public void deleteAllPostsByTopic(Topic topic) {
242         List JavaDoc posts = this.findByTopic(topic.getIdTopic(), 1);
243         Iterator JavaDoc it = posts.iterator();
244         while (it.hasNext()) {
245             Post post = (Post) it.next();
246             _userTransaction.subNumberMsgUser(post.getUser().getIdUser());
247         }
248         _topicDAO.deleteAllPostOfTopic(topic.getId());
249     }
250
251     /**
252      * @param topic
253      * @return list
254      */

255     public List JavaDoc listPostsByTopicRev(Topic topic) {
256         List JavaDoc posts = _postDAO.findByTopic(topic.getId(),
257                 DAOConstants.ALL_PAGES, 0);
258         Collections.reverse(posts);
259         return posts;
260     }
261
262     /**
263      * @param userId
264      * @return list
265      */

266     public List JavaDoc listPostsByUser(Long JavaDoc userId, int pageNumber) {
267
268         // PAGING ** Obtendo informações
269
int itemsPerPage = ConfigurationFactory.getConf().postsPage.intValue();
270         long postsCount = _postDAO.countPostsByUser(userId);
271         int totalPages = Paging.getNroPages(itemsPerPage, postsCount);
272         Paging.setPageList(pageNumber, totalPages);
273
274         List JavaDoc posts = _postDAO.findByUser(userId, pageNumber, itemsPerPage);
275         return posts;
276     }
277
278     /**
279      * @return list
280      */

281     public List JavaDoc listUnAnswaredPosts(int pageNumber) {
282         // PAGING ** Obtendo informações
283
int itemsPerPage = ConfigurationFactory.getConf().postsPage.intValue();
284         return _postDAO.findUnanswered(pageNumber, itemsPerPage);
285     }
286
287     /**
288      * @param query
289      * @return post list
290      */

291     public List JavaDoc findByQuery(String JavaDoc query, int pageNumber) {
292
293         if ((query == null) && query.trim().equals("")) {
294             throw new IllegalArgumentException JavaDoc(
295                     "You should fill the query field");
296         }
297         int itemsPerPage = ConfigurationFactory.getConf().getTopicsPage().intValue();
298         return _postDAO.search(query, pageNumber, itemsPerPage);
299     }
300
301     /**
302      * Number of rows found into Lucene index
303      * @param query - keywords to search rows
304      * @return number of rows
305      */

306     public int getTotalRowsOfLucene(String JavaDoc query){
307         return _postDAO.getTotalRowsOfLucene(query);
308     }
309     
310     
311     /**
312      * @param post
313      */

314     public void updatePost(Post post) {
315         _postDAO.update(post);
316     }
317
318     /**
319      * Search for all last posts
320      *
321      * @return
322      */

323     public List JavaDoc findLasPosts() {
324         return _postDAO.findLastPosts(new Configuration().getTopicsPage()
325                 .intValue());
326     }
327     
328     /**
329      * Search for all last posts
330      * @param limit
331      * @return
332      */

333     public List JavaDoc findAllByTopicDesc(Topic topic) {
334         return _postDAO.findByTopicDesc(topic);
335     }
336     
337     public List JavaDoc findInPosts(final String JavaDoc query, final int page){
338         // PAGING ** Getting informations
339
/*int itemsPerPage = ConfigurationFactory.getConf().postsPage.intValue();
340         long postsCount = _postDAO.countUnanswered();
341         int totalPages = Paging.getNroPages(itemsPerPage, postsCount);
342         Paging.setPageList(page, totalPages);*/

343         
344         return _postDAO.search(query, page);
345     }
346
347     /**
348      * Send an email notification to User if the notify flag is set
349      *
350      * @param topic
351      * @throws Exception
352      */

353     public void notifyUserTopicByMail(Topic topic) throws Exception JavaDoc {
354         long userId = UserContext.getContext().getUser().getIdUser()
355                 .longValue();
356
357         if (topic != null && topic.getNotifyMe() != null) {
358             if (topic.getNotifyMe().intValue() == 1
359                     && topic.getUser().getIdUser().longValue() != userId) {
360                 Configuration conf = new Configuration();
361
362                 String JavaDoc bodyMail = conf.emailNofityTopic
363                         .replaceAll("\n", "<br>")
364                         + "<br><br>"
365                         + "\"<b>"
366                         + topic.getTitleTopic()
367                         + "</b>\""
368                         + "<br><Br> "
369                         + "<b>Link:</b> <a HREF=\""
370                         + conf.domain
371                         + "viewtopic.jbb?t="
372                         + topic.getIdTopic()
373                         + "\">"
374                         + conf.domain
375                         + "viewtopic.jbb?t="
376                         + topic.getIdTopic();
377
378                 Email.sendMail(conf.adminMail, topic.getUser().getEmail(),
379                         conf.forumName, bodyMail, true);
380             }
381         }
382     }
383
384     /**
385      * Send mail to all watch users of this topic
386      */

387     public void nofityWatchUsers(Topic topic, String JavaDoc url, String JavaDoc message1_i18n,
388             String JavaDoc message2_i18n, String JavaDoc topic_i18n, String JavaDoc watch_i18n) {
389
390         // Getting the userId of user logged
391
long userId = UserContext.getContext().getUser().getIdUser()
392                 .longValue();
393
394         // Loading topic informations
395
topic = _topicDAO.load(topic.getIdTopic());
396
397         //Verify if this topic has users watching it
398
if (topic.getAnswerNotifies() != null) {
399             Configuration conf = new Configuration();
400
401             Map JavaDoc mailMap = new HashMap JavaDoc();
402             mailMap.put("conf", conf);
403             mailMap.put("message1", message1_i18n);
404             mailMap.put("message2", message2_i18n);
405             mailMap.put("topicId", topic.getIdTopic());
406             mailMap.put("topicName", topic.getTitleTopic());
407             mailMap.put("url", url);
408             mailMap.put("topic", topic_i18n);
409
410             String JavaDoc message = VelocityTemplate.makeTemplate(mailMap,
411                     Constants.watchTopicTemplate);
412
413             Iterator JavaDoc it = topic.getAnswerNotifies().iterator();
414             Set JavaDoc users = new HashSet JavaDoc();
415             while (it.hasNext()) {
416                 AnswerNotify answer = (AnswerNotify) it.next();
417                 User user = answer.getUser();
418                 //Dont send mail to current user post
419
if (user.getIdUser().longValue() != userId) {
420                     users.add(user.getEmail());
421                 }
422             }
423
424             String JavaDoc subject = conf.forumName + " - " + watch_i18n + " - "
425                     + topic.getTitleTopic();
426
427             it = users.iterator();
428             while (it.hasNext()) {
429                 String JavaDoc mail = (String JavaDoc) it.next();
430                 Email.sendMail(conf.adminMail, mail, subject, message, true);
431             }
432         }
433     }
434     
435     public void indexPost(Post post){
436         indexer.index(post);
437     }
438     
439     /**
440      * @param post
441      * @return
442      */

443     public Long JavaDoc createPost(Post post) {
444         return _postDAO.create(post);
445     }
446
447     /**
448      * @param postId
449      */

450     public void deletePost(Long JavaDoc postId) {
451         _postDAO.delete(postId);
452     }
453 }
Popular Tags