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.portal.model;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    
019    import java.io.Serializable;
020    
021    import java.util.Collection;
022    import java.util.Collections;
023    import java.util.HashMap;
024    import java.util.HashSet;
025    import java.util.Iterator;
026    import java.util.Map;
027    import java.util.Set;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Dennis Ju
032     */
033    public class PortletCategory implements Serializable {
034    
035            public PortletCategory() {
036                    this("root");
037            }
038    
039            public PortletCategory(String name) {
040                    this(name, new HashSet<String>());
041            }
042    
043            public PortletCategory(String name, Set<String> portletIds) {
044                    _categories = new HashMap<String, PortletCategory>();
045                    _portletIds = portletIds;
046    
047                    if (name.contains(_DELIMITER)) {
048                            int index = name.lastIndexOf(_DELIMITER);
049    
050                            _name = name.substring(index + _DELIMITER.length());
051    
052                            String parentName = name.substring(0, index);
053    
054                            PortletCategory parentPortletCategory = new PortletCategory(
055                                    parentName);
056    
057                            parentPortletCategory.addCategory(this);
058                    }
059                    else {
060                            _name = name;
061                            _parentPortletCategory = null;
062                            _path = name;
063                    }
064            }
065    
066            public void addCategory(PortletCategory portletCategory) {
067                    portletCategory.setParentCategory(this);
068    
069                    String path = _path.concat(_DELIMITER).concat(
070                            portletCategory.getName());
071    
072                    portletCategory.setPath(path);
073    
074                    _categories.put(portletCategory.getName(), portletCategory);
075            }
076    
077            public Collection<PortletCategory> getCategories() {
078                    return Collections.unmodifiableCollection(_categories.values());
079            }
080    
081            public PortletCategory getCategory(String name) {
082                    return _categories.get(name);
083            }
084    
085            public String getName() {
086                    return _name;
087            }
088    
089            public PortletCategory getParentCategory() {
090                    return _parentPortletCategory;
091            }
092    
093            public String getPath() {
094                    return _path;
095            }
096    
097            public Set<String> getPortletIds() {
098                    return _portletIds;
099            }
100    
101            public PortletCategory getRootCategory() {
102                    if (_parentPortletCategory == null) {
103                            return this;
104                    }
105    
106                    return _parentPortletCategory.getRootCategory();
107            }
108    
109            public boolean isHidden() {
110                    if (_name.equals(PortletCategoryConstants.NAME_HIDDEN)) {
111                            return true;
112                    }
113                    else {
114                            return false;
115                    }
116            }
117    
118            public void merge(PortletCategory newPortletCategory) {
119                    _merge(this, newPortletCategory);
120            }
121    
122            public void separate(Set<String> portletIds) {
123                    Iterator<PortletCategory> categoriesItr =
124                            _categories.values().iterator();
125    
126                    while (categoriesItr.hasNext()) {
127                            PortletCategory category = categoriesItr.next();
128    
129                            category.separate(portletIds);
130                    }
131    
132                    Iterator<String>portletIdsItr = _portletIds.iterator();
133    
134                    while (portletIdsItr.hasNext()) {
135                            String portletId = portletIdsItr.next();
136    
137                            if (portletIds.contains(portletId)) {
138                                    portletIdsItr.remove();
139                            }
140                    }
141            }
142    
143            public void separate(String portletId) {
144                    Set<String> portletIds = new HashSet<String>(1);
145    
146                    portletIds.add(portletId);
147    
148                    separate(portletIds);
149            }
150    
151            public void setPortletIds(Set<String> portletIds) {
152                    _portletIds = portletIds;
153            }
154    
155            protected void setParentCategory(PortletCategory portletCategory) {
156                    _parentPortletCategory = portletCategory;
157            }
158    
159            protected void setPath(String path) {
160                    _path = path;
161            }
162    
163            private void _merge(
164                    PortletCategory portletCategory1, PortletCategory portletCategory2) {
165    
166                    Iterator<PortletCategory> itr =
167                            portletCategory2.getCategories().iterator();
168    
169                    while (itr.hasNext()) {
170                            PortletCategory curCategory2 = itr.next();
171    
172                            PortletCategory curCategory1 = portletCategory1.getCategory(
173                                    curCategory2.getName());
174    
175                            if (curCategory1 != null) {
176                                    _merge(curCategory1, curCategory2);
177                            }
178                            else {
179                                    portletCategory1.addCategory(curCategory2);
180                            }
181                    }
182    
183                    Set<String> portletIds1 = portletCategory1.getPortletIds();
184                    Set<String> portletIds2 = portletCategory2.getPortletIds();
185    
186                    portletIds1.addAll(portletIds2);
187            }
188    
189            private static final String _DELIMITER = StringPool.DOUBLE_SLASH;
190    
191            private Map<String, PortletCategory> _categories;
192            private String _name;
193            private PortletCategory _parentPortletCategory;
194            private String _path;
195            private Set<String> _portletIds;
196    
197    }