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