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