AbstractCall.java
Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2005-2015 Team XBMC
00003  *      http://xbmc.org
00004  *
00005  *  This Program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2, or (at your option)
00008  *  any later version.
00009  *
00010  *  This Program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with XBMC Remote; see the file license.  If not, write to
00017  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
00018  *  http://www.gnu.org/copyleft/gpl.html
00019  *
00020  */
00021 
00022 package org.xbmc.android.jsonrpc.api;
00023 
00024 import java.io.IOException;
00025 import java.util.ArrayList;
00026 import java.util.HashMap;
00027 import java.util.Random;
00028 
00029 import org.codehaus.jackson.JsonNode;
00030 import org.codehaus.jackson.JsonProcessingException;
00031 import org.codehaus.jackson.map.ObjectMapper;
00032 import org.codehaus.jackson.node.ArrayNode;
00033 import org.codehaus.jackson.node.NullNode;
00034 import org.codehaus.jackson.node.ObjectNode;
00035 
00036 import android.os.Parcel;
00037 import android.os.Parcelable;
00038 import android.util.Log;
00039 
00077 public abstract class AbstractCall<T> implements Parcelable {
00078         
00079 //      private static final String TAG = AbstractCall.class.getSimpleName();
00080 
00081         public static final String RESULT = "result";
00082         
00083         private final static Random RND = new Random(System.currentTimeMillis());
00084         protected final static ObjectMapper OM = new ObjectMapper();
00085         
00089         private static final String PARAMS = "params";
00090         
00095         public abstract String getName();
00096 
00106         protected abstract boolean returnsList();
00107         
00115         public ObjectNode mRequest = OM.createObjectNode();
00116         
00125         protected T mResult = null;
00126         protected ArrayList<T> mResults = null;
00127         
00131         private final String mId;
00132         
00137         protected AbstractCall() {
00138                 final ObjectNode request = mRequest;
00139                 
00140                 mId = String.valueOf(RND.nextLong());
00141                 request.put("jsonrpc", "2.0");
00142                 request.put("id", mId);
00143                 request.put("method", getName());
00144         }
00145         
00150         public ObjectNode getRequest() {
00151                 return mRequest;
00152         }
00153         
00161         public void setResponse(JsonNode response) {
00162                 if (returnsList()) {
00163                         mResults = parseMany(response.get(RESULT));
00164                 } else {
00165                         mResult = parseOne(response.get(RESULT));
00166                 }
00167         }
00168         
00169         @SuppressWarnings("unchecked")
00170         public void copyResponse(AbstractCall<?> call) {
00171                 if (returnsList()) {
00172                         mResults = (ArrayList<T>)call.getResults();
00173                 } else {
00174                         mResult = (T)call.getResult();
00175                 }
00176         }
00177         
00186         public T getResult() {
00187                 if (returnsList()) {
00188                         return mResults.get(0);
00189                 }
00190                 return mResult;
00191         }
00192         
00201         public ArrayList<T> getResults() {
00202                 if (!returnsList()) {
00203                         final ArrayList<T> results = new ArrayList<T>(1);
00204                         results.add(mResult);
00205                         return results;
00206                 }
00207                 return mResults;
00208         }
00209         
00214         public String getId() {
00215                 return mId;
00216         }
00217         
00223         protected JsonNode parseResult(JsonNode obj) {
00224                 return obj.get(RESULT);
00225         }
00226         
00227         protected ArrayNode parseResults(JsonNode obj, String key) {
00228                 if(obj.get(key) instanceof NullNode) {
00229                         return null;
00230                 }
00231                 return (ArrayNode)obj.get(key);
00232         }
00233         
00243         protected T parseOne(JsonNode obj) {
00244                 return null;
00245         }
00246         
00256         protected ArrayList<T> parseMany(JsonNode obj) {
00257                 return null;
00258         }
00259         
00265         protected void addParameter(String name, String value) {
00266                 if (value != null) {
00267                         getParameters().put(name, value);
00268                 }
00269         }
00270         
00276         protected void addParameter(String name, Integer value) {
00277                 if (value != null) {
00278                         getParameters().put(name, value);
00279                 }
00280         }
00281         
00287         protected void addParameter(String name, Boolean value) {
00288                 if (value != null) {
00289                         getParameters().put(name, value);
00290                 }
00291         }
00292         
00293         protected void addParameter(String name, Double value) {
00294                 if (value != null) {
00295                         getParameters().put(name, value);
00296                 }
00297         }
00298         
00299         protected void addParameter(String name, AbstractModel value) {
00300                 if (value != null) {
00301                         getParameters().put(name, value.toJsonNode());
00302                 }
00303         }
00304         
00310         protected void addParameter(String name, String[] values) {
00311                 // don't add if nothing to add
00312                 if (values == null || values.length == 0) {
00313                         return;
00314                 }
00315                 final ArrayNode props = OM.createArrayNode();
00316                 for (int i = 0; i < values.length; i++) {
00317                         props.add(values[i]);
00318                 }
00319                 getParameters().put(name, props);
00320         }
00321         
00327         protected void addParameter(String name, HashMap<String, String> map) {
00328                 if (map == null || map.size() == 0) {
00329                         return;
00330                 }
00331                 final ObjectNode props = OM.createObjectNode();
00332                 for (String key : map.values()) {
00333                         props.put(key, map.get(key));
00334                 }
00335                 getParameters().put(name, props);
00336         }
00337         
00343         private ObjectNode getParameters() {
00344                 final ObjectNode request = mRequest;
00345                 if (request.has(PARAMS)) {
00346                         return (ObjectNode)request.get(PARAMS);
00347                 } else {
00348                         final ObjectNode parameters = OM.createObjectNode();
00349                         request.put(PARAMS, parameters);
00350                         return parameters;
00351                 }
00352         }
00353         
00359         @Override
00360         public void writeToParcel(Parcel parcel, int flags) {
00361                 parcel.writeString(mId);
00362                 parcel.writeValue(mRequest.toString());
00363         }
00364         @Override
00365         public int describeContents() {
00366                 return 0;
00367         }
00368         
00369         protected AbstractCall(Parcel parcel) {
00370                 mId = parcel.readString();
00371                 try {
00372                         mRequest = (ObjectNode)OM.readTree(parcel.readString());
00373                 } catch (JsonProcessingException e) {
00374                         Log.e(getName(), "Error reading JSON object from parcel: " + e.getMessage(), e);
00375                 } catch (IOException e) {
00376                         Log.e(getName(), "I/O exception reading JSON object from parcel: " + e.getMessage(), e);
00377                 }
00378         }
00379         
00380 }


smarthome_media_kodi_driver
Author(s): Mickael Gaillard , Erwan Le Huitouze
autogenerated on Thu Jun 6 2019 21:03:49