001    /**
002     * Copyright (c) 2000-2010 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.portal.model;
016    
017    import java.io.Serializable;
018    
019    import java.util.Collection;
020    import java.util.Collections;
021    import java.util.HashMap;
022    import java.util.HashSet;
023    import java.util.Iterator;
024    import java.util.Map;
025    import java.util.Set;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class PortletCategory implements Serializable {
031    
032            public PortletCategory() {
033                    this("root");
034            }
035    
036            public PortletCategory(String name) {
037                    this(name, new HashSet<String>());
038            }
039    
040            public PortletCategory(String name, Set<String> portletIds) {
041                    _name = name;
042                    _categories = new HashMap<String, PortletCategory>();
043                    _portletIds = portletIds;
044            }
045    
046            public void addCategory(PortletCategory portletCategory) {
047                    _categories.put(portletCategory.getName(), portletCategory);
048            }
049    
050            public Collection<PortletCategory> getCategories() {
051                    return Collections.unmodifiableCollection(_categories.values());
052            }
053    
054            public PortletCategory getCategory(String name) {
055                    return _categories.get(name);
056            }
057    
058            public String getName() {
059                    return _name;
060            }
061    
062            public Set<String> getPortletIds() {
063                    return _portletIds;
064            }
065    
066            public boolean isHidden() {
067                    if (_name.equals(PortletCategoryConstants.NAME_HIDDEN)) {
068                            return true;
069                    }
070                    else {
071                            return false;
072                    }
073            }
074    
075            public void merge(PortletCategory newPortletCategory) {
076                    _merge(this, newPortletCategory);
077            }
078    
079            public void separate(Set<String> portletIds) {
080                    Iterator<PortletCategory> categoriesItr =
081                            _categories.values().iterator();
082    
083                    while (categoriesItr.hasNext()) {
084                            PortletCategory category = categoriesItr.next();
085    
086                            category.separate(portletIds);
087                    }
088    
089                    Iterator<String>portletIdsItr = _portletIds.iterator();
090    
091                    while (portletIdsItr.hasNext()) {
092                            String portletId = portletIdsItr.next();
093    
094                            if (portletIds.contains(portletId)) {
095                                    portletIdsItr.remove();
096                            }
097                    }
098            }
099    
100            public void setPortletIds(Set<String> portletIds) {
101                    _portletIds = portletIds;
102            }
103    
104            private void _merge(
105                    PortletCategory portletCategory1, PortletCategory portletCategory2) {
106    
107                    Iterator<PortletCategory> itr =
108                            portletCategory2.getCategories().iterator();
109    
110                    while (itr.hasNext()) {
111                            PortletCategory curCategory2 = itr.next();
112    
113                            PortletCategory curCategory1 =
114                                    portletCategory1.getCategory(curCategory2.getName());
115    
116                            if (curCategory1 != null) {
117                                    _merge(curCategory1, curCategory2);
118                            }
119                            else {
120                                    portletCategory1.addCategory(curCategory2);
121                            }
122                    }
123    
124                    Set<String> portletIds1 = portletCategory1.getPortletIds();
125                    Set<String> portletIds2 = portletCategory2.getPortletIds();
126    
127                    portletIds1.addAll(portletIds2);
128            }
129    
130            private Map<String, PortletCategory> _categories;
131            private String _name;
132            private Set<String> _portletIds;
133    
134    }