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