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