![]() |
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 java.util.Calendar; 00024 import java.util.GregorianCalendar; 00025 00026 import org.hubiquitus.hapi.util.DateISO8601; 00027 import org.hubiquitus.hapi.util.HJsonDictionnary; 00028 import org.json.JSONException; 00029 import org.json.JSONObject; 00030 00036 public class HCommand implements HJsonObj { 00037 00038 private JSONObject hcommand = new JSONObject(); 00039 00040 public HCommand() { 00041 setSent(new GregorianCalendar()); 00042 setTransient(true); 00043 } 00044 00045 public HCommand(String entity, String cmd, HJsonObj params) { 00046 this(); 00047 setEntity(entity); 00048 setCmd(cmd); 00049 setParams(params); 00050 } 00051 00052 public HCommand(JSONObject jsonObj) { 00053 fromJSON(jsonObj); 00054 } 00055 00056 /* HJsonObj interface */ 00057 00058 public JSONObject toJSON() { 00059 return this.hcommand; 00060 } 00061 00062 public void fromJSON(JSONObject jsonObj) { 00063 if(jsonObj != null){ 00064 this.hcommand = jsonObj; 00065 } else { 00066 this.hcommand = new JSONObject(); 00067 } 00068 } 00069 00070 public String getHType() { 00071 return "hcommand"; 00072 } 00073 00074 @Override 00075 public String toString() { 00076 return hcommand.toString(); 00077 } 00078 00084 public boolean equals(HCommand obj) { 00085 if(obj.getReqid() != this.getReqid()) 00086 return false; 00087 if(obj.getRequester() != this.getRequester()) 00088 return false; 00089 if(obj.getSender() != this.getSender()) 00090 return false; 00091 if(obj.getEntity() != this.getEntity()) 00092 return false; 00093 if(obj.getSent() != this.getSent()) 00094 return false; 00095 if(obj.getCmd() != this.getCmd()) 00096 return false; 00097 if(obj.getTransient() != this.getTransient()) 00098 return false; 00099 return true; 00100 } 00101 00102 @Override 00103 public int hashCode() { 00104 return hcommand.hashCode(); 00105 } 00106 00107 /* Getters & Setters */ 00108 00113 public String getReqid() { 00114 String reqid; 00115 try { 00116 reqid = hcommand.getString("reqid"); 00117 } catch (Exception e) { 00118 reqid = null; 00119 } 00120 return reqid; 00121 } 00122 00123 public void setReqid(String reqid) { 00124 try { 00125 if(reqid == null) { 00126 hcommand.remove("reqid"); 00127 } else { 00128 hcommand.put("reqid", reqid); 00129 } 00130 } catch (JSONException e) { 00131 } 00132 } 00133 00138 public String getRequester() { 00139 String requester; 00140 try { 00141 requester = hcommand.getString("requester"); 00142 } catch (Exception e) { 00143 requester = null; 00144 } 00145 return requester; 00146 } 00147 00148 public void setRequester(String requester) { 00149 try { 00150 if(requester == null) { 00151 hcommand.remove("requester"); 00152 } else { 00153 hcommand.put("requester", requester); 00154 } 00155 } catch (JSONException e) { 00156 } 00157 } 00158 00163 public String getSender() { 00164 String sender; 00165 try { 00166 sender = hcommand.getString("sender"); 00167 } catch (Exception e) { 00168 sender = null; 00169 } 00170 return sender; 00171 } 00172 00173 public void setSender(String sender) { 00174 try { 00175 if(sender == null) { 00176 hcommand.remove("sender"); 00177 } else { 00178 hcommand.put("sender", sender); 00179 } 00180 } catch (JSONException e) { 00181 } 00182 } 00183 00188 public String getEntity() { 00189 String entity; 00190 try { 00191 entity = hcommand.getString("entity"); 00192 } catch (Exception e) { 00193 entity = null; 00194 } 00195 return entity; 00196 } 00197 00198 public void setEntity(String entity) { 00199 try { 00200 if(entity == null) { 00201 hcommand.remove("entity"); 00202 } else { 00203 hcommand.put("entity", entity); 00204 } 00205 } catch (JSONException e) { 00206 } 00207 } 00208 00213 public Calendar getSent() { 00214 Calendar sent; 00215 try { 00216 sent = (DateISO8601.toCalendar(hcommand.getString("sent"))); 00217 } catch (JSONException e) { 00218 sent = null; 00219 } 00220 return sent; 00221 } 00222 00223 public void setSent(Calendar sent) { 00224 try { 00225 if(sent == null) { 00226 hcommand.remove("sent"); 00227 } else { 00228 hcommand.put("sent", DateISO8601.fromCalendar(sent)); 00229 } 00230 } catch (JSONException e) { 00231 } 00232 } 00233 00238 public String getCmd() { 00239 String cmd; 00240 try { 00241 cmd = hcommand.getString("cmd"); 00242 } catch (JSONException e) { 00243 cmd = null; 00244 } 00245 return cmd; 00246 } 00247 00248 public void setCmd(String cmd) { 00249 try { 00250 if(cmd == null) { 00251 hcommand.remove("cmd"); 00252 } else { 00253 hcommand.put("cmd", cmd); 00254 } 00255 } catch (JSONException e) { 00256 e.printStackTrace(); 00257 } 00258 } 00259 00263 public HJsonObj getParams() { 00264 HJsonObj params; 00265 try { 00266 params = new HJsonDictionnary(hcommand.getJSONObject("params")); 00267 } catch (JSONException e) { 00268 params = null; 00269 } 00270 return params; 00271 } 00272 00273 public void setParams(HJsonObj params) { 00274 try { 00275 if(params == null) { 00276 hcommand.remove("params"); 00277 } else { 00278 hcommand.put("params", params.toJSON()); 00279 } 00280 } catch (JSONException e) { 00281 } 00282 } 00283 00287 public Boolean getTransient() { 00288 Boolean _transient; 00289 try { 00290 _transient = hcommand.getBoolean("transient"); 00291 } catch (JSONException e) { 00292 _transient = null; 00293 } 00294 return _transient; 00295 } 00296 00297 public void setTransient(Boolean _transient) { 00298 try { 00299 if(_transient == null) { 00300 hcommand.remove("transient"); 00301 } else { 00302 hcommand.put("transient", _transient); 00303 } 00304 } catch (JSONException e) { 00305 } 00306 } 00307 00308 00309 }