1   /**
2    * Copyright (c) 2000-2009 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          this(name, new HashSet<String>());
49      }
50  
51      public PortletCategory(String name, Set<String> portletIds) {
52          _name = name;
53          _categories = new HashMap<String, PortletCategory>();
54          _portletIds = portletIds;
55      }
56  
57      public String getName() {
58          return _name;
59      }
60  
61      public Collection<PortletCategory> getCategories() {
62          return Collections.unmodifiableCollection(_categories.values());
63      }
64  
65      public void addCategory(PortletCategory portletCategory) {
66          _categories.put(portletCategory.getName(), portletCategory);
67      }
68  
69      public PortletCategory getCategory(String name) {
70          return _categories.get(name);
71      }
72  
73      public Set<String> getPortletIds() {
74          return _portletIds;
75      }
76  
77      public void setPortletIds(Set<String> portletIds) {
78          _portletIds = portletIds;
79      }
80  
81      public void merge(PortletCategory newPortletCategory) {
82          _merge(this, newPortletCategory);
83      }
84  
85      public void separate(Set<String> portletIds) {
86          Iterator<PortletCategory> categoriesItr =
87              _categories.values().iterator();
88  
89          while (categoriesItr.hasNext()) {
90              PortletCategory category = categoriesItr.next();
91  
92              category.separate(portletIds);
93          }
94  
95          Iterator<String>portletIdsItr = _portletIds.iterator();
96  
97          while (portletIdsItr.hasNext()) {
98              String portletId = portletIdsItr.next();
99  
100             if (portletIds.contains(portletId)) {
101                 portletIdsItr.remove();
102             }
103         }
104     }
105 
106     private void _merge(
107         PortletCategory portletCategory1, PortletCategory portletCategory2) {
108 
109         Iterator<PortletCategory> itr =
110             portletCategory2.getCategories().iterator();
111 
112         while (itr.hasNext()) {
113             PortletCategory curCategory2 = itr.next();
114 
115             PortletCategory curCategory1 =
116                 portletCategory1.getCategory(curCategory2.getName());
117 
118             if (curCategory1 != null) {
119                 _merge(curCategory1, curCategory2);
120             }
121             else {
122                 portletCategory1.addCategory(curCategory2);
123             }
124         }
125 
126         Set<String> portletIds1 = portletCategory1.getPortletIds();
127         Set<String> portletIds2 = portletCategory2.getPortletIds();
128 
129         portletIds1.addAll(portletIds2);
130     }
131 
132     private String _name;
133     private Map<String, PortletCategory> _categories;
134     private Set<String> _portletIds;
135 
136 }