001
014
015 package com.liferay.portlet.blogs.action;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.json.JSONFactoryUtil;
020 import com.liferay.portal.kernel.json.JSONObject;
021 import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
022 import com.liferay.portal.kernel.portlet.LiferayWindowState;
023 import com.liferay.portal.kernel.sanitizer.SanitizerException;
024 import com.liferay.portal.kernel.servlet.SessionErrors;
025 import com.liferay.portal.kernel.servlet.SessionMessages;
026 import com.liferay.portal.kernel.upload.UploadPortletRequest;
027 import com.liferay.portal.kernel.util.ArrayUtil;
028 import com.liferay.portal.kernel.util.Constants;
029 import com.liferay.portal.kernel.util.HttpUtil;
030 import com.liferay.portal.kernel.util.ParamUtil;
031 import com.liferay.portal.kernel.util.StreamUtil;
032 import com.liferay.portal.kernel.util.StringPool;
033 import com.liferay.portal.kernel.util.StringUtil;
034 import com.liferay.portal.kernel.util.Validator;
035 import com.liferay.portal.kernel.workflow.WorkflowConstants;
036 import com.liferay.portal.security.auth.PrincipalException;
037 import com.liferay.portal.service.ServiceContext;
038 import com.liferay.portal.service.ServiceContextFactory;
039 import com.liferay.portal.struts.PortletAction;
040 import com.liferay.portal.theme.ThemeDisplay;
041 import com.liferay.portal.util.PortalUtil;
042 import com.liferay.portal.util.PortletKeys;
043 import com.liferay.portal.util.PropsValues;
044 import com.liferay.portal.util.WebKeys;
045 import com.liferay.portlet.PortletURLImpl;
046 import com.liferay.portlet.asset.AssetCategoryException;
047 import com.liferay.portlet.asset.AssetTagException;
048 import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
049 import com.liferay.portlet.blogs.EntryContentException;
050 import com.liferay.portlet.blogs.EntryDisplayDateException;
051 import com.liferay.portlet.blogs.EntrySmallImageNameException;
052 import com.liferay.portlet.blogs.EntrySmallImageSizeException;
053 import com.liferay.portlet.blogs.EntryTitleException;
054 import com.liferay.portlet.blogs.NoSuchEntryException;
055 import com.liferay.portlet.blogs.model.BlogsEntry;
056 import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
057 import com.liferay.portlet.blogs.service.BlogsEntryServiceUtil;
058
059 import java.io.InputStream;
060
061 import java.util.Calendar;
062 import java.util.HashMap;
063 import java.util.Map;
064
065 import javax.portlet.ActionRequest;
066 import javax.portlet.ActionResponse;
067 import javax.portlet.PortletConfig;
068 import javax.portlet.PortletRequest;
069 import javax.portlet.RenderRequest;
070 import javax.portlet.RenderResponse;
071 import javax.portlet.WindowState;
072
073 import javax.servlet.http.HttpServletResponse;
074
075 import org.apache.struts.action.ActionForm;
076 import org.apache.struts.action.ActionForward;
077 import org.apache.struts.action.ActionMapping;
078
079
087 public class EditEntryAction extends PortletAction {
088
089 @Override
090 public void processAction(
091 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
092 ActionRequest actionRequest, ActionResponse actionResponse)
093 throws Exception {
094
095 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
096
097 try {
098 BlogsEntry entry = null;
099 String oldUrlTitle = StringPool.BLANK;
100
101 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
102 Object[] returnValue = updateEntry(actionRequest);
103
104 entry = (BlogsEntry)returnValue[0];
105 oldUrlTitle = ((String)returnValue[1]);
106 }
107 else if (cmd.equals(Constants.DELETE)) {
108 deleteEntries(
109 (LiferayPortletConfig)portletConfig, actionRequest, false);
110 }
111 else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
112 deleteEntries(
113 (LiferayPortletConfig)portletConfig, actionRequest, true);
114 }
115 else if (cmd.equals(Constants.SUBSCRIBE)) {
116 subscribe(actionRequest);
117 }
118 else if (cmd.equals(Constants.RESTORE)) {
119 restoreEntries(actionRequest);
120 }
121 else if (cmd.equals(Constants.UNSUBSCRIBE)) {
122 unsubscribe(actionRequest);
123 }
124
125 String redirect = ParamUtil.getString(actionRequest, "redirect");
126 boolean updateRedirect = false;
127
128 if (Validator.isNotNull(oldUrlTitle)) {
129 String portletId = HttpUtil.getParameter(
130 redirect, "p_p_id", false);
131
132 String oldRedirectParam =
133 PortalUtil.getPortletNamespace(portletId) + "redirect";
134
135 String oldRedirect = HttpUtil.getParameter(
136 redirect, oldRedirectParam, false);
137
138 if (Validator.isNotNull(oldRedirect)) {
139 String newRedirect = HttpUtil.decodeURL(oldRedirect);
140
141 newRedirect = StringUtil.replace(
142 newRedirect, oldUrlTitle, entry.getUrlTitle());
143 newRedirect = StringUtil.replace(
144 newRedirect, oldRedirectParam, "redirect");
145
146 redirect = StringUtil.replace(
147 redirect, oldRedirect, newRedirect);
148 }
149 else if (redirect.endsWith("/blogs/" + oldUrlTitle) ||
150 redirect.contains("/blogs/" + oldUrlTitle + "?") ||
151 redirect.contains("/blog/" + oldUrlTitle + "?")) {
152
153 redirect = StringUtil.replace(
154 redirect, oldUrlTitle, entry.getUrlTitle());
155 }
156
157 updateRedirect = true;
158 }
159
160 int workflowAction = ParamUtil.getInteger(
161 actionRequest, "workflowAction",
162 WorkflowConstants.ACTION_SAVE_DRAFT);
163
164 boolean ajax = ParamUtil.getBoolean(actionRequest, "ajax");
165
166 if (ajax) {
167 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
168
169 jsonObject.put("entryId", entry.getEntryId());
170 jsonObject.put("redirect", redirect);
171 jsonObject.put("updateRedirect", updateRedirect);
172
173 writeJSON(actionRequest, actionResponse, jsonObject);
174
175 return;
176 }
177
178 if ((entry != null) &&
179 (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT)) {
180
181 redirect = getSaveAndContinueRedirect(
182 portletConfig, actionRequest, entry, redirect);
183
184 sendRedirect(actionRequest, actionResponse, redirect);
185 }
186 else {
187 WindowState windowState = actionRequest.getWindowState();
188
189 if (!windowState.equals(LiferayWindowState.POP_UP)) {
190 sendRedirect(actionRequest, actionResponse, redirect);
191 }
192 else {
193 redirect = PortalUtil.escapeRedirect(redirect);
194
195 if (Validator.isNotNull(redirect)) {
196 actionResponse.sendRedirect(redirect);
197 }
198 }
199 }
200 }
201 catch (Exception e) {
202 if (e instanceof NoSuchEntryException ||
203 e instanceof PrincipalException) {
204
205 SessionErrors.add(actionRequest, e.getClass());
206
207 setForward(actionRequest, "portlet.blogs.error");
208 }
209 else if (e instanceof EntryContentException ||
210 e instanceof EntryDisplayDateException ||
211 e instanceof EntrySmallImageNameException ||
212 e instanceof EntrySmallImageSizeException ||
213 e instanceof EntryTitleException ||
214 e instanceof SanitizerException) {
215
216 SessionErrors.add(actionRequest, e.getClass());
217 }
218 else if (e instanceof AssetCategoryException ||
219 e instanceof AssetTagException) {
220
221 SessionErrors.add(actionRequest, e.getClass(), e);
222 }
223 else {
224 Throwable cause = e.getCause();
225
226 if (cause instanceof SanitizerException) {
227 SessionErrors.add(actionRequest, SanitizerException.class);
228 }
229 else {
230 throw e;
231 }
232 }
233 }
234 }
235
236 @Override
237 public ActionForward render(
238 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
239 RenderRequest renderRequest, RenderResponse renderResponse)
240 throws Exception {
241
242 try {
243 ActionUtil.getEntry(renderRequest);
244
245 if (PropsValues.BLOGS_PINGBACK_ENABLED) {
246 BlogsEntry entry = (BlogsEntry)renderRequest.getAttribute(
247 WebKeys.BLOGS_ENTRY);
248
249 if ((entry != null) && entry.isAllowPingbacks()) {
250 HttpServletResponse response =
251 PortalUtil.getHttpServletResponse(renderResponse);
252
253 response.addHeader(
254 "X-Pingback",
255 PortalUtil.getPortalURL(renderRequest) +
256 "/xmlrpc/pingback");
257 }
258 }
259 }
260 catch (Exception e) {
261 if (e instanceof NoSuchEntryException ||
262 e instanceof PrincipalException) {
263
264 SessionErrors.add(renderRequest, e.getClass());
265
266 return mapping.findForward("portlet.blogs.error");
267 }
268 else {
269 throw e;
270 }
271 }
272
273 return mapping.findForward(
274 getForward(renderRequest, "portlet.blogs.edit_entry"));
275 }
276
277 protected void deleteEntries(
278 LiferayPortletConfig liferayPortletConfig,
279 ActionRequest actionRequest, boolean moveToTrash)
280 throws Exception {
281
282 long[] deleteEntryIds = null;
283
284 long entryId = ParamUtil.getLong(actionRequest, "entryId");
285
286 if (entryId > 0) {
287 deleteEntryIds = new long[] {entryId};
288 }
289 else {
290 deleteEntryIds = StringUtil.split(
291 ParamUtil.getString(actionRequest, "deleteEntryIds"), 0L);
292 }
293
294 for (long deleteEntryId : deleteEntryIds) {
295 if (moveToTrash) {
296 BlogsEntryServiceUtil.moveEntryToTrash(deleteEntryId);
297 }
298 else {
299 BlogsEntryServiceUtil.deleteEntry(deleteEntryId);
300 }
301 }
302
303 if (moveToTrash && (deleteEntryIds.length > 0)) {
304 Map<String, String[]> data = new HashMap<String, String[]>();
305
306 data.put(
307 "restoreEntryIds", ArrayUtil.toStringArray(deleteEntryIds));
308
309 SessionMessages.add(
310 actionRequest,
311 liferayPortletConfig.getPortletId() +
312 SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
313
314 SessionMessages.add(
315 actionRequest,
316 liferayPortletConfig.getPortletId() +
317 SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_SUCCESS_MESSAGE);
318 }
319 }
320
321 protected String getSaveAndContinueRedirect(
322 PortletConfig portletConfig, ActionRequest actionRequest,
323 BlogsEntry entry, String redirect)
324 throws Exception {
325
326 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
327 WebKeys.THEME_DISPLAY);
328
329 String backURL = ParamUtil.getString(actionRequest, "backURL");
330
331 boolean preview = ParamUtil.getBoolean(actionRequest, "preview");
332
333 PortletURLImpl portletURL = new PortletURLImpl(
334 actionRequest, portletConfig.getPortletName(),
335 themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
336
337 portletURL.setWindowState(actionRequest.getWindowState());
338
339 String portletName = portletConfig.getPortletName();
340
341 if (portletName.equals(PortletKeys.BLOGS_ADMIN)) {
342 portletURL.setParameter("struts_action", "/blogs_admin/edit_entry");
343 }
344 else {
345 portletURL.setParameter("struts_action", "/blogs/edit_entry");
346 }
347
348 portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
349 portletURL.setParameter("redirect", redirect, false);
350 portletURL.setParameter("backURL", backURL, false);
351 portletURL.setParameter(
352 "groupId", String.valueOf(entry.getGroupId()), false);
353 portletURL.setParameter(
354 "entryId", String.valueOf(entry.getEntryId()), false);
355 portletURL.setParameter("preview", String.valueOf(preview), false);
356
357 return portletURL.toString();
358 }
359
360 protected void restoreEntries(ActionRequest actionRequest)
361 throws PortalException, SystemException {
362
363 long[] restoreEntryIds = StringUtil.split(
364 ParamUtil.getString(actionRequest, "restoreEntryIds"), 0L);
365
366 for (long restoreEntryId : restoreEntryIds) {
367 BlogsEntryServiceUtil.restoreEntryFromTrash(restoreEntryId);
368 }
369 }
370
371 protected void subscribe(ActionRequest actionRequest) throws Exception {
372 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
373 WebKeys.THEME_DISPLAY);
374
375 BlogsEntryServiceUtil.subscribe(themeDisplay.getScopeGroupId());
376 }
377
378 protected void unsubscribe(ActionRequest actionRequest) throws Exception {
379 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
380 WebKeys.THEME_DISPLAY);
381
382 BlogsEntryServiceUtil.unsubscribe(themeDisplay.getScopeGroupId());
383 }
384
385 protected Object[] updateEntry(ActionRequest actionRequest)
386 throws Exception {
387
388 long entryId = ParamUtil.getLong(actionRequest, "entryId");
389
390 String title = ParamUtil.getString(actionRequest, "title");
391 String description = ParamUtil.getString(actionRequest, "description");
392 String content = ParamUtil.getString(actionRequest, "content");
393
394 int displayDateMonth = ParamUtil.getInteger(
395 actionRequest, "displayDateMonth");
396 int displayDateDay = ParamUtil.getInteger(
397 actionRequest, "displayDateDay");
398 int displayDateYear = ParamUtil.getInteger(
399 actionRequest, "displayDateYear");
400 int displayDateHour = ParamUtil.getInteger(
401 actionRequest, "displayDateHour");
402 int displayDateMinute = ParamUtil.getInteger(
403 actionRequest, "displayDateMinute");
404 int displayDateAmPm = ParamUtil.getInteger(
405 actionRequest, "displayDateAmPm");
406
407 if (displayDateAmPm == Calendar.PM) {
408 displayDateHour += 12;
409 }
410
411 boolean allowPingbacks = ParamUtil.getBoolean(
412 actionRequest, "allowPingbacks");
413 boolean allowTrackbacks = ParamUtil.getBoolean(
414 actionRequest, "allowTrackbacks");
415 String[] trackbacks = StringUtil.split(
416 ParamUtil.getString(actionRequest, "trackbacks"));
417
418 boolean smallImage = false;
419 String smallImageURL = null;
420 String smallImageFileName = null;
421 InputStream smallImageInputStream = null;
422
423 BlogsEntry entry = null;
424 String oldUrlTitle = null;
425
426 try {
427 boolean ajax = ParamUtil.getBoolean(actionRequest, "ajax");
428
429 if (!ajax) {
430 smallImage = ParamUtil.getBoolean(actionRequest, "smallImage");
431 smallImageURL = ParamUtil.getString(
432 actionRequest, "smallImageURL");
433
434 if (smallImage && Validator.isNull(smallImageURL)) {
435 boolean attachments = ParamUtil.getBoolean(
436 actionRequest, "attachments");
437
438 if (attachments) {
439 UploadPortletRequest uploadPortletRequest =
440 PortalUtil.getUploadPortletRequest(actionRequest);
441
442 smallImageFileName = uploadPortletRequest.getFileName(
443 "smallFile");
444 smallImageInputStream =
445 uploadPortletRequest.getFileAsStream("smallFile");
446 }
447 }
448 }
449
450 ServiceContext serviceContext = ServiceContextFactory.getInstance(
451 BlogsEntry.class.getName(), actionRequest);
452
453 entry = null;
454 oldUrlTitle = StringPool.BLANK;
455
456 if (entryId <= 0) {
457
458
459
460 entry = BlogsEntryServiceUtil.addEntry(
461 title, description, content, displayDateMonth,
462 displayDateDay, displayDateYear, displayDateHour,
463 displayDateMinute, allowPingbacks, allowTrackbacks,
464 trackbacks, smallImage, smallImageURL, smallImageFileName,
465 smallImageInputStream, serviceContext);
466
467 AssetPublisherUtil.addAndStoreSelection(
468 actionRequest, BlogsEntry.class.getName(),
469 entry.getEntryId(), -1);
470 }
471 else {
472
473
474
475 entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
476
477 String tempOldUrlTitle = entry.getUrlTitle();
478
479 entry = BlogsEntryServiceUtil.updateEntry(
480 entryId, title, description, content, displayDateMonth,
481 displayDateDay, displayDateYear, displayDateHour,
482 displayDateMinute, allowPingbacks, allowTrackbacks,
483 trackbacks, smallImage, smallImageURL, smallImageFileName,
484 smallImageInputStream, serviceContext);
485
486 if (!tempOldUrlTitle.equals(entry.getUrlTitle())) {
487 oldUrlTitle = tempOldUrlTitle;
488 }
489
490 AssetPublisherUtil.addAndStoreSelection(
491 actionRequest, BlogsEntry.class.getName(),
492 entry.getEntryId(), -1);
493 }
494 }
495 finally {
496 StreamUtil.cleanUp(smallImageInputStream);
497 }
498
499 return new Object[] {entry, oldUrlTitle};
500 }
501
502 }