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