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                    _portletCategories = 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                    _portletCategories.put(portletCategory.getName(), portletCategory);
075            }
076    
077            public Collection<PortletCategory> getCategories() {
078                    return Collections.unmodifiableCollection(_portletCategories.values());
079            }
080    
081            public PortletCategory getCategory(String name) {
082                    return _portletCategories.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                    for (PortletCategory portletCategory : _portletCategories.values()) {
124                            portletCategory.separate(portletIds);
125                    }
126    
127                    Iterator<String>itr = _portletIds.iterator();
128    
129                    while (itr.hasNext()) {
130                            String portletId = itr.next();
131    
132                            if (portletIds.contains(portletId)) {
133                                    itr.remove();
134                            }
135                    }
136            }
137    
138            public void separate(String portletId) {
139                    Set<String> portletIds = new HashSet<String>(1);
140    
141                    portletIds.add(portletId);
142    
143                    separate(portletIds);
144            }
145    
146            public void setPortletIds(Set<String> portletIds) {
147                    _portletIds = portletIds;
148            }
149    
150            protected void merge(
151                    PortletCategory portletCategory1, PortletCategory portletCategory2) {
152    
153                    Collection<PortletCategory> portletCategories =
154                            portletCategory2.getCategories();
155    
156                    for (PortletCategory curPortletCategory2 : portletCategories) {
157                            PortletCategory curPortletCategory1 = portletCategory1.getCategory(
158                                    curPortletCategory2.getName());
159    
160                            if (curPortletCategory1 != null) {
161                                    merge(curPortletCategory1, curPortletCategory2);
162                            }
163                            else {
164                                    portletCategory1.addCategory(curPortletCategory2);
165                            }
166                    }
167    
168                    Set<String> portletIds1 = portletCategory1.getPortletIds();
169                    Set<String> portletIds2 = portletCategory2.getPortletIds();
170    
171                    portletIds1.addAll(portletIds2);
172            }
173    
174            protected void setParentCategory(PortletCategory portletCategory) {
175                    _parentPortletCategory = portletCategory;
176            }
177    
178            protected void setPath(String path) {
179                    _path = path;
180            }
181    
182            private static final String _DELIMITER = StringPool.DOUBLE_SLASH;
183    
184            private String _name;
185            private PortletCategory _parentPortletCategory;
186            private String _path;
187            private Map<String, PortletCategory> _portletCategories;
188            private Set<String> _portletIds;
189    
190    }