1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet.tagscompiler.util;
16  
17  import com.liferay.portal.util.WebKeys;
18  
19  import java.util.Collection;
20  import java.util.List;
21  import java.util.Set;
22  import java.util.TreeSet;
23  
24  import javax.portlet.PortletRequest;
25  import javax.portlet.PortletSession;
26  
27  /**
28   * <a href="TagsCompilerSessionUtil.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class TagsCompilerSessionUtil {
33  
34      public static void addEntries(
35          PortletRequest portletRequest, List<String> entries) {
36  
37          Set<String> entriesSet = _getEntriesSet(portletRequest);
38  
39          entriesSet.addAll(entries);
40      }
41  
42      public static void addEntry(PortletRequest portletRequest, String entry) {
43          Set<String> entriesSet = _getEntriesSet(portletRequest);
44  
45          entriesSet.add(entry);
46      }
47  
48      public static void clearEntries(PortletRequest portletRequest) {
49          Set<String> entriesSet = _getEntriesSet(portletRequest);
50  
51          entriesSet.clear();
52      }
53  
54      public static Collection<String> getEntries(PortletRequest portletRequest) {
55          Set<String> entriesSet = _getEntriesSet(portletRequest);
56  
57          return entriesSet;
58      }
59  
60      public static void removeEntries(
61          PortletRequest portletRequest, List<String> entries) {
62  
63          Set<String> entriesSet = _getEntriesSet(portletRequest);
64  
65          entriesSet.removeAll(entries);
66      }
67  
68      public static void setEntries(
69          PortletRequest portletRequest, List<String> entries) {
70  
71          Set<String> entriesSet = _getEntriesSet(portletRequest);
72  
73          entriesSet.clear();
74  
75          entriesSet.addAll(entries);
76      }
77  
78      private static Set<String> _getEntriesSet(PortletRequest portletRequest) {
79          PortletSession portletSession = portletRequest.getPortletSession();
80  
81          Set<String> entriesSet = (Set<String>)portletSession.getAttribute(
82              WebKeys.TAGS_COMPILER_ENTRIES, PortletSession.APPLICATION_SCOPE);
83  
84          if (entriesSet == null) {
85              entriesSet = new TreeSet<String>();
86  
87              portletSession.setAttribute(
88                  WebKeys.TAGS_COMPILER_ENTRIES, entriesSet,
89                  PortletSession.APPLICATION_SCOPE);
90          }
91  
92          return entriesSet;
93      }
94  
95  }