1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.util;
24  
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portlet.PortalPreferences;
28  import com.liferay.portlet.PortletPreferencesFactoryUtil;
29  
30  import javax.servlet.http.HttpServletRequest;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  /**
36   * <a href="SessionTreeJSClicks.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class SessionTreeJSClicks {
42  
43      public static final String CLASS_NAME = SessionTreeJSClicks.class.getName();
44  
45      public static void closeNode(
46          HttpServletRequest req, String treeId, String nodeId) {
47  
48          try {
49              PortalPreferences prefs =
50                  PortletPreferencesFactoryUtil.getPortalPreferences(req);
51  
52              String openNodesString = prefs.getValue(CLASS_NAME, treeId);
53  
54              openNodesString = StringUtil.remove(openNodesString, nodeId);
55  
56              prefs.setValue(CLASS_NAME, treeId, openNodesString);
57          }
58          catch (Exception e) {
59              _log.error(e, e);
60          }
61      }
62  
63      public static void closeNodes(HttpServletRequest req, String treeId) {
64          try {
65              PortalPreferences prefs =
66                  PortletPreferencesFactoryUtil.getPortalPreferences(req);
67  
68              String openNodesString = StringPool.BLANK;
69  
70              prefs.setValue(CLASS_NAME, treeId, openNodesString);
71          }
72          catch (Exception e) {
73              _log.error(e, e);
74          }
75      }
76  
77      public static String getOpenNodes(HttpServletRequest req, String treeId) {
78          try {
79              PortalPreferences prefs =
80                  PortletPreferencesFactoryUtil.getPortalPreferences(req);
81  
82              return prefs.getValue(CLASS_NAME, treeId);
83          }
84          catch (Exception e) {
85              _log.error(e, e);
86  
87              return null;
88          }
89      }
90  
91      public static void openNode(
92          HttpServletRequest req, String treeId, String nodeId) {
93  
94          try {
95              PortalPreferences prefs =
96                  PortletPreferencesFactoryUtil.getPortalPreferences(req);
97  
98              String openNodesString = prefs.getValue(CLASS_NAME, treeId);
99  
100             openNodesString = StringUtil.add(openNodesString, nodeId);
101 
102             prefs.setValue(CLASS_NAME, treeId, openNodesString);
103         }
104         catch (Exception e) {
105             _log.error(e, e);
106         }
107     }
108 
109     public static void openNodes(
110         HttpServletRequest req, String treeId, String[] nodeIds) {
111 
112         try {
113             PortalPreferences prefs =
114                 PortletPreferencesFactoryUtil.getPortalPreferences(req);
115 
116             String openNodesString = prefs.getValue(CLASS_NAME, treeId);
117 
118             for (int i = 0; i < nodeIds.length; i++) {
119                 openNodesString = StringUtil.add(openNodesString, nodeIds[i]);
120             }
121 
122             prefs.setValue(CLASS_NAME, treeId, openNodesString);
123         }
124         catch (Exception e) {
125             _log.error(e, e);
126         }
127     }
128 
129     private static Log _log = LogFactory.getLog(SessionTreeJSClicks.class);
130 
131 }