1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.model;
21  
22  import java.io.Serializable;
23  
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.HashMap;
27  import java.util.HashSet;
28  import java.util.Iterator;
29  import java.util.Map;
30  import java.util.Set;
31  
32  /**
33   * <a href="PortletCategory.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   *
37   */
38  public class PortletCategory implements Serializable {
39  
40      public PortletCategory() {
41          this("root");
42      }
43  
44      public PortletCategory(String name) {
45          this(name, new HashSet<String>());
46      }
47  
48      public PortletCategory(String name, Set<String> portletIds) {
49          _name = name;
50          _categories = new HashMap<String, PortletCategory>();
51          _portletIds = portletIds;
52      }
53  
54      public String getName() {
55          return _name;
56      }
57  
58      public Collection<PortletCategory> getCategories() {
59          return Collections.unmodifiableCollection(_categories.values());
60      }
61  
62      public void addCategory(PortletCategory portletCategory) {
63          _categories.put(portletCategory.getName(), portletCategory);
64      }
65  
66      public PortletCategory getCategory(String name) {
67          return _categories.get(name);
68      }
69  
70      public Set<String> getPortletIds() {
71          return _portletIds;
72      }
73  
74      public void setPortletIds(Set<String> portletIds) {
75          _portletIds = portletIds;
76      }
77  
78      public void merge(PortletCategory newPortletCategory) {
79          _merge(this, newPortletCategory);
80      }
81  
82      public void separate(Set<String> portletIds) {
83          Iterator<PortletCategory> categoriesItr =
84              _categories.values().iterator();
85  
86          while (categoriesItr.hasNext()) {
87              PortletCategory category = categoriesItr.next();
88  
89              category.separate(portletIds);
90          }
91  
92          Iterator<String>portletIdsItr = _portletIds.iterator();
93  
94          while (portletIdsItr.hasNext()) {
95              String portletId = portletIdsItr.next();
96  
97              if (portletIds.contains(portletId)) {
98                  portletIdsItr.remove();
99              }
100         }
101     }
102 
103     private void _merge(
104         PortletCategory portletCategory1, PortletCategory portletCategory2) {
105 
106         Iterator<PortletCategory> itr =
107             portletCategory2.getCategories().iterator();
108 
109         while (itr.hasNext()) {
110             PortletCategory curCategory2 = itr.next();
111 
112             PortletCategory curCategory1 =
113                 portletCategory1.getCategory(curCategory2.getName());
114 
115             if (curCategory1 != null) {
116                 _merge(curCategory1, curCategory2);
117             }
118             else {
119                 portletCategory1.addCategory(curCategory2);
120             }
121         }
122 
123         Set<String> portletIds1 = portletCategory1.getPortletIds();
124         Set<String> portletIds2 = portletCategory2.getPortletIds();
125 
126         portletIds1.addAll(portletIds2);
127     }
128 
129     private String _name;
130     private Map<String, PortletCategory> _categories;
131     private Set<String> _portletIds;
132 
133 }