001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.action;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.json.JSONArray;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
021    import com.liferay.portal.kernel.util.Constants;
022    import com.liferay.portal.kernel.util.ContentTypes;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.model.Layout;
027    import com.liferay.portal.model.LayoutConstants;
028    import com.liferay.portal.service.LayoutLocalServiceUtil;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portal.util.SessionTreeJSClicks;
031    import com.liferay.portlet.PortalPreferences;
032    import com.liferay.portlet.PortletPreferencesFactoryUtil;
033    
034    import java.util.List;
035    
036    import javax.servlet.http.HttpServletRequest;
037    import javax.servlet.http.HttpServletResponse;
038    
039    import org.apache.struts.action.Action;
040    import org.apache.struts.action.ActionForm;
041    import org.apache.struts.action.ActionForward;
042    import org.apache.struts.action.ActionMapping;
043    
044    /**
045     * @author Brian Wing Shun Chan
046     */
047    public class SessionTreeJSClickAction extends Action {
048    
049            @Override
050            public ActionForward execute(
051                            ActionMapping actionMapping, ActionForm actionForm,
052                            HttpServletRequest request, HttpServletResponse response)
053                    throws Exception {
054    
055                    try {
056                            String cmd = ParamUtil.getString(request, Constants.CMD);
057    
058                            String treeId = ParamUtil.getString(request, "treeId");
059    
060                            if (cmd.equals("collapse")) {
061                                    SessionTreeJSClicks.closeNodes(request, treeId);
062                            }
063                            else if (cmd.equals("expand")) {
064                                    String[] nodeIds = StringUtil.split(
065                                            ParamUtil.getString(request, "nodeIds"));
066    
067                                    SessionTreeJSClicks.openNodes(request, treeId, nodeIds);
068                            }
069                            else if (cmd.equals("layoutCheck")) {
070                                    long plid = ParamUtil.getLong(request, "plid");
071    
072                                    if (plid == LayoutConstants.DEFAULT_PLID) {
073                                            long groupId = ParamUtil.getLong(request, "groupId");
074                                            boolean privateLayout = ParamUtil.getBoolean(
075                                                    request, "privateLayout");
076    
077                                            List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
078                                                    groupId, privateLayout,
079                                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
080    
081                                            for (Layout layout : layouts) {
082                                                    SessionTreeJSClicks.openLayoutNodes(
083                                                            request, treeId, layout.isPrivateLayout(),
084                                                            layout.getLayoutId(), true);
085                                            }
086                                    }
087                                    else {
088                                            boolean recursive = ParamUtil.getBoolean(
089                                                    request, "recursive");
090    
091                                            Layout layout = LayoutLocalServiceUtil.getLayout(plid);
092    
093                                            SessionTreeJSClicks.openLayoutNodes(
094                                                    request, treeId, layout.isPrivateLayout(),
095                                                    layout.getLayoutId(), recursive);
096                                    }
097                            }
098                            else if (cmd.equals("layoutCollapse")) {
099                            }
100                            else if (cmd.equals("layoutUncheck")) {
101                                    long plid = ParamUtil.getLong(request, "plid");
102    
103                                    if (plid == LayoutConstants.DEFAULT_PLID) {
104                                            long groupId = ParamUtil.getLong(request, "groupId");
105                                            boolean privateLayout = ParamUtil.getBoolean(
106                                                    request, "privateLayout");
107    
108                                            List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
109                                                    groupId, privateLayout,
110                                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
111    
112                                            for (Layout layout : layouts) {
113                                                    SessionTreeJSClicks.closeLayoutNodes(
114                                                            request, treeId, layout.isPrivateLayout(),
115                                                            layout.getLayoutId(), true);
116                                            }
117                                    }
118                                    else {
119                                            boolean recursive = ParamUtil.getBoolean(
120                                                    request, "recursive");
121    
122                                            Layout layout = LayoutLocalServiceUtil.getLayout(plid);
123    
124                                            SessionTreeJSClicks.closeLayoutNodes(
125                                                    request, treeId, layout.isPrivateLayout(),
126                                                    layout.getLayoutId(), recursive);
127                                    }
128                            }
129                            else if (cmd.equals("layoutUncollapse")) {
130                            }
131                            else {
132                                    String nodeId = ParamUtil.getString(request, "nodeId");
133                                    boolean openNode = ParamUtil.getBoolean(request, "openNode");
134    
135                                    if (openNode) {
136                                            SessionTreeJSClicks.openNode(request, treeId, nodeId);
137                                    }
138                                    else {
139                                            SessionTreeJSClicks.closeNode(request, treeId, nodeId);
140                                    }
141                            }
142    
143                            if (!cmd.isEmpty()) {
144                                    updateCheckedLayoutPlids(request, treeId);
145                            }
146    
147                            response.setContentType(ContentTypes.APPLICATION_JSON);
148    
149                            PortalPreferences portalPreferences =
150                                    PortletPreferencesFactoryUtil.getPortalPreferences(request);
151    
152                            String json = portalPreferences.getValue(
153                                    SessionTreeJSClicks.class.getName(), treeId + "Plid");
154    
155                            if (Validator.isNotNull(json)) {
156                                    ServletResponseUtil.write(response, json);
157                            }
158    
159                            return null;
160                    }
161                    catch (Exception e) {
162                            PortalUtil.sendError(e, request, response);
163    
164                            return null;
165                    }
166            }
167    
168            protected void updateCheckedLayoutPlids(
169                            HttpServletRequest request, String treeId)
170                    throws SystemException {
171    
172                    long groupId = ParamUtil.getLong(request, "groupId");
173                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
174    
175                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
176    
177                    PortalPreferences portalPreferences =
178                            PortletPreferencesFactoryUtil.getPortalPreferences(request);
179    
180                    long[] checkedLayoutIds = StringUtil.split(
181                            portalPreferences.getValue(
182                                    SessionTreeJSClicks.class.getName(), treeId),
183                            0L);
184    
185                    for (long checkedLayoutId : checkedLayoutIds) {
186                            Layout checkedLayout = LayoutLocalServiceUtil.fetchLayout(
187                                    groupId, privateLayout, checkedLayoutId);
188    
189                            if (checkedLayout == null) {
190                                    continue;
191                            }
192    
193                            jsonArray.put(String.valueOf(checkedLayout.getPlid()));
194                    }
195    
196                    portalPreferences.setValue(
197                            SessionTreeJSClicks.class.getName(), treeId + "Plid",
198                            jsonArray.toString());
199            }
200    
201    }