1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portlet.PortalPreferences;
30  import com.liferay.portlet.PortletPreferencesFactoryUtil;
31  
32  import javax.servlet.http.HttpServletRequest;
33  
34  /**
35   * <a href="SessionTreeJSClicks.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   */
39  public class SessionTreeJSClicks {
40  
41      public static final String CLASS_NAME = SessionTreeJSClicks.class.getName();
42  
43      public static void closeNode(
44          HttpServletRequest request, String treeId, String nodeId) {
45  
46          try {
47              PortalPreferences prefs =
48                  PortletPreferencesFactoryUtil.getPortalPreferences(request);
49  
50              String openNodesString = prefs.getValue(CLASS_NAME, treeId);
51  
52              openNodesString = StringUtil.remove(openNodesString, nodeId);
53  
54              prefs.setValue(CLASS_NAME, treeId, openNodesString);
55          }
56          catch (Exception e) {
57              _log.error(e, e);
58          }
59      }
60  
61      public static void closeNodes(HttpServletRequest request, String treeId) {
62          try {
63              PortalPreferences prefs =
64                  PortletPreferencesFactoryUtil.getPortalPreferences(request);
65  
66              String openNodesString = StringPool.BLANK;
67  
68              prefs.setValue(CLASS_NAME, treeId, openNodesString);
69          }
70          catch (Exception e) {
71              _log.error(e, e);
72          }
73      }
74  
75      public static String getOpenNodes(
76          HttpServletRequest request, String treeId) {
77  
78          try {
79              PortalPreferences prefs =
80                  PortletPreferencesFactoryUtil.getPortalPreferences(request);
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 request, String treeId, String nodeId) {
93  
94          try {
95              PortalPreferences prefs =
96                  PortletPreferencesFactoryUtil.getPortalPreferences(request);
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 request, String treeId, String[] nodeIds) {
111 
112         try {
113             PortalPreferences prefs =
114                 PortletPreferencesFactoryUtil.getPortalPreferences(request);
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 = LogFactoryUtil.getLog(SessionTreeJSClicks.class);
130 
131 }