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.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.Constants;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.service.ServiceContext;
022    import com.liferay.portal.service.ServiceContextFactory;
023    import com.liferay.portal.struts.PortletAction;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.WebKeys;
026    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
027    import com.liferay.portlet.bookmarks.FolderNameException;
028    import com.liferay.portlet.bookmarks.NoSuchFolderException;
029    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
030    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
031    import com.liferay.portlet.bookmarks.service.BookmarksFolderServiceUtil;
032    
033    import javax.portlet.ActionRequest;
034    import javax.portlet.ActionResponse;
035    import javax.portlet.PortletConfig;
036    import javax.portlet.RenderRequest;
037    import javax.portlet.RenderResponse;
038    
039    import org.apache.struts.action.ActionForm;
040    import org.apache.struts.action.ActionForward;
041    import org.apache.struts.action.ActionMapping;
042    
043    /**
044     * @author Brian Wing Shun Chan
045     */
046    public class EditFolderAction extends PortletAction {
047    
048            @Override
049            public void processAction(
050                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
051                            ActionRequest actionRequest, ActionResponse actionResponse)
052                    throws Exception {
053    
054                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
055    
056                    try {
057                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
058                                    updateFolder(actionRequest);
059                            }
060                            else if (cmd.equals(Constants.DELETE)) {
061                                    deleteFolder(actionRequest);
062                            }
063                            else if (cmd.equals(Constants.SUBSCRIBE)) {
064                                    subscribeFolder(actionRequest);
065                            }
066                            else if (cmd.equals(Constants.UNSUBSCRIBE)) {
067                                    unsubscribeFolder(actionRequest);
068                            }
069    
070                            sendRedirect(actionRequest, actionResponse);
071                    }
072                    catch (Exception e) {
073                            if (e instanceof NoSuchFolderException ||
074                                    e instanceof PrincipalException) {
075    
076                                    SessionErrors.add(actionRequest, e.getClass());
077    
078                                    setForward(actionRequest, "portlet.bookmarks.error");
079                            }
080                            else if (e instanceof FolderNameException) {
081                                    SessionErrors.add(actionRequest, e.getClass());
082                            }
083                            else {
084                                    throw e;
085                            }
086                    }
087            }
088    
089            @Override
090            public ActionForward render(
091                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
092                            RenderRequest renderRequest, RenderResponse renderResponse)
093                    throws Exception {
094    
095                    try {
096                            ActionUtil.getFolder(renderRequest);
097                    }
098                    catch (Exception e) {
099                            if (e instanceof NoSuchFolderException ||
100                                    e instanceof PrincipalException) {
101    
102                                    SessionErrors.add(renderRequest, e.getClass());
103    
104                                    return mapping.findForward("portlet.bookmarks.error");
105                            }
106                            else {
107                                    throw e;
108                            }
109                    }
110    
111                    return mapping.findForward(
112                            getForward(renderRequest, "portlet.bookmarks.edit_folder"));
113            }
114    
115            protected void deleteFolder(ActionRequest actionRequest) throws Exception {
116                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
117    
118                    BookmarksFolderServiceUtil.deleteFolder(folderId);
119    
120                    AssetPublisherUtil.removeRecentFolderId(
121                            actionRequest, BookmarksEntry.class.getName(), folderId);
122            }
123    
124            protected void subscribeFolder(ActionRequest actionRequest)
125                    throws Exception {
126    
127                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
128                            WebKeys.THEME_DISPLAY);
129    
130                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
131    
132                    BookmarksFolderServiceUtil.subscribeFolder(
133                            themeDisplay.getScopeGroupId(), folderId);
134            }
135    
136            protected void unsubscribeFolder(ActionRequest actionRequest)
137                    throws Exception {
138    
139                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
140                            WebKeys.THEME_DISPLAY);
141    
142                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
143    
144                    BookmarksFolderServiceUtil.unsubscribeFolder(
145                            themeDisplay.getScopeGroupId(), folderId);
146            }
147    
148            protected void updateFolder(ActionRequest actionRequest) throws Exception {
149                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
150    
151                    long parentFolderId = ParamUtil.getLong(
152                            actionRequest, "parentFolderId");
153                    String name = ParamUtil.getString(actionRequest, "name");
154                    String description = ParamUtil.getString(actionRequest, "description");
155    
156                    boolean mergeWithParentFolder = ParamUtil.getBoolean(
157                            actionRequest, "mergeWithParentFolder");
158    
159                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
160                            BookmarksFolder.class.getName(), actionRequest);
161    
162                    if (folderId <= 0) {
163    
164                            // Add folder
165    
166                            BookmarksFolderServiceUtil.addFolder(
167                                    parentFolderId, name, description, serviceContext);
168                    }
169                    else {
170    
171                            // Update folder
172    
173                            BookmarksFolderServiceUtil.updateFolder(
174                                    folderId, parentFolderId, name, description,
175                                    mergeWithParentFolder, serviceContext);
176                    }
177            }
178    
179    }