1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.util;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portlet.PortalPreferences;
27  import com.liferay.portlet.PortletPreferencesFactoryUtil;
28  
29  import javax.servlet.http.HttpServletRequest;
30  
31  /**
32   * <a href="SessionTreeJSClicks.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   *
36   */
37  public class SessionTreeJSClicks {
38  
39      public static final String CLASS_NAME = SessionTreeJSClicks.class.getName();
40  
41      public static void closeNode(
42          HttpServletRequest request, String treeId, String nodeId) {
43  
44          try {
45              PortalPreferences prefs =
46                  PortletPreferencesFactoryUtil.getPortalPreferences(request);
47  
48              String openNodesString = prefs.getValue(CLASS_NAME, treeId);
49  
50              openNodesString = StringUtil.remove(openNodesString, nodeId);
51  
52              prefs.setValue(CLASS_NAME, treeId, openNodesString);
53          }
54          catch (Exception e) {
55              _log.error(e, e);
56          }
57      }
58  
59      public static void closeNodes(HttpServletRequest request, String treeId) {
60          try {
61              PortalPreferences prefs =
62                  PortletPreferencesFactoryUtil.getPortalPreferences(request);
63  
64              String openNodesString = StringPool.BLANK;
65  
66              prefs.setValue(CLASS_NAME, treeId, openNodesString);
67          }
68          catch (Exception e) {
69              _log.error(e, e);
70          }
71      }
72  
73      public static String getOpenNodes(
74          HttpServletRequest request, String treeId) {
75  
76          try {
77              PortalPreferences prefs =
78                  PortletPreferencesFactoryUtil.getPortalPreferences(request);
79  
80              return prefs.getValue(CLASS_NAME, treeId);
81          }
82          catch (Exception e) {
83              _log.error(e, e);
84  
85              return null;
86          }
87      }
88  
89      public static void openNode(
90          HttpServletRequest request, String treeId, String nodeId) {
91  
92          try {
93              PortalPreferences prefs =
94                  PortletPreferencesFactoryUtil.getPortalPreferences(request);
95  
96              String openNodesString = prefs.getValue(CLASS_NAME, treeId);
97  
98              openNodesString = StringUtil.add(openNodesString, nodeId);
99  
100             prefs.setValue(CLASS_NAME, treeId, openNodesString);
101         }
102         catch (Exception e) {
103             _log.error(e, e);
104         }
105     }
106 
107     public static void openNodes(
108         HttpServletRequest request, String treeId, String[] nodeIds) {
109 
110         try {
111             PortalPreferences prefs =
112                 PortletPreferencesFactoryUtil.getPortalPreferences(request);
113 
114             String openNodesString = prefs.getValue(CLASS_NAME, treeId);
115 
116             for (int i = 0; i < nodeIds.length; i++) {
117                 openNodesString = StringUtil.add(openNodesString, nodeIds[i]);
118             }
119 
120             prefs.setValue(CLASS_NAME, treeId, openNodesString);
121         }
122         catch (Exception e) {
123             _log.error(e, e);
124         }
125     }
126 
127     private static Log _log = LogFactoryUtil.getLog(SessionTreeJSClicks.class);
128 
129 }