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