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.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.servlet.SessionMessages;
021    import com.liferay.portal.kernel.util.ArrayUtil;
022    import com.liferay.portal.kernel.util.Constants;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.service.ServiceContextFactory;
029    import com.liferay.portal.struts.PortletAction;
030    import com.liferay.portal.theme.ThemeDisplay;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portal.util.WebKeys;
033    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
034    import com.liferay.portlet.bookmarks.FolderNameException;
035    import com.liferay.portlet.bookmarks.NoSuchFolderException;
036    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
037    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
038    import com.liferay.portlet.bookmarks.service.BookmarksFolderServiceUtil;
039    
040    import java.util.HashMap;
041    import java.util.Map;
042    
043    import javax.portlet.ActionRequest;
044    import javax.portlet.ActionResponse;
045    import javax.portlet.PortletConfig;
046    import javax.portlet.RenderRequest;
047    import javax.portlet.RenderResponse;
048    
049    import org.apache.struts.action.ActionForm;
050    import org.apache.struts.action.ActionForward;
051    import org.apache.struts.action.ActionMapping;
052    
053    /**
054     * @author Brian Wing Shun Chan
055     */
056    public class EditFolderAction extends PortletAction {
057    
058            @Override
059            public void processAction(
060                            ActionMapping actionMapping, ActionForm actionForm,
061                            PortletConfig portletConfig, ActionRequest actionRequest,
062                            ActionResponse actionResponse)
063                    throws Exception {
064    
065                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
066    
067                    try {
068                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
069                                    updateFolder(actionRequest);
070                            }
071                            else if (cmd.equals(Constants.DELETE)) {
072                                    deleteFolders(actionRequest, false);
073                            }
074                            else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
075                                    deleteFolders(actionRequest, true);
076                            }
077                            else if (cmd.equals(Constants.RESTORE)) {
078                                    restoreFolderFromTrash(actionRequest);
079                            }
080                            else if (cmd.equals(Constants.SUBSCRIBE)) {
081                                    subscribeFolder(actionRequest);
082                            }
083                            else if (cmd.equals(Constants.UNSUBSCRIBE)) {
084                                    unsubscribeFolder(actionRequest);
085                            }
086    
087                            sendRedirect(actionRequest, actionResponse);
088                    }
089                    catch (Exception e) {
090                            if (e instanceof NoSuchFolderException ||
091                                    e instanceof PrincipalException) {
092    
093                                    SessionErrors.add(actionRequest, e.getClass());
094    
095                                    setForward(actionRequest, "portlet.bookmarks.error");
096                            }
097                            else if (e instanceof FolderNameException) {
098                                    SessionErrors.add(actionRequest, e.getClass());
099                            }
100                            else {
101                                    throw e;
102                            }
103                    }
104            }
105    
106            @Override
107            public ActionForward render(
108                            ActionMapping actionMapping, ActionForm actionForm,
109                            PortletConfig portletConfig, RenderRequest renderRequest,
110                            RenderResponse renderResponse)
111                    throws Exception {
112    
113                    try {
114                            ActionUtil.getFolder(renderRequest);
115                    }
116                    catch (Exception e) {
117                            if (e instanceof NoSuchFolderException ||
118                                    e instanceof PrincipalException) {
119    
120                                    SessionErrors.add(renderRequest, e.getClass());
121    
122                                    return actionMapping.findForward("portlet.bookmarks.error");
123                            }
124                            else {
125                                    throw e;
126                            }
127                    }
128    
129                    return actionMapping.findForward(
130                            getForward(renderRequest, "portlet.bookmarks.edit_folder"));
131            }
132    
133            protected void deleteFolders(
134                            ActionRequest actionRequest, boolean moveToTrash)
135                    throws Exception {
136    
137                    String deleteFolderTitle = null;
138    
139                    long[] deleteFolderIds = null;
140    
141                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
142    
143                    if (folderId > 0) {
144                            deleteFolderIds = new long[] {folderId};
145                    }
146                    else {
147                            deleteFolderIds = StringUtil.split(
148                                    ParamUtil.getString(actionRequest, "folderIds"), 0L);
149                    }
150    
151                    for (int i = 0; i < deleteFolderIds.length; i++) {
152                            long deleteFolderId = deleteFolderIds[i];
153    
154                            if (moveToTrash) {
155                                    BookmarksFolder folder =
156                                            BookmarksFolderServiceUtil.moveFolderToTrash(
157                                                    deleteFolderId);
158    
159                                    if (i == 0) {
160                                            deleteFolderTitle = folder.getName();
161                                    }
162                            }
163                            else {
164                                    BookmarksFolderServiceUtil.deleteFolder(deleteFolderId);
165                            }
166    
167                            AssetPublisherUtil.removeRecentFolderId(
168                                    actionRequest, BookmarksEntry.class.getName(), deleteFolderId);
169                    }
170    
171                    if (moveToTrash && (deleteFolderIds.length > 0)) {
172                            Map<String, String[]> data = new HashMap<String, String[]>();
173    
174                            data.put(
175                                    "deleteEntryClassName",
176                                    new String[] {BookmarksFolder.class.getName()});
177    
178                            if (Validator.isNotNull(deleteFolderTitle)) {
179                                    data.put("deleteEntryTitle", new String[] {deleteFolderTitle});
180                            }
181    
182                            data.put(
183                                    "restoreFolderIds", ArrayUtil.toStringArray(deleteFolderIds));
184    
185                            SessionMessages.add(
186                                    actionRequest,
187                                    PortalUtil.getPortletId(actionRequest) +
188                                            SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
189    
190                            hideDefaultSuccessMessage(actionRequest);
191                    }
192            }
193    
194            protected void restoreFolderFromTrash(ActionRequest actionRequest)
195                    throws PortalException, SystemException {
196    
197                    long[] restoreEntryIds = StringUtil.split(
198                            ParamUtil.getString(actionRequest, "restoreFolderIds"), 0L);
199    
200                    for (long restoreEntryId : restoreEntryIds) {
201                            BookmarksFolderServiceUtil.restoreFolderFromTrash(restoreEntryId);
202                    }
203            }
204    
205            protected void subscribeFolder(ActionRequest actionRequest)
206                    throws Exception {
207    
208                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
209                            WebKeys.THEME_DISPLAY);
210    
211                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
212    
213                    BookmarksFolderServiceUtil.subscribeFolder(
214                            themeDisplay.getScopeGroupId(), folderId);
215            }
216    
217            protected void unsubscribeFolder(ActionRequest actionRequest)
218                    throws Exception {
219    
220                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
221                            WebKeys.THEME_DISPLAY);
222    
223                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
224    
225                    BookmarksFolderServiceUtil.unsubscribeFolder(
226                            themeDisplay.getScopeGroupId(), folderId);
227            }
228    
229            protected void updateFolder(ActionRequest actionRequest) throws Exception {
230                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
231    
232                    long parentFolderId = ParamUtil.getLong(
233                            actionRequest, "parentFolderId");
234                    String name = ParamUtil.getString(actionRequest, "name");
235                    String description = ParamUtil.getString(actionRequest, "description");
236    
237                    boolean mergeWithParentFolder = ParamUtil.getBoolean(
238                            actionRequest, "mergeWithParentFolder");
239    
240                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
241                            BookmarksFolder.class.getName(), actionRequest);
242    
243                    if (folderId <= 0) {
244    
245                            // Add folder
246    
247                            BookmarksFolderServiceUtil.addFolder(
248                                    parentFolderId, name, description, serviceContext);
249                    }
250                    else {
251    
252                            // Update folder
253    
254                            BookmarksFolderServiceUtil.updateFolder(
255                                    folderId, parentFolderId, name, description,
256                                    mergeWithParentFolder, serviceContext);
257                    }
258            }
259    
260    }