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