1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.blogs.action;
24  
25  import com.liferay.portal.kernel.json.JSONFactoryUtil;
26  import com.liferay.portal.kernel.json.JSONObject;
27  import com.liferay.portal.kernel.servlet.SessionErrors;
28  import com.liferay.portal.kernel.util.Constants;
29  import com.liferay.portal.kernel.util.ContentTypes;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.StringUtil;
33  import com.liferay.portal.kernel.util.Validator;
34  import com.liferay.portal.model.Layout;
35  import com.liferay.portal.model.LayoutTypePortlet;
36  import com.liferay.portal.security.auth.PrincipalException;
37  import com.liferay.portal.struts.ActionConstants;
38  import com.liferay.portal.struts.PortletAction;
39  import com.liferay.portal.theme.ThemeDisplay;
40  import com.liferay.portal.util.PortalUtil;
41  import com.liferay.portal.util.WebKeys;
42  import com.liferay.portlet.blogs.EntryContentException;
43  import com.liferay.portlet.blogs.EntryDisplayDateException;
44  import com.liferay.portlet.blogs.EntryTitleException;
45  import com.liferay.portlet.blogs.NoSuchEntryException;
46  import com.liferay.portlet.blogs.model.BlogsEntry;
47  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
48  import com.liferay.portlet.blogs.service.BlogsEntryServiceUtil;
49  import com.liferay.portlet.taggedcontent.util.AssetPublisherUtil;
50  import com.liferay.portlet.tags.TagsEntryException;
51  import com.liferay.util.servlet.ServletResponseUtil;
52  
53  import java.io.ByteArrayInputStream;
54  import java.io.InputStream;
55  
56  import java.util.Calendar;
57  
58  import javax.portlet.ActionRequest;
59  import javax.portlet.ActionResponse;
60  import javax.portlet.PortletConfig;
61  import javax.portlet.RenderRequest;
62  import javax.portlet.RenderResponse;
63  
64  import javax.servlet.http.HttpServletResponse;
65  
66  import org.apache.struts.action.ActionForm;
67  import org.apache.struts.action.ActionForward;
68  import org.apache.struts.action.ActionMapping;
69  
70  /**
71   * <a href="EditEntryAction.java.html"><b><i>View Source</i></b></a>
72   *
73   * @author Brian Wing Shun Chan
74   * @author Wilson S. Man
75   *
76   */
77  public class EditEntryAction extends PortletAction {
78  
79      public void processAction(
80              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
81              ActionRequest actionRequest, ActionResponse actionResponse)
82          throws Exception {
83  
84          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
85  
86          try {
87              BlogsEntry entry = null;
88              String oldUrlTitle = StringPool.BLANK;
89  
90              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
91                  Object[] returnValue = updateEntry(actionRequest);
92  
93                  entry = (BlogsEntry)returnValue[0];
94                  oldUrlTitle = ((String)returnValue[1]);
95              }
96              else if (cmd.equals(Constants.DELETE)) {
97                  deleteEntry(actionRequest);
98              }
99  
100             String redirect = ParamUtil.getString(actionRequest, "redirect");
101             boolean updateRedirect = false;
102 
103             if (redirect.indexOf(
104                     "/blogs/" + oldUrlTitle + "/maximized") != -1) {
105 
106                 oldUrlTitle += "/maximized";
107             }
108 
109             if ((entry != null) && (Validator.isNotNull(oldUrlTitle)) &&
110                 (redirect.endsWith("/blogs/" + oldUrlTitle) ||
111                  redirect.indexOf("/blogs/" + oldUrlTitle + "?") != -1)) {
112 
113                 int pos = redirect.indexOf("?");
114 
115                 if (pos == -1) {
116                     pos = redirect.length();
117                 }
118 
119                 String newRedirect = redirect.substring(
120                     0, pos - oldUrlTitle.length());
121 
122                 newRedirect += entry.getUrlTitle();
123 
124                 if (oldUrlTitle.indexOf("/maximized") != -1) {
125                     newRedirect += "/maximized";
126                 }
127 
128                 if (pos < redirect.length()) {
129                     newRedirect +=
130                         "?" + redirect.substring(pos + 1, redirect.length());
131                 }
132 
133                 redirect = newRedirect;
134                 updateRedirect = true;
135             }
136 
137             if ((entry != null) && entry.isDraft()) {
138                 JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
139 
140                 jsonObj.put("entryId", entry.getEntryId());
141                 jsonObj.put("redirect", redirect);
142                 jsonObj.put("updateRedirect", updateRedirect);
143 
144                 HttpServletResponse response =
145                     PortalUtil.getHttpServletResponse(actionResponse);
146                 InputStream is = new ByteArrayInputStream(
147                     jsonObj.toString().getBytes());
148                 String contentType = ContentTypes.TEXT_JAVASCRIPT;
149 
150                 ServletResponseUtil.sendFile(
151                     response, null, is, contentType);
152 
153                 setForward(actionRequest, ActionConstants.COMMON_NULL);
154             }
155             else {
156                 ThemeDisplay themeDisplay =
157                     (ThemeDisplay)actionRequest.getAttribute(
158                         WebKeys.THEME_DISPLAY);
159 
160                 LayoutTypePortlet layoutTypePortlet =
161                     themeDisplay.getLayoutTypePortlet();
162 
163                 if (layoutTypePortlet.hasPortletId(
164                         portletConfig.getPortletName())) {
165 
166                     sendRedirect(actionRequest, actionResponse, redirect);
167                 }
168                 else {
169                     actionResponse.sendRedirect(redirect);
170                 }
171             }
172         }
173         catch (Exception e) {
174             if (e instanceof NoSuchEntryException ||
175                 e instanceof PrincipalException) {
176 
177                 SessionErrors.add(actionRequest, e.getClass().getName());
178 
179                 setForward(actionRequest, "portlet.blogs.error");
180             }
181             else if (e instanceof EntryContentException ||
182                      e instanceof EntryDisplayDateException ||
183                      e instanceof EntryTitleException) {
184 
185                 SessionErrors.add(actionRequest, e.getClass().getName());
186             }
187             else if (e instanceof TagsEntryException) {
188                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
189             }
190             else {
191                 throw e;
192             }
193         }
194     }
195 
196     public ActionForward render(
197             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
198             RenderRequest renderRequest, RenderResponse renderResponse)
199         throws Exception {
200 
201         try {
202             ActionUtil.getEntry(renderRequest);
203         }
204         catch (Exception e) {
205             if (e instanceof NoSuchEntryException ||
206                 e instanceof PrincipalException) {
207 
208                 SessionErrors.add(renderRequest, e.getClass().getName());
209 
210                 return mapping.findForward("portlet.blogs.error");
211             }
212             else {
213                 throw e;
214             }
215         }
216 
217         return mapping.findForward(
218             getForward(renderRequest, "portlet.blogs.edit_entry"));
219     }
220 
221     protected void deleteEntry(ActionRequest actionRequest) throws Exception {
222         long entryId = ParamUtil.getLong(actionRequest, "entryId");
223 
224         BlogsEntryServiceUtil.deleteEntry(entryId);
225     }
226 
227     protected Object[] updateEntry(ActionRequest actionRequest)
228         throws Exception {
229 
230         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
231             WebKeys.THEME_DISPLAY);
232 
233         Layout layout = themeDisplay.getLayout();
234 
235         long entryId = ParamUtil.getLong(actionRequest, "entryId");
236 
237         String title = ParamUtil.getString(actionRequest, "title");
238         String content = ParamUtil.getString(actionRequest, "content");
239 
240         int displayDateMonth = ParamUtil.getInteger(
241             actionRequest, "displayDateMonth");
242         int displayDateDay = ParamUtil.getInteger(
243             actionRequest, "displayDateDay");
244         int displayDateYear = ParamUtil.getInteger(
245             actionRequest, "displayDateYear");
246         int displayDateHour = ParamUtil.getInteger(
247             actionRequest, "displayDateHour");
248         int displayDateMinute = ParamUtil.getInteger(
249             actionRequest, "displayDateMinute");
250         int displayDateAmPm = ParamUtil.getInteger(
251             actionRequest, "displayDateAmPm");
252 
253         if (displayDateAmPm == Calendar.PM) {
254             displayDateHour += 12;
255         }
256 
257         boolean draft = ParamUtil.getBoolean(actionRequest, "draft");
258         boolean allowTrackbacks = ParamUtil.getBoolean(
259             actionRequest, "allowTrackbacks");
260         String[] trackbacks = StringUtil.split(
261             ParamUtil.getString(actionRequest, "trackbacks"));
262 
263         String[] tagsEntries = StringUtil.split(
264             ParamUtil.getString(actionRequest, "tagsEntries"));
265 
266         boolean addCommunityPermissions = true;
267         boolean addGuestPermissions = true;
268 
269         BlogsEntry entry = null;
270         String oldUrlTitle = StringPool.BLANK;
271 
272         if (entryId <= 0) {
273 
274             // Add entry
275 
276             entry = BlogsEntryServiceUtil.addEntry(
277                 layout.getPlid(), title, content, displayDateMonth,
278                 displayDateDay, displayDateYear, displayDateHour,
279                 displayDateMinute, draft, allowTrackbacks, trackbacks,
280                 tagsEntries, addCommunityPermissions, addGuestPermissions,
281                 themeDisplay);
282 
283             if (!draft) {
284                 AssetPublisherUtil.addAndStoreSelection(
285                     actionRequest, BlogsEntry.class.getName(),
286                     entry.getEntryId(), -1);
287             }
288         }
289         else {
290 
291             // Update entry
292 
293             entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
294 
295             String tempOldUrlTitle = entry.getUrlTitle();
296             boolean oldDraft = entry.isDraft();
297 
298             entry = BlogsEntryServiceUtil.updateEntry(
299                 entryId, title, content, displayDateMonth, displayDateDay,
300                 displayDateYear, displayDateHour, displayDateMinute, draft,
301                 allowTrackbacks, trackbacks, tagsEntries, themeDisplay);
302 
303             if (!tempOldUrlTitle.equals(entry.getUrlTitle())) {
304                 oldUrlTitle = tempOldUrlTitle;
305             }
306 
307             if (oldDraft && !draft) {
308                 AssetPublisherUtil.addAndStoreSelection(
309                     actionRequest, BlogsEntry.class.getName(),
310                     entry.getEntryId(), -1);
311             }
312         }
313 
314         return new Object[] {entry, oldUrlTitle};
315     }
316 
317 }