1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.model;
16  
17  import java.io.Serializable;
18  
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.HashSet;
23  import java.util.Iterator;
24  import java.util.Map;
25  import java.util.Set;
26  
27  /**
28   * <a href="PortletCategory.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class PortletCategory implements Serializable {
33  
34      public PortletCategory() {
35          this("root");
36      }
37  
38      public PortletCategory(String name) {
39          this(name, new HashSet<String>());
40      }
41  
42      public PortletCategory(String name, Set<String> portletIds) {
43          _name = name;
44          _categories = new HashMap<String, PortletCategory>();
45          _portletIds = portletIds;
46      }
47  
48      public void addCategory(PortletCategory portletCategory) {
49          _categories.put(portletCategory.getName(), portletCategory);
50      }
51  
52      public Collection<PortletCategory> getCategories() {
53          return Collections.unmodifiableCollection(_categories.values());
54      }
55  
56      public PortletCategory getCategory(String name) {
57          return _categories.get(name);
58      }
59  
60      public String getName() {
61          return _name;
62      }
63  
64      public Set<String> getPortletIds() {
65          return _portletIds;
66      }
67  
68      public boolean isHidden() {
69          if (_name.equals(PortletCategoryConstants.NAME_HIDDEN)) {
70              return true;
71          }
72          else {
73              return false;
74          }
75      }
76  
77      public void merge(PortletCategory newPortletCategory) {
78          _merge(this, newPortletCategory);
79      }
80  
81      public void separate(Set<String> portletIds) {
82          Iterator<PortletCategory> categoriesItr =
83              _categories.values().iterator();
84  
85          while (categoriesItr.hasNext()) {
86              PortletCategory category = categoriesItr.next();
87  
88              category.separate(portletIds);
89          }
90  
91          Iterator<String>portletIdsItr = _portletIds.iterator();
92  
93          while (portletIdsItr.hasNext()) {
94              String portletId = portletIdsItr.next();
95  
96              if (portletIds.contains(portletId)) {
97                  portletIdsItr.remove();
98              }
99          }
100     }
101 
102     public void setPortletIds(Set<String> portletIds) {
103         _portletIds = portletIds;
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 Map<String, PortletCategory> _categories;
133     private String _name;
134     private Set<String> _portletIds;
135 
136 }