1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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 String getName() {
49          return _name;
50      }
51  
52      public Collection<PortletCategory> getCategories() {
53          return Collections.unmodifiableCollection(_categories.values());
54      }
55  
56      public void addCategory(PortletCategory portletCategory) {
57          _categories.put(portletCategory.getName(), portletCategory);
58      }
59  
60      public PortletCategory getCategory(String name) {
61          return _categories.get(name);
62      }
63  
64      public Set<String> getPortletIds() {
65          return _portletIds;
66      }
67  
68      public void setPortletIds(Set<String> portletIds) {
69          _portletIds = portletIds;
70      }
71  
72      public void merge(PortletCategory newPortletCategory) {
73          _merge(this, newPortletCategory);
74      }
75  
76      public void separate(Set<String> portletIds) {
77          Iterator<PortletCategory> categoriesItr =
78              _categories.values().iterator();
79  
80          while (categoriesItr.hasNext()) {
81              PortletCategory category = categoriesItr.next();
82  
83              category.separate(portletIds);
84          }
85  
86          Iterator<String>portletIdsItr = _portletIds.iterator();
87  
88          while (portletIdsItr.hasNext()) {
89              String portletId = portletIdsItr.next();
90  
91              if (portletIds.contains(portletId)) {
92                  portletIdsItr.remove();
93              }
94          }
95      }
96  
97      private void _merge(
98          PortletCategory portletCategory1, PortletCategory portletCategory2) {
99  
100         Iterator<PortletCategory> itr =
101             portletCategory2.getCategories().iterator();
102 
103         while (itr.hasNext()) {
104             PortletCategory curCategory2 = itr.next();
105 
106             PortletCategory curCategory1 =
107                 portletCategory1.getCategory(curCategory2.getName());
108 
109             if (curCategory1 != null) {
110                 _merge(curCategory1, curCategory2);
111             }
112             else {
113                 portletCategory1.addCategory(curCategory2);
114             }
115         }
116 
117         Set<String> portletIds1 = portletCategory1.getPortletIds();
118         Set<String> portletIds2 = portletCategory2.getPortletIds();
119 
120         portletIds1.addAll(portletIds2);
121     }
122 
123     private String _name;
124     private Map<String, PortletCategory> _categories;
125     private Set<String> _portletIds;
126 
127 }