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.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 mapping, ActionForm form, HttpServletRequest request,
044                            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.getPrivateLayout(),
076                                                            layout.getLayoutId(), true);
077                                            }
078                                    }
079                                    else {
080                                            Layout layout = LayoutLocalServiceUtil.getLayout(plid);
081    
082                                            SessionTreeJSClicks.openLayoutNodes(
083                                                    request, treeId, layout.getPrivateLayout(),
084                                                    layout.getLayoutId(), true);
085                                    }
086                            }
087                            else if (cmd.equals("layoutCollapse")) {
088                            }
089                            else if (cmd.equals("layoutUncheck")) {
090                                    long plid = ParamUtil.getLong(request, "plid");
091    
092                                    if (plid == LayoutConstants.DEFAULT_PLID) {
093                                            long groupId = ParamUtil.getLong(request, "groupId");
094                                            boolean privateLayout = ParamUtil.getBoolean(
095                                                    request, "privateLayout");
096    
097                                            List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
098                                                    groupId, privateLayout,
099                                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
100    
101                                            for (Layout layout : layouts) {
102                                                    SessionTreeJSClicks.closeLayoutNodes(
103                                                            request, treeId, layout.getPrivateLayout(),
104                                                            layout.getLayoutId(), true);
105                                            }
106                                    }
107                                    else {
108                                            Layout layout = LayoutLocalServiceUtil.getLayout(plid);
109    
110                                            SessionTreeJSClicks.closeLayoutNodes(
111                                                    request, treeId, layout.getPrivateLayout(),
112                                                    layout.getLayoutId(), true);
113                                    }
114                            }
115                            else if (cmd.equals("layoutUncollapse")) {
116                            }
117                            else {
118                                    String nodeId = ParamUtil.getString(request, "nodeId");
119                                    boolean openNode = ParamUtil.getBoolean(request, "openNode");
120    
121                                    if (openNode) {
122                                            SessionTreeJSClicks.openNode(request, treeId, nodeId);
123                                    }
124                                    else {
125                                            SessionTreeJSClicks.closeNode(request, treeId, nodeId);
126                                    }
127                            }
128    
129                            return null;
130                    }
131                    catch (Exception e) {
132                            PortalUtil.sendError(e, request, response);
133    
134                            return null;
135                    }
136            }
137    
138    }