1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.blogs.action;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
18  import com.liferay.portal.kernel.json.JSONFactoryUtil;
19  import com.liferay.portal.kernel.json.JSONObject;
20  import com.liferay.portal.kernel.servlet.SessionErrors;
21  import com.liferay.portal.kernel.util.Constants;
22  import com.liferay.portal.kernel.util.ContentTypes;
23  import com.liferay.portal.kernel.util.ParamUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.model.LayoutTypePortlet;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.security.permission.ActionKeys;
30  import com.liferay.portal.security.permission.PermissionChecker;
31  import com.liferay.portal.service.ServiceContext;
32  import com.liferay.portal.service.ServiceContextFactory;
33  import com.liferay.portal.service.SubscriptionLocalServiceUtil;
34  import com.liferay.portal.struts.ActionConstants;
35  import com.liferay.portal.struts.PortletAction;
36  import com.liferay.portal.theme.ThemeDisplay;
37  import com.liferay.portal.util.PortalUtil;
38  import com.liferay.portal.util.WebKeys;
39  import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
40  import com.liferay.portlet.blogs.EntryContentException;
41  import com.liferay.portlet.blogs.EntryDisplayDateException;
42  import com.liferay.portlet.blogs.EntryTitleException;
43  import com.liferay.portlet.blogs.NoSuchEntryException;
44  import com.liferay.portlet.blogs.model.BlogsEntry;
45  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
46  import com.liferay.portlet.blogs.service.BlogsEntryServiceUtil;
47  import com.liferay.portlet.blogs.service.permission.BlogsPermission;
48  import com.liferay.portlet.tags.TagsEntryException;
49  import com.liferay.util.servlet.ServletResponseUtil;
50  
51  import java.io.InputStream;
52  
53  import java.util.Calendar;
54  
55  import javax.portlet.ActionRequest;
56  import javax.portlet.ActionResponse;
57  import javax.portlet.PortletConfig;
58  import javax.portlet.RenderRequest;
59  import javax.portlet.RenderResponse;
60  
61  import javax.servlet.http.HttpServletRequest;
62  import javax.servlet.http.HttpServletResponse;
63  
64  import org.apache.struts.action.ActionForm;
65  import org.apache.struts.action.ActionForward;
66  import org.apache.struts.action.ActionMapping;
67  
68  /**
69   * <a href="EditEntryAction.java.html"><b><i>View Source</i></b></a>
70   *
71   * @author Brian Wing Shun Chan
72   * @author Wilson S. Man
73   * @author Thiago Moreira
74   */
75  public class EditEntryAction extends PortletAction {
76  
77      public void processAction(
78              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
79              ActionRequest actionRequest, ActionResponse actionResponse)
80          throws Exception {
81  
82          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
83  
84          try {
85              BlogsEntry entry = null;
86              String oldUrlTitle = StringPool.BLANK;
87  
88              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
89                  Object[] returnValue = updateEntry(actionRequest);
90  
91                  entry = (BlogsEntry)returnValue[0];
92                  oldUrlTitle = ((String)returnValue[1]);
93              }
94              else if (cmd.equals(Constants.DELETE)) {
95                  deleteEntry(actionRequest);
96              }
97              else if (cmd.equals(Constants.SUBSCRIBE)) {
98                  subscribe(actionRequest);
99              }
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             if ((entry != null) && entry.isDraft()) {
142                 JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
143 
144                 jsonObj.put("entryId", entry.getEntryId());
145                 jsonObj.put("redirect", redirect);
146                 jsonObj.put("updateRedirect", updateRedirect);
147 
148                 HttpServletRequest request = PortalUtil.getHttpServletRequest(
149                     actionRequest);
150                 HttpServletResponse response =
151                     PortalUtil.getHttpServletResponse(actionResponse);
152                 InputStream is = new UnsyncByteArrayInputStream(
153                     jsonObj.toString().getBytes());
154                 String contentType = ContentTypes.TEXT_JAVASCRIPT;
155 
156                 ServletResponseUtil.sendFile(
157                     request, response, null, is, contentType);
158 
159                 setForward(actionRequest, ActionConstants.COMMON_NULL);
160             }
161             else {
162                 ThemeDisplay themeDisplay =
163                     (ThemeDisplay)actionRequest.getAttribute(
164                         WebKeys.THEME_DISPLAY);
165 
166                 LayoutTypePortlet layoutTypePortlet =
167                     themeDisplay.getLayoutTypePortlet();
168 
169                 if (layoutTypePortlet.hasPortletId(
170                         portletConfig.getPortletName())) {
171 
172                     sendRedirect(actionRequest, actionResponse, redirect);
173                 }
174                 else {
175                     actionResponse.sendRedirect(redirect);
176                 }
177             }
178         }
179         catch (Exception e) {
180             if (e instanceof NoSuchEntryException ||
181                 e instanceof PrincipalException) {
182 
183                 SessionErrors.add(actionRequest, e.getClass().getName());
184 
185                 setForward(actionRequest, "portlet.blogs.error");
186             }
187             else if (e instanceof EntryContentException ||
188                      e instanceof EntryDisplayDateException ||
189                      e instanceof EntryTitleException) {
190 
191                 SessionErrors.add(actionRequest, e.getClass().getName());
192             }
193             else if (e instanceof TagsEntryException) {
194                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
195             }
196             else {
197                 throw e;
198             }
199         }
200     }
201 
202     public ActionForward render(
203             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
204             RenderRequest renderRequest, RenderResponse renderResponse)
205         throws Exception {
206 
207         try {
208             ActionUtil.getEntry(renderRequest);
209         }
210         catch (Exception e) {
211             if (e instanceof NoSuchEntryException ||
212                 e instanceof PrincipalException) {
213 
214                 SessionErrors.add(renderRequest, e.getClass().getName());
215 
216                 return mapping.findForward("portlet.blogs.error");
217             }
218             else {
219                 throw e;
220             }
221         }
222 
223         return mapping.findForward(
224             getForward(renderRequest, "portlet.blogs.edit_entry"));
225     }
226 
227     protected void deleteEntry(ActionRequest actionRequest) throws Exception {
228         long entryId = ParamUtil.getLong(actionRequest, "entryId");
229 
230         BlogsEntryServiceUtil.deleteEntry(entryId);
231     }
232 
233     protected void subscribe(ActionRequest actionRequest) throws Exception {
234         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
235             WebKeys.THEME_DISPLAY);
236 
237         PermissionChecker permissionChecker =
238             themeDisplay.getPermissionChecker();
239 
240         if (BlogsPermission.contains(
241                 permissionChecker, themeDisplay.getScopeGroupId(),
242                 ActionKeys.SUBSCRIBE)) {
243 
244             SubscriptionLocalServiceUtil.addSubscription(
245                 themeDisplay.getUserId(), BlogsEntry.class.getName(),
246                 themeDisplay.getScopeGroupId());
247         }
248     }
249 
250     protected void unsubscribe(ActionRequest actionRequest) throws Exception {
251         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
252             WebKeys.THEME_DISPLAY);
253 
254         PermissionChecker permissionChecker =
255             themeDisplay.getPermissionChecker();
256 
257         if (BlogsPermission.contains(
258                 permissionChecker, themeDisplay.getScopeGroupId(),
259                 ActionKeys.SUBSCRIBE)) {
260 
261             SubscriptionLocalServiceUtil.deleteSubscription(
262                 themeDisplay.getUserId(), BlogsEntry.class.getName(),
263                 themeDisplay.getScopeGroupId());
264         }
265     }
266 
267     protected Object[] updateEntry(ActionRequest actionRequest)
268         throws Exception {
269 
270         long entryId = ParamUtil.getLong(actionRequest, "entryId");
271 
272         String title = ParamUtil.getString(actionRequest, "title");
273         String content = ParamUtil.getString(actionRequest, "content");
274 
275         int displayDateMonth = ParamUtil.getInteger(
276             actionRequest, "displayDateMonth");
277         int displayDateDay = ParamUtil.getInteger(
278             actionRequest, "displayDateDay");
279         int displayDateYear = ParamUtil.getInteger(
280             actionRequest, "displayDateYear");
281         int displayDateHour = ParamUtil.getInteger(
282             actionRequest, "displayDateHour");
283         int displayDateMinute = ParamUtil.getInteger(
284             actionRequest, "displayDateMinute");
285         int displayDateAmPm = ParamUtil.getInteger(
286             actionRequest, "displayDateAmPm");
287 
288         if (displayDateAmPm == Calendar.PM) {
289             displayDateHour += 12;
290         }
291 
292         boolean draft = ParamUtil.getBoolean(actionRequest, "draft");
293         boolean allowTrackbacks = ParamUtil.getBoolean(
294             actionRequest, "allowTrackbacks");
295         String[] trackbacks = StringUtil.split(
296             ParamUtil.getString(actionRequest, "trackbacks"));
297 
298         ServiceContext serviceContext = ServiceContextFactory.getInstance(
299             BlogsEntry.class.getName(), actionRequest);
300 
301         BlogsEntry entry = null;
302         String oldUrlTitle = StringPool.BLANK;
303 
304         if (entryId <= 0) {
305 
306             // Add entry
307 
308             entry = BlogsEntryServiceUtil.addEntry(
309                 title, content, displayDateMonth, displayDateDay,
310                 displayDateYear, displayDateHour, displayDateMinute, draft,
311                 allowTrackbacks, trackbacks, serviceContext);
312 
313             if (!draft) {
314                 AssetPublisherUtil.addAndStoreSelection(
315                     actionRequest, BlogsEntry.class.getName(),
316                     entry.getEntryId(), -1);
317             }
318         }
319         else {
320 
321             // Update entry
322 
323             entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
324 
325             String tempOldUrlTitle = entry.getUrlTitle();
326             boolean oldDraft = entry.isDraft();
327 
328             entry = BlogsEntryServiceUtil.updateEntry(
329                 entryId, title, content, displayDateMonth, displayDateDay,
330                 displayDateYear, displayDateHour, displayDateMinute, draft,
331                 allowTrackbacks, trackbacks, serviceContext);
332 
333             if (!tempOldUrlTitle.equals(entry.getUrlTitle())) {
334                 oldUrlTitle = tempOldUrlTitle;
335             }
336 
337             if (oldDraft && !draft) {
338                 AssetPublisherUtil.addAndStoreSelection(
339                     actionRequest, BlogsEntry.class.getName(),
340                     entry.getEntryId(), -1);
341             }
342         }
343 
344         return new Object[] {entry, oldUrlTitle};
345     }
346 
347 }