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