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