001    /**
002     * Copyright (c) 2000-present 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.json.JSONArray;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.ContentTypes;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.model.Layout;
026    import com.liferay.portal.model.LayoutConstants;
027    import com.liferay.portal.service.LayoutLocalServiceUtil;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portlet.PortalPreferences;
030    import com.liferay.portlet.PortletPreferencesFactoryUtil;
031    import com.liferay.taglib.ui.util.SessionTreeJSClicks;
032    
033    import java.util.ConcurrentModificationException;
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    
171                    long groupId = ParamUtil.getLong(request, "groupId");
172                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
173    
174                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
175    
176                    while (true) {
177                            try {
178                                    PortalPreferences portalPreferences =
179                                            PortletPreferencesFactoryUtil.getPortalPreferences(request);
180    
181                                    long[] checkedLayoutIds = StringUtil.split(
182                                            portalPreferences.getValue(
183                                                    SessionTreeJSClicks.class.getName(), treeId),
184                                            0L);
185    
186                                    for (long checkedLayoutId : checkedLayoutIds) {
187                                            Layout checkedLayout = LayoutLocalServiceUtil.fetchLayout(
188                                                    groupId, privateLayout, checkedLayoutId);
189    
190                                            if (checkedLayout == null) {
191                                                    continue;
192                                            }
193    
194                                            jsonArray.put(String.valueOf(checkedLayout.getPlid()));
195                                    }
196    
197                                    portalPreferences.setValue(
198                                            SessionTreeJSClicks.class.getName(), treeId + "Plid",
199                                            jsonArray.toString());
200    
201                                    return;
202                            }
203                            catch (ConcurrentModificationException cme) {
204                                    continue;
205                            }
206                    }
207            }
208    
209    }