001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.blogs.action;
022:
023: import com.liferay.portal.kernel.util.Constants;
024: import com.liferay.portal.kernel.util.ParamUtil;
025: import com.liferay.portal.kernel.util.StringPool;
026: import com.liferay.portal.kernel.util.StringUtil;
027: import com.liferay.portal.kernel.util.Validator;
028: import com.liferay.portal.model.Layout;
029: import com.liferay.portal.model.LayoutTypePortlet;
030: import com.liferay.portal.security.auth.PrincipalException;
031: import com.liferay.portal.struts.PortletAction;
032: import com.liferay.portal.theme.ThemeDisplay;
033: import com.liferay.portal.util.WebKeys;
034: import com.liferay.portlet.blogs.EntryContentException;
035: import com.liferay.portlet.blogs.EntryDisplayDateException;
036: import com.liferay.portlet.blogs.EntryTitleException;
037: import com.liferay.portlet.blogs.NoSuchEntryException;
038: import com.liferay.portlet.blogs.model.BlogsEntry;
039: import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
040: import com.liferay.portlet.blogs.service.BlogsEntryServiceUtil;
041: import com.liferay.portlet.taggedcontent.util.AssetPublisherUtil;
042: import com.liferay.portlet.tags.TagsEntryException;
043: import com.liferay.util.servlet.SessionErrors;
044:
045: import java.util.Calendar;
046:
047: import javax.portlet.ActionRequest;
048: import javax.portlet.ActionResponse;
049: import javax.portlet.PortletConfig;
050: import javax.portlet.RenderRequest;
051: import javax.portlet.RenderResponse;
052:
053: import org.apache.struts.action.ActionForm;
054: import org.apache.struts.action.ActionForward;
055: import org.apache.struts.action.ActionMapping;
056:
057: /**
058: * <a href="EditEntryAction.java.html"><b><i>View Source</i></b></a>
059: *
060: * @author Brian Wing Shun Chan
061: * @author Wilson S. Man
062: *
063: */
064: public class EditEntryAction extends PortletAction {
065:
066: public void processAction(ActionMapping mapping, ActionForm form,
067: PortletConfig config, ActionRequest req, ActionResponse res)
068: throws Exception {
069:
070: String cmd = ParamUtil.getString(req, Constants.CMD);
071:
072: try {
073: BlogsEntry entry = null;
074: String oldUrlTitle = StringPool.BLANK;
075:
076: if (cmd.equals(Constants.ADD)
077: || cmd.equals(Constants.UPDATE)) {
078: Object[] returnValue = updateEntry(req);
079:
080: entry = (BlogsEntry) returnValue[0];
081: oldUrlTitle = ((String) returnValue[1]);
082: } else if (cmd.equals(Constants.DELETE)) {
083: deleteEntry(req);
084: }
085:
086: String redirect = ParamUtil.getString(req, "redirect");
087:
088: if ((entry != null)
089: && (Validator.isNotNull(oldUrlTitle))
090: && (redirect.endsWith("/blogs/" + oldUrlTitle) || redirect
091: .indexOf("/blogs/" + oldUrlTitle + "?") != -1)) {
092:
093: int pos = redirect.indexOf("?");
094:
095: if (pos == -1) {
096: pos = redirect.length();
097: }
098:
099: String newRedirect = redirect.substring(0, pos
100: - oldUrlTitle.length());
101:
102: newRedirect += entry.getUrlTitle();
103:
104: if (pos < redirect.length()) {
105: newRedirect += "?"
106: + redirect.substring(pos + 1, redirect
107: .length());
108: }
109:
110: redirect = newRedirect;
111: }
112:
113: ThemeDisplay themeDisplay = (ThemeDisplay) req
114: .getAttribute(WebKeys.THEME_DISPLAY);
115:
116: LayoutTypePortlet layoutTypePortlet = themeDisplay
117: .getLayoutTypePortlet();
118:
119: if (layoutTypePortlet.hasPortletId(config.getPortletName())) {
120: sendRedirect(req, res, redirect);
121: } else {
122: res.sendRedirect(redirect);
123: }
124: } catch (Exception e) {
125: if (e instanceof NoSuchEntryException
126: || e instanceof PrincipalException) {
127:
128: SessionErrors.add(req, e.getClass().getName());
129:
130: setForward(req, "portlet.blogs.error");
131: } else if (e instanceof EntryContentException
132: || e instanceof EntryDisplayDateException
133: || e instanceof EntryTitleException) {
134:
135: SessionErrors.add(req, e.getClass().getName());
136: } else if (e instanceof TagsEntryException) {
137: SessionErrors.add(req, e.getClass().getName(), e);
138: } else {
139: throw e;
140: }
141: }
142: }
143:
144: public ActionForward render(ActionMapping mapping, ActionForm form,
145: PortletConfig config, RenderRequest req, RenderResponse res)
146: throws Exception {
147:
148: try {
149: ActionUtil.getEntry(req);
150: } catch (Exception e) {
151: if (e instanceof NoSuchEntryException
152: || e instanceof PrincipalException) {
153:
154: SessionErrors.add(req, e.getClass().getName());
155:
156: return mapping.findForward("portlet.blogs.error");
157: } else {
158: throw e;
159: }
160: }
161:
162: return mapping.findForward(getForward(req,
163: "portlet.blogs.edit_entry"));
164: }
165:
166: protected void deleteEntry(ActionRequest req) throws Exception {
167: long entryId = ParamUtil.getLong(req, "entryId");
168:
169: BlogsEntryServiceUtil.deleteEntry(entryId);
170: }
171:
172: protected Object[] updateEntry(ActionRequest req) throws Exception {
173: ThemeDisplay themeDisplay = (ThemeDisplay) req
174: .getAttribute(WebKeys.THEME_DISPLAY);
175:
176: Layout layout = themeDisplay.getLayout();
177:
178: long entryId = ParamUtil.getLong(req, "entryId");
179:
180: String title = ParamUtil.getString(req, "title");
181: String content = ParamUtil.getString(req, "content");
182:
183: int displayDateMonth = ParamUtil.getInteger(req,
184: "displayDateMonth");
185: int displayDateDay = ParamUtil
186: .getInteger(req, "displayDateDay");
187: int displayDateYear = ParamUtil.getInteger(req,
188: "displayDateYear");
189: int displayDateHour = ParamUtil.getInteger(req,
190: "displayDateHour");
191: int displayDateMinute = ParamUtil.getInteger(req,
192: "displayDateMinute");
193: int displayDateAmPm = ParamUtil.getInteger(req,
194: "displayDateAmPm");
195:
196: if (displayDateAmPm == Calendar.PM) {
197: displayDateHour += 12;
198: }
199:
200: String[] tagsEntries = StringUtil.split(ParamUtil.getString(
201: req, "tagsEntries"));
202:
203: String[] communityPermissions = req
204: .getParameterValues("communityPermissions");
205: String[] guestPermissions = req
206: .getParameterValues("guestPermissions");
207:
208: BlogsEntry entry = null;
209: String oldUrlTitle = StringPool.BLANK;
210:
211: if (entryId <= 0) {
212:
213: // Add entry
214:
215: entry = BlogsEntryServiceUtil.addEntry(layout.getPlid(),
216: title, content, displayDateMonth, displayDateDay,
217: displayDateYear, displayDateHour,
218: displayDateMinute, tagsEntries,
219: communityPermissions, guestPermissions,
220: themeDisplay);
221:
222: AssetPublisherUtil.addAndStoreSelection(req,
223: BlogsEntry.class.getName(), entry.getEntryId(), -1);
224: } else {
225:
226: // Update entry
227:
228: entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
229:
230: String tempOldUrlTitle = entry.getUrlTitle();
231:
232: entry = BlogsEntryServiceUtil.updateEntry(entryId, title,
233: content, displayDateMonth, displayDateDay,
234: displayDateYear, displayDateHour,
235: displayDateMinute, tagsEntries, themeDisplay);
236:
237: if (!tempOldUrlTitle.equals(entry.getUrlTitle())) {
238: oldUrlTitle = tempOldUrlTitle;
239: }
240: }
241:
242: return new Object[] { entry, oldUrlTitle };
243: }
244:
245: }
|