1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  }