KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portlet > forums > commands > post > EditPostCommand


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Forums JBoss Portlet *
6  * *
7  * Distributable under GPL license. *
8  * See terms of license at gnu.org. *
9  * *
10  *****************************************/

11 package org.jboss.portlet.forums.commands.post;
12
13 import java.util.Date JavaDoc;
14
15 import org.jboss.portal.common.command.result.Result;
16 import org.jboss.portlet.forums.commands.ValidationException;
17 import org.jboss.portlet.forums.model.AuthType;
18 import org.jboss.portlet.JBossActionRequest;
19 import org.jboss.portlet.JBossActionResponse;
20
21 /**
22  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
23  * @author <a HREF="mailto:theute@users.sourceforge.net">Thomas Heute</a>
24  * @version $Revision: 1.4 $
25  */

26 public class EditPostCommand
27    extends PostCommand
28 {
29    /** DOCUMENT_ME */
30    public int topic_type;
31
32    /** DOCUMENT_ME */
33    public Date JavaDoc current_time;
34
35    /** DOCUMENT_ME */
36    public boolean notify;
37
38    /** DOCUMENT_ME */
39    public boolean poll_delete;
40
41    /** DOCUMENT_ME */
42    public boolean pd_first_post;
43
44    /** DOCUMENT_ME */
45    public boolean pd_last_post;
46
47    /** DOCUMENT_ME */
48    public boolean pd_edit_poll;
49
50    /** DOCUMENT_ME */
51    public Integer JavaDoc[] deletedAttachments;
52
53    public EditPostCommand(JBossActionRequest request, JBossActionResponse response)
54    {
55       super(request, response);
56    }
57
58    /**
59     * DOCUMENT_ME
60     *
61     * @return DOCUMENT_ME
62     */

63    public int authType()
64    {
65       return AuthType.EDIT;
66    }
67
68    /**
69     * DOCUMENT_ME
70     *
71     * @throws ValidationException DOCUMENT_ME
72     */

73    protected void prepare()
74    throws ValidationException
75    {
76       /*
77          super.prepare();
78          if (!isAuthMod && !EJB.areIdentical(post.getUser(), getPoster().getUser()))
79          {
80             throw new ValidationException(TYPE_CANNOT_XXX_OTHER_POST_WHEN_NOT_MODERATOR);
81          }
82          pd_first_post = EJB.areIdentical(topic.getFirstPost(), post);
83          pd_last_post = EJB.areIdentical(topic.getLastPost(), post);
84          if (pd_first_post)
85          {
86             // Same in BBModule
87             switch (topic.getPollLevel())
88             {
89             case TopicEJB.POLL_LEVEL_EMPTY:
90                pd_edit_poll = isAuthPollCreate || isAuthMod;
91                break;
92             case TopicEJB.POLL_LEVEL_NO_POLL:
93                pd_edit_poll = isAuthPollCreate || isAuthMod;
94                break;
95             case TopicEJB.POLL_LEVEL_NON_EMPTY:
96                pd_edit_poll = isAuthMod;
97                break;
98             }
99          }
100          if (!isAuthMod && getPoster() != null)
101          {
102             poster.setUserName(getPoster().getUserName());
103          }
104          if (deletedAttachments.length > 0 && !(isAuthMod || isAuthAttachments))
105          {
106             throw new ValidationException(TYPE_CANNOT_DELETE_ATTACHMENT);
107          }
108          PostTools.validate(message);
109          PostTools.validate(poll);
110        */

111    }
112
113    /**
114     * DOCUMENT_ME
115     *
116     * @return DOCUMENT_ME
117     */

118    protected Result __execute()
119    {
120       if (pd_first_post)
121       {
122          topic.setSubject(message.getSubject());
123          topic.setType(topic_type);
124       }
125
126       // post.setPosterUserName(poster.getUsername());
127
post.setMessage(message);
128
129       /*
130          try
131          {
132             try
133             {
134                TopicWatch watch = homes.topicWatch.findByPrimaryKey(new TopicWatchEJBPK(getPoster().getUser().getId(), topic.getId()));
135                if (!notify)
136                {
137                   watch.remove();
138                }
139             }
140             catch (ObjectNotFoundException e)
141             {
142                if (notify)
143                {
144                   homes.topicWatch.create(getPoster().getUser(), topic);
145                }
146             }
147          }
148          catch (Exception e)
149          {
150             log.error("Cannot get watch", e);
151             return TYPE_ERROR_IN_POSTING;
152          }
153          if (pd_last_post && EJB.areIdentical(post.getUser(), getPoster().getUser()))
154          {
155             post.setEditDate(current_time);
156             post.setEditCount(post.getEditCount() + 1);
157          }
158          try
159          {
160             if (pd_edit_poll && poll.getTitle() != null && poll.getOptions().size() >= 2)
161             {
162                // Get the poll
163                PollEJBLocal pollEJB = topic.getPoll();
164                //
165                String pollTitle = poll.getTitle();
166                int pollLength = poll.getLength();
167                // Update infos
168                pollEJB.setText(pollTitle);
169                pollEJB.setLength(pollLength);
170                // Update the options
171                Collection options = homes.option.findByPoll(pollEJB);
172                for (IndexIterator iterator = IndexIterator.wrap(options.iterator(), -1);iterator.hasNext();)
173                {
174                   OptionEJBLocal optionEJB = (OptionEJBLocal)iterator.next();
175                   int index = iterator.getIndex();
176                   if (index < poll.getOptions().size())
177                   {
178                      String text = (String)poll.getOptions().get(index);
179                      if (text != null)
180                      {
181                         // $option_text = str_replace("\'", "''", htmlspecialchars($option_text));
182                         optionEJB.setText(text);
183                      }
184                      else
185                      {
186                         optionEJB.remove();
187                      }
188                   }
189                   else
190                   {
191                      optionEJB.remove();
192                   }
193                }
194                topic.setVote(true);
195             }
196             else
197             {
198                topic.setVote(false);
199             }
200          }
201          catch (FinderException e)
202          {
203             log.error("Cannot find options for a poll", e);
204             return TYPE_ERROR_IN_POSTING;
205          }
206          catch (RemoveException e)
207          {
208             log.error("Cannot remove target option", e);
209             return TYPE_ERROR_IN_POSTING;
210          }
211          try
212          {
213             // Remove deleted attachments
214             for (Iterator i = post.getAttachments().iterator();i.hasNext();)
215             {
216                AttachmentEJBLocal attachment = (AttachmentEJBLocal)i.next();
217                Integer id = attachment.getId();
218                for (int j = 0;j < deletedAttachments.length;j++)
219                {
220                   if (id.equals(deletedAttachments[j]))
221                   {
222                      i.remove();
223                      attachment.remove();
224                   }
225                }
226             }
227          }
228          catch (RemoveException e)
229          {
230             log.error("Cannot remove attachment", e);
231             return TYPE_ERROR_IN_POSTING;
232          }
233          try
234          {
235             // Add attachments
236             for (int i = 0;i < attachments.length;i++)
237             {
238                Attachment attachment = attachments[i];
239                homes.attachment.create(post,
240                      attachment.getFile().getName(),
241                      attachment.getFile().getContent(),
242                      attachment.getComment(),
243                      attachment.getFile().getContentType(),
244                      attachment.getFile().getSize(),
245                      current_time);
246             }
247          }
248          catch (CreateException e)
249          {
250             log.error("Cannot insert attachment", e);
251             return TYPE_ERROR_IN_POSTING;
252          }
253        */

254       return TYPE_EDIT_POST_POSTED;
255    }
256 }
Popular Tags