001    /**
002     * Copyright (c) 2000-present 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.kernel.util.StringUtil;
021    import com.liferay.portal.model.TrashedModel;
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.WebKeys;
028    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
029    import com.liferay.portlet.bookmarks.FolderNameException;
030    import com.liferay.portlet.bookmarks.NoSuchFolderException;
031    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
032    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
033    import com.liferay.portlet.bookmarks.service.BookmarksFolderServiceUtil;
034    import com.liferay.portlet.trash.util.TrashUtil;
035    
036    import java.util.ArrayList;
037    import java.util.List;
038    
039    import javax.portlet.ActionRequest;
040    import javax.portlet.ActionResponse;
041    import javax.portlet.PortletConfig;
042    import javax.portlet.RenderRequest;
043    import javax.portlet.RenderResponse;
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 EditFolderAction 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                                    updateFolder(actionRequest);
066                            }
067                            else if (cmd.equals(Constants.DELETE)) {
068                                    deleteFolders(actionRequest, false);
069                            }
070                            else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
071                                    deleteFolders(actionRequest, true);
072                            }
073                            else if (cmd.equals(Constants.SUBSCRIBE)) {
074                                    subscribeFolder(actionRequest);
075                            }
076                            else if (cmd.equals(Constants.UNSUBSCRIBE)) {
077                                    unsubscribeFolder(actionRequest);
078                            }
079    
080                            sendRedirect(actionRequest, actionResponse);
081                    }
082                    catch (Exception e) {
083                            if (e instanceof NoSuchFolderException ||
084                                    e instanceof PrincipalException) {
085    
086                                    SessionErrors.add(actionRequest, e.getClass());
087    
088                                    setForward(actionRequest, "portlet.bookmarks.error");
089                            }
090                            else if (e instanceof FolderNameException) {
091                                    SessionErrors.add(actionRequest, e.getClass());
092                            }
093                            else {
094                                    throw e;
095                            }
096                    }
097            }
098    
099            @Override
100            public ActionForward render(
101                            ActionMapping actionMapping, ActionForm actionForm,
102                            PortletConfig portletConfig, RenderRequest renderRequest,
103                            RenderResponse renderResponse)
104                    throws Exception {
105    
106                    try {
107                            ActionUtil.getFolder(renderRequest);
108                    }
109                    catch (Exception e) {
110                            if (e instanceof NoSuchFolderException ||
111                                    e instanceof PrincipalException) {
112    
113                                    SessionErrors.add(renderRequest, e.getClass());
114    
115                                    return actionMapping.findForward("portlet.bookmarks.error");
116                            }
117                            else {
118                                    throw e;
119                            }
120                    }
121    
122                    return actionMapping.findForward(
123                            getForward(renderRequest, "portlet.bookmarks.edit_folder"));
124            }
125    
126            protected void deleteFolders(
127                            ActionRequest actionRequest, boolean moveToTrash)
128                    throws Exception {
129    
130                    long[] deleteFolderIds = null;
131    
132                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
133    
134                    if (folderId > 0) {
135                            deleteFolderIds = new long[] {folderId};
136                    }
137                    else {
138                            deleteFolderIds = StringUtil.split(
139                                    ParamUtil.getString(actionRequest, "folderIds"), 0L);
140                    }
141    
142                    List<TrashedModel> trashedModels = new ArrayList<TrashedModel>();
143    
144                    for (long deleteFolderId : deleteFolderIds) {
145                            if (moveToTrash) {
146                                    BookmarksFolder folder =
147                                            BookmarksFolderServiceUtil.moveFolderToTrash(
148                                                    deleteFolderId);
149    
150                                    trashedModels.add(folder);
151                            }
152                            else {
153                                    BookmarksFolderServiceUtil.deleteFolder(deleteFolderId);
154                            }
155    
156                            AssetPublisherUtil.removeRecentFolderId(
157                                    actionRequest, BookmarksEntry.class.getName(), deleteFolderId);
158                    }
159    
160                    if (moveToTrash && !trashedModels.isEmpty()) {
161                            TrashUtil.addTrashSessionMessages(actionRequest, trashedModels);
162    
163                            hideDefaultSuccessMessage(actionRequest);
164                    }
165            }
166    
167            protected void subscribeFolder(ActionRequest actionRequest)
168                    throws Exception {
169    
170                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
171                            WebKeys.THEME_DISPLAY);
172    
173                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
174    
175                    BookmarksFolderServiceUtil.subscribeFolder(
176                            themeDisplay.getScopeGroupId(), folderId);
177            }
178    
179            protected void unsubscribeFolder(ActionRequest actionRequest)
180                    throws Exception {
181    
182                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
183                            WebKeys.THEME_DISPLAY);
184    
185                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
186    
187                    BookmarksFolderServiceUtil.unsubscribeFolder(
188                            themeDisplay.getScopeGroupId(), folderId);
189            }
190    
191            protected void updateFolder(ActionRequest actionRequest) throws Exception {
192                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
193    
194                    long parentFolderId = ParamUtil.getLong(
195                            actionRequest, "parentFolderId");
196                    String name = ParamUtil.getString(actionRequest, "name");
197                    String description = ParamUtil.getString(actionRequest, "description");
198    
199                    boolean mergeWithParentFolder = ParamUtil.getBoolean(
200                            actionRequest, "mergeWithParentFolder");
201    
202                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
203                            BookmarksFolder.class.getName(), actionRequest);
204    
205                    if (folderId <= 0) {
206    
207                            // Add folder
208    
209                            BookmarksFolderServiceUtil.addFolder(
210                                    parentFolderId, name, description, serviceContext);
211                    }
212                    else {
213    
214                            // Update folder
215    
216                            BookmarksFolderServiceUtil.updateFolder(
217                                    folderId, parentFolderId, name, description,
218                                    mergeWithParentFolder, serviceContext);
219                    }
220            }
221    
222    }