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