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                                            SessionTreeJSClicks.openLayoutNodes(
078                                                    request, treeId, false, LayoutConstants.DEFAULT_PLID,
079                                                    false);
080    
081                                            List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
082                                                    groupId, privateLayout,
083                                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
084    
085                                            for (Layout layout : layouts) {
086                                                    SessionTreeJSClicks.openLayoutNodes(
087                                                            request, treeId, layout.isPrivateLayout(),
088                                                            layout.getLayoutId(), true);
089                                            }
090                                    }
091                                    else {
092                                            boolean recursive = ParamUtil.getBoolean(
093                                                    request, "recursive");
094    
095                                            Layout layout = LayoutLocalServiceUtil.getLayout(plid);
096    
097                                            SessionTreeJSClicks.openLayoutNodes(
098                                                    request, treeId, layout.isPrivateLayout(),
099                                                    layout.getLayoutId(), recursive);
100                                    }
101                            }
102                            else if (cmd.equals("layoutCollapse")) {
103                            }
104                            else if (cmd.equals("layoutUncheck")) {
105                                    long plid = ParamUtil.getLong(request, "plid");
106    
107                                    if (plid == LayoutConstants.DEFAULT_PLID) {
108                                            long groupId = ParamUtil.getLong(request, "groupId");
109                                            boolean privateLayout = ParamUtil.getBoolean(
110                                                    request, "privateLayout");
111    
112                                            SessionTreeJSClicks.closeLayoutNodes(
113                                                    request, treeId, false, LayoutConstants.DEFAULT_PLID,
114                                                    false);
115    
116                                            List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
117                                                    groupId, privateLayout,
118                                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
119    
120                                            for (Layout layout : layouts) {
121                                                    SessionTreeJSClicks.closeLayoutNodes(
122                                                            request, treeId, layout.isPrivateLayout(),
123                                                            layout.getLayoutId(), true);
124                                            }
125                                    }
126                                    else {
127                                            boolean recursive = ParamUtil.getBoolean(
128                                                    request, "recursive");
129    
130                                            Layout layout = LayoutLocalServiceUtil.getLayout(plid);
131    
132                                            SessionTreeJSClicks.closeLayoutNodes(
133                                                    request, treeId, layout.isPrivateLayout(),
134                                                    layout.getLayoutId(), recursive);
135                                    }
136                            }
137                            else if (cmd.equals("layoutUncollapse")) {
138                            }
139                            else {
140                                    String nodeId = ParamUtil.getString(request, "nodeId");
141                                    boolean openNode = ParamUtil.getBoolean(request, "openNode");
142    
143                                    if (openNode) {
144                                            SessionTreeJSClicks.openNode(request, treeId, nodeId);
145                                    }
146                                    else {
147                                            SessionTreeJSClicks.closeNode(request, treeId, nodeId);
148                                    }
149                            }
150    
151                            if (!cmd.isEmpty()) {
152                                    updateCheckedLayoutPlids(request, treeId);
153                            }
154    
155                            response.setContentType(ContentTypes.APPLICATION_JSON);
156    
157                            PortalPreferences portalPreferences =
158                                    PortletPreferencesFactoryUtil.getPortalPreferences(request);
159    
160                            String json = portalPreferences.getValue(
161                                    SessionTreeJSClicks.class.getName(), treeId + "Plid");
162    
163                            if (Validator.isNotNull(json)) {
164                                    ServletResponseUtil.write(response, json);
165                            }
166    
167                            return null;
168                    }
169                    catch (Exception e) {
170                            PortalUtil.sendError(e, request, response);
171    
172                            return null;
173                    }
174            }
175    
176            protected void updateCheckedLayoutPlids(
177                            HttpServletRequest request, String treeId)
178                    throws SystemException {
179    
180                    long groupId = ParamUtil.getLong(request, "groupId");
181                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
182    
183                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
184    
185                    PortalPreferences portalPreferences =
186                            PortletPreferencesFactoryUtil.getPortalPreferences(request);
187    
188                    long[] checkedLayoutIds = StringUtil.split(
189                            portalPreferences.getValue(
190                                    SessionTreeJSClicks.class.getName(), treeId),
191                            0L);
192    
193                    for (long checkedLayoutId : checkedLayoutIds) {
194                            if (checkedLayoutId == LayoutConstants.DEFAULT_PLID) {
195                                    jsonArray.put(String.valueOf(LayoutConstants.DEFAULT_PLID));
196                            }
197    
198                            Layout checkedLayout = LayoutLocalServiceUtil.fetchLayout(
199                                    groupId, privateLayout, checkedLayoutId);
200    
201                            if (checkedLayout == null) {
202                                    continue;
203                            }
204    
205                            jsonArray.put(String.valueOf(checkedLayout.getPlid()));
206                    }
207    
208                    portalPreferences.setValue(
209                            SessionTreeJSClicks.class.getName(), treeId + "Plid",
210                            jsonArray.toString());
211            }
212    
213    }