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