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    
070                            WindowState windowState = actionRequest.getWindowState();
071    
072                            if (!windowState.equals(LiferayWindowState.POP_UP)) {
073                                    sendRedirect(actionRequest, actionResponse);
074                            }
075                            else {
076                                    String redirect = PortalUtil.escapeRedirect(
077                                            ParamUtil.getString(actionRequest, "redirect"));
078    
079                                    if (Validator.isNotNull(redirect)) {
080                                            actionResponse.sendRedirect(redirect);
081                                    }
082                            }
083                    }
084                    catch (Exception e) {
085                            if (e instanceof NoSuchEntryException ||
086                                    e instanceof PrincipalException) {
087    
088                                    SessionErrors.add(actionRequest, e.getClass().getName());
089    
090                                    setForward(actionRequest, "portlet.bookmarks.error");
091                            }
092                            else if (e instanceof EntryURLException ||
093                                             e instanceof NoSuchFolderException) {
094    
095                                    SessionErrors.add(actionRequest, e.getClass().getName());
096                            }
097                            else if (e instanceof AssetCategoryException ||
098                                             e instanceof AssetTagException) {
099    
100                                    SessionErrors.add(actionRequest, e.getClass().getName(), e);
101                            }
102                            else {
103                                    throw e;
104                            }
105                    }
106            }
107    
108            @Override
109            public ActionForward render(
110                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
111                            RenderRequest renderRequest, RenderResponse renderResponse)
112                    throws Exception {
113    
114                    try {
115                            ActionUtil.getEntry(renderRequest);
116                    }
117                    catch (Exception e) {
118                            if (e instanceof NoSuchEntryException ||
119                                    e instanceof PrincipalException) {
120    
121                                    SessionErrors.add(renderRequest, e.getClass().getName());
122    
123                                    return mapping.findForward("portlet.bookmarks.error");
124                            }
125                            else {
126                                    throw e;
127                            }
128                    }
129    
130                    return mapping.findForward(
131                            getForward(renderRequest, "portlet.bookmarks.edit_entry"));
132            }
133    
134            protected void deleteEntry(ActionRequest actionRequest) throws Exception {
135                    long entryId = ParamUtil.getLong(actionRequest, "entryId");
136    
137                    BookmarksEntryServiceUtil.deleteEntry(entryId);
138            }
139    
140            protected void updateEntry(ActionRequest actionRequest) throws Exception {
141                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
142                            WebKeys.THEME_DISPLAY);
143    
144                    long entryId = ParamUtil.getLong(actionRequest, "entryId");
145    
146                    long groupId = themeDisplay.getScopeGroupId();
147                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
148                    String name = ParamUtil.getString(actionRequest, "name");
149                    String url = ParamUtil.getString(actionRequest, "url");
150                    String description = ParamUtil.getString(actionRequest, "description");
151    
152                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
153                            BookmarksEntry.class.getName(), actionRequest);
154    
155                    if (entryId <= 0) {
156    
157                            // Add entry
158    
159                            BookmarksEntry entry = BookmarksEntryServiceUtil.addEntry(
160                                    groupId, folderId, name, url, description, serviceContext);
161    
162                            AssetPublisherUtil.addAndStoreSelection(
163                                    actionRequest, BookmarksEntry.class.getName(),
164                                    entry.getEntryId(), -1);
165                    }
166                    else {
167    
168                            // Update entry
169    
170                            BookmarksEntryServiceUtil.updateEntry(
171                                    entryId, groupId, folderId, name, url, description,
172                                    serviceContext);
173                    }
174    
175                    AssetPublisherUtil.addRecentFolderId(
176                            actionRequest, BookmarksEntry.class.getName(), folderId);
177            }
178    
179    }