KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javabb > action > PostAction


1 package org.javabb.action;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Date JavaDoc;
5 import java.util.List JavaDoc;
6
7 import org.apache.commons.lang.StringUtils;
8 import org.javabb.action.infra.BaseAction;
9 import org.javabb.component.PostFormatter;
10 import org.javabb.component.UserFormatter;
11 import org.javabb.infra.UserContext;
12 import org.javabb.infra.Utils;
13 import org.javabb.transaction.BadWordTransaction;
14 import org.javabb.transaction.ForumTransaction;
15 import org.javabb.transaction.PostTransaction;
16 import org.javabb.transaction.TopicTransaction;
17 import org.javabb.transaction.UserTransaction;
18 import org.javabb.vo.Forum;
19 import org.javabb.vo.Post;
20 import org.javabb.vo.PostText;
21 import org.javabb.vo.Topic;
22 import org.javabb.vo.User;
23
24 import com.opensymphony.webwork.ServletActionContext;
25
26 /*
27  * Copyright 2004 JavaFree.org
28  *
29  * Licensed under the Apache License, Version 2.0 (the "License");
30  * you may not use this file except in compliance with the License.
31  * You may obtain a copy of the License at
32  *
33  * http://www.apache.org/licenses/LICENSE-2.0
34  *
35  * Unless required by applicable law or agreed to in writing, software
36  * distributed under the License is distributed on an "AS IS" BASIS,
37  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
38  * See the License for the specific language governing permissions and
39  * limitations under the License.
40  */

41
42 /**
43  * $Id: PostAction.java,v 1.43.2.1.4.2 2006/04/17 17:47:07 daltoncamargo Exp $
44  * @author Dalton Camargo - <a HREF="mailto:dalton@javabb.org">dalton@javabb.org </a> <br>
45  * @author Lucas Frare Teixeira - <a HREF="mailto:lucas@javabb.org">lucas@javabb.org </a> <br>
46  * @author Ronald Tetsuo Miura
47  */

48 public class PostAction extends BaseAction {
49     private PostText _post = new PostText();
50
51     private ForumTransaction _forumTransaction;
52
53     private PostTransaction _postTransaction;
54
55     private TopicTransaction topicTransaction;
56
57     private Topic _topic = new Topic();
58
59     private BadWordTransaction badWordTransaction;
60
61     private UserTransaction _userTransaction;
62
63     private PostFormatter postFormatter;
64     
65     private UserFormatter userFormatter;
66
67     private List JavaDoc _posts = new ArrayList JavaDoc();
68
69     private String JavaDoc quote;
70
71     private String JavaDoc whoQuote;
72
73     // ####################################################################
74
// Dependencies
75
// ####################################################################
76

77     /**
78      * @param forumTransaction The forumTransaction to set.
79      */

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

87     public void setUserTransaction(UserTransaction userTransaction) {
88         this._userTransaction = userTransaction;
89     }
90
91     /**
92      * @param topicTransaction the new topicTransaction value
93      */

94     public void setTopicTransaction(TopicTransaction topicTransaction) {
95         this.topicTransaction = topicTransaction;
96     }
97
98     /**
99      * @param postFormatter the new postFormatter value
100      */

101     public void setPostFormatter(PostFormatter postFormatter) {
102         this.postFormatter = postFormatter;
103     }
104
105     /**
106      * @param postTransaction the new postTransaction value
107      */

108     public void setPostTransaction(PostTransaction postTransaction) {
109         this._postTransaction = postTransaction;
110     }
111
112     /**
113      * @param badWordComponent the new badWordComponent value
114      */

115     public void setBadWordTransaction(BadWordTransaction badWordComponent) {
116         this.badWordTransaction = badWordComponent;
117     }
118
119     public void setUserFormatter(UserFormatter userFormatter) {
120         this.userFormatter = userFormatter;
121     }
122     
123     // ####################################################################
124
// Actions
125
// ####################################################################
126

127
128     /**
129      * @return Action status
130      */

131     public String JavaDoc listaPost() {
132         _posts = _postTransaction.findByTopic(_post.getTopic().getIdTopic(), getPage());
133         return SUCCESS;
134     }
135
136     /**
137      * This method is used as much as to reply a post as to quote some text.
138      * @return Action status
139      */

140     public String JavaDoc returnQuoteMsg() {
141         if (log.isDebugEnabled()) {
142             log.debug("whoQuote = '" + whoQuote + "'");
143         }
144         if (StringUtils.trimToNull(whoQuote) != null) {
145             Post tmpPost = new Post();
146             tmpPost = _postTransaction.loadPost(_post.getId());
147             quote = "[quote=\"" + whoQuote + "\"]" + tmpPost.getPostBody() + " [/quote]";
148             _post = (PostText) tmpPost;
149         }
150
151         return SUCCESS;
152     }
153
154     /**
155      * @return Action status
156      */

157     public String JavaDoc loadPost() {
158         if(_post.getIdPost() != null){
159             _postId = _post.getIdPost();
160         }
161         _post = (PostText) _postTransaction.loadPost(_postId);
162         return SUCCESS;
163     }
164
165     /**
166      * @return Action status
167      */

168     public String JavaDoc updatePost() {
169         Post alterPost = _postTransaction.loadPost(_post.getId());
170         User userLogado = UserContext.getContext().getUser();
171
172         // se o usuário não for administrador:
173
if (userLogado.getAdmin().equals(new Integer JavaDoc(0))) {
174             // se ele não for o dono da mensagem:
175
if (!alterPost.getUser().equals(userLogado)) {
176                 addActionError(getText("you_can_edit_yours_messages"));
177
178                 return ERROR;
179             }
180         }
181
182         alterPost.setSubject(_post.getSubject());
183         alterPost.setPostBody(_post.getPostBody());
184
185         alterPost.setSig(_post.getSig());
186         _postTransaction.updatePost(alterPost);
187
188         if(getUrl() != null && "topic".equals(getUrl())){
189             setUrl("viewtopic.jbb?t=" + _post.getTopic().getIdTopic());
190         } else {
191             setUrl("viewtopic.jbb?t=" + _post.getTopic().getIdTopic()
192                     + "&page="+ _page + "#" + _post.getIdPost());
193         }
194         
195
196         // Atualiza a informacao dos posts
197
_post.setIdPost(_post.getId());
198         _forumTransaction.refreshPost(_post.getId());
199
200         
201         
202         return SUCCESS;
203     }
204
205     /**
206      * @return Action status
207      */

208     public String JavaDoc updatePostTopic() {
209         Post alterPost = _postTransaction.loadPost(_post.getId());
210         User userLogado = UserContext.getContext().getUser();
211
212         // se o usuário não for administrador:
213
if (userLogado.getAdmin().equals(new Integer JavaDoc(0))) {
214             // se ele não for o dono da mensagem:
215
if (!alterPost.getUser().equals(userLogado)) {
216                 addActionError(getText("you_can_edit_yours_messages"));
217                 return ERROR;
218             }
219         }
220
221         alterPost.setSubject(_post.getSubject());
222         alterPost.setPostBody(_post.getPostBody());
223         alterPost.setSig(_post.getSig());
224         _postTransaction.updatePost(alterPost);
225
226         Topic alterTopic = topicTransaction.loadTopic(_post.getTopic().getId());
227         alterTopic.setTitleTopic(Utils.replaceHTML(_topic.getTitleTopic()));
228         alterTopic.setTopicModel(_post.getTopic().getTopicModel());
229         alterTopic.setNotifyMe(_post.getTopic().getNotifyMe());
230         
231         if(userLogado.getAdmin().intValue() != 1){
232             alterTopic.setTopicModel(new Integer JavaDoc(0));
233         }
234         
235         topicTransaction.updateTopic(alterTopic);
236         
237         setUrl("viewtopic.jbb?t=" + _post.getTopic().getIdTopic());
238
239         
240         // Atualiza a informacao dos posts
241
_post.setIdPost(_post.getId());
242         _forumTransaction.refreshPost(_post.getId());
243         // Atualiza a informaçao dos topicos
244
_forumTransaction.refreshTopic(_post.getTopic().getIdTopic());
245         
246         return SUCCESS;
247     }
248
249     /**
250      * @return Action status
251      */

252     public String JavaDoc insertPost() throws Exception JavaDoc{
253         Topic topic = _post.getTopic();
254         topic = topicTransaction.loadTopic(topic.getId());
255
256         // mark as unread
257
UserContext.getContext().setTopicUnread(topic.getId());
258
259         // Proteção contra flood... 7 segundos
260
long currTime = System.currentTimeMillis();
261         Long JavaDoc lastPost = (Long JavaDoc) getSessionAttribute("my_last_post");
262
263         if ((lastPost != null) && ((currTime - lastPost.longValue()) < 7000)) {
264             // 15 milisegundos
265
this.addActionError(this.getText("message_so_quickly"));
266             return ERROR;
267             
268         }
269         setSessionAttribute("my_last_post", new Long JavaDoc(currTime));
270
271         if ((topic.getTopicStatus() != null) && topic.getTopicStatus().equals(new Integer JavaDoc(1))) {
272             this.addActionError(this.getText("answer_locked_topic"));
273             return ERROR;
274         }
275
276         _post.setPostDate(new Date JavaDoc());
277         _post.setIp(ServletActionContext.getRequest().getRemoteAddr());
278         _post.setPost_state(new Integer JavaDoc(0));
279         _post.setPostBody(_post.getPostBody());
280
281         User up = UserContext.getContext().getUser();
282         _post.setUser(up);
283
284         Long JavaDoc idPost = _postTransaction.createPost(_post);
285
286         
287         // manutenção de resposta de posts
288
topicTransaction.sumNumberReplysByTopic(_post.getTopic().getIdTopic());
289         topicTransaction.updateDatePostTopic(_post.getTopic().getIdTopic());
290
291         // manutenção de mensagens do usuário
292
_userTransaction.sumNumberMsgUser(up.getIdUser());
293
294         // Getting number of page and setting for redirect
295
Integer JavaDoc currentPage = _postTransaction.getPageOfLastPostByTopic(topic);
296         setPage(currentPage.intValue());
297
298         int lastId = idPost.intValue();
299         int topicId = _post.getTopic().getIdTopic().intValue();
300         String JavaDoc url = "viewtopic.jbb?t=" + topicId + "&page=" + currentPage + "#" + lastId;
301         setUrl(url);
302
303         // Atualiza a informacao dos posts
304
_post.setIdPost(idPost);
305         _forumTransaction.refreshPost(_post.getId());
306         // Atualiza a informaçao dos topicos
307
_forumTransaction.refreshTopic(_post.getTopic().getId());
308
309 // Verify and send an email notification to User if the notify flag is set
310
_postTransaction.notifyUserTopicByMail(topic);
311         
312 // Send an email to all users that watch this topic
313
_postTransaction.nofityWatchUsers(topic, url, getText("watch_topics_message1"),
314                 getText("watch_topics_message2"), getText("topic"),
315                 getText("watch_topic"));
316         
317         return SUCCESS;
318     }
319
320     /**
321      * Insert a new topic and a new post
322      * @return Action status
323      */

324     public String JavaDoc insertTopicPost() {
325
326         Long JavaDoc idTopicInserted = topicTransaction.createTopic(_topic);
327
328         _post.getUser().setId(UserContext.getContext().getUser().getId());
329         _post.setPost_state(new Integer JavaDoc(0));
330         _post.setPostDate(new Date JavaDoc());
331         _post.setPostBody(_post.getPostBody());
332         _post.setSubject(badWordTransaction.verifyBadWords(_post.getSubject()));
333
334         Topic topic = topicTransaction.loadTopic(idTopicInserted);
335         topic.setTitleTopic(badWordTransaction.verifyBadWords(topic.getTitleTopic()));
336         topic.setTopicStatus(new Integer JavaDoc(0));
337         topic.setPageLastPost(new Integer JavaDoc(1));
338
339         _post.setTopic(topic);
340
341         Long JavaDoc lastPostId = _postTransaction.createPost(_post);
342         _topic.setLastPost(_postTransaction.loadPost(lastPostId));
343
344         // manutenção de mensagens do usuário
345
User up = UserContext.getContext().getUser();
346         _userTransaction.sumNumberMsgUser(up.getIdUser());
347
348         int topicId = _post.getTopic().getIdTopic().intValue();
349         setUrl("viewtopic.jbb?t=" + topicId + "#" + lastPostId.toString());
350
351         // Atualiza a informacao dos posts
352
_post.setIdPost(lastPostId);
353         _forumTransaction.refreshPost(_post.getId());
354         // Atualiza a informaçao dos topicos
355
_forumTransaction.refreshTopic(topic.getId());
356
357         return SUCCESS;
358     }
359
360     /**
361      * @return Action status
362      */

363     public String JavaDoc deletePost() {
364         Topic topic = null;
365         Forum forum = null;
366
367         // Obtém o usuário logado da sessão
368
User up = UserContext.getContext().getUser();
369
370         //Logging
371
log.debug("Deleting post User:" + up.getUser());
372         log.debug("Deleting post ID:" + up.getIdUser());
373         
374         // Verifica se o usuário tem privilégios para excluir o post
375
if (_postTransaction.canDeletePost(_post)) {
376
377             // guardando referencias para refresh posterior
378
topic = _post.getTopic();
379             forum = topic.getForum();
380
381             _postTransaction.deletePost(_post.getId());
382
383             // manutenção de resposta de posts
384
topicTransaction.subNumberReplysByTopic(_post.getTopic().getId());
385
386             // manutenção de mensagens do usuário
387
_userTransaction.subNumberMsgUser(_post.getUser().getId());
388
389             // Verifica se o post que foi excluído
390
// é o único post do tópico, se for, ele exclui
391
// o tópico também.
392
List JavaDoc postsByT = _postTransaction.findByTopic(_post.getTopic().getIdTopic(), getPage());
393
394             if (postsByT.size() <= 0) {
395                 // Exclui o topico do post
396
_topic = new Topic();
397                 _topic.setIdTopic(_post.getTopic().getIdTopic());
398                 topicTransaction.deleteTopic(_topic.getId());
399
400                 // nao dar refresh no topico, pois foi excluido
401
topic = new Topic();
402
403                 setUrl("viewforum.jbb?f=" + _post.getTopic().getForum().getIdForum());
404             } else {
405                 // Exclui apenas o post
406
int topicId = _post.getTopic().getIdTopic().intValue();
407                 setUrl("viewtopic.jbb?t=" + topicId);
408             }
409
410             // Atualiza a informacao do topico
411
_forumTransaction.refreshTopic(topic.getId());
412             // Atualiza a informaçao do forum
413
_forumTransaction.refreshForum(forum.getId());
414
415             return SUCCESS;
416         }
417
418         addActionError(getText("no_permission_to_delete_post"));
419
420         return ERROR;
421     }
422
423     /**
424      * @return result
425      */

426     public String JavaDoc searchAuthor() {
427         _posts = _postTransaction.listPostsByUser(_userId, _page);
428         return SUCCESS;
429     }
430
431     /**
432      * @return result
433      */

434     public String JavaDoc listUnAnswaredPosts() {
435         _posts = _postTransaction.listUnAnswaredPosts(_page);
436         return SUCCESS;
437     }
438
439     
440     public String JavaDoc listAllLastPosts() {
441         _posts = _postTransaction.findLasPosts();
442         return SUCCESS;
443     }
444     
445     // ####################################################################
446
// View objects accessors
447
// ####################################################################
448

449
450
451     /**
452      * @return Returns the post.
453      */

454     public Post getPost() {
455         return _post;
456     }
457
458     /**
459      * @return Returns the posts.
460      */

461     public List JavaDoc getPosts() {
462         return _posts;
463     }
464
465     /**
466      * @return Returns the quote.
467      */

468     public String JavaDoc getQuote() {
469         return quote;
470     }
471
472     /**
473      * @param quote The quote to set.
474      */

475     public void setQuote(String JavaDoc quote) {
476         this.quote = quote;
477     }
478
479     /**
480      * @return Returns the whoQuote.
481      */

482     public String JavaDoc getWhoQuote() {
483         return whoQuote;
484     }
485
486     /**
487      * @param whoQuote The whoQuote to set.
488      */

489     public void setWhoQuote(String JavaDoc whoQuote) {
490         this.whoQuote = whoQuote;
491     }
492
493     /**
494      * @param post
495      * @return formated post
496      */

497     public String JavaDoc formatPost(Post post) {
498         return postFormatter.formatPost(post);
499     }
500
501     /**
502      * @param text
503      * @return formated post
504      */

505     public String JavaDoc formatEscaped(String JavaDoc text) {
506         return postFormatter.formatEscaped(text);
507     }
508
509     /**
510      * @return Returns the topic.
511      */

512     public Topic getTopic() {
513         return _topic;
514     }
515     
516     public UserFormatter getUserFormatter() {
517         return userFormatter;
518     }
519
520 }
Popular Tags