1   /**
2    * Copyright (c) 2000-2008 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 ((entry != null) && (Validator.isNotNull(oldUrlTitle)) &&
104                 (redirect.endsWith("/blogs/" + oldUrlTitle) ||
105                  redirect.indexOf("/blogs/" + oldUrlTitle + "?") != -1)) {
106 
107                 int pos = redirect.indexOf("?");
108 
109                 if (pos == -1) {
110                     pos = redirect.length();
111                 }
112 
113                 String newRedirect = redirect.substring(
114                     0, pos - oldUrlTitle.length());
115 
116                 newRedirect += entry.getUrlTitle();
117 
118                 if (pos < redirect.length()) {
119                     newRedirect +=
120                         "?" + redirect.substring(pos + 1, redirect.length());
121                 }
122 
123                 redirect = newRedirect;
124                 updateRedirect = true;
125             }
126 
127             if ((entry != null) && entry.isDraft()) {
128                 JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
129 
130                 jsonObj.put("entryId", entry.getEntryId());
131                 jsonObj.put("redirect", redirect);
132                 jsonObj.put("updateRedirect", updateRedirect);
133 
134                 HttpServletResponse response =
135                     PortalUtil.getHttpServletResponse(actionResponse);
136                 InputStream is = new ByteArrayInputStream(
137                     jsonObj.toString().getBytes());
138                 String contentType = ContentTypes.TEXT_JAVASCRIPT;
139 
140                 ServletResponseUtil.sendFile(
141                     response, null, is, contentType);
142 
143                 setForward(actionRequest, ActionConstants.COMMON_NULL);
144             }
145             else {
146                 ThemeDisplay themeDisplay =
147                     (ThemeDisplay)actionRequest.getAttribute(
148                         WebKeys.THEME_DISPLAY);
149 
150                 LayoutTypePortlet layoutTypePortlet =
151                     themeDisplay.getLayoutTypePortlet();
152 
153                 if (layoutTypePortlet.hasPortletId(
154                         portletConfig.getPortletName())) {
155 
156                     sendRedirect(actionRequest, actionResponse, redirect);
157                 }
158                 else {
159                     actionResponse.sendRedirect(redirect);
160                 }
161             }
162         }
163         catch (Exception e) {
164             if (e instanceof NoSuchEntryException ||
165                 e instanceof PrincipalException) {
166 
167                 SessionErrors.add(actionRequest, e.getClass().getName());
168 
169                 setForward(actionRequest, "portlet.blogs.error");
170             }
171             else if (e instanceof EntryContentException ||
172                      e instanceof EntryDisplayDateException ||
173                      e instanceof EntryTitleException) {
174 
175                 SessionErrors.add(actionRequest, e.getClass().getName());
176             }
177             else if (e instanceof TagsEntryException) {
178                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
179             }
180             else {
181                 throw e;
182             }
183         }
184     }
185 
186     public ActionForward render(
187             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
188             RenderRequest renderRequest, RenderResponse renderResponse)
189         throws Exception {
190 
191         try {
192             ActionUtil.getEntry(renderRequest);
193         }
194         catch (Exception e) {
195             if (e instanceof NoSuchEntryException ||
196                 e instanceof PrincipalException) {
197 
198                 SessionErrors.add(renderRequest, e.getClass().getName());
199 
200                 return mapping.findForward("portlet.blogs.error");
201             }
202             else {
203                 throw e;
204             }
205         }
206 
207         return mapping.findForward(
208             getForward(renderRequest, "portlet.blogs.edit_entry"));
209     }
210 
211     protected void deleteEntry(ActionRequest actionRequest) throws Exception {
212         long entryId = ParamUtil.getLong(actionRequest, "entryId");
213 
214         BlogsEntryServiceUtil.deleteEntry(entryId);
215     }
216 
217     protected Object[] updateEntry(ActionRequest actionRequest)
218         throws Exception {
219 
220         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
221             WebKeys.THEME_DISPLAY);
222 
223         Layout layout = themeDisplay.getLayout();
224 
225         long entryId = ParamUtil.getLong(actionRequest, "entryId");
226 
227         String title = ParamUtil.getString(actionRequest, "title");
228         String content = ParamUtil.getString(actionRequest, "content");
229 
230         int displayDateMonth = ParamUtil.getInteger(
231             actionRequest, "displayDateMonth");
232         int displayDateDay = ParamUtil.getInteger(
233             actionRequest, "displayDateDay");
234         int displayDateYear = ParamUtil.getInteger(
235             actionRequest, "displayDateYear");
236         int displayDateHour = ParamUtil.getInteger(
237             actionRequest, "displayDateHour");
238         int displayDateMinute = ParamUtil.getInteger(
239             actionRequest, "displayDateMinute");
240         int displayDateAmPm = ParamUtil.getInteger(
241             actionRequest, "displayDateAmPm");
242 
243         if (displayDateAmPm == Calendar.PM) {
244             displayDateHour += 12;
245         }
246 
247         boolean draft = ParamUtil.getBoolean(actionRequest, "draft");
248         boolean allowTrackbacks = ParamUtil.getBoolean(
249             actionRequest, "allowTrackbacks");
250         String[] trackbacks = StringUtil.split(
251             ParamUtil.getString(actionRequest, "trackbacks"));
252 
253         String[] tagsEntries = StringUtil.split(
254             ParamUtil.getString(actionRequest, "tagsEntries"));
255 
256         boolean addCommunityPermissions = true;
257         boolean addGuestPermissions = true;
258 
259         BlogsEntry entry = null;
260         String oldUrlTitle = StringPool.BLANK;
261 
262         if (entryId <= 0) {
263 
264             // Add entry
265 
266             entry = BlogsEntryServiceUtil.addEntry(
267                 layout.getPlid(), title, content, displayDateMonth,
268                 displayDateDay, displayDateYear, displayDateHour,
269                 displayDateMinute, draft, allowTrackbacks, trackbacks,
270                 tagsEntries, addCommunityPermissions, addGuestPermissions,
271                 themeDisplay);
272 
273             if (!draft) {
274                 AssetPublisherUtil.addAndStoreSelection(
275                     actionRequest, BlogsEntry.class.getName(),
276                     entry.getEntryId(), -1);
277             }
278         }
279         else {
280 
281             // Update entry
282 
283             entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
284 
285             String tempOldUrlTitle = entry.getUrlTitle();
286             boolean oldDraft = entry.isDraft();
287 
288             entry = BlogsEntryServiceUtil.updateEntry(
289                 entryId, title, content, displayDateMonth, displayDateDay,
290                 displayDateYear, displayDateHour, displayDateMinute, draft,
291                 allowTrackbacks, trackbacks, tagsEntries, themeDisplay);
292 
293             if (!tempOldUrlTitle.equals(entry.getUrlTitle())) {
294                 oldUrlTitle = tempOldUrlTitle;
295             }
296 
297             if (oldDraft && !draft) {
298                 AssetPublisherUtil.addAndStoreSelection(
299                     actionRequest, BlogsEntry.class.getName(),
300                     entry.getEntryId(), -1);
301             }
302         }
303 
304         return new Object[] {entry, oldUrlTitle};
305     }
306 
307 }