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