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.taglib.ui.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 void closeLayoutNodes(
039                    HttpServletRequest request, String treeId, boolean privateLayout,
040                    long layoutId, boolean recursive) {
041    
042                    try {
043                            List<String> layoutIds = new ArrayList<String>();
044    
045                            layoutIds.add(String.valueOf(layoutId));
046    
047                            if (recursive) {
048                                    getLayoutIds(request, privateLayout, layoutId, layoutIds);
049                            }
050    
051                            closeNodes(
052                                    request, treeId,
053                                    layoutIds.toArray(new String[layoutIds.size()]));
054                    }
055                    catch (Exception e) {
056                            _log.error(e, e);
057                    }
058            }
059    
060            public static void closeNode(
061                    HttpServletRequest request, String treeId, String nodeId) {
062    
063                    try {
064                            String openNodesString = get(request, treeId);
065    
066                            openNodesString = StringUtil.removeFromList(
067                                    openNodesString, nodeId);
068    
069                            put(request, treeId, openNodesString);
070                    }
071                    catch (Exception e) {
072                            _log.error(e, e);
073                    }
074            }
075    
076            public static void closeNodes(HttpServletRequest request, String treeId) {
077                    try {
078                            String openNodesString = StringPool.BLANK;
079    
080                            put(request, treeId, openNodesString);
081                    }
082                    catch (Exception e) {
083                            _log.error(e, e);
084                    }
085            }
086    
087            public static void closeNodes(
088                    HttpServletRequest request, String treeId, String[] nodeIds) {
089    
090                    try {
091                            String openNodesString = get(request, treeId);
092    
093                            for (String nodeId : nodeIds) {
094                                    openNodesString = StringUtil.removeFromList(
095                                            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(
178                                    SessionTreeJSClicks.class.getName(), key);
179                    }
180                    catch (Exception e) {
181                            _log.error(e, e);
182    
183                            return null;
184                    }
185            }
186    
187            protected static List<String> getLayoutIds(
188                            HttpServletRequest request, boolean privateLayout,
189                            long parentLayoutId, List<String> layoutIds)
190                    throws Exception {
191    
192                    long groupId = ParamUtil.getLong(request, "groupId");
193    
194                    List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
195                            groupId, privateLayout, parentLayoutId);
196    
197                    for (Layout layout : layouts) {
198                            layoutIds.add(String.valueOf(layout.getLayoutId()));
199    
200                            getLayoutIds(
201                                    request, privateLayout, layout.getLayoutId(), layoutIds);
202                    }
203    
204                    return layoutIds;
205            }
206    
207            protected static void put(
208                    HttpServletRequest request, String key, String value) {
209    
210                    try {
211                            PortalPreferences preferences =
212                                    PortletPreferencesFactoryUtil.getPortalPreferences(request);
213    
214                            preferences.setValue(
215                                    SessionTreeJSClicks.class.getName(), key, value);
216                    }
217                    catch (Exception e) {
218                            _log.error(e, e);
219                    }
220            }
221    
222            private static final Log _log = LogFactoryUtil.getLog(
223                    SessionTreeJSClicks.class);
224    
225    }