1
22
23 package com.liferay.portlet.blogs.action;
24
25 import com.liferay.portal.kernel.util.Constants;
26 import com.liferay.portal.kernel.util.ParamUtil;
27 import com.liferay.portal.kernel.util.StringPool;
28 import com.liferay.portal.kernel.util.StringUtil;
29 import com.liferay.portal.kernel.util.Validator;
30 import com.liferay.portal.model.Layout;
31 import com.liferay.portal.security.auth.PrincipalException;
32 import com.liferay.portal.struts.PortletAction;
33 import com.liferay.portal.theme.ThemeDisplay;
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.NoSuchCategoryException;
39 import com.liferay.portlet.blogs.NoSuchEntryException;
40 import com.liferay.portlet.blogs.model.BlogsEntry;
41 import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
42 import com.liferay.portlet.blogs.service.BlogsEntryServiceUtil;
43 import com.liferay.portlet.taggedcontent.util.AssetPublisherUtil;
44 import com.liferay.portlet.tags.TagsEntryException;
45 import com.liferay.util.servlet.SessionErrors;
46
47 import java.util.Calendar;
48
49 import javax.portlet.ActionRequest;
50 import javax.portlet.ActionResponse;
51 import javax.portlet.PortletConfig;
52 import javax.portlet.RenderRequest;
53 import javax.portlet.RenderResponse;
54
55 import org.apache.struts.action.ActionForm;
56 import org.apache.struts.action.ActionForward;
57 import org.apache.struts.action.ActionMapping;
58
59
66 public class EditEntryAction extends PortletAction {
67
68 public void processAction(
69 ActionMapping mapping, ActionForm form, PortletConfig config,
70 ActionRequest req, ActionResponse res)
71 throws Exception {
72
73 String cmd = ParamUtil.getString(req, Constants.CMD);
74
75 try {
76 BlogsEntry entry = null;
77 String oldUrlTitle = StringPool.BLANK;
78
79 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
80 Object[] returnValue = updateEntry(req);
81
82 entry = (BlogsEntry)returnValue[0];
83 oldUrlTitle = ((String)returnValue[1]);
84 }
85 else if (cmd.equals(Constants.DELETE)) {
86 deleteEntry(req);
87 }
88
89 String redirect = ParamUtil.getString(req, "redirect");
90
91 if ((entry != null) && (Validator.isNotNull(oldUrlTitle)) &&
92 (redirect.endsWith("/blogs/" + oldUrlTitle) ||
93 redirect.indexOf("/blogs/" + oldUrlTitle + "?") != -1)) {
94
95 int pos = redirect.indexOf("?");
96
97 if (pos == -1) {
98 pos = redirect.length();
99 }
100
101 String newRedirect = redirect.substring(
102 0, pos - oldUrlTitle.length());
103
104 newRedirect += entry.getUrlTitle();
105
106 if (pos < redirect.length()) {
107 newRedirect +=
108 "?" + redirect.substring(pos + 1, redirect.length());
109 }
110
111 redirect = newRedirect;
112 }
113
114 sendRedirect(req, res, redirect);
115 }
116 catch (Exception e) {
117 if (e instanceof NoSuchCategoryException ||
118 e instanceof NoSuchEntryException ||
119 e instanceof PrincipalException) {
120
121 SessionErrors.add(req, e.getClass().getName());
122
123 setForward(req, "portlet.blogs.error");
124 }
125 else if (e instanceof EntryContentException ||
126 e instanceof EntryDisplayDateException ||
127 e instanceof EntryTitleException) {
128
129 SessionErrors.add(req, e.getClass().getName());
130 }
131 else if (e instanceof TagsEntryException) {
132 SessionErrors.add(req, e.getClass().getName(), e);
133 }
134 else {
135 throw e;
136 }
137 }
138 }
139
140 public ActionForward render(
141 ActionMapping mapping, ActionForm form, PortletConfig config,
142 RenderRequest req, RenderResponse res)
143 throws Exception {
144
145 try {
146 ActionUtil.getEntry(req);
147 }
148 catch (Exception e) {
149 if (e instanceof NoSuchEntryException ||
150 e instanceof PrincipalException) {
151
152 SessionErrors.add(req, e.getClass().getName());
153
154 return mapping.findForward("portlet.blogs.error");
155 }
156 else {
157 throw e;
158 }
159 }
160
161 return mapping.findForward(getForward(req, "portlet.blogs.edit_entry"));
162 }
163
164 protected void deleteEntry(ActionRequest req) throws Exception {
165 long entryId = ParamUtil.getLong(req, "entryId");
166
167 BlogsEntryServiceUtil.deleteEntry(entryId);
168 }
169
170 protected Object[] updateEntry(ActionRequest req) throws Exception {
171 ThemeDisplay themeDisplay =
172 (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
173
174 Layout layout = themeDisplay.getLayout();
175
176 long entryId = ParamUtil.getLong(req, "entryId");
177
178 long categoryId = ParamUtil.getLong(req, "categoryId");
179 String title = ParamUtil.getString(req, "title");
180 String content = ParamUtil.getString(req, "content");
181
182 int displayDateMonth = ParamUtil.getInteger(req, "displayDateMonth");
183 int displayDateDay = ParamUtil.getInteger(req, "displayDateDay");
184 int displayDateYear = ParamUtil.getInteger(req, "displayDateYear");
185 int displayDateHour = ParamUtil.getInteger(req, "displayDateHour");
186 int displayDateMinute = ParamUtil.getInteger(req, "displayDateMinute");
187 int displayDateAmPm = ParamUtil.getInteger(req, "displayDateAmPm");
188
189 if (displayDateAmPm == Calendar.PM) {
190 displayDateHour += 12;
191 }
192
193 String[] tagsEntries = StringUtil.split(
194 ParamUtil.getString(req, "tagsEntries"));
195
196 String[] communityPermissions = req.getParameterValues(
197 "communityPermissions");
198 String[] guestPermissions = req.getParameterValues(
199 "guestPermissions");
200
201 BlogsEntry entry = null;
202 String oldUrlTitle = StringPool.BLANK;
203
204 if (entryId <= 0) {
205
206
208 entry = BlogsEntryServiceUtil.addEntry(
209 layout.getPlid(), categoryId, title, content, displayDateMonth,
210 displayDateDay, displayDateYear, displayDateHour,
211 displayDateMinute, themeDisplay, tagsEntries,
212 communityPermissions, guestPermissions);
213
214 AssetPublisherUtil.addAndStoreSelection(
215 req, BlogsEntry.class.getName(), entry.getEntryId(), -1);
216 }
217 else {
218
219
221 entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
222
223 String tempOldUrlTitle = entry.getUrlTitle();
224
225 entry = BlogsEntryServiceUtil.updateEntry(
226 entryId, categoryId, title, content, displayDateMonth,
227 displayDateDay, displayDateYear, displayDateHour,
228 displayDateMinute, themeDisplay, tagsEntries);
229
230 if (!tempOldUrlTitle.equals(entry.getUrlTitle())) {
231 oldUrlTitle = tempOldUrlTitle;
232 }
233 }
234
235 return new Object[] {entry, oldUrlTitle};
236 }
237
238 }