001    /**
002     * Copyright (c) 2000-2010 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.io.unsync.UnsyncByteArrayInputStream;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.servlet.SessionErrors;
021    import com.liferay.portal.kernel.util.Constants;
022    import com.liferay.portal.kernel.util.ContentTypes;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.kernel.workflow.WorkflowConstants;
028    import com.liferay.portal.model.LayoutTypePortlet;
029    import com.liferay.portal.security.auth.PrincipalException;
030    import com.liferay.portal.security.permission.ActionKeys;
031    import com.liferay.portal.security.permission.PermissionChecker;
032    import com.liferay.portal.service.ServiceContext;
033    import com.liferay.portal.service.ServiceContextFactory;
034    import com.liferay.portal.service.SubscriptionLocalServiceUtil;
035    import com.liferay.portal.struts.ActionConstants;
036    import com.liferay.portal.struts.PortletAction;
037    import com.liferay.portal.theme.ThemeDisplay;
038    import com.liferay.portal.util.PortalUtil;
039    import com.liferay.portal.util.PropsValues;
040    import com.liferay.portal.util.WebKeys;
041    import com.liferay.portlet.asset.AssetTagException;
042    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
043    import com.liferay.portlet.blogs.EntryContentException;
044    import com.liferay.portlet.blogs.EntryDisplayDateException;
045    import com.liferay.portlet.blogs.EntryTitleException;
046    import com.liferay.portlet.blogs.NoSuchEntryException;
047    import com.liferay.portlet.blogs.model.BlogsEntry;
048    import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
049    import com.liferay.portlet.blogs.service.BlogsEntryServiceUtil;
050    import com.liferay.portlet.blogs.service.permission.BlogsPermission;
051    import com.liferay.util.servlet.ServletResponseUtil;
052    
053    import java.io.InputStream;
054    
055    import java.util.Calendar;
056    
057    import javax.portlet.ActionRequest;
058    import javax.portlet.ActionResponse;
059    import javax.portlet.PortletConfig;
060    import javax.portlet.RenderRequest;
061    import javax.portlet.RenderResponse;
062    
063    import javax.servlet.http.HttpServletRequest;
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     */
075    public class EditEntryAction extends PortletAction {
076    
077            public void processAction(
078                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
079                            ActionRequest actionRequest, ActionResponse actionResponse)
080                    throws Exception {
081    
082                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
083    
084                    try {
085                            BlogsEntry entry = null;
086                            String oldUrlTitle = StringPool.BLANK;
087    
088                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
089                                    Object[] returnValue = updateEntry(actionRequest);
090    
091                                    entry = (BlogsEntry)returnValue[0];
092                                    oldUrlTitle = ((String)returnValue[1]);
093                            }
094                            else if (cmd.equals(Constants.DELETE)) {
095                                    deleteEntry(actionRequest);
096                            }
097                            else if (cmd.equals(Constants.SUBSCRIBE)) {
098                                    subscribe(actionRequest);
099                            }
100                            else if (cmd.equals(Constants.UNSUBSCRIBE)) {
101                                    unsubscribe(actionRequest);
102                            }
103    
104                            String redirect = ParamUtil.getString(actionRequest, "redirect");
105                            boolean updateRedirect = false;
106    
107                            if (redirect.indexOf(
108                                            "/blogs/" + oldUrlTitle + "/maximized") != -1) {
109    
110                                    oldUrlTitle += "/maximized";
111                            }
112    
113                            if ((entry != null) && (Validator.isNotNull(oldUrlTitle)) &&
114                                    (redirect.endsWith("/blogs/" + oldUrlTitle) ||
115                                     redirect.indexOf("/blogs/" + oldUrlTitle + "?") != -1)) {
116    
117                                    int pos = redirect.indexOf("?");
118    
119                                    if (pos == -1) {
120                                            pos = redirect.length();
121                                    }
122    
123                                    String newRedirect = redirect.substring(
124                                            0, pos - oldUrlTitle.length());
125    
126                                    newRedirect += entry.getUrlTitle();
127    
128                                    if (oldUrlTitle.indexOf("/maximized") != -1) {
129                                            newRedirect += "/maximized";
130                                    }
131    
132                                    if (pos < redirect.length()) {
133                                            newRedirect +=
134                                                    "?" + redirect.substring(pos + 1, redirect.length());
135                                    }
136    
137                                    redirect = newRedirect;
138                                    updateRedirect = true;
139                            }
140    
141                            int workflowAction = ParamUtil.getInteger(
142                                    actionRequest, "workflowAction");
143    
144                            if ((entry != null) &&
145                                    (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT)) {
146    
147                                    JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
148    
149                                    jsonObj.put("entryId", entry.getEntryId());
150                                    jsonObj.put("redirect", redirect);
151                                    jsonObj.put("updateRedirect", updateRedirect);
152    
153                                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
154                                            actionRequest);
155                                    HttpServletResponse response =
156                                            PortalUtil.getHttpServletResponse(actionResponse);
157                                    InputStream is = new UnsyncByteArrayInputStream(
158                                            jsonObj.toString().getBytes());
159                                    String contentType = ContentTypes.TEXT_JAVASCRIPT;
160    
161                                    ServletResponseUtil.sendFile(
162                                            request, response, null, is, contentType);
163    
164                                    setForward(actionRequest, ActionConstants.COMMON_NULL);
165                            }
166                            else {
167                                    ThemeDisplay themeDisplay =
168                                            (ThemeDisplay)actionRequest.getAttribute(
169                                                    WebKeys.THEME_DISPLAY);
170    
171                                    LayoutTypePortlet layoutTypePortlet =
172                                            themeDisplay.getLayoutTypePortlet();
173    
174                                    if (layoutTypePortlet.hasPortletId(
175                                                    portletConfig.getPortletName())) {
176    
177                                            sendRedirect(actionRequest, actionResponse, redirect);
178                                    }
179                                    else {
180                                            actionResponse.sendRedirect(redirect);
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 EntryTitleException) {
195    
196                                    SessionErrors.add(actionRequest, e.getClass().getName());
197                            }
198                            else if (e instanceof AssetTagException) {
199                                    SessionErrors.add(actionRequest, e.getClass().getName(), e);
200                            }
201                            else {
202                                    throw e;
203                            }
204                    }
205            }
206    
207            public ActionForward render(
208                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
209                            RenderRequest renderRequest, RenderResponse renderResponse)
210                    throws Exception {
211    
212                    try {
213                            ActionUtil.getEntry(renderRequest);
214    
215                            if (PropsValues.BLOGS_PINGBACK_ENABLED) {
216                                    BlogsEntry entry = (BlogsEntry)renderRequest.getAttribute(
217                                            WebKeys.BLOGS_ENTRY);
218    
219                                    if ((entry != null) && entry.isAllowPingbacks()) {
220                                            HttpServletResponse response =
221                                                    PortalUtil.getHttpServletResponse(renderResponse);
222    
223                                            response.addHeader(
224                                                    "X-Pingback",
225                                                    PortalUtil.getPortalURL(renderRequest) +
226                                                            "/xmlrpc/pingback");
227                                    }
228                            }
229                    }
230                    catch (Exception e) {
231                            if (e instanceof NoSuchEntryException ||
232                                    e instanceof PrincipalException) {
233    
234                                    SessionErrors.add(renderRequest, e.getClass().getName());
235    
236                                    return mapping.findForward("portlet.blogs.error");
237                            }
238                            else {
239                                    throw e;
240                            }
241                    }
242    
243                    return mapping.findForward(
244                            getForward(renderRequest, "portlet.blogs.edit_entry"));
245            }
246    
247            protected void deleteEntry(ActionRequest actionRequest) throws Exception {
248                    long entryId = ParamUtil.getLong(actionRequest, "entryId");
249    
250                    BlogsEntryServiceUtil.deleteEntry(entryId);
251            }
252    
253            protected void subscribe(ActionRequest actionRequest) throws Exception {
254                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
255                            WebKeys.THEME_DISPLAY);
256    
257                    PermissionChecker permissionChecker =
258                            themeDisplay.getPermissionChecker();
259    
260                    if (BlogsPermission.contains(
261                                    permissionChecker, themeDisplay.getScopeGroupId(),
262                                    ActionKeys.SUBSCRIBE)) {
263    
264                            SubscriptionLocalServiceUtil.addSubscription(
265                                    themeDisplay.getUserId(), BlogsEntry.class.getName(),
266                                    themeDisplay.getScopeGroupId());
267                    }
268            }
269    
270            protected void unsubscribe(ActionRequest actionRequest) throws Exception {
271                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
272                            WebKeys.THEME_DISPLAY);
273    
274                    PermissionChecker permissionChecker =
275                            themeDisplay.getPermissionChecker();
276    
277                    if (BlogsPermission.contains(
278                                    permissionChecker, themeDisplay.getScopeGroupId(),
279                                    ActionKeys.SUBSCRIBE)) {
280    
281                            SubscriptionLocalServiceUtil.deleteSubscription(
282                                    themeDisplay.getUserId(), BlogsEntry.class.getName(),
283                                    themeDisplay.getScopeGroupId());
284                    }
285            }
286    
287            protected Object[] updateEntry(ActionRequest actionRequest)
288                    throws Exception {
289    
290                    long entryId = ParamUtil.getLong(actionRequest, "entryId");
291    
292                    String title = ParamUtil.getString(actionRequest, "title");
293                    String content = ParamUtil.getString(actionRequest, "content");
294    
295                    int displayDateMonth = ParamUtil.getInteger(
296                            actionRequest, "displayDateMonth");
297                    int displayDateDay = ParamUtil.getInteger(
298                            actionRequest, "displayDateDay");
299                    int displayDateYear = ParamUtil.getInteger(
300                            actionRequest, "displayDateYear");
301                    int displayDateHour = ParamUtil.getInteger(
302                            actionRequest, "displayDateHour");
303                    int displayDateMinute = ParamUtil.getInteger(
304                            actionRequest, "displayDateMinute");
305                    int displayDateAmPm = ParamUtil.getInteger(
306                            actionRequest, "displayDateAmPm");
307    
308                    if (displayDateAmPm == Calendar.PM) {
309                            displayDateHour += 12;
310                    }
311    
312                    boolean allowPingbacks = ParamUtil.getBoolean(
313                            actionRequest, "allowPingbacks");
314                    boolean allowTrackbacks = ParamUtil.getBoolean(
315                            actionRequest, "allowTrackbacks");
316                    String[] trackbacks = StringUtil.split(
317                            ParamUtil.getString(actionRequest, "trackbacks"));
318    
319                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
320                            BlogsEntry.class.getName(), actionRequest);
321    
322                    BlogsEntry entry = null;
323                    String oldUrlTitle = StringPool.BLANK;
324    
325                    if (entryId <= 0) {
326    
327                            // Add entry
328    
329                            entry = BlogsEntryServiceUtil.addEntry(
330                                    title, content, displayDateMonth, displayDateDay,
331                                    displayDateYear, displayDateHour, displayDateMinute,
332                                    allowPingbacks, allowTrackbacks, trackbacks, serviceContext);
333    
334                            AssetPublisherUtil.addAndStoreSelection(
335                                    actionRequest, BlogsEntry.class.getName(), entry.getEntryId(),
336                                    -1);
337                    }
338                    else {
339    
340                            // Update entry
341    
342                            entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
343    
344                            String tempOldUrlTitle = entry.getUrlTitle();
345    
346                            entry = BlogsEntryServiceUtil.updateEntry(
347                                    entryId, title, content, displayDateMonth, displayDateDay,
348                                    displayDateYear, displayDateHour, displayDateMinute,
349                                    allowPingbacks, allowTrackbacks, trackbacks, serviceContext);
350    
351                            if (!tempOldUrlTitle.equals(entry.getUrlTitle())) {
352                                    oldUrlTitle = tempOldUrlTitle;
353                            }
354    
355                            AssetPublisherUtil.addAndStoreSelection(
356                                    actionRequest, BlogsEntry.class.getName(), entry.getEntryId(),
357                                    -1);
358                    }
359    
360                    return new Object[] {entry, oldUrlTitle};
361            }
362    
363    }