This project has retired. For details please refer to its Attic page.
ViewBean 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.nio.charset.Charset;
22  import java.text.ParseException;
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  import org.json.simple.JSONArray;
31  import org.json.simple.JSONObject;
32  import org.json.simple.parser.JSONParser;
33  import org.apache.hadoop.chukwa.util.ExceptionUtil;
34  
35  @XmlRootElement
36  @XmlType
37  public class ViewBean {
38    private PagesBean[] pages;
39    private String description;
40    private String owner;
41    
42    private String name;
43    private String permissionType;
44    private static Log log = LogFactory.getLog(ViewBean.class);
45  
46    public ViewBean() {  
47    }
48    
49    public ViewBean(byte[] buffer) throws ParseException {
50      JSONParser parser = new JSONParser();
51      try {
52        JSONObject json = (JSONObject) parser.parse(new String(buffer, Charset.forName("UTF-8")));
53        if(json.containsKey("description")) {
54          this.description = (String) json.get("description");
55        } else {
56          this.description = "";
57        }
58        this.owner= (String) json.get("owner");
59        this.name= (String) json.get("name");
60        this.permissionType= (String) json.get("permissionType");
61        int size = ((JSONArray) json.get("pages")).size();
62        PagesBean[] pages = new PagesBean[size];
63        JSONArray pagesArray = (JSONArray) json.get("pages");
64        for(int i=0;i<size;i++) {
65          pages[i] = new PagesBean((JSONObject) pagesArray.get(i));
66        }
67        this.pages=pages;
68      } catch (Exception e) {
69        log.error(ExceptionUtil.getStackTrace(e));
70        throw new ParseException(ExceptionUtil.getStackTrace(e), 0);
71      }
72    }
73    
74    @XmlElement
75    public PagesBean[] getPages() {
76      return pages.clone();
77    }
78    
79    @XmlElement
80    public int getPagesCount() {
81      return pages.length;  
82    }
83    
84    @XmlElement
85    public String getDescription() {
86      return this.description;
87    }
88    
89    @XmlElement
90    public String getOwner() {
91      return this.owner;
92    }
93    
94    @XmlElement
95    public String getName() {
96      return this.name;
97    }
98    
99    @XmlElement
100   public String getPermissionType() {
101     return this.permissionType;
102   }
103   
104   public void setPages(PagesBean[] pages) {
105     this.pages = (PagesBean[]) pages.clone();
106   }
107   
108   public void setDescription(String description) {
109     this.description = description;
110   }
111   
112   public void setOwner(String owner) {
113     this.owner = owner;
114   }
115   
116   public void setName(String name) {
117     this.name = name;
118   }
119   
120   public void setPermissionType(String permissionType) {
121     this.permissionType = permissionType;
122   }
123   
124   public void update() {
125     if(this.pages!=null) {
126       for(PagesBean page : pages) {
127         page.update();
128       }
129     }
130   }
131   
132   @SuppressWarnings("unchecked")
133   public JSONObject deserialize() {
134     update();
135     JSONObject view = new JSONObject();
136     try {
137       view.put("name", this.name);
138       view.put("owner", this.owner);
139       view.put("permissionType", this.permissionType);
140       view.put("description", this.description);
141       JSONArray ja = new JSONArray();
142       for(int i=0;i<this.pages.length;i++) {
143         ja.add(this.pages[i].deserialize());
144       }
145       view.put("pages", (JSONArray) ja);
146     } catch (Exception e){
147       log.error(ExceptionUtil.getStackTrace(e));
148     }
149     return view;
150   }
151 }