1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.model;
24  
25  import java.io.Serializable;
26  
27  import java.util.Collection;
28  import java.util.Collections;
29  import java.util.HashMap;
30  import java.util.HashSet;
31  import java.util.Iterator;
32  import java.util.Map;
33  import java.util.Set;
34  
35  /**
36   * <a href="PortletCategory.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class PortletCategory implements Serializable {
42  
43      public PortletCategory() {
44          this("root");
45      }
46  
47      public PortletCategory(String name) {
48          _name = name;
49          _categories = new HashMap();
50          _portlets = new HashSet();
51      }
52  
53      public String getName() {
54          return _name;
55      }
56  
57      public Collection getCategories() {
58          return Collections.unmodifiableCollection(_categories.values());
59      }
60  
61      public void addCategory(PortletCategory portletCategory) {
62          _categories.put(portletCategory.getName(), portletCategory);
63      }
64  
65      public PortletCategory getCategory(String name) {
66          return (PortletCategory)_categories.get(name);
67      }
68  
69      public Set getPortlets() {
70          return _portlets;
71      }
72  
73      public void merge(PortletCategory newPortletCategory) {
74          _merge(this, newPortletCategory);
75      }
76  
77      public void separate(Set portletIds) {
78          Iterator itr = _categories.values().iterator();
79  
80          while (itr.hasNext()) {
81              PortletCategory category = (PortletCategory)itr.next();
82  
83              category.separate(portletIds);
84          }
85  
86          itr = _portlets.iterator();
87  
88          while (itr.hasNext()) {
89              String portletId = (String)itr.next();
90  
91              if (portletIds.contains(portletId)) {
92                  itr.remove();
93              }
94          }
95      }
96  
97      private void _merge(
98          PortletCategory portletCategory1, PortletCategory portletCategory2) {
99  
100         Iterator itr = portletCategory2.getCategories().iterator();
101 
102         while (itr.hasNext()) {
103             PortletCategory curCategory2 = (PortletCategory)itr.next();
104 
105             PortletCategory curCategory1 =
106                 portletCategory1.getCategory(curCategory2.getName());
107 
108             if (curCategory1 != null) {
109                 _merge(curCategory1, curCategory2);
110             }
111             else {
112                 portletCategory1.addCategory(curCategory2);
113             }
114         }
115 
116         Set portlets1 = portletCategory1.getPortlets();
117         Set portlets2 = portletCategory2.getPortlets();
118 
119         portlets1.addAll(portlets2);
120     }
121 
122     private String _name;
123     private Map _categories;
124     private Set _portlets;
125 
126 }