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.ConcurrentModificationException;
029    import java.util.List;
030    
031    import javax.servlet.http.HttpServletRequest;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Eduardo Lundgren
036     */
037    public class SessionTreeJSClicks {
038    
039            public static void closeLayoutNodes(
040                    HttpServletRequest request, String treeId, boolean privateLayout,
041                    long layoutId, boolean recursive) {
042    
043                    try {
044                            List<String> layoutIds = new ArrayList<String>();
045    
046                            layoutIds.add(String.valueOf(layoutId));
047    
048                            if (recursive) {
049                                    getLayoutIds(request, privateLayout, layoutId, layoutIds);
050                            }
051    
052                            closeNodes(
053                                    request, treeId,
054                                    layoutIds.toArray(new String[layoutIds.size()]));
055                    }
056                    catch (Exception e) {
057                            _log.error(e, e);
058                    }
059            }
060    
061            public static void closeNode(
062                    HttpServletRequest request, String treeId, String nodeId) {
063    
064                    while (true) {
065                            try {
066                                    PortalPreferences portalPreferences =
067                                            PortletPreferencesFactoryUtil.getPortalPreferences(request);
068    
069                                    String openNodesString = portalPreferences.getValue(
070                                            SessionTreeJSClicks.class.getName(), treeId);
071    
072                                    openNodesString = StringUtil.remove(openNodesString, nodeId);
073    
074                                    portalPreferences.setValue(
075                                            SessionTreeJSClicks.class.getName(), treeId,
076                                            openNodesString);
077    
078                                    return;
079                            }
080                            catch (ConcurrentModificationException cme) {
081                                    continue;
082                            }
083                            catch (Exception e) {
084                                    _log.error(e, e);
085    
086                                    return;
087                            }
088                    }
089            }
090    
091            public static void closeNodes(HttpServletRequest request, String treeId) {
092                    while (true) {
093                            try {
094                                    PortalPreferences portalPreferences =
095                                            PortletPreferencesFactoryUtil.getPortalPreferences(request);
096    
097                                    portalPreferences.setValue(
098                                            SessionTreeJSClicks.class.getName(), treeId,
099                                            StringPool.BLANK);
100    
101                                    return;
102                            }
103                            catch (ConcurrentModificationException cme) {
104                                    continue;
105                            }
106                            catch (Exception e) {
107                                    _log.error(e, e);
108    
109                                    return;
110                            }
111                    }
112            }
113    
114            public static void closeNodes(
115                    HttpServletRequest request, String treeId, String[] nodeIds) {
116    
117                    while (true) {
118                            try {
119                                    PortalPreferences portalPreferences =
120                                            PortletPreferencesFactoryUtil.getPortalPreferences(request);
121    
122                                    String openNodesString = portalPreferences.getValue(
123                                            SessionTreeJSClicks.class.getName(), treeId);
124    
125                                    for (String nodeId : nodeIds) {
126                                            openNodesString = StringUtil.remove(
127                                                    openNodesString, nodeId);
128                                    }
129    
130                                    portalPreferences.setValue(
131                                            SessionTreeJSClicks.class.getName(), treeId,
132                                            openNodesString);
133    
134                                    return;
135                            }
136                            catch (ConcurrentModificationException cme) {
137                                    continue;
138                            }
139                            catch (Exception e) {
140                                    _log.error(e, e);
141    
142                                    return;
143                            }
144                    }
145            }
146    
147            public static String getOpenNodes(
148                    HttpServletRequest request, String treeId) {
149    
150                    try {
151                            PortalPreferences portalPreferences =
152                                    PortletPreferencesFactoryUtil.getPortalPreferences(request);
153    
154                            return portalPreferences.getValue(
155                                    SessionTreeJSClicks.class.getName(), treeId);
156                    }
157                    catch (Exception e) {
158                            _log.error(e, e);
159    
160                            return null;
161                    }
162            }
163    
164            public static void openLayoutNodes(
165                    HttpServletRequest request, String treeId, boolean privateLayout,
166                    long layoutId, boolean recursive) {
167    
168                    try {
169                            List<String> layoutIds = new ArrayList<String>();
170    
171                            layoutIds.add(String.valueOf(layoutId));
172    
173                            if (recursive) {
174                                    getLayoutIds(request, privateLayout, layoutId, layoutIds);
175                            }
176    
177                            openNodes(
178                                    request, treeId,
179                                    layoutIds.toArray(new String[layoutIds.size()]));
180                    }
181                    catch (Exception e) {
182                            _log.error(e, e);
183                    }
184            }
185    
186            public static void openNode(
187                    HttpServletRequest request, String treeId, String nodeId) {
188    
189                    while (true) {
190                            try {
191                                    PortalPreferences portalPreferences =
192                                            PortletPreferencesFactoryUtil.getPortalPreferences(request);
193    
194                                    String openNodesString = portalPreferences.getValue(
195                                            SessionTreeJSClicks.class.getName(), treeId);
196    
197                                    openNodesString = StringUtil.add(openNodesString, nodeId);
198    
199                                    portalPreferences.setValue(
200                                            SessionTreeJSClicks.class.getName(), treeId,
201                                            openNodesString);
202    
203                                    return;
204                            }
205                            catch (ConcurrentModificationException cme) {
206                                    continue;
207                            }
208                            catch (Exception e) {
209                                    _log.error(e, e);
210    
211                                    return;
212                            }
213                    }
214            }
215    
216            public static void openNodes(
217                    HttpServletRequest request, String treeId, String[] nodeIds) {
218    
219                    while (true) {
220                            try {
221                                    PortalPreferences portalPreferences =
222                                            PortletPreferencesFactoryUtil.getPortalPreferences(request);
223    
224                                    String openNodesString = portalPreferences.getValue(
225                                            SessionTreeJSClicks.class.getName(), treeId);
226    
227                                    for (String nodeId : nodeIds) {
228                                            openNodesString = StringUtil.add(openNodesString, nodeId);
229                                    }
230    
231                                    portalPreferences.setValue(
232                                            SessionTreeJSClicks.class.getName(), treeId,
233                                            openNodesString);
234    
235                                    return;
236                            }
237                            catch (ConcurrentModificationException cme) {
238                                    continue;
239                            }
240                            catch (Exception e) {
241                                    _log.error(e, e);
242    
243                                    return;
244                            }
245                    }
246            }
247    
248            protected static List<String> getLayoutIds(
249                            HttpServletRequest request, boolean privateLayout,
250                            long parentLayoutId, List<String> layoutIds)
251                    throws Exception {
252    
253                    long groupId = ParamUtil.getLong(request, "groupId");
254    
255                    List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
256                            groupId, privateLayout, parentLayoutId);
257    
258                    for (Layout layout : layouts) {
259                            layoutIds.add(String.valueOf(layout.getLayoutId()));
260    
261                            getLayoutIds(
262                                    request, privateLayout, layout.getLayoutId(), layoutIds);
263                    }
264    
265                    return layoutIds;
266            }
267    
268            private static Log _log = LogFactoryUtil.getLog(SessionTreeJSClicks.class);
269    
270    }