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.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.model.Layout;
023    import com.liferay.portal.service.LayoutLocalServiceUtil;
024    import com.liferay.portlet.PortalPreferences;
025    import com.liferay.portlet.PortletPreferencesFactoryUtil;
026    
027    import java.util.ArrayList;
028    import java.util.List;
029    
030    import javax.servlet.http.HttpServletRequest;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Eduardo Lundgren
035     */
036    public class SessionTreeJSClicks {
037    
038            public static final String CLASS_NAME = SessionTreeJSClicks.class.getName();
039    
040            public static void closeLayoutNodes(
041                    HttpServletRequest request, String treeId, boolean privateLayout,
042                    long layoutId, boolean recursive) {
043    
044                    try {
045                            List<String> layoutIds = new ArrayList<String>();
046    
047                            layoutIds.add(String.valueOf(layoutId));
048    
049                            if (recursive) {
050                                    getLayoutIds(request, privateLayout, layoutId, layoutIds);
051                            }
052    
053                            closeNodes(
054                                    request, treeId,
055                                    layoutIds.toArray(new String[layoutIds.size()]));
056                    }
057                    catch (Exception e) {
058                            _log.error(e, e);
059                    }
060            }
061    
062            public static void closeNode(
063                    HttpServletRequest request, String treeId, String nodeId) {
064    
065                    try {
066                            String openNodesString = get(request, treeId);
067    
068                            openNodesString = StringUtil.remove(openNodesString, nodeId);
069    
070                            put(request, treeId, openNodesString);
071                    }
072                    catch (Exception e) {
073                            _log.error(e, e);
074                    }
075            }
076    
077            public static void closeNodes(HttpServletRequest request, String treeId) {
078                    try {
079                            String openNodesString = StringPool.BLANK;
080    
081                            put(request, treeId, openNodesString);
082                    }
083                    catch (Exception e) {
084                            _log.error(e, e);
085                    }
086            }
087    
088            public static void closeNodes(
089                    HttpServletRequest request, String treeId, String[] nodeIds) {
090    
091                    try {
092                            String openNodesString = get(request, treeId);
093    
094                            for (String nodeId : nodeIds) {
095                                    openNodesString = StringUtil.remove(openNodesString, nodeId);
096                            }
097    
098                            put(request, treeId, openNodesString);
099                    }
100                    catch (Exception e) {
101                            _log.error(e, e);
102                    }
103            }
104    
105            public static String getOpenNodes(
106                    HttpServletRequest request, String treeId) {
107    
108                    try {
109                            return get(request, treeId);
110                    }
111                    catch (Exception e) {
112                            _log.error(e, e);
113    
114                            return null;
115                    }
116            }
117    
118            public static void openLayoutNodes(
119                    HttpServletRequest request, String treeId, boolean privateLayout,
120                    long layoutId, boolean recursive) {
121    
122                    try {
123                            List<String> layoutIds = new ArrayList<String>();
124    
125                            layoutIds.add(String.valueOf(layoutId));
126    
127                            if (recursive) {
128                                    getLayoutIds(request, privateLayout, layoutId, layoutIds);
129                            }
130    
131                            openNodes(
132                                    request, treeId,
133                                    layoutIds.toArray(new String[layoutIds.size()]));
134                    }
135                    catch (Exception e) {
136                            _log.error(e, e);
137                    }
138            }
139    
140            public static void openNode(
141                    HttpServletRequest request, String treeId, String nodeId) {
142    
143                    try {
144                            String openNodesString = get(request, treeId);
145    
146                            openNodesString = StringUtil.add(openNodesString, nodeId);
147    
148                            put(request, treeId, openNodesString);
149                    }
150                    catch (Exception e) {
151                            _log.error(e, e);
152                    }
153            }
154    
155            public static void openNodes(
156                    HttpServletRequest request, String treeId, String[] nodeIds) {
157    
158                    try {
159                            String openNodesString = get(request, treeId);
160    
161                            for (String nodeId : nodeIds) {
162                                    openNodesString = StringUtil.add(openNodesString, nodeId);
163                            }
164    
165                            put(request, treeId, openNodesString);
166                    }
167                    catch (Exception e) {
168                            _log.error(e, e);
169                    }
170            }
171    
172            protected static String get(HttpServletRequest request, String key) {
173                    try {
174                            PortalPreferences preferences =
175                                    PortletPreferencesFactoryUtil.getPortalPreferences(request);
176    
177                            return preferences.getValue(CLASS_NAME, key);
178                    }
179                    catch (Exception e) {
180                            _log.error(e, e);
181    
182                            return null;
183                    }
184            }
185    
186            protected static List<String> getLayoutIds(
187                            HttpServletRequest request, boolean privateLayout,
188                            long parentLayoutId, List<String> layoutIds)
189                    throws Exception {
190    
191                    long groupId = ParamUtil.getLong(request, "groupId");
192    
193                    List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
194                            groupId, privateLayout, parentLayoutId);
195    
196                    for (Layout layout : layouts) {
197                            layoutIds.add(String.valueOf(layout.getLayoutId()));
198    
199                            getLayoutIds(
200                                    request, privateLayout, layout.getLayoutId(), layoutIds);
201                    }
202    
203                    return layoutIds;
204            }
205    
206            protected static void put(
207                    HttpServletRequest request, String key, String value) {
208    
209                    try {
210                            PortalPreferences preferences =
211                                    PortletPreferencesFactoryUtil.getPortalPreferences(request);
212    
213                            preferences.setValue(CLASS_NAME, key, value);
214                    }
215                    catch (Exception e) {
216                            _log.error(e, e);
217                    }
218            }
219    
220            private static Log _log = LogFactoryUtil.getLog(SessionTreeJSClicks.class);
221    
222    }