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