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.portal.action;
016    
017    import com.liferay.portal.kernel.util.Constants;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.model.Layout;
021    import com.liferay.portal.model.LayoutConstants;
022    import com.liferay.portal.service.LayoutLocalServiceUtil;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.portal.util.SessionTreeJSClicks;
025    
026    import java.util.List;
027    
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletResponse;
030    
031    import org.apache.struts.action.Action;
032    import org.apache.struts.action.ActionForm;
033    import org.apache.struts.action.ActionForward;
034    import org.apache.struts.action.ActionMapping;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     */
039    public class SessionTreeJSClickAction extends Action {
040    
041            @Override
042            public ActionForward execute(
043                            ActionMapping actionMapping, ActionForm actionForm,
044                            HttpServletRequest request, HttpServletResponse response)
045                    throws Exception {
046    
047                    try {
048                            String cmd = ParamUtil.getString(request, Constants.CMD);
049    
050                            String treeId = ParamUtil.getString(request, "treeId");
051    
052                            if (cmd.equals("collapse")) {
053                                    SessionTreeJSClicks.closeNodes(request, treeId);
054                            }
055                            else if (cmd.equals("expand")) {
056                                    String[] nodeIds = StringUtil.split(
057                                            ParamUtil.getString(request, "nodeIds"));
058    
059                                    SessionTreeJSClicks.openNodes(request, treeId, nodeIds);
060                            }
061                            else if (cmd.equals("layoutCheck")) {
062                                    long plid = ParamUtil.getLong(request, "plid");
063    
064                                    if (plid == LayoutConstants.DEFAULT_PLID) {
065                                            long groupId = ParamUtil.getLong(request, "groupId");
066                                            boolean privateLayout = ParamUtil.getBoolean(
067                                                    request, "privateLayout");
068    
069                                            List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
070                                                    groupId, privateLayout,
071                                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
072    
073                                            for (Layout layout : layouts) {
074                                                    SessionTreeJSClicks.openLayoutNodes(
075                                                            request, treeId, layout.isPrivateLayout(),
076                                                            layout.getLayoutId(), true);
077                                            }
078                                    }
079                                    else {
080                                            boolean recursive = ParamUtil.getBoolean(
081                                                    request, "recursive");
082    
083                                            Layout layout = LayoutLocalServiceUtil.getLayout(plid);
084    
085                                            SessionTreeJSClicks.openLayoutNodes(
086                                                    request, treeId, layout.isPrivateLayout(),
087                                                    layout.getLayoutId(), recursive);
088                                    }
089                            }
090                            else if (cmd.equals("layoutCollapse")) {
091                            }
092                            else if (cmd.equals("layoutUncheck")) {
093                                    long plid = ParamUtil.getLong(request, "plid");
094    
095                                    if (plid == LayoutConstants.DEFAULT_PLID) {
096                                            long groupId = ParamUtil.getLong(request, "groupId");
097                                            boolean privateLayout = ParamUtil.getBoolean(
098                                                    request, "privateLayout");
099    
100                                            List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
101                                                    groupId, privateLayout,
102                                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
103    
104                                            for (Layout layout : layouts) {
105                                                    SessionTreeJSClicks.closeLayoutNodes(
106                                                            request, treeId, layout.isPrivateLayout(),
107                                                            layout.getLayoutId(), true);
108                                            }
109                                    }
110                                    else {
111                                            boolean recursive = ParamUtil.getBoolean(
112                                                    request, "recursive");
113    
114                                            Layout layout = LayoutLocalServiceUtil.getLayout(plid);
115    
116                                            SessionTreeJSClicks.closeLayoutNodes(
117                                                    request, treeId, layout.isPrivateLayout(),
118                                                    layout.getLayoutId(), recursive);
119                                    }
120                            }
121                            else if (cmd.equals("layoutUncollapse")) {
122                            }
123                            else {
124                                    String nodeId = ParamUtil.getString(request, "nodeId");
125                                    boolean openNode = ParamUtil.getBoolean(request, "openNode");
126    
127                                    if (openNode) {
128                                            SessionTreeJSClicks.openNode(request, treeId, nodeId);
129                                    }
130                                    else {
131                                            SessionTreeJSClicks.closeNode(request, treeId, nodeId);
132                                    }
133                            }
134    
135                            return null;
136                    }
137                    catch (Exception e) {
138                            PortalUtil.sendError(e, request, response);
139    
140                            return null;
141                    }
142            }
143    
144    }