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