This project has retired. For details please refer to its Attic page.
ParametersBean 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  import java.util.Collection;
23  import java.util.HashSet;
24  
25  import javax.xml.bind.annotation.XmlElement;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.json.simple.JSONArray;
29  import org.json.simple.JSONObject;
30  
31  import org.apache.hadoop.chukwa.util.ExceptionUtil;
32  
33  public class ParametersBean {
34    private static Log log = LogFactory.getLog(ParametersBean.class);
35    private String name=null;
36    private String type=null;
37    private Collection<String> value=null;
38    private String control=null;
39    private String label=null;
40    private String callback=null;
41    private int edit=1;
42    private OptionBean[] options=null;
43    
44    public ParametersBean() {    
45    }
46    
47    public ParametersBean(JSONObject json) throws ParseException {
48      name=(String) json.get("name");
49      type=(String) json.get("type");
50      if(json.containsKey("value")) {
51        if(json.get("value").getClass()==JSONArray.class) {
52          JSONArray ja = (JSONArray) json.get("value");
53          Collection<String> c = new HashSet<String>();
54          for(int i = 0; i < ja.size(); i++) {
55            c.add((String) ja.get(i));
56          }
57          this.value = c;
58        } else {
59          Collection<String> c = new HashSet<String>();
60          c.add((String)json.get("value"));
61          this.value = c;
62        }        
63      }
64      if(json.containsKey("label")) {
65        label=(String) json.get("label");
66      } else {
67        label=(String) json.get("name");
68      }
69      if(json.get("type").toString().intern()=="custom".intern()) {
70        control=(String) json.get("control");
71      }
72      if(json.containsKey("callback")) {
73        callback=(String) json.get("callback");
74      }
75      if(json.containsKey("options")) {
76        JSONArray aj = (JSONArray) json.get("options");
77        options = new OptionBean[aj.size()];
78        for(int i=0;i<aj.size();i++) {
79          OptionBean o = new OptionBean((JSONObject) aj.get(i));
80          options[i]=o;
81        }
82      }
83      if(json.containsKey("edit")) {
84        if(json.get("edit").getClass().equals(String.class)) {
85          edit=Integer.parseInt((String)json.get("edit"));
86        } else if(json.get("edit").getClass().equals(Long.class)) {
87          edit=((Long)json.get("edit")).intValue();          
88        }
89      }
90    }
91    
92    @XmlElement
93    public String getName() {
94      return name;
95    }
96    
97    @XmlElement
98    public String getType() {
99      return type;
100   }
101 
102   @XmlElement
103   public Collection<String> getValue() {
104     return value;
105   }
106 
107   @XmlElement
108   public String getControl() {
109     return control;  
110   }
111   
112   @XmlElement
113   public String getLabel() {
114     return label;  
115   }
116   
117   @XmlElement
118   public OptionBean[] getOptions() {
119     if(options==null) {
120       options = new OptionBean[1];
121     }
122     return options.clone();       
123   }
124   
125   @XmlElement
126   public int getEdit() {
127     return edit;
128   }
129 
130   @XmlElement
131   public String getCallback() {
132     return callback;
133   }
134 
135   public void setName(String name) {
136     this.name = name;
137   }
138   
139   public void setType(String type) {
140     this.type = type;
141   }
142 
143   public void setValue(Collection<String> value) {
144     this.value = value;
145   }
146   
147   public void setControl(String control) {
148     this.control = control;
149   }
150   
151   public void setLabel(String label) {
152     this.label = label;
153   }
154   
155   public void setOptions(OptionBean[] options) {
156     this.options = (OptionBean[]) options.clone();
157   }
158   
159   public void setEdit(int edit) {
160     this.edit = edit;
161   }
162   
163   public void setCallback(String callback) {
164     this.callback = callback;  
165   }
166 
167   public JSONObject deserialize() {
168     JSONObject json = new JSONObject();
169     try {
170       json.put("name",this.name);
171       json.put("type",this.type);
172       if(this.value!=null) {
173         JSONArray ja = new JSONArray();
174         for(String s : this.value) {
175           ja.add(s);
176         }
177         json.put("value", ja);
178       }
179       if(control!=null) {
180         json.put("control",this.control);
181       }
182       json.put("label",this.label);
183       json.put("edit",this.edit);
184       if(this.callback!=null) {
185         json.put("callback", callback);
186       }
187       if(options!=null) {
188         JSONArray ja = new JSONArray();
189         for(int i=0;i<options.length;i++) {
190           ja.add(this.options[i].deserialize());          
191         }
192         json.put("options", ja);
193       }
194     } catch (Exception e) {
195       log.error(ExceptionUtil.getStackTrace(e));
196     }
197     return json;
198   }
199 }