![]() |
Hubiquitus Android
0.3
Android client for hubiquitus protocol
|
00001 /* 00002 * Copyright (c) Novedia Group 2012. 00003 * 00004 * This file is part of Hubiquitus. 00005 * 00006 * Hubiquitus is free software: you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation, either version 3 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * Hubiquitus is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with Hubiquitus. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 00021 package org.hubiquitus.hapi.hStructures; 00022 00023 import org.hubiquitus.hapi.util.HJsonDictionnary; 00024 import org.json.JSONArray; 00025 import org.json.JSONException; 00026 import org.json.JSONObject; 00027 00034 public class HResult implements HJsonObj { 00035 00036 private JSONObject hresult = new JSONObject(); 00037 00038 public HResult() { } 00039 00040 public HResult(String reqid, String cmd, HJsonObj result) { 00041 setReqid(reqid); 00042 setCmd(cmd); 00043 setResult(result); 00044 } 00045 00046 public HResult(JSONObject jsonObj) { 00047 this.fromJSON(jsonObj); 00048 } 00049 00050 /* HJsonObj interface */ 00051 00052 public JSONObject toJSON() { 00053 return this.hresult; 00054 } 00055 00056 public String getHType() { 00057 return "hresult"; 00058 } 00059 00060 public void fromJSON(JSONObject jsonObj) { 00061 if( jsonObj != null) { 00062 this.hresult = jsonObj; 00063 } else { 00064 this.hresult = new JSONObject(); 00065 } 00066 } 00067 00068 @Override 00069 public String toString() { 00070 return hresult.toString(); 00071 } 00072 00078 public boolean equals(HResult obj) { 00079 if(obj.getCmd() != this.getCmd()) 00080 return false; 00081 if(obj.getReqid() != this.getReqid()) 00082 return false; 00083 if(obj.getStatus().value() != this.getStatus().value()) 00084 return false; 00085 return true; 00086 } 00087 00088 @Override 00089 public int hashCode() { 00090 return hresult.hashCode(); 00091 } 00092 00093 /* Getters & Setters */ 00094 00099 public String getCmd() { 00100 String cmd; 00101 try { 00102 cmd = hresult.getString("cmd"); 00103 } catch (JSONException e) { 00104 cmd = null; 00105 } 00106 return cmd; 00107 } 00108 00109 public void setCmd(String cmd) { 00110 try { 00111 if(cmd == null) { 00112 hresult.remove("cmd"); 00113 } else { 00114 hresult.put("cmd", cmd); 00115 } 00116 } catch (JSONException e) { 00117 } 00118 } 00119 00124 public String getReqid() { 00125 String reqid; 00126 try { 00127 reqid = hresult.getString("reqid"); 00128 } catch (JSONException e) { 00129 reqid = null; 00130 } 00131 return reqid; 00132 } 00133 00134 public void setReqid(String reqid) { 00135 try { 00136 if(reqid == null) { 00137 hresult.remove("reqid"); 00138 } else { 00139 hresult.put("reqid", reqid); 00140 } 00141 } catch (JSONException e) { 00142 } 00143 } 00144 00149 public ResultStatus getStatus() { 00150 ResultStatus reqid; 00151 try { 00152 reqid = ResultStatus.constant(hresult.getInt("status")); 00153 } catch (Exception e1) { 00154 reqid = null; 00155 } 00156 return reqid; 00157 } 00158 00159 public void setStatus(ResultStatus status) { 00160 try { 00161 if(status == null) { 00162 hresult.remove("status"); 00163 } else { 00164 hresult.put("status", status.value()); 00165 } 00166 } catch (JSONException e) { 00167 } 00168 } 00169 00176 public HJsonObj getResult() { 00177 HJsonObj result; 00178 try { 00179 result = new HJsonDictionnary(hresult.getJSONObject("result")); 00180 } catch (JSONException e) { 00181 result = null; 00182 } 00183 return result; 00184 } 00185 00190 public String getResultString() { 00191 String result; 00192 try { 00193 result = hresult.getString("result"); 00194 } catch (JSONException e) { 00195 result = null; 00196 } 00197 return result; 00198 } 00199 00204 public JSONArray getResultArray() { 00205 JSONArray result; 00206 try { 00207 result = hresult.getJSONArray("result"); 00208 } catch (JSONException e) { 00209 result = null; 00210 } 00211 return result; 00212 } 00213 00214 public void setResult(HJsonObj result) { 00215 try { 00216 if(result == null) { 00217 hresult.remove("result"); 00218 } else { 00219 hresult.put("result", result.toJSON()); 00220 } 00221 } catch (JSONException e) { 00222 } 00223 } 00224 00225 00226 }