001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
081     * @author Brian Wing Shun Chan
082     * @author Wilson S. Man
083     * @author Thiago Moreira
084     * @author Juan Fernández
085     * @author Zsolt Berentey
086     * @author Levente Hudák
087     */
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                    String portletName = portletConfig.getPortletName();
344    
345                    if (portletName.equals(PortletKeys.BLOGS_ADMIN)) {
346                            portletURL.setParameter("struts_action", "/blogs_admin/edit_entry");
347                    }
348                    else {
349                            portletURL.setParameter("struts_action", "/blogs/edit_entry");
350                    }
351    
352                    portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
353                    portletURL.setParameter("redirect", redirect, false);
354                    portletURL.setParameter("backURL", backURL, false);
355                    portletURL.setParameter(
356                            "groupId", String.valueOf(entry.getGroupId()), false);
357                    portletURL.setParameter(
358                            "entryId", String.valueOf(entry.getEntryId()), false);
359                    portletURL.setParameter("preview", String.valueOf(preview), false);
360                    portletURL.setWindowState(actionRequest.getWindowState());
361    
362                    return portletURL.toString();
363            }
364    
365            protected void restoreEntries(ActionRequest actionRequest)
366                    throws PortalException, SystemException {
367    
368                    long[] restoreEntryIds = StringUtil.split(
369                            ParamUtil.getString(actionRequest, "restoreEntryIds"), 0L);
370    
371                    for (long restoreEntryId : restoreEntryIds) {
372                            BlogsEntryServiceUtil.restoreEntryFromTrash(restoreEntryId);
373                    }
374            }
375    
376            protected void subscribe(ActionRequest actionRequest) throws Exception {
377                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
378                            WebKeys.THEME_DISPLAY);
379    
380                    BlogsEntryServiceUtil.subscribe(themeDisplay.getScopeGroupId());
381            }
382    
383            protected void unsubscribe(ActionRequest actionRequest) throws Exception {
384                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
385                            WebKeys.THEME_DISPLAY);
386    
387                    BlogsEntryServiceUtil.unsubscribe(themeDisplay.getScopeGroupId());
388            }
389    
390            protected void updateContent(
391                            ActionRequest actionRequest, ActionResponse actionResponse)
392                    throws Exception {
393    
394                    long entryId = ParamUtil.getLong(actionRequest, "entryId");
395    
396                    String content = ParamUtil.getString(actionRequest, "content");
397    
398                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
399    
400                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
401                            actionRequest);
402    
403                    try {
404                            Calendar displayDateCal = CalendarFactoryUtil.getCalendar();
405    
406                            displayDateCal.setTime(entry.getDisplayDate());
407    
408                            int displayDateMonth = displayDateCal.get(Calendar.MONTH);
409                            int displayDateDay = displayDateCal.get(Calendar.DATE);
410                            int displayDateYear = displayDateCal.get(Calendar.YEAR);
411                            int displayDateHour = displayDateCal.get(Calendar.HOUR);
412                            int displayDateMinute = displayDateCal.get(Calendar.MINUTE);
413    
414                            if (displayDateCal.get(Calendar.AM_PM) == Calendar.PM) {
415                                    displayDateHour += 12;
416                            }
417    
418                            BlogsEntryServiceUtil.updateEntry(
419                                    entryId, entry.getTitle(), entry.getDescription(), content,
420                                    displayDateMonth, displayDateDay, displayDateYear,
421                                    displayDateHour, displayDateMinute, entry.getAllowPingbacks(),
422                                    entry.getAllowTrackbacks(), null, entry.getSmallImage(),
423                                    entry.getSmallImageURL(), null, null, serviceContext);
424    
425                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
426    
427                            jsonObject.put("success", Boolean.TRUE);
428    
429                            writeJSON(actionRequest, actionResponse, jsonObject);
430                    }
431                    catch (Exception e) {
432                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
433    
434                            jsonObject.put("success", Boolean.FALSE);
435    
436                            jsonObject.putException(e);
437    
438                            writeJSON(actionRequest, actionResponse, jsonObject);
439                    }
440            }
441    
442            protected Object[] updateEntry(ActionRequest actionRequest)
443                    throws Exception {
444    
445                    long entryId = ParamUtil.getLong(actionRequest, "entryId");
446    
447                    String title = ParamUtil.getString(actionRequest, "title");
448                    String description = ParamUtil.getString(actionRequest, "description");
449                    String content = ParamUtil.getString(actionRequest, "content");
450    
451                    int displayDateMonth = ParamUtil.getInteger(
452                            actionRequest, "displayDateMonth");
453                    int displayDateDay = ParamUtil.getInteger(
454                            actionRequest, "displayDateDay");
455                    int displayDateYear = ParamUtil.getInteger(
456                            actionRequest, "displayDateYear");
457                    int displayDateHour = ParamUtil.getInteger(
458                            actionRequest, "displayDateHour");
459                    int displayDateMinute = ParamUtil.getInteger(
460                            actionRequest, "displayDateMinute");
461                    int displayDateAmPm = ParamUtil.getInteger(
462                            actionRequest, "displayDateAmPm");
463    
464                    if (displayDateAmPm == Calendar.PM) {
465                            displayDateHour += 12;
466                    }
467    
468                    boolean allowPingbacks = ParamUtil.getBoolean(
469                            actionRequest, "allowPingbacks");
470                    boolean allowTrackbacks = ParamUtil.getBoolean(
471                            actionRequest, "allowTrackbacks");
472                    String[] trackbacks = StringUtil.split(
473                            ParamUtil.getString(actionRequest, "trackbacks"));
474    
475                    boolean smallImage = false;
476                    String smallImageURL = null;
477                    String smallImageFileName = null;
478                    InputStream smallImageInputStream = null;
479    
480                    BlogsEntry entry = null;
481                    String oldUrlTitle = null;
482    
483                    try {
484                            boolean ajax = ParamUtil.getBoolean(actionRequest, "ajax");
485    
486                            if (!ajax) {
487                                    smallImage = ParamUtil.getBoolean(actionRequest, "smallImage");
488                                    smallImageURL = ParamUtil.getString(
489                                            actionRequest, "smallImageURL");
490    
491                                    if (smallImage && Validator.isNull(smallImageURL)) {
492                                            boolean attachments = ParamUtil.getBoolean(
493                                                    actionRequest, "attachments");
494    
495                                            if (attachments) {
496                                                    UploadPortletRequest uploadPortletRequest =
497                                                            PortalUtil.getUploadPortletRequest(actionRequest);
498    
499                                                    smallImageFileName = uploadPortletRequest.getFileName(
500                                                            "smallFile");
501                                                    smallImageInputStream =
502                                                            uploadPortletRequest.getFileAsStream("smallFile");
503                                            }
504                                    }
505                            }
506    
507                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
508                                    BlogsEntry.class.getName(), actionRequest);
509    
510                            entry = null;
511                            oldUrlTitle = StringPool.BLANK;
512    
513                            if (entryId <= 0) {
514    
515                                    // Add entry
516    
517                                    entry = BlogsEntryServiceUtil.addEntry(
518                                            title, description, content, displayDateMonth,
519                                            displayDateDay, displayDateYear, displayDateHour,
520                                            displayDateMinute, allowPingbacks, allowTrackbacks,
521                                            trackbacks, smallImage, smallImageURL, smallImageFileName,
522                                            smallImageInputStream, serviceContext);
523    
524                                    AssetPublisherUtil.addAndStoreSelection(
525                                            actionRequest, BlogsEntry.class.getName(),
526                                            entry.getEntryId(), -1);
527                            }
528                            else {
529    
530                                    // Update entry
531    
532                                    entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
533    
534                                    String tempOldUrlTitle = entry.getUrlTitle();
535    
536                                    entry = BlogsEntryServiceUtil.updateEntry(
537                                            entryId, title, description, content, displayDateMonth,
538                                            displayDateDay, displayDateYear, displayDateHour,
539                                            displayDateMinute, allowPingbacks, allowTrackbacks,
540                                            trackbacks, smallImage, smallImageURL, smallImageFileName,
541                                            smallImageInputStream, serviceContext);
542    
543                                    if (!tempOldUrlTitle.equals(entry.getUrlTitle())) {
544                                            oldUrlTitle = tempOldUrlTitle;
545                                    }
546    
547                                    AssetPublisherUtil.addAndStoreSelection(
548                                            actionRequest, BlogsEntry.class.getName(),
549                                            entry.getEntryId(), -1);
550                            }
551                    }
552                    finally {
553                            StreamUtil.cleanUp(smallImageInputStream);
554                    }
555    
556                    return new Object[] {entry, oldUrlTitle};
557            }
558    
559    }