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 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.remove(openNodesString, nodeId);
067    
068                            put(request, treeId, openNodesString);
069                    }
070                    catch (Exception e) {
071                            _log.error(e, e);
072                    }
073            }
074    
075            public static void closeNodes(HttpServletRequest request, String treeId) {
076                    try {
077                            String openNodesString = StringPool.BLANK;
078    
079                            put(request, treeId, openNodesString);
080                    }
081                    catch (Exception e) {
082                            _log.error(e, e);
083                    }
084            }
085    
086            public static void closeNodes(
087                    HttpServletRequest request, String treeId, String[] nodeIds) {
088    
089                    try {
090                            String openNodesString = get(request, treeId);
091    
092                            for (String nodeId : nodeIds) {
093                                    openNodesString = StringUtil.remove(openNodesString, nodeId);
094                            }
095    
096                            put(request, treeId, openNodesString);
097                    }
098                    catch (Exception e) {
099                            _log.error(e, e);
100                    }
101            }
102    
103            public static String getOpenNodes(
104                    HttpServletRequest request, String treeId) {
105    
106                    try {
107                            return get(request, treeId);
108                    }
109                    catch (Exception e) {
110                            _log.error(e, e);
111    
112                            return null;
113                    }
114            }
115    
116            public static void openLayoutNodes(
117                    HttpServletRequest request, String treeId, boolean privateLayout,
118                    long layoutId, boolean recursive) {
119    
120                    try {
121                            List<String> layoutIds = new ArrayList<String>();
122    
123                            layoutIds.add(String.valueOf(layoutId));
124    
125                            if (recursive) {
126                                    getLayoutIds(request, privateLayout, layoutId, layoutIds);
127                            }
128    
129                            openNodes(
130                                    request, treeId,
131                                    layoutIds.toArray(new String[layoutIds.size()]));
132                    }
133                    catch (Exception e) {
134                            _log.error(e, e);
135                    }
136            }
137    
138            public static void openNode(
139                    HttpServletRequest request, String treeId, String nodeId) {
140    
141                    try {
142                            String openNodesString = get(request, treeId);
143    
144                            openNodesString = StringUtil.add(openNodesString, nodeId);
145    
146                            put(request, treeId, openNodesString);
147                    }
148                    catch (Exception e) {
149                            _log.error(e, e);
150                    }
151            }
152    
153            public static void openNodes(
154                    HttpServletRequest request, String treeId, String[] nodeIds) {
155    
156                    try {
157                            String openNodesString = get(request, treeId);
158    
159                            for (String nodeId : nodeIds) {
160                                    openNodesString = StringUtil.add(openNodesString, nodeId);
161                            }
162    
163                            put(request, treeId, openNodesString);
164                    }
165                    catch (Exception e) {
166                            _log.error(e, e);
167                    }
168            }
169    
170            protected static String get(HttpServletRequest request, String key) {
171                    try {
172                            PortalPreferences preferences =
173                                    PortletPreferencesFactoryUtil.getPortalPreferences(request);
174    
175                            return preferences.getValue(
176                                    SessionTreeJSClicks.class.getName(), key);
177                    }
178                    catch (Exception e) {
179                            _log.error(e, e);
180    
181                            return null;
182                    }
183            }
184    
185            protected static List<String> getLayoutIds(
186                            HttpServletRequest request, boolean privateLayout,
187                            long parentLayoutId, List<String> layoutIds)
188                    throws Exception {
189    
190                    long groupId = ParamUtil.getLong(request, "groupId");
191    
192                    List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
193                            groupId, privateLayout, parentLayoutId);
194    
195                    for (Layout layout : layouts) {
196                            layoutIds.add(String.valueOf(layout.getLayoutId()));
197    
198                            getLayoutIds(
199                                    request, privateLayout, layout.getLayoutId(), layoutIds);
200                    }
201    
202                    return layoutIds;
203            }
204    
205            protected static void put(
206                    HttpServletRequest request, String key, String value) {
207    
208                    try {
209                            PortalPreferences preferences =
210                                    PortletPreferencesFactoryUtil.getPortalPreferences(request);
211    
212                            preferences.setValue(
213                                    SessionTreeJSClicks.class.getName(), 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    }