1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class TagsCompilerSessionUtil {
41  
42      public static void addEntries(
43          PortletRequest portletRequest, List<String> entries) {
44  
45          Set<String> entriesSet = _getEntriesSet(portletRequest);
46  
47          entriesSet.addAll(entries);
48      }
49  
50      public static void addEntry(PortletRequest portletRequest, String entry) {
51          Set<String> entriesSet = _getEntriesSet(portletRequest);
52  
53          entriesSet.add(entry);
54      }
55  
56      public static void clearEntries(PortletRequest portletRequest) {
57          Set<String> entriesSet = _getEntriesSet(portletRequest);
58  
59          entriesSet.clear();
60      }
61  
62      public static Collection<String> getEntries(PortletRequest portletRequest) {
63          Set<String> entriesSet = _getEntriesSet(portletRequest);
64  
65          return entriesSet;
66      }
67  
68      public static void removeEntries(
69          PortletRequest portletRequest, List<String> entries) {
70  
71          Set<String> entriesSet = _getEntriesSet(portletRequest);
72  
73          entriesSet.removeAll(entries);
74      }
75  
76      public static void setEntries(
77          PortletRequest portletRequest, List<String> entries) {
78  
79          Set<String> entriesSet = _getEntriesSet(portletRequest);
80  
81          entriesSet.clear();
82  
83          entriesSet.addAll(entries);
84      }
85  
86      private static Set<String> _getEntriesSet(PortletRequest portletRequest) {
87          PortletSession portletSession = portletRequest.getPortletSession();
88  
89          Set<String> entriesSet = (Set<String>)portletSession.getAttribute(
90              WebKeys.TAGS_COMPILER_ENTRIES, PortletSession.APPLICATION_SCOPE);
91  
92          if (entriesSet == null) {
93              entriesSet = new TreeSet<String>();
94  
95              portletSession.setAttribute(
96                  WebKeys.TAGS_COMPILER_ENTRIES, entriesSet,
97                  PortletSession.APPLICATION_SCOPE);
98          }
99  
100         return entriesSet;
101     }
102 
103 }