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.model.LayoutTypePortlet;
32 import com.liferay.portal.security.auth.PrincipalException;
33 import com.liferay.portal.struts.PortletAction;
34 import com.liferay.portal.theme.ThemeDisplay;
35 import com.liferay.portal.util.WebKeys;
36 import com.liferay.portlet.blogs.EntryContentException;
37 import com.liferay.portlet.blogs.EntryDisplayDateException;
38 import com.liferay.portlet.blogs.EntryTitleException;
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 ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
115 WebKeys.THEME_DISPLAY);
116
117 LayoutTypePortlet layoutTypePortlet =
118 themeDisplay.getLayoutTypePortlet();
119
120 if (layoutTypePortlet.hasPortletId(config.getPortletName())) {
121 sendRedirect(req, res, redirect);
122 }
123 else {
124 res.sendRedirect(redirect);
125 }
126 }
127 catch (Exception e) {
128 if (e instanceof NoSuchEntryException ||
129 e instanceof PrincipalException) {
130
131 SessionErrors.add(req, e.getClass().getName());
132
133 setForward(req, "portlet.blogs.error");
134 }
135 else if (e instanceof EntryContentException ||
136 e instanceof EntryDisplayDateException ||
137 e instanceof EntryTitleException) {
138
139 SessionErrors.add(req, e.getClass().getName());
140 }
141 else if (e instanceof TagsEntryException) {
142 SessionErrors.add(req, e.getClass().getName(), e);
143 }
144 else {
145 throw e;
146 }
147 }
148 }
149
150 public ActionForward render(
151 ActionMapping mapping, ActionForm form, PortletConfig config,
152 RenderRequest req, RenderResponse res)
153 throws Exception {
154
155 try {
156 ActionUtil.getEntry(req);
157 }
158 catch (Exception e) {
159 if (e instanceof NoSuchEntryException ||
160 e instanceof PrincipalException) {
161
162 SessionErrors.add(req, e.getClass().getName());
163
164 return mapping.findForward("portlet.blogs.error");
165 }
166 else {
167 throw e;
168 }
169 }
170
171 return mapping.findForward(getForward(req, "portlet.blogs.edit_entry"));
172 }
173
174 protected void deleteEntry(ActionRequest req) throws Exception {
175 long entryId = ParamUtil.getLong(req, "entryId");
176
177 BlogsEntryServiceUtil.deleteEntry(entryId);
178 }
179
180 protected Object[] updateEntry(ActionRequest req) throws Exception {
181 ThemeDisplay themeDisplay =
182 (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
183
184 Layout layout = themeDisplay.getLayout();
185
186 long entryId = ParamUtil.getLong(req, "entryId");
187
188 String title = ParamUtil.getString(req, "title");
189 String content = ParamUtil.getString(req, "content");
190
191 int displayDateMonth = ParamUtil.getInteger(req, "displayDateMonth");
192 int displayDateDay = ParamUtil.getInteger(req, "displayDateDay");
193 int displayDateYear = ParamUtil.getInteger(req, "displayDateYear");
194 int displayDateHour = ParamUtil.getInteger(req, "displayDateHour");
195 int displayDateMinute = ParamUtil.getInteger(req, "displayDateMinute");
196 int displayDateAmPm = ParamUtil.getInteger(req, "displayDateAmPm");
197
198 if (displayDateAmPm == Calendar.PM) {
199 displayDateHour += 12;
200 }
201
202 String[] tagsEntries = StringUtil.split(
203 ParamUtil.getString(req, "tagsEntries"));
204
205 String[] communityPermissions = req.getParameterValues(
206 "communityPermissions");
207 String[] guestPermissions = req.getParameterValues(
208 "guestPermissions");
209
210 BlogsEntry entry = null;
211 String oldUrlTitle = StringPool.BLANK;
212
213 if (entryId <= 0) {
214
215
217 entry = BlogsEntryServiceUtil.addEntry(
218 layout.getPlid(), title, content, displayDateMonth,
219 displayDateDay, displayDateYear, displayDateHour,
220 displayDateMinute, tagsEntries, communityPermissions,
221 guestPermissions, themeDisplay);
222
223 AssetPublisherUtil.addAndStoreSelection(
224 req, BlogsEntry.class.getName(), entry.getEntryId(), -1);
225 }
226 else {
227
228
230 entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
231
232 String tempOldUrlTitle = entry.getUrlTitle();
233
234 entry = BlogsEntryServiceUtil.updateEntry(
235 entryId, title, content, displayDateMonth, displayDateDay,
236 displayDateYear, displayDateHour, displayDateMinute,
237 tagsEntries, themeDisplay);
238
239 if (!tempOldUrlTitle.equals(entry.getUrlTitle())) {
240 oldUrlTitle = tempOldUrlTitle;
241 }
242 }
243
244 return new Object[] {entry, oldUrlTitle};
245 }
246
247 }