This project has retired. For details please refer to its Attic page.
CatalogBean xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.chukwa.rest.bean;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import javax.xml.bind.annotation.XmlElement;
25  import javax.xml.bind.annotation.XmlRootElement;
26  import javax.xml.bind.annotation.XmlType;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  @XmlRootElement
32  @XmlType(propOrder={"type", "id", "label", "children"})
33  public class CatalogBean {
34    private static final Log log = LogFactory.getLog(CatalogBean.class);
35    private List<CatalogBean> children = new ArrayList<CatalogBean>();
36    private String type = "text";
37    private String label = null;
38    private String id = null;
39    
40    public CatalogBean() {
41    }
42    
43    @XmlElement
44    public String getType() {
45      return type;
46    }
47    
48    @XmlElement
49    public String getId() {
50      return id;
51    }
52    
53    @XmlElement
54    public String getLabel() {
55      return label;
56    }
57    
58    @XmlElement
59    public List<CatalogBean> getChildren() {
60      return children;
61    }
62    
63    public void setType(String type) {
64      this.type = type;  
65    }
66    
67    public void setId(String id) {
68      this.id = id;
69    }
70    
71    public void setLabel(String label) {
72      this.label = label;
73    }
74    
75    public void setChildren(List<CatalogBean> children) {
76      this.children = children;
77    }
78    
79    public void addCatalog(WidgetBean widget) {
80      String[] path = widget.getCategories().split(",");
81      List<CatalogBean> tracker = this.children;
82      if(tracker==null) {
83        tracker = new ArrayList<CatalogBean>();
84      }
85      for(int i=0;i<path.length;i++) {
86        boolean duplicate = false;
87        for(int j=0;j<tracker.size();j++) {
88          if(tracker.get(j).getLabel().intern()==path[i].intern()) {
89            duplicate = true;
90            tracker = tracker.get(j).getChildren();
91            continue;
92          }
93        }
94        if(!duplicate) {
95          tracker = addCategory(tracker, widget.getId(), path[i]);
96        }
97      }
98      tracker = addCategory(tracker, widget.getId(), widget.getTitle());
99    }
100   
101   public List<CatalogBean> addCategory(List<CatalogBean> tracker, String id, String label) {
102     CatalogBean c = new CatalogBean();
103     c.setId(id);
104     c.setLabel(label);
105     tracker.add(c);
106     return c.getChildren();
107   }
108   
109 }