001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.model;
022:
023: import java.io.Serializable;
024:
025: import java.util.Collection;
026: import java.util.Collections;
027: import java.util.HashMap;
028: import java.util.HashSet;
029: import java.util.Iterator;
030: import java.util.Map;
031: import java.util.Set;
032:
033: /**
034: * <a href="PortletCategory.java.html"><b><i>View Source</i></b></a>
035: *
036: * @author Brian Wing Shun Chan
037: *
038: */
039: public class PortletCategory implements Serializable {
040:
041: public PortletCategory() {
042: this ("root");
043: }
044:
045: public PortletCategory(String name) {
046: _name = name;
047: _categories = new HashMap();
048: _portlets = new HashSet();
049: }
050:
051: public String getName() {
052: return _name;
053: }
054:
055: public Collection getCategories() {
056: return Collections.unmodifiableCollection(_categories.values());
057: }
058:
059: public void addCategory(PortletCategory portletCategory) {
060: _categories.put(portletCategory.getName(), portletCategory);
061: }
062:
063: public PortletCategory getCategory(String name) {
064: return (PortletCategory) _categories.get(name);
065: }
066:
067: public Set getPortlets() {
068: return _portlets;
069: }
070:
071: public void merge(PortletCategory newPortletCategory) {
072: _merge(this , newPortletCategory);
073: }
074:
075: public void separate(Set portletIds) {
076: Iterator itr = _categories.values().iterator();
077:
078: while (itr.hasNext()) {
079: PortletCategory category = (PortletCategory) itr.next();
080:
081: category.separate(portletIds);
082: }
083:
084: itr = _portlets.iterator();
085:
086: while (itr.hasNext()) {
087: String portletId = (String) itr.next();
088:
089: if (portletIds.contains(portletId)) {
090: itr.remove();
091: }
092: }
093: }
094:
095: private void _merge(PortletCategory portletCategory1,
096: PortletCategory portletCategory2) {
097:
098: Iterator itr = portletCategory2.getCategories().iterator();
099:
100: while (itr.hasNext()) {
101: PortletCategory curCategory2 = (PortletCategory) itr.next();
102:
103: PortletCategory curCategory1 = portletCategory1
104: .getCategory(curCategory2.getName());
105:
106: if (curCategory1 != null) {
107: _merge(curCategory1, curCategory2);
108: } else {
109: portletCategory1.addCategory(curCategory2);
110: }
111: }
112:
113: Set portlets1 = portletCategory1.getPortlets();
114: Set portlets2 = portletCategory2.getPortlets();
115:
116: portlets1.addAll(portlets2);
117: }
118:
119: private String _name;
120: private Map _categories;
121: private Set _portlets;
122:
123: }
|