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.Layout;
28  import com.liferay.portal.model.LayoutTypePortlet;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.struts.ActionConstants;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.blogs.EntryContentException;
36  import com.liferay.portlet.blogs.EntryDisplayDateException;
37  import com.liferay.portlet.blogs.EntryTitleException;
38  import com.liferay.portlet.blogs.NoSuchEntryException;
39  import com.liferay.portlet.blogs.model.BlogsEntry;
40  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
41  import com.liferay.portlet.blogs.service.BlogsEntryServiceUtil;
42  import com.liferay.portlet.taggedcontent.util.AssetPublisherUtil;
43  import com.liferay.portlet.tags.TagsEntryException;
44  import com.liferay.util.servlet.ServletResponseUtil;
45  
46  import java.io.InputStream;
47  
48  import java.util.Calendar;
49  
50  import javax.portlet.ActionRequest;
51  import javax.portlet.ActionResponse;
52  import javax.portlet.PortletConfig;
53  import javax.portlet.RenderRequest;
54  import javax.portlet.RenderResponse;
55  
56  import javax.servlet.http.HttpServletResponse;
57  
58  import org.apache.struts.action.ActionForm;
59  import org.apache.struts.action.ActionForward;
60  import org.apache.struts.action.ActionMapping;
61  
62  /**
63   * <a href="EditEntryAction.java.html"><b><i>View Source</i></b></a>
64   *
65   * @author Brian Wing Shun Chan
66   * @author Wilson S. Man
67   */
68  public class EditEntryAction extends PortletAction {
69  
70      public void processAction(
71              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
72              ActionRequest actionRequest, ActionResponse actionResponse)
73          throws Exception {
74  
75          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
76  
77          try {
78              BlogsEntry entry = null;
79              String oldUrlTitle = StringPool.BLANK;
80  
81              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
82                  Object[] returnValue = updateEntry(actionRequest);
83  
84                  entry = (BlogsEntry)returnValue[0];
85                  oldUrlTitle = ((String)returnValue[1]);
86              }
87              else if (cmd.equals(Constants.DELETE)) {
88                  deleteEntry(actionRequest);
89              }
90  
91              String redirect = ParamUtil.getString(actionRequest, "redirect");
92              boolean updateRedirect = false;
93  
94              if (redirect.indexOf(
95                      "/blogs/" + oldUrlTitle + "/maximized") != -1) {
96  
97                  oldUrlTitle += "/maximized";
98              }
99  
100             if ((entry != null) && (Validator.isNotNull(oldUrlTitle)) &&
101                 (redirect.endsWith("/blogs/" + oldUrlTitle) ||
102                  redirect.indexOf("/blogs/" + oldUrlTitle + "?") != -1)) {
103 
104                 int pos = redirect.indexOf("?");
105 
106                 if (pos == -1) {
107                     pos = redirect.length();
108                 }
109 
110                 String newRedirect = redirect.substring(
111                     0, pos - oldUrlTitle.length());
112 
113                 newRedirect += entry.getUrlTitle();
114 
115                 if (oldUrlTitle.indexOf("/maximized") != -1) {
116                     newRedirect += "/maximized";
117                 }
118 
119                 if (pos < redirect.length()) {
120                     newRedirect +=
121                         "?" + redirect.substring(pos + 1, redirect.length());
122                 }
123 
124                 redirect = newRedirect;
125                 updateRedirect = true;
126             }
127 
128             if ((entry != null) && entry.isDraft()) {
129                 JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
130 
131                 jsonObj.put("entryId", entry.getEntryId());
132                 jsonObj.put("redirect", redirect);
133                 jsonObj.put("updateRedirect", updateRedirect);
134 
135                 HttpServletResponse response =
136                     PortalUtil.getHttpServletResponse(actionResponse);
137                 InputStream is = new UnsyncByteArrayInputStream(
138                     jsonObj.toString().getBytes());
139                 String contentType = ContentTypes.TEXT_JAVASCRIPT;
140 
141                 ServletResponseUtil.sendFile(
142                     response, null, is, contentType);
143 
144                 setForward(actionRequest, ActionConstants.COMMON_NULL);
145             }
146             else {
147                 ThemeDisplay themeDisplay =
148                     (ThemeDisplay)actionRequest.getAttribute(
149                         WebKeys.THEME_DISPLAY);
150 
151                 LayoutTypePortlet layoutTypePortlet =
152                     themeDisplay.getLayoutTypePortlet();
153 
154                 if (layoutTypePortlet.hasPortletId(
155                         portletConfig.getPortletName())) {
156 
157                     sendRedirect(actionRequest, actionResponse, redirect);
158                 }
159                 else {
160                     actionResponse.sendRedirect(redirect);
161                 }
162             }
163         }
164         catch (Exception e) {
165             if (e instanceof NoSuchEntryException ||
166                 e instanceof PrincipalException) {
167 
168                 SessionErrors.add(actionRequest, e.getClass().getName());
169 
170                 setForward(actionRequest, "portlet.blogs.error");
171             }
172             else if (e instanceof EntryContentException ||
173                      e instanceof EntryDisplayDateException ||
174                      e instanceof EntryTitleException) {
175 
176                 SessionErrors.add(actionRequest, e.getClass().getName());
177             }
178             else if (e instanceof TagsEntryException) {
179                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
180             }
181             else {
182                 throw e;
183             }
184         }
185     }
186 
187     public ActionForward render(
188             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
189             RenderRequest renderRequest, RenderResponse renderResponse)
190         throws Exception {
191 
192         try {
193             ActionUtil.getEntry(renderRequest);
194         }
195         catch (Exception e) {
196             if (e instanceof NoSuchEntryException ||
197                 e instanceof PrincipalException) {
198 
199                 SessionErrors.add(renderRequest, e.getClass().getName());
200 
201                 return mapping.findForward("portlet.blogs.error");
202             }
203             else {
204                 throw e;
205             }
206         }
207 
208         return mapping.findForward(
209             getForward(renderRequest, "portlet.blogs.edit_entry"));
210     }
211 
212     protected void deleteEntry(ActionRequest actionRequest) throws Exception {
213         long entryId = ParamUtil.getLong(actionRequest, "entryId");
214 
215         BlogsEntryServiceUtil.deleteEntry(entryId);
216     }
217 
218     protected Object[] updateEntry(ActionRequest actionRequest)
219         throws Exception {
220 
221         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
222             WebKeys.THEME_DISPLAY);
223 
224         Layout layout = themeDisplay.getLayout();
225 
226         long entryId = ParamUtil.getLong(actionRequest, "entryId");
227 
228         String title = ParamUtil.getString(actionRequest, "title");
229         String content = ParamUtil.getString(actionRequest, "content");
230 
231         int displayDateMonth = ParamUtil.getInteger(
232             actionRequest, "displayDateMonth");
233         int displayDateDay = ParamUtil.getInteger(
234             actionRequest, "displayDateDay");
235         int displayDateYear = ParamUtil.getInteger(
236             actionRequest, "displayDateYear");
237         int displayDateHour = ParamUtil.getInteger(
238             actionRequest, "displayDateHour");
239         int displayDateMinute = ParamUtil.getInteger(
240             actionRequest, "displayDateMinute");
241         int displayDateAmPm = ParamUtil.getInteger(
242             actionRequest, "displayDateAmPm");
243 
244         if (displayDateAmPm == Calendar.PM) {
245             displayDateHour += 12;
246         }
247 
248         boolean draft = ParamUtil.getBoolean(actionRequest, "draft");
249         boolean allowTrackbacks = ParamUtil.getBoolean(
250             actionRequest, "allowTrackbacks");
251         String[] trackbacks = StringUtil.split(
252             ParamUtil.getString(actionRequest, "trackbacks"));
253 
254         String[] tagsEntries = StringUtil.split(
255             ParamUtil.getString(actionRequest, "tagsEntries"));
256 
257         boolean addCommunityPermissions = true;
258         boolean addGuestPermissions = true;
259 
260         BlogsEntry entry = null;
261         String oldUrlTitle = StringPool.BLANK;
262 
263         if (entryId <= 0) {
264 
265             // Add entry
266 
267             entry = BlogsEntryServiceUtil.addEntry(
268                 layout.getPlid(), title, content, displayDateMonth,
269                 displayDateDay, displayDateYear, displayDateHour,
270                 displayDateMinute, draft, allowTrackbacks, trackbacks,
271                 tagsEntries, addCommunityPermissions, addGuestPermissions,
272                 themeDisplay);
273 
274             if (!draft) {
275                 AssetPublisherUtil.addAndStoreSelection(
276                     actionRequest, BlogsEntry.class.getName(),
277                     entry.getEntryId(), -1);
278             }
279         }
280         else {
281 
282             // Update entry
283 
284             entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
285 
286             String tempOldUrlTitle = entry.getUrlTitle();
287             boolean oldDraft = entry.isDraft();
288 
289             entry = BlogsEntryServiceUtil.updateEntry(
290                 entryId, title, content, displayDateMonth, displayDateDay,
291                 displayDateYear, displayDateHour, displayDateMinute, draft,
292                 allowTrackbacks, trackbacks, tagsEntries, themeDisplay);
293 
294             if (!tempOldUrlTitle.equals(entry.getUrlTitle())) {
295                 oldUrlTitle = tempOldUrlTitle;
296             }
297 
298             if (oldDraft && !draft) {
299                 AssetPublisherUtil.addAndStoreSelection(
300                     actionRequest, BlogsEntry.class.getName(),
301                     entry.getEntryId(), -1);
302             }
303         }
304 
305         return new Object[] {entry, oldUrlTitle};
306     }
307 
308 }