This project has retired. For details please refer to its Attic page.
PagesBean 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.text.ParseException;
22  
23  import javax.xml.bind.annotation.XmlElement;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.json.simple.JSONArray;
28  import org.json.simple.JSONObject;
29  
30  import org.apache.hadoop.chukwa.util.ExceptionUtil;
31  
32  public class PagesBean {
33    private static Log log = LogFactory.getLog(PagesBean.class);
34    private String title;
35    private int[] columnSizes;
36    private ColumnBean[] layout;
37    private int columns;
38    
39    public PagesBean() {
40    }
41  
42    
43    public PagesBean(JSONObject json) throws ParseException {
44      try {
45        title = (String) json.get("title");
46        columns = ((Long) json.get("columns")).intValue();
47        JSONArray layout = (JSONArray) json.get("layout");
48        this.layout = new ColumnBean[layout.size()];
49        for(int i=0;i<layout.size();i++) {
50          ColumnBean c = new ColumnBean((JSONArray) layout.get(i));
51          this.layout[i]=c;
52        }
53        if(json.containsKey("colSize")) {
54          JSONArray ja = (JSONArray) json.get("colSize");
55          columnSizes = new int[ja.size()];
56          for(int i=0; i< ja.size(); i++) {
57            columnSizes[i] = ((Long) ja.get(i)).intValue();
58          }
59        }
60      } catch (Exception e) {
61        log.error(ExceptionUtil.getStackTrace(e));
62        throw new ParseException(ExceptionUtil.getStackTrace(e), 0);
63      }
64    }
65    
66    @XmlElement
67    public String getTitle() {
68      return title;
69    }
70  
71  //  @XmlElement
72  //  public String getLayoutStyle() {
73  //    return layoutStyle;
74  //  }
75  
76    @XmlElement(name="layout")
77    public ColumnBean[] getLayout() {
78      return layout.clone();
79    }
80    
81    public void update() {
82      for(int i=0;i<layout.length;i++) {
83        layout[i].update();
84      }
85    }
86    
87    public void setTitle(String title) {
88      this.title = title;
89    }
90  
91    public void setLayout(ColumnBean[] layout) {
92      this.layout = (ColumnBean[]) layout.clone();
93    }
94  
95    @XmlElement(name="colSize")
96    public int[] getColSize() {
97      return this.columnSizes.clone();  
98    }
99    
100   public void setColSize(int[] size) {
101     this.columnSizes = (int[]) size.clone();
102   }
103 
104   @XmlElement(name="columns")
105   public int getColumns() {
106     return this.columns;
107   }
108   
109   public void setColumns(int columns) {
110     this.columns = columns;
111   }
112   
113   public JSONObject deserialize() {
114     JSONObject json = new JSONObject();
115     JSONArray ja = new JSONArray();
116     JSONArray sizes = new JSONArray();
117     try {
118       json.put("title", this.title);
119       for(int i=0;i<layout.length;i++) {
120         ja.add(layout[i].deserialize());
121       }
122       json.put("layout", (JSONArray) ja);
123       json.put("columns", layout.length);
124       if(columnSizes!=null) {
125         for(int colSize : columnSizes) {
126           sizes.add(colSize);
127         }
128       }
129       json.put("colSize", (JSONArray) sizes);
130     } catch (Exception e) {
131       log.error(ExceptionUtil.getStackTrace(e));
132     }
133     return json;
134   }
135 
136 }