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 actionMapping, ActionForm actionForm,
093 PortletConfig portletConfig, ActionRequest actionRequest,
094 ActionResponse actionResponse)
095 throws Exception {
096
097 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
098
099 try {
100 BlogsEntry entry = null;
101 String oldUrlTitle = StringPool.BLANK;
102
103 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
104 Object[] returnValue = updateEntry(actionRequest);
105
106 entry = (BlogsEntry)returnValue[0];
107 oldUrlTitle = ((String)returnValue[1]);
108 }
109 else if (cmd.equals(Constants.DELETE)) {
110 deleteEntries(
111 (LiferayPortletConfig)portletConfig, actionRequest, false);
112 }
113 else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
114 deleteEntries(
115 (LiferayPortletConfig)portletConfig, actionRequest, true);
116 }
117 else if (cmd.equals(Constants.SUBSCRIBE)) {
118 subscribe(actionRequest);
119 }
120 else if (cmd.equals(Constants.RESTORE)) {
121 restoreEntries(actionRequest);
122 }
123 else if (cmd.equals(Constants.UNSUBSCRIBE)) {
124 unsubscribe(actionRequest);
125 }
126 else if (cmd.equals(Constants.UPDATE_CONTENT)) {
127 updateContent(actionRequest, actionResponse);
128
129 return;
130 }
131
132 String redirect = ParamUtil.getString(actionRequest, "redirect");
133 boolean updateRedirect = false;
134
135 String portletId = HttpUtil.getParameter(redirect, "p_p_id", false);
136
137 if (Validator.isNotNull(oldUrlTitle)) {
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 if (cmd.equals(Constants.ADD) && (entry != null)) {
203 String namespace = PortalUtil.getPortletNamespace(
204 portletId);
205
206 redirect = HttpUtil.addParameter(
207 redirect, namespace + "className",
208 BlogsEntry.class.getName());
209 redirect = HttpUtil.addParameter(
210 redirect, namespace + "classPK",
211 entry.getEntryId());
212 }
213
214 actionResponse.sendRedirect(redirect);
215 }
216 }
217 }
218 }
219 catch (Exception e) {
220 if (e instanceof NoSuchEntryException ||
221 e instanceof PrincipalException) {
222
223 SessionErrors.add(actionRequest, e.getClass());
224
225 setForward(actionRequest, "portlet.blogs.error");
226 }
227 else if (e instanceof EntryContentException ||
228 e instanceof EntryDisplayDateException ||
229 e instanceof EntrySmallImageNameException ||
230 e instanceof EntrySmallImageSizeException ||
231 e instanceof EntryTitleException ||
232 e instanceof SanitizerException) {
233
234 SessionErrors.add(actionRequest, e.getClass());
235 }
236 else if (e instanceof AssetCategoryException ||
237 e instanceof AssetTagException) {
238
239 SessionErrors.add(actionRequest, e.getClass(), e);
240 }
241 else {
242 Throwable cause = e.getCause();
243
244 if (cause instanceof SanitizerException) {
245 SessionErrors.add(actionRequest, SanitizerException.class);
246 }
247 else {
248 throw e;
249 }
250 }
251 }
252 }
253
254 @Override
255 public ActionForward render(
256 ActionMapping actionMapping, ActionForm actionForm,
257 PortletConfig portletConfig, RenderRequest renderRequest,
258 RenderResponse renderResponse)
259 throws Exception {
260
261 try {
262 ActionUtil.getEntry(renderRequest);
263
264 if (PropsValues.BLOGS_PINGBACK_ENABLED) {
265 BlogsEntry entry = (BlogsEntry)renderRequest.getAttribute(
266 WebKeys.BLOGS_ENTRY);
267
268 if ((entry != null) && entry.isAllowPingbacks()) {
269 HttpServletResponse response =
270 PortalUtil.getHttpServletResponse(renderResponse);
271
272 response.addHeader(
273 "X-Pingback",
274 PortalUtil.getPortalURL(renderRequest) +
275 "/xmlrpc/pingback");
276 }
277 }
278 }
279 catch (Exception e) {
280 if (e instanceof NoSuchEntryException ||
281 e instanceof PrincipalException) {
282
283 SessionErrors.add(renderRequest, e.getClass());
284
285 return actionMapping.findForward("portlet.blogs.error");
286 }
287 else {
288 throw e;
289 }
290 }
291
292 return actionMapping.findForward(
293 getForward(renderRequest, "portlet.blogs.edit_entry"));
294 }
295
296 protected void deleteEntries(
297 LiferayPortletConfig liferayPortletConfig,
298 ActionRequest actionRequest, boolean moveToTrash)
299 throws Exception {
300
301 long[] deleteEntryIds = null;
302
303 long entryId = ParamUtil.getLong(actionRequest, "entryId");
304
305 if (entryId > 0) {
306 deleteEntryIds = new long[] {entryId};
307 }
308 else {
309 deleteEntryIds = StringUtil.split(
310 ParamUtil.getString(actionRequest, "deleteEntryIds"), 0L);
311 }
312
313 String deleteEntryTitle = null;
314
315 for (int i = 0; i < deleteEntryIds.length; i++) {
316 long deleteEntryId = deleteEntryIds[i];
317
318 if (moveToTrash) {
319 BlogsEntry entry = BlogsEntryServiceUtil.moveEntryToTrash(
320 deleteEntryId);
321
322 if (i == 0) {
323 deleteEntryTitle = entry.getTitle();
324 }
325 }
326 else {
327 BlogsEntryServiceUtil.deleteEntry(deleteEntryId);
328 }
329 }
330
331 if (moveToTrash && (deleteEntryIds.length > 0)) {
332 Map<String, String[]> data = new HashMap<String, String[]>();
333
334 data.put(
335 "deleteEntryClassName",
336 new String[] {BlogsEntry.class.getName()});
337
338 if (Validator.isNotNull(deleteEntryTitle)) {
339 data.put("deleteEntryTitle", new String[] {deleteEntryTitle});
340 }
341
342 data.put(
343 "restoreEntryIds", ArrayUtil.toStringArray(deleteEntryIds));
344
345 SessionMessages.add(
346 actionRequest,
347 liferayPortletConfig.getPortletId() +
348 SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
349
350 hideDefaultSuccessMessage(liferayPortletConfig, actionRequest);
351 }
352 }
353
354 protected String getSaveAndContinueRedirect(
355 PortletConfig portletConfig, ActionRequest actionRequest,
356 BlogsEntry entry, String redirect)
357 throws Exception {
358
359 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
360 WebKeys.THEME_DISPLAY);
361
362 String backURL = ParamUtil.getString(actionRequest, "backURL");
363
364 boolean preview = ParamUtil.getBoolean(actionRequest, "preview");
365
366 PortletURLImpl portletURL = new PortletURLImpl(
367 actionRequest, portletConfig.getPortletName(),
368 themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
369
370 String portletName = portletConfig.getPortletName();
371
372 if (portletName.equals(PortletKeys.BLOGS_ADMIN)) {
373 portletURL.setParameter("struts_action", "/blogs_admin/edit_entry");
374 }
375 else {
376 portletURL.setParameter("struts_action", "/blogs/edit_entry");
377 }
378
379 portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
380 portletURL.setParameter("redirect", redirect, false);
381 portletURL.setParameter("backURL", backURL, false);
382 portletURL.setParameter(
383 "groupId", String.valueOf(entry.getGroupId()), false);
384 portletURL.setParameter(
385 "entryId", String.valueOf(entry.getEntryId()), false);
386 portletURL.setParameter("preview", String.valueOf(preview), false);
387 portletURL.setWindowState(actionRequest.getWindowState());
388
389 return portletURL.toString();
390 }
391
392 protected void restoreEntries(ActionRequest actionRequest)
393 throws PortalException, SystemException {
394
395 long[] restoreEntryIds = StringUtil.split(
396 ParamUtil.getString(actionRequest, "restoreEntryIds"), 0L);
397
398 for (long restoreEntryId : restoreEntryIds) {
399 BlogsEntryServiceUtil.restoreEntryFromTrash(restoreEntryId);
400 }
401 }
402
403 protected void subscribe(ActionRequest actionRequest) throws Exception {
404 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
405 WebKeys.THEME_DISPLAY);
406
407 BlogsEntryServiceUtil.subscribe(themeDisplay.getScopeGroupId());
408 }
409
410 protected void unsubscribe(ActionRequest actionRequest) throws Exception {
411 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
412 WebKeys.THEME_DISPLAY);
413
414 BlogsEntryServiceUtil.unsubscribe(themeDisplay.getScopeGroupId());
415 }
416
417 protected void updateContent(
418 ActionRequest actionRequest, ActionResponse actionResponse)
419 throws Exception {
420
421 long entryId = ParamUtil.getLong(actionRequest, "entryId");
422
423 String content = ParamUtil.getString(actionRequest, "content");
424
425 BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
426
427 ServiceContext serviceContext = ServiceContextFactory.getInstance(
428 actionRequest);
429
430 try {
431 Calendar displayDateCal = CalendarFactoryUtil.getCalendar();
432
433 displayDateCal.setTime(entry.getDisplayDate());
434
435 int displayDateMonth = displayDateCal.get(Calendar.MONTH);
436 int displayDateDay = displayDateCal.get(Calendar.DATE);
437 int displayDateYear = displayDateCal.get(Calendar.YEAR);
438 int displayDateHour = displayDateCal.get(Calendar.HOUR);
439 int displayDateMinute = displayDateCal.get(Calendar.MINUTE);
440
441 if (displayDateCal.get(Calendar.AM_PM) == Calendar.PM) {
442 displayDateHour += 12;
443 }
444
445 BlogsEntryServiceUtil.updateEntry(
446 entryId, entry.getTitle(), entry.getDescription(), content,
447 displayDateMonth, displayDateDay, displayDateYear,
448 displayDateHour, displayDateMinute, entry.getAllowPingbacks(),
449 entry.getAllowTrackbacks(), null, entry.getSmallImage(),
450 entry.getSmallImageURL(), null, null, serviceContext);
451
452 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
453
454 jsonObject.put("success", Boolean.TRUE);
455
456 writeJSON(actionRequest, actionResponse, jsonObject);
457 }
458 catch (Exception e) {
459 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
460
461 jsonObject.put("success", Boolean.FALSE);
462
463 jsonObject.putException(e);
464
465 writeJSON(actionRequest, actionResponse, jsonObject);
466 }
467 }
468
469 protected Object[] updateEntry(ActionRequest actionRequest)
470 throws Exception {
471
472 long entryId = ParamUtil.getLong(actionRequest, "entryId");
473
474 String title = ParamUtil.getString(actionRequest, "title");
475 String description = ParamUtil.getString(actionRequest, "description");
476 String content = ParamUtil.getString(actionRequest, "content");
477
478 int displayDateMonth = ParamUtil.getInteger(
479 actionRequest, "displayDateMonth");
480 int displayDateDay = ParamUtil.getInteger(
481 actionRequest, "displayDateDay");
482 int displayDateYear = ParamUtil.getInteger(
483 actionRequest, "displayDateYear");
484 int displayDateHour = ParamUtil.getInteger(
485 actionRequest, "displayDateHour");
486 int displayDateMinute = ParamUtil.getInteger(
487 actionRequest, "displayDateMinute");
488 int displayDateAmPm = ParamUtil.getInteger(
489 actionRequest, "displayDateAmPm");
490
491 if (displayDateAmPm == Calendar.PM) {
492 displayDateHour += 12;
493 }
494
495 boolean allowPingbacks = ParamUtil.getBoolean(
496 actionRequest, "allowPingbacks");
497 boolean allowTrackbacks = ParamUtil.getBoolean(
498 actionRequest, "allowTrackbacks");
499 String[] trackbacks = StringUtil.split(
500 ParamUtil.getString(actionRequest, "trackbacks"));
501
502 boolean smallImage = false;
503 String smallImageURL = null;
504 String smallImageFileName = null;
505 InputStream smallImageInputStream = null;
506
507 BlogsEntry entry = null;
508 String oldUrlTitle = null;
509
510 try {
511 boolean ajax = ParamUtil.getBoolean(actionRequest, "ajax");
512
513 if (!ajax) {
514 smallImage = ParamUtil.getBoolean(actionRequest, "smallImage");
515 smallImageURL = ParamUtil.getString(
516 actionRequest, "smallImageURL");
517
518 if (smallImage && Validator.isNull(smallImageURL)) {
519 UploadPortletRequest uploadPortletRequest =
520 PortalUtil.getUploadPortletRequest(actionRequest);
521
522 smallImageFileName = uploadPortletRequest.getFileName(
523 "smallFile");
524 smallImageInputStream =
525 uploadPortletRequest.getFileAsStream("smallFile");
526 }
527 }
528
529 ServiceContext serviceContext = ServiceContextFactory.getInstance(
530 BlogsEntry.class.getName(), actionRequest);
531
532 entry = null;
533 oldUrlTitle = StringPool.BLANK;
534
535 if (entryId <= 0) {
536
537
538
539 entry = BlogsEntryServiceUtil.addEntry(
540 title, description, content, displayDateMonth,
541 displayDateDay, displayDateYear, displayDateHour,
542 displayDateMinute, allowPingbacks, allowTrackbacks,
543 trackbacks, smallImage, smallImageURL, smallImageFileName,
544 smallImageInputStream, serviceContext);
545
546 AssetPublisherUtil.addAndStoreSelection(
547 actionRequest, BlogsEntry.class.getName(),
548 entry.getEntryId(), -1);
549 }
550 else {
551
552
553
554 entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
555
556 String tempOldUrlTitle = entry.getUrlTitle();
557
558 entry = BlogsEntryServiceUtil.updateEntry(
559 entryId, title, description, content, displayDateMonth,
560 displayDateDay, displayDateYear, displayDateHour,
561 displayDateMinute, allowPingbacks, allowTrackbacks,
562 trackbacks, smallImage, smallImageURL, smallImageFileName,
563 smallImageInputStream, serviceContext);
564
565 if (!tempOldUrlTitle.equals(entry.getUrlTitle())) {
566 oldUrlTitle = tempOldUrlTitle;
567 }
568
569 AssetPublisherUtil.addAndStoreSelection(
570 actionRequest, BlogsEntry.class.getName(),
571 entry.getEntryId(), -1);
572 }
573 }
574 finally {
575 StreamUtil.cleanUp(smallImageInputStream);
576 }
577
578 return new Object[] {entry, oldUrlTitle};
579 }
580
581 }