001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 += "?" + redirect.substring(pos + 1);
136                                    }
137    
138                                    redirect = newRedirect;
139                                    updateRedirect = true;
140                            }
141    
142                            int workflowAction = ParamUtil.getInteger(
143                                    actionRequest, "workflowAction",
144                                    WorkflowConstants.ACTION_SAVE_DRAFT);
145    
146                            boolean ajax = ParamUtil.getBoolean(actionRequest, "ajax");
147    
148                            if (ajax) {
149                                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
150    
151                                    jsonObject.put("entryId", entry.getEntryId());
152                                    jsonObject.put("redirect", redirect);
153                                    jsonObject.put("updateRedirect", updateRedirect);
154    
155                                    writeJSON(actionRequest, actionResponse, jsonObject);
156    
157                                    return;
158                            }
159    
160                            if ((entry != null) &&
161                                    (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT)) {
162    
163                                    redirect = getSaveAndContinueRedirect(
164                                            portletConfig, actionRequest, entry, redirect);
165    
166                                    sendRedirect(actionRequest, actionResponse, redirect);
167                            }
168                            else {
169                                    WindowState windowState = actionRequest.getWindowState();
170    
171                                    if (!windowState.equals(LiferayWindowState.POP_UP)) {
172                                            sendRedirect(actionRequest, actionResponse, redirect);
173                                    }
174                                    else {
175                                            redirect = PortalUtil.escapeRedirect(redirect);
176    
177                                            if (Validator.isNotNull(redirect)) {
178                                                    actionResponse.sendRedirect(redirect);
179                                            }
180                                    }
181                            }
182                    }
183                    catch (Exception e) {
184                            if (e instanceof NoSuchEntryException ||
185                                    e instanceof PrincipalException) {
186    
187                                    SessionErrors.add(actionRequest, e.getClass());
188    
189                                    setForward(actionRequest, "portlet.blogs.error");
190                            }
191                            else if (e instanceof EntryContentException ||
192                                             e instanceof EntryDisplayDateException ||
193                                             e instanceof EntrySmallImageNameException ||
194                                             e instanceof EntrySmallImageSizeException ||
195                                             e instanceof EntryTitleException) {
196    
197                                    SessionErrors.add(actionRequest, e.getClass());
198                            }
199                            else if (e instanceof AssetCategoryException ||
200                                             e instanceof AssetTagException) {
201    
202                                    SessionErrors.add(actionRequest, e.getClass(), e);
203                            }
204                            else {
205                                    throw e;
206                            }
207                    }
208            }
209    
210            @Override
211            public ActionForward render(
212                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
213                            RenderRequest renderRequest, RenderResponse renderResponse)
214                    throws Exception {
215    
216                    try {
217                            ActionUtil.getEntry(renderRequest);
218    
219                            if (PropsValues.BLOGS_PINGBACK_ENABLED) {
220                                    BlogsEntry entry = (BlogsEntry)renderRequest.getAttribute(
221                                            WebKeys.BLOGS_ENTRY);
222    
223                                    if ((entry != null) && entry.isAllowPingbacks()) {
224                                            HttpServletResponse response =
225                                                    PortalUtil.getHttpServletResponse(renderResponse);
226    
227                                            response.addHeader(
228                                                    "X-Pingback",
229                                                    PortalUtil.getPortalURL(renderRequest) +
230                                                            "/xmlrpc/pingback");
231                                    }
232                            }
233                    }
234                    catch (Exception e) {
235                            if (e instanceof NoSuchEntryException ||
236                                    e instanceof PrincipalException) {
237    
238                                    SessionErrors.add(renderRequest, e.getClass());
239    
240                                    return mapping.findForward("portlet.blogs.error");
241                            }
242                            else {
243                                    throw e;
244                            }
245                    }
246    
247                    return mapping.findForward(
248                            getForward(renderRequest, "portlet.blogs.edit_entry"));
249            }
250    
251            protected void deleteEntries(ActionRequest actionRequest) throws Exception {
252                    long entryId = ParamUtil.getLong(actionRequest, "entryId");
253    
254                    if (entryId > 0) {
255                            BlogsEntryServiceUtil.deleteEntry(entryId);
256                    }
257                    else {
258                            long[] deleteEntryIds = StringUtil.split(
259                                    ParamUtil.getString(actionRequest, "deleteEntryIds"), 0L);
260    
261                            for (int i = 0; i < deleteEntryIds.length; i++) {
262                                    BlogsEntryServiceUtil.deleteEntry(deleteEntryIds[i]);
263                            }
264                    }
265            }
266    
267            protected String getSaveAndContinueRedirect(
268                            PortletConfig portletConfig, ActionRequest actionRequest,
269                            BlogsEntry entry, String redirect)
270                    throws Exception {
271    
272                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
273                            WebKeys.THEME_DISPLAY);
274    
275                    String backURL = ParamUtil.getString(actionRequest, "backURL");
276    
277                    boolean preview = ParamUtil.getBoolean(actionRequest, "preview");
278    
279                    PortletURLImpl portletURL = new PortletURLImpl(
280                            actionRequest, portletConfig.getPortletName(),
281                            themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
282    
283                    portletURL.setWindowState(actionRequest.getWindowState());
284    
285                    String portletName = portletConfig.getPortletName();
286    
287                    if (portletName.equals(PortletKeys.BLOGS_ADMIN)) {
288                            portletURL.setParameter("struts_action", "/blogs_admin/edit_entry");
289                    }
290                    else {
291                            portletURL.setParameter("struts_action", "/blogs/edit_entry");
292                    }
293    
294                    portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
295                    portletURL.setParameter("redirect", redirect, false);
296                    portletURL.setParameter("backURL", backURL, false);
297                    portletURL.setParameter(
298                            "groupId", String.valueOf(entry.getGroupId()), false);
299                    portletURL.setParameter(
300                            "entryId", String.valueOf(entry.getEntryId()), false);
301                    portletURL.setParameter("preview", String.valueOf(preview), false);
302    
303                    return portletURL.toString();
304            }
305    
306            protected void subscribe(ActionRequest actionRequest) throws Exception {
307                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
308                            WebKeys.THEME_DISPLAY);
309    
310                    BlogsEntryServiceUtil.subscribe(themeDisplay.getScopeGroupId());
311            }
312    
313            protected void unsubscribe(ActionRequest actionRequest) throws Exception {
314                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
315                            WebKeys.THEME_DISPLAY);
316    
317                    BlogsEntryServiceUtil.unsubscribe(themeDisplay.getScopeGroupId());
318            }
319    
320            protected Object[] updateEntry(ActionRequest actionRequest)
321                    throws Exception {
322    
323                    long entryId = ParamUtil.getLong(actionRequest, "entryId");
324    
325                    String title = ParamUtil.getString(actionRequest, "title");
326                    String description = ParamUtil.getString(actionRequest, "description");
327                    String content = ParamUtil.getString(actionRequest, "content");
328    
329                    int displayDateMonth = ParamUtil.getInteger(
330                            actionRequest, "displayDateMonth");
331                    int displayDateDay = ParamUtil.getInteger(
332                            actionRequest, "displayDateDay");
333                    int displayDateYear = ParamUtil.getInteger(
334                            actionRequest, "displayDateYear");
335                    int displayDateHour = ParamUtil.getInteger(
336                            actionRequest, "displayDateHour");
337                    int displayDateMinute = ParamUtil.getInteger(
338                            actionRequest, "displayDateMinute");
339                    int displayDateAmPm = ParamUtil.getInteger(
340                            actionRequest, "displayDateAmPm");
341    
342                    if (displayDateAmPm == Calendar.PM) {
343                            displayDateHour += 12;
344                    }
345    
346                    boolean allowPingbacks = ParamUtil.getBoolean(
347                            actionRequest, "allowPingbacks");
348                    boolean allowTrackbacks = ParamUtil.getBoolean(
349                            actionRequest, "allowTrackbacks");
350                    String[] trackbacks = StringUtil.split(
351                            ParamUtil.getString(actionRequest, "trackbacks"));
352    
353                    boolean smallImage = false;
354                    String smallImageURL = null;
355                    String smallImageFileName = null;
356                    InputStream smallImageInputStream = null;
357    
358                    BlogsEntry entry = null;
359                    String oldUrlTitle = null;
360    
361                    try {
362                            boolean ajax = ParamUtil.getBoolean(actionRequest, "ajax");
363    
364                            if (!ajax) {
365                                    smallImage = ParamUtil.getBoolean(actionRequest, "smallImage");
366                                    smallImageURL = ParamUtil.getString(
367                                            actionRequest, "smallImageURL");
368    
369                                    if (smallImage && Validator.isNull(smallImageURL)) {
370                                            boolean attachments = ParamUtil.getBoolean(
371                                                    actionRequest, "attachments");
372    
373                                            if (attachments) {
374                                                    UploadPortletRequest uploadPortletRequest =
375                                                            PortalUtil.getUploadPortletRequest(actionRequest);
376    
377                                                    smallImageFileName = uploadPortletRequest.getFileName(
378                                                            "smallFile");
379                                                    smallImageInputStream =
380                                                            uploadPortletRequest.getFileAsStream("smallFile");
381                                            }
382                                    }
383                            }
384    
385                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
386                                    BlogsEntry.class.getName(), actionRequest);
387    
388                            entry = null;
389                            oldUrlTitle = StringPool.BLANK;
390    
391                            if (entryId <= 0) {
392    
393                                    // Add entry
394    
395                                    entry = BlogsEntryServiceUtil.addEntry(
396                                            title, description, content, displayDateMonth,
397                                            displayDateDay, displayDateYear, displayDateHour,
398                                            displayDateMinute, allowPingbacks, allowTrackbacks,
399                                            trackbacks, smallImage, smallImageURL, smallImageFileName,
400                                            smallImageInputStream, serviceContext);
401    
402                                    AssetPublisherUtil.addAndStoreSelection(
403                                            actionRequest, BlogsEntry.class.getName(),
404                                            entry.getEntryId(), -1);
405                            }
406                            else {
407    
408                                    // Update entry
409    
410                                    entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
411    
412                                    String tempOldUrlTitle = entry.getUrlTitle();
413    
414                                    entry = BlogsEntryServiceUtil.updateEntry(
415                                            entryId, title, description, content, displayDateMonth,
416                                            displayDateDay, displayDateYear, displayDateHour,
417                                            displayDateMinute, allowPingbacks, allowTrackbacks,
418                                            trackbacks, smallImage, smallImageURL, smallImageFileName,
419                                            smallImageInputStream, serviceContext);
420    
421                                    if (!tempOldUrlTitle.equals(entry.getUrlTitle())) {
422                                            oldUrlTitle = tempOldUrlTitle;
423                                    }
424    
425                                    AssetPublisherUtil.addAndStoreSelection(
426                                            actionRequest, BlogsEntry.class.getName(),
427                                            entry.getEntryId(), -1);
428                            }
429                    }
430                    finally {
431                            StreamUtil.cleanUp(smallImageInputStream);
432                    }
433    
434                    return new Object[] {entry, oldUrlTitle};
435            }
436    
437    }