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.bookmarks.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.StringUtil;
026: import com.liferay.portal.model.LayoutTypePortlet;
027: import com.liferay.portal.security.auth.PrincipalException;
028: import com.liferay.portal.struts.PortletAction;
029: import com.liferay.portal.theme.ThemeDisplay;
030: import com.liferay.portal.util.WebKeys;
031: import com.liferay.portlet.bookmarks.EntryURLException;
032: import com.liferay.portlet.bookmarks.NoSuchEntryException;
033: import com.liferay.portlet.bookmarks.NoSuchFolderException;
034: import com.liferay.portlet.bookmarks.model.BookmarksEntry;
035: import com.liferay.portlet.bookmarks.service.BookmarksEntryServiceUtil;
036: import com.liferay.portlet.taggedcontent.util.AssetPublisherUtil;
037: import com.liferay.portlet.tags.TagsEntryException;
038: import com.liferay.util.servlet.SessionErrors;
039:
040: import javax.portlet.ActionRequest;
041: import javax.portlet.ActionResponse;
042: import javax.portlet.PortletConfig;
043: import javax.portlet.RenderRequest;
044: import javax.portlet.RenderResponse;
045:
046: import org.apache.struts.action.ActionForm;
047: import org.apache.struts.action.ActionForward;
048: import org.apache.struts.action.ActionMapping;
049:
050: /**
051: * <a href="EditEntryAction.java.html"><b><i>View Source</i></b></a>
052: *
053: * @author Brian Wing Shun Chan
054: *
055: */
056: public class EditEntryAction extends PortletAction {
057:
058: public void processAction(ActionMapping mapping, ActionForm form,
059: PortletConfig config, ActionRequest req, ActionResponse res)
060: throws Exception {
061:
062: String cmd = ParamUtil.getString(req, Constants.CMD);
063:
064: try {
065: if (cmd.equals(Constants.ADD)
066: || cmd.equals(Constants.UPDATE)) {
067: updateEntry(req);
068: } else if (cmd.equals(Constants.DELETE)) {
069: deleteEntry(req);
070: }
071:
072: ThemeDisplay themeDisplay = (ThemeDisplay) req
073: .getAttribute(WebKeys.THEME_DISPLAY);
074:
075: LayoutTypePortlet layoutTypePortlet = themeDisplay
076: .getLayoutTypePortlet();
077:
078: if (layoutTypePortlet.hasPortletId(config.getPortletName())) {
079: sendRedirect(req, res);
080: } else {
081: String redirect = ParamUtil.getString(req, "redirect");
082:
083: res.sendRedirect(redirect);
084: }
085: } catch (Exception e) {
086: if (e instanceof NoSuchEntryException
087: || e instanceof PrincipalException) {
088:
089: SessionErrors.add(req, e.getClass().getName());
090:
091: setForward(req, "portlet.bookmarks.error");
092: } else if (e instanceof EntryURLException
093: || e instanceof NoSuchFolderException) {
094:
095: SessionErrors.add(req, e.getClass().getName());
096: } else if (e instanceof TagsEntryException) {
097: SessionErrors.add(req, e.getClass().getName(), e);
098: } else {
099: throw e;
100: }
101: }
102: }
103:
104: public ActionForward render(ActionMapping mapping, ActionForm form,
105: PortletConfig config, RenderRequest req, RenderResponse res)
106: throws Exception {
107:
108: try {
109: ActionUtil.getEntry(req);
110: } catch (Exception e) {
111: if (e instanceof NoSuchEntryException
112: || e instanceof PrincipalException) {
113:
114: SessionErrors.add(req, e.getClass().getName());
115:
116: return mapping.findForward("portlet.bookmarks.error");
117: } else {
118: throw e;
119: }
120: }
121:
122: return mapping.findForward(getForward(req,
123: "portlet.bookmarks.edit_entry"));
124: }
125:
126: protected void deleteEntry(ActionRequest req) throws Exception {
127: long entryId = ParamUtil.getLong(req, "entryId");
128:
129: BookmarksEntryServiceUtil.deleteEntry(entryId);
130: }
131:
132: protected void updateEntry(ActionRequest req) throws Exception {
133: long entryId = ParamUtil.getLong(req, "entryId");
134:
135: long folderId = ParamUtil.getLong(req, "folderId");
136: String name = ParamUtil.getString(req, "name");
137: String url = ParamUtil.getString(req, "url");
138: String comments = ParamUtil.getString(req, "comments");
139:
140: String[] tagsEntries = StringUtil.split(ParamUtil.getString(
141: req, "tagsEntries"));
142:
143: String[] communityPermissions = req
144: .getParameterValues("communityPermissions");
145: String[] guestPermissions = req
146: .getParameterValues("guestPermissions");
147:
148: if (entryId <= 0) {
149:
150: // Add entry
151:
152: BookmarksEntry entry = BookmarksEntryServiceUtil.addEntry(
153: folderId, name, url, comments, tagsEntries,
154: communityPermissions, guestPermissions);
155:
156: AssetPublisherUtil.addAndStoreSelection(req,
157: BookmarksEntry.class.getName(), entry.getEntryId(),
158: -1);
159: } else {
160:
161: // Update entry
162:
163: BookmarksEntryServiceUtil.updateEntry(entryId, folderId,
164: name, url, comments, tagsEntries);
165: }
166:
167: AssetPublisherUtil.addRecentFolderId(req, BookmarksEntry.class
168: .getName(), folderId);
169: }
170:
171: }
|