Hubiquitus Android  0.3
Android client for hubiquitus protocol
HMessage.java
Go to the documentation of this file.
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 
00025 import org.hubiquitus.hapi.util.DateISO8601;
00026 import org.hubiquitus.hapi.util.HJsonDictionnary;
00027 import org.json.JSONException;
00028 import org.json.JSONObject;
00029 
00035 public class HMessage implements HJsonObj {
00036          
00037          private JSONObject hmessage = new JSONObject();
00038          
00039          public HMessage() {
00040          }
00041          
00042          public HMessage(JSONObject jsonObj) {
00043                   fromJSON(jsonObj);                  
00044          }
00045          
00046          /* HJsonObj interface */
00047          
00048          public JSONObject toJSON() {
00049                   return this.hmessage;
00050          }
00051          
00052          public void fromJSON(JSONObject jsonObj) {
00053                   if(jsonObj != null){
00054                            this.hmessage = jsonObj;
00055                   } else {
00056                            this.hmessage = new JSONObject();
00057                   }
00058          }
00059          
00060          public String getHType() {
00061                   return "hmessage";
00062          }
00063          
00064          @Override
00065          public String toString() {
00066                   return hmessage.toString();
00067          }
00068          
00075          public boolean equals(HMessage obj) {
00076                   if(obj.getMsgid() != this.getMsgid())
00077                            return false;
00078                   if(obj.getChid() != this.getChid())
00079                            return false;
00080                   if(obj.getConvid() != this.getConvid())
00081                            return false;
00082                   if(obj.getType() != this.getType())
00083                            return false;
00084                   if(obj.getPriority().value() != this.getPriority().value())
00085                            return false;
00086                   if(obj.getRelevance() != this.getRelevance())
00087                            return false;
00088                   if(obj.getTransient() != this.getTransient())
00089                            return false;
00090                   if(obj.getAuthor() != this.getAuthor())
00091                            return false;
00092                   if(obj.getPublisher() != this.getPublisher())
00093                            return false;
00094                   if(obj.getPublished() != this.getPublished())
00095                            return false;
00096                   if(obj.getLocation().equals(this.getLocation()))
00097                            return false;
00098                   return true;
00099          }
00100          
00101          @Override
00102          public int hashCode() {
00103                   return hmessage.hashCode();
00104          }
00105          
00106          /* Getters & Setters */
00107          
00112          public String getMsgid() {
00113                   String msgid;
00114                   try {
00115                            msgid = hmessage.getString("msgid");
00116                   } catch (Exception e) {
00117                            msgid = null;
00118                   }
00119                   return msgid;
00120          }
00121 
00122          public void setMsgid(String msgid) {
00123                   try {
00124                            if(msgid == null) {
00125                                     hmessage.remove("msgid");
00126                            } else {
00127                                     hmessage.put("msgid", msgid);
00128                            }
00129                   } catch (JSONException e) {
00130                   }
00131          }
00132 
00137          public String getChid() {
00138                   String chid;
00139                   try {
00140                            chid = hmessage.getString("chid");
00141                   } catch (Exception e) {
00142                            chid = null;
00143                   }
00144                   return chid;
00145          }
00146 
00147          public void setChid(String chid) {
00148                   try {
00149                            if(chid == null) {
00150                                     hmessage.remove("chid");
00151                            } else {
00152                                     hmessage.put("chid", chid);
00153                            }
00154                   } catch (JSONException e) {
00155                   }
00156          }
00157 
00162          public String getConvid() {
00163                   String convid;
00164                   try {
00165                            convid = hmessage.getString("convid");
00166                   } catch (Exception e) {
00167                            convid = null;
00168                   }
00169                   return convid;
00170          }
00171 
00172          public void setConvid(String convid) {
00173                   try {
00174                            if(convid == null) {
00175                                     hmessage.remove("convid");
00176                            } else {
00177                                     hmessage.put("convid", convid);
00178                            }
00179                   } catch (JSONException e) {
00180                   }
00181          }
00182 
00186          public String getType() {
00187                   String type;
00188                   try {
00189                            type = hmessage.getString("type");
00190                   } catch (Exception e) {
00191                            type = null;
00192                   }
00193                   return type;
00194          }
00195          
00196          public void setType(String type) {
00197                   try {
00198                            if(type == null) {
00199                                     hmessage.remove("type");
00200                            } else {
00201                                     hmessage.put("type", type);
00202                            }
00203                   } catch (JSONException e) {
00204                   }
00205          }
00206 
00211          public HMessagePriority getPriority() {
00212                   HMessagePriority priority;
00213                   try {
00214                            int priorityInt = hmessage.getInt("priority");
00215                            if(priorityInt < 0 || priorityInt > 5) {
00216                                     priority = null;
00217                            } else {
00218                                     priority = HMessagePriority.constant(priorityInt);
00219                            }
00220                   } catch (Exception e1) {
00221                            priority = null;
00222                   }
00223                   return priority;
00224          }
00225 
00226          public void setPriority(HMessagePriority priority) {
00227                   try {
00228                            if(priority == null) {
00229                                     hmessage.remove("priority");
00230                            } else {
00231                                     hmessage.put("priority", priority.value());
00232                            }
00233                   } catch (JSONException e) {
00234                   }
00235          }
00236 
00237          
00242          public Calendar getRelevance() {
00243                   Calendar relevance;
00244                   try {
00245                            relevance = (DateISO8601.toCalendar(hmessage.getString("relevance")));;
00246                   } catch (JSONException e) {
00247                            relevance = null;
00248                   }
00249                   return relevance;
00250          }
00251 
00252          public void setRelevance(Calendar relevance) {
00253                   try {
00254                            if(relevance == null) {
00255                                     hmessage.remove("relevance");
00256                            } else {
00257                                     hmessage.put("relevance", DateISO8601.fromCalendar(relevance));
00258                            }
00259                   } catch (JSONException e) {
00260                            e.printStackTrace();
00261                   }
00262          }
00263 
00268          public Boolean getTransient() {
00269                   Boolean _transient;
00270                   try {
00271                            _transient = hmessage.getBoolean("transient");
00272                   } catch (JSONException e) {
00273                            _transient = null;
00274                   }
00275                   return _transient;
00276          }
00277 
00278          public void setTransient(Boolean _transient) {
00279                   try {
00280                            if(_transient == null) {
00281                                     hmessage.remove("transient");
00282                            } else {
00283                                     hmessage.put("transient", _transient);
00284                            }
00285                   } catch (JSONException e) {
00286                   }
00287          }        
00288          
00293          public HLocation getLocation() {
00294                   HLocation location;
00295                   try {
00296                            location = new HLocation(hmessage.getJSONObject("location"));
00297                   } catch (JSONException e) {
00298                            location = null;
00299                   }
00300                   return location;
00301          }
00302 
00303          public void setLocation(HLocation location) {
00304                   try {
00305                            if(location == null) {
00306                                     hmessage.remove("location");
00307                            } else {
00308                                     hmessage.put("location", location.toJSON());
00309                            }
00310                   } catch (JSONException e) {
00311                            e.printStackTrace();
00312                   }
00313          }
00314          
00318          public String getAuthor() {
00319                   String author;
00320                   try {
00321                            author = hmessage.getString("author");
00322                   } catch (Exception e) {
00323                            author = null;
00324                   }
00325                   return author;
00326          }
00327          
00328          public void setAuthor(String author) {
00329                   try {
00330                            if(author == null) {
00331                                     hmessage.remove("author");
00332                            } else {
00333                                     hmessage.put("author", author);
00334                            }
00335                   } catch (JSONException e) {
00336                   }
00337          }
00338          
00343          public String getPublisher() {
00344                   String publisher;
00345                   try {
00346                            publisher = hmessage.getString("publisher");
00347                   } catch (Exception e) {
00348                            publisher = null;
00349                   }
00350                   return publisher;
00351          }
00352          
00353          public void setPublisher(String publisher) {
00354                   try {
00355                            if(publisher == null) {
00356                                     hmessage.remove("publisher");
00357                            } else {
00358                                     hmessage.put("publisher", publisher);
00359                            }
00360                   } catch (JSONException e) {
00361                   }
00362          }
00363          
00369          public Calendar getPublished() {
00370                   Calendar published;
00371                   try {
00372                            published = (DateISO8601.toCalendar(hmessage.getString("published")));
00373                   } catch (JSONException e) {
00374                            published = null;
00375                   }
00376                   return published;
00377          }
00378 
00379          public void setPublished(Calendar published) {
00380                   try {
00381                            if(published == null) {
00382                                     hmessage.remove("published");
00383                            } else {
00384                                     hmessage.put("published", DateISO8601.fromCalendar(published));
00385                            }
00386                   } catch (JSONException e) {
00387                            e.printStackTrace();
00388                   }
00389          }
00390          
00395          public HJsonObj getHeaders() {
00396                   HJsonDictionnary headers = new HJsonDictionnary();
00397                   try {
00398                            headers.fromJSON(hmessage.getJSONObject("headers"));
00399                   } catch (JSONException e) {
00400                            headers = null;
00401                   }
00402                   return headers;
00403          }
00404 
00405          public void setHeaders(HJsonObj headers) {
00406                   try {
00407                            if(headers == null) {
00408                                     hmessage.remove("headers");
00409                            } else {
00410                                     hmessage.put("headers", headers.toJSON());
00411                            }
00412                   } catch (JSONException e) {
00413                            e.printStackTrace();
00414                   }
00415          }
00416          
00421          public HJsonObj getPayload() {
00422                   HJsonObj payload;
00423                   try {
00424                            JSONObject jsonPayload = hmessage.getJSONObject("payload");
00425                            String type = this.getType().toLowerCase();
00426                            if (type.equalsIgnoreCase("hmeasure")) {
00427                                     payload = new HMeasure(jsonPayload);
00428                            } else if (type.equalsIgnoreCase("halert")) {
00429                                     payload = new HAlert(jsonPayload);
00430                            } else if (type.equalsIgnoreCase("hack")) {
00431                                     payload = new HAck(jsonPayload);
00432                            } else if (type.equalsIgnoreCase("hconvstate")) {
00433                                     payload = new HConvState(jsonPayload);
00434                            } else {
00435                                     payload = new HJsonDictionnary(jsonPayload);
00436                            }
00437                   } catch (JSONException e) {
00438                            payload = null;
00439                   }
00440                   return payload;
00441          }
00442 
00443          public void setPayload(HJsonObj payload) {
00444                   try {
00445                            if(payload == null) {
00446                                     hmessage.remove("payload");
00447                            } else {
00448                                     hmessage.put("payload", payload.toJSON());
00449                            }
00450                   } catch (JSONException e) {
00451                            e.printStackTrace();
00452                   }
00453          }
00454 }
 All Classes Namespaces Files Functions Variables