001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.tagscompiler.util;
016    
017    import com.liferay.portal.util.WebKeys;
018    
019    import java.util.Collection;
020    import java.util.List;
021    import java.util.Set;
022    import java.util.TreeSet;
023    
024    import javax.portlet.PortletRequest;
025    import javax.portlet.PortletSession;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class TagsCompilerSessionUtil {
031    
032            public static void addEntries(
033                    PortletRequest portletRequest, List<String> entries) {
034    
035                    Set<String> entriesSet = _getEntriesSet(portletRequest);
036    
037                    entriesSet.addAll(entries);
038            }
039    
040            public static void addEntry(PortletRequest portletRequest, String entry) {
041                    Set<String> entriesSet = _getEntriesSet(portletRequest);
042    
043                    entriesSet.add(entry);
044            }
045    
046            public static void clearEntries(PortletRequest portletRequest) {
047                    Set<String> entriesSet = _getEntriesSet(portletRequest);
048    
049                    entriesSet.clear();
050            }
051    
052            public static Collection<String> getEntries(PortletRequest portletRequest) {
053                    Set<String> entriesSet = _getEntriesSet(portletRequest);
054    
055                    return entriesSet;
056            }
057    
058            public static void removeEntries(
059                    PortletRequest portletRequest, List<String> entries) {
060    
061                    Set<String> entriesSet = _getEntriesSet(portletRequest);
062    
063                    entriesSet.removeAll(entries);
064            }
065    
066            public static void setEntries(
067                    PortletRequest portletRequest, List<String> entries) {
068    
069                    Set<String> entriesSet = _getEntriesSet(portletRequest);
070    
071                    entriesSet.clear();
072    
073                    entriesSet.addAll(entries);
074            }
075    
076            private static Set<String> _getEntriesSet(PortletRequest portletRequest) {
077                    PortletSession portletSession = portletRequest.getPortletSession();
078    
079                    Set<String> entriesSet = (Set<String>)portletSession.getAttribute(
080                            WebKeys.TAGS_COMPILER_ENTRIES, PortletSession.APPLICATION_SCOPE);
081    
082                    if (entriesSet == null) {
083                            entriesSet = new TreeSet<String>();
084    
085                            portletSession.setAttribute(
086                                    WebKeys.TAGS_COMPILER_ENTRIES, entriesSet,
087                                    PortletSession.APPLICATION_SCOPE);
088                    }
089    
090                    return entriesSet;
091            }
092    
093    }