1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class PortletCategory implements Serializable {
41  
42      public PortletCategory() {
43          this("root");
44      }
45  
46      public PortletCategory(String name) {
47          this(name, new HashSet<String>());
48      }
49  
50      public PortletCategory(String name, Set<String> portletIds) {
51          _name = name;
52          _categories = new HashMap<String, PortletCategory>();
53          _portletIds = portletIds;
54      }
55  
56      public String getName() {
57          return _name;
58      }
59  
60      public Collection<PortletCategory> getCategories() {
61          return Collections.unmodifiableCollection(_categories.values());
62      }
63  
64      public void addCategory(PortletCategory portletCategory) {
65          _categories.put(portletCategory.getName(), portletCategory);
66      }
67  
68      public PortletCategory getCategory(String name) {
69          return _categories.get(name);
70      }
71  
72      public Set<String> getPortletIds() {
73          return _portletIds;
74      }
75  
76      public void setPortletIds(Set<String> portletIds) {
77          _portletIds = portletIds;
78      }
79  
80      public void merge(PortletCategory newPortletCategory) {
81          _merge(this, newPortletCategory);
82      }
83  
84      public void separate(Set<String> portletIds) {
85          Iterator<PortletCategory> categoriesItr =
86              _categories.values().iterator();
87  
88          while (categoriesItr.hasNext()) {
89              PortletCategory category = categoriesItr.next();
90  
91              category.separate(portletIds);
92          }
93  
94          Iterator<String>portletIdsItr = _portletIds.iterator();
95  
96          while (portletIdsItr.hasNext()) {
97              String portletId = portletIdsItr.next();
98  
99              if (portletIds.contains(portletId)) {
100                 portletIdsItr.remove();
101             }
102         }
103     }
104 
105     private void _merge(
106         PortletCategory portletCategory1, PortletCategory portletCategory2) {
107 
108         Iterator<PortletCategory> itr =
109             portletCategory2.getCategories().iterator();
110 
111         while (itr.hasNext()) {
112             PortletCategory curCategory2 = itr.next();
113 
114             PortletCategory curCategory1 =
115                 portletCategory1.getCategory(curCategory2.getName());
116 
117             if (curCategory1 != null) {
118                 _merge(curCategory1, curCategory2);
119             }
120             else {
121                 portletCategory1.addCategory(curCategory2);
122             }
123         }
124 
125         Set<String> portletIds1 = portletCategory1.getPortletIds();
126         Set<String> portletIds2 = portletCategory2.getPortletIds();
127 
128         portletIds1.addAll(portletIds2);
129     }
130 
131     private String _name;
132     private Map<String, PortletCategory> _categories;
133     private Set<String> _portletIds;
134 
135 }