001    /**
002     * Copyright (c) 2000-2010 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            public void processAction(
047                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
048                            ActionRequest actionRequest, ActionResponse actionResponse)
049                    throws Exception {
050    
051                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
052    
053                    try {
054                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
055                                    updateFolder(actionRequest);
056                            }
057                            else if (cmd.equals(Constants.DELETE)) {
058                                    deleteFolder(actionRequest);
059                            }
060    
061                            sendRedirect(actionRequest, actionResponse);
062                    }
063                    catch (Exception e) {
064                            if (e instanceof NoSuchFolderException ||
065                                    e instanceof PrincipalException) {
066    
067                                    SessionErrors.add(actionRequest, e.getClass().getName());
068    
069                                    setForward(actionRequest, "portlet.bookmarks.error");
070                            }
071                            else if (e instanceof FolderNameException) {
072                                    SessionErrors.add(actionRequest, e.getClass().getName());
073                            }
074                            else {
075                                    throw e;
076                            }
077                    }
078            }
079    
080            public ActionForward render(
081                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
082                            RenderRequest renderRequest, RenderResponse renderResponse)
083                    throws Exception {
084    
085                    try {
086                            ActionUtil.getFolder(renderRequest);
087                    }
088                    catch (Exception e) {
089                            if (e instanceof NoSuchFolderException ||
090                                    e instanceof PrincipalException) {
091    
092                                    SessionErrors.add(renderRequest, e.getClass().getName());
093    
094                                    return mapping.findForward("portlet.bookmarks.error");
095                            }
096                            else {
097                                    throw e;
098                            }
099                    }
100    
101                    return mapping.findForward(
102                            getForward(renderRequest, "portlet.bookmarks.edit_folder"));
103            }
104    
105            protected void deleteFolder(ActionRequest actionRequest) throws Exception {
106                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
107    
108                    BookmarksFolderServiceUtil.deleteFolder(folderId);
109    
110                    AssetPublisherUtil.removeRecentFolderId(
111                            actionRequest, BookmarksEntry.class.getName(), folderId);
112            }
113    
114            protected void updateFolder(ActionRequest actionRequest) throws Exception {
115                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
116    
117                    long parentFolderId = ParamUtil.getLong(
118                            actionRequest, "parentFolderId");
119                    String name = ParamUtil.getString(actionRequest, "name");
120                    String description = ParamUtil.getString(actionRequest, "description");
121    
122                    boolean mergeWithParentFolder = ParamUtil.getBoolean(
123                            actionRequest, "mergeWithParentFolder");
124    
125                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
126                            BookmarksFolder.class.getName(), actionRequest);
127    
128                    if (folderId <= 0) {
129    
130                            // Add folder
131    
132                            BookmarksFolderServiceUtil.addFolder(
133                                    parentFolderId, name, description, serviceContext);
134                    }
135                    else {
136    
137                            // Update folder
138    
139                            BookmarksFolderServiceUtil.updateFolder(
140                                    folderId, parentFolderId, name, description,
141                                    mergeWithParentFolder, serviceContext);
142                    }
143            }
144    
145    }