001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.bookmarks.action;
016    
017    import com.liferay.portal.kernel.portlet.LiferayWindowState;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.security.auth.PrincipalException;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.service.ServiceContextFactory;
025    import com.liferay.portal.struts.PortletAction;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portal.util.WebKeys;
029    import com.liferay.portlet.asset.AssetCategoryException;
030    import com.liferay.portlet.asset.AssetTagException;
031    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
032    import com.liferay.portlet.bookmarks.EntryURLException;
033    import com.liferay.portlet.bookmarks.NoSuchEntryException;
034    import com.liferay.portlet.bookmarks.NoSuchFolderException;
035    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
036    import com.liferay.portlet.bookmarks.service.BookmarksEntryServiceUtil;
037    
038    import javax.portlet.ActionRequest;
039    import javax.portlet.ActionResponse;
040    import javax.portlet.PortletConfig;
041    import javax.portlet.RenderRequest;
042    import javax.portlet.RenderResponse;
043    import javax.portlet.WindowState;
044    
045    import org.apache.struts.action.ActionForm;
046    import org.apache.struts.action.ActionForward;
047    import org.apache.struts.action.ActionMapping;
048    
049    /**
050     * @author Brian Wing Shun Chan
051     */
052    public class EditEntryAction extends PortletAction {
053    
054            @Override
055            public void processAction(
056                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
057                            ActionRequest actionRequest, ActionResponse actionResponse)
058                    throws Exception {
059    
060                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
061    
062                    try {
063                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
064                                    updateEntry(actionRequest);
065                            }
066                            else if (cmd.equals(Constants.DELETE)) {
067                                    deleteEntry(actionRequest);
068                            }
069                            else if (cmd.equals(Constants.SUBSCRIBE)) {
070                                    subscribeEntry(actionRequest);
071                            }
072                            else if (cmd.equals(Constants.UNSUBSCRIBE)) {
073                                    unsubscribeEntry(actionRequest);
074                            }
075    
076                            WindowState windowState = actionRequest.getWindowState();
077    
078                            if (!windowState.equals(LiferayWindowState.POP_UP)) {
079                                    sendRedirect(actionRequest, actionResponse);
080                            }
081                            else {
082                                    String redirect = PortalUtil.escapeRedirect(
083                                            ParamUtil.getString(actionRequest, "redirect"));
084    
085                                    if (Validator.isNotNull(redirect)) {
086                                            actionResponse.sendRedirect(redirect);
087                                    }
088                            }
089                    }
090                    catch (Exception e) {
091                            if (e instanceof NoSuchEntryException ||
092                                    e instanceof PrincipalException) {
093    
094                                    SessionErrors.add(actionRequest, e.getClass());
095    
096                                    setForward(actionRequest, "portlet.bookmarks.error");
097                            }
098                            else if (e instanceof EntryURLException ||
099                                             e instanceof NoSuchFolderException) {
100    
101                                    SessionErrors.add(actionRequest, e.getClass());
102                            }
103                            else if (e instanceof AssetCategoryException ||
104                                             e instanceof AssetTagException) {
105    
106                                    SessionErrors.add(actionRequest, e.getClass(), e);
107                            }
108                            else {
109                                    throw e;
110                            }
111                    }
112            }
113    
114            @Override
115            public ActionForward render(
116                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
117                            RenderRequest renderRequest, RenderResponse renderResponse)
118                    throws Exception {
119    
120                    try {
121                            ActionUtil.getEntry(renderRequest);
122                    }
123                    catch (Exception e) {
124                            if (e instanceof NoSuchEntryException ||
125                                    e instanceof PrincipalException) {
126    
127                                    SessionErrors.add(renderRequest, e.getClass());
128    
129                                    return mapping.findForward("portlet.bookmarks.error");
130                            }
131                            else {
132                                    throw e;
133                            }
134                    }
135    
136                    return mapping.findForward(
137                            getForward(renderRequest, "portlet.bookmarks.edit_entry"));
138            }
139    
140            protected void deleteEntry(ActionRequest actionRequest) throws Exception {
141                    long entryId = ParamUtil.getLong(actionRequest, "entryId");
142    
143                    BookmarksEntryServiceUtil.deleteEntry(entryId);
144            }
145    
146            protected void subscribeEntry(ActionRequest actionRequest)
147                    throws Exception {
148    
149                    long entryId = ParamUtil.getLong(actionRequest, "entryId");
150    
151                    BookmarksEntryServiceUtil.subscribeEntry(entryId);
152            }
153    
154            protected void unsubscribeEntry(ActionRequest actionRequest)
155                    throws Exception {
156    
157                    long entryId = ParamUtil.getLong(actionRequest, "entryId");
158    
159                    BookmarksEntryServiceUtil.unsubscribeEntry(entryId);
160            }
161    
162            protected void updateEntry(ActionRequest actionRequest) throws Exception {
163                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
164                            WebKeys.THEME_DISPLAY);
165    
166                    long entryId = ParamUtil.getLong(actionRequest, "entryId");
167    
168                    long groupId = themeDisplay.getScopeGroupId();
169                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
170                    String name = ParamUtil.getString(actionRequest, "name");
171                    String url = ParamUtil.getString(actionRequest, "url");
172                    String description = ParamUtil.getString(actionRequest, "description");
173    
174                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
175                            BookmarksEntry.class.getName(), actionRequest);
176    
177                    if (entryId <= 0) {
178    
179                            // Add entry
180    
181                            BookmarksEntry entry = BookmarksEntryServiceUtil.addEntry(
182                                    groupId, folderId, name, url, description, serviceContext);
183    
184                            AssetPublisherUtil.addAndStoreSelection(
185                                    actionRequest, BookmarksEntry.class.getName(),
186                                    entry.getEntryId(), -1);
187                    }
188                    else {
189    
190                            // Update entry
191    
192                            BookmarksEntryServiceUtil.updateEntry(
193                                    entryId, groupId, folderId, name, url, description,
194                                    serviceContext);
195                    }
196    
197                    AssetPublisherUtil.addRecentFolderId(
198                            actionRequest, BookmarksEntry.class.getName(), folderId);
199            }
200    
201    }