1
22
23 package com.liferay.portlet.messageboards.action;
24
25 import com.liferay.documentlibrary.FileNameException;
26 import com.liferay.documentlibrary.FileSizeException;
27 import com.liferay.portal.captcha.CaptchaTextException;
28 import com.liferay.portal.captcha.CaptchaUtil;
29 import com.liferay.portal.kernel.util.Constants;
30 import com.liferay.portal.kernel.util.ObjectValuePair;
31 import com.liferay.portal.kernel.util.ParamUtil;
32 import com.liferay.portal.kernel.util.StringUtil;
33 import com.liferay.portal.kernel.util.Validator;
34 import com.liferay.portal.security.auth.PrincipalException;
35 import com.liferay.portal.struts.PortletAction;
36 import com.liferay.portal.theme.ThemeDisplay;
37 import com.liferay.portal.util.UploadRequestUtil;
38 import com.liferay.portal.util.WebKeys;
39 import com.liferay.portlet.ActionResponseImpl;
40 import com.liferay.portlet.messageboards.MessageBodyException;
41 import com.liferay.portlet.messageboards.MessageSubjectException;
42 import com.liferay.portlet.messageboards.NoSuchMessageException;
43 import com.liferay.portlet.messageboards.RequiredMessageException;
44 import com.liferay.portlet.messageboards.model.MBMessage;
45 import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
46 import com.liferay.portlet.tags.TagsEntryException;
47 import com.liferay.util.FileUtil;
48 import com.liferay.util.servlet.SessionErrors;
49 import com.liferay.util.servlet.UploadPortletRequest;
50
51 import java.io.File;
52
53 import java.util.ArrayList;
54 import java.util.List;
55
56 import javax.portlet.ActionRequest;
57 import javax.portlet.ActionResponse;
58 import javax.portlet.PortletConfig;
59 import javax.portlet.PortletPreferences;
60 import javax.portlet.PortletURL;
61 import javax.portlet.RenderRequest;
62 import javax.portlet.RenderResponse;
63
64 import org.apache.struts.action.ActionForm;
65 import org.apache.struts.action.ActionForward;
66 import org.apache.struts.action.ActionMapping;
67
68
74 public class EditMessageAction extends PortletAction {
75
76 public void processAction(
77 ActionMapping mapping, ActionForm form, PortletConfig config,
78 ActionRequest req, ActionResponse res)
79 throws Exception {
80
81 String cmd = ParamUtil.getString(req, Constants.CMD);
82
83 try {
84 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
85 updateMessage(req, res);
86 }
87 else if (cmd.equals(Constants.DELETE)) {
88 deleteMessage(req);
89 }
90 else if (cmd.equals(Constants.SUBSCRIBE)) {
91 subscribeMessage(req);
92 }
93 else if (cmd.equals(Constants.UNSUBSCRIBE)) {
94 unsubscribeMessage(req);
95 }
96
97 if (cmd.equals(Constants.DELETE) ||
98 cmd.equals(Constants.SUBSCRIBE) ||
99 cmd.equals(Constants.UNSUBSCRIBE)) {
100
101 sendRedirect(req, res);
102 }
103 }
104 catch (Exception e) {
105 if (e instanceof NoSuchMessageException ||
106 e instanceof PrincipalException ||
107 e instanceof RequiredMessageException) {
108
109 SessionErrors.add(req, e.getClass().getName());
110
111 setForward(req, "portlet.message_boards.error");
112 }
113 else if (e instanceof CaptchaTextException ||
114 e instanceof FileNameException ||
115 e instanceof FileSizeException ||
116 e instanceof MessageBodyException ||
117 e instanceof MessageSubjectException) {
118
119 SessionErrors.add(req, e.getClass().getName());
120 }
121 else if (e instanceof TagsEntryException) {
122 SessionErrors.add(req, e.getClass().getName(), e);
123 }
124 else {
125 throw e;
126 }
127 }
128 }
129
130 public ActionForward render(
131 ActionMapping mapping, ActionForm form, PortletConfig config,
132 RenderRequest req, RenderResponse res)
133 throws Exception {
134
135 try {
136 ActionUtil.getMessage(req);
137 }
138 catch (Exception e) {
139 if (e instanceof NoSuchMessageException ||
140 e instanceof PrincipalException) {
141
142 SessionErrors.add(req, e.getClass().getName());
143
144 return mapping.findForward("portlet.message_boards.error");
145 }
146 else {
147 throw e;
148 }
149 }
150
151 return mapping.findForward(
152 getForward(req, "portlet.message_boards.edit_message"));
153 }
154
155 protected void deleteMessage(ActionRequest req) throws Exception {
156 long messageId = ParamUtil.getLong(req, "messageId");
157
158 MBMessageServiceUtil.deleteMessage(messageId);
159 }
160
161 protected void subscribeMessage(ActionRequest req) throws Exception {
162 long messageId = ParamUtil.getLong(req, "messageId");
163
164 MBMessageServiceUtil.subscribeMessage(messageId);
165 }
166
167 protected void unsubscribeMessage(ActionRequest req) throws Exception {
168 long messageId = ParamUtil.getLong(req, "messageId");
169
170 MBMessageServiceUtil.unsubscribeMessage(messageId);
171 }
172
173 protected void updateMessage(ActionRequest req, ActionResponse res)
174 throws Exception {
175
176 ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
177 WebKeys.THEME_DISPLAY);
178
179 PortletPreferences prefs = req.getPreferences();
180
181 long messageId = ParamUtil.getLong(req, "messageId");
182
183 long categoryId = ParamUtil.getLong(req, "categoryId");
184 long threadId = ParamUtil.getLong(req, "threadId");
185 long parentMessageId = ParamUtil.getLong(req, "parentMessageId");
186 String subject = ParamUtil.getString(req, "subject");
187 String body = ParamUtil.getString(req, "body");
188 boolean attachments = ParamUtil.getBoolean(req, "attachments");
189
190 List<ObjectValuePair<String, byte[]>> files =
191 new ArrayList<ObjectValuePair<String, byte[]>>();
192
193 if (attachments) {
194 UploadPortletRequest uploadReq =
195 UploadRequestUtil.getUploadPortletRequest(req);
196
197 for (int i = 1; i <= 5; i++) {
198 File file = uploadReq.getFile("msgFile" + i);
199 String fileName = uploadReq.getFileName("msgFile" + i);
200 byte[] bytes = FileUtil.getBytes(file);
201
202 if ((bytes != null) && (bytes.length > 0)) {
203 ObjectValuePair<String, byte[]> ovp =
204 new ObjectValuePair<String, byte[]>(fileName, bytes);
205
206 files.add(ovp);
207 }
208 }
209 }
210
211 boolean anonymous = ParamUtil.getBoolean(req, "anonymous");
212 double priority = ParamUtil.getDouble(req, "priority");
213
214 String[] tagsEntries = StringUtil.split(
215 ParamUtil.getString(req, "tagsEntries"));
216
217 String[] communityPermissions = req.getParameterValues(
218 "communityPermissions");
219 String[] guestPermissions = req.getParameterValues(
220 "guestPermissions");
221
222 MBMessage message = null;
223
224 if (messageId <= 0) {
225 if (false) {
226 CaptchaUtil.check(req);
227 }
228
229 if (threadId <= 0) {
230
231
233 message = MBMessageServiceUtil.addMessage(
234 categoryId, subject, body, files, anonymous, priority,
235 tagsEntries, prefs, communityPermissions, guestPermissions,
236 themeDisplay);
237 }
238 else {
239
240
242 message = MBMessageServiceUtil.addMessage(
243 categoryId, threadId, parentMessageId, subject, body, files,
244 anonymous, priority, tagsEntries, prefs,
245 communityPermissions, guestPermissions, themeDisplay);
246 }
247 }
248 else {
249 List<String> existingFiles = new ArrayList<String>();
250
251 for (int i = 1; i <= 5; i++) {
252 String path = ParamUtil.getString(req, "existingPath" + i);
253
254 if (Validator.isNotNull(path)) {
255 existingFiles.add(path);
256 }
257 }
258
259
261 message = MBMessageServiceUtil.updateMessage(
262 messageId, subject, body, files, existingFiles, priority,
263 tagsEntries, prefs, themeDisplay);
264 }
265
266 PortletURL portletURL = ((ActionResponseImpl)res).createRenderURL();
267
268 portletURL.setParameter(
269 "struts_action", "/message_boards/view_message");
270 portletURL.setParameter(
271 "messageId", String.valueOf(message.getMessageId()));
272
273 res.sendRedirect(portletURL.toString());
274 }
275
276 }