@Override
 public String getSerializationBodyFormat() {
   final HttpAdapter routerAdapter =
       (HttpAdapter) RemoteRailsConfig.getRouterAdapterByClass(this.getClass());
   if (routerAdapter == null || routerAdapter.getConfigurationsAsConfiguration("GET") == null) {
     return RemoteRailsConfig.getInstance().getDefaultRouterAdapter().getSuffix();
   }
   return routerAdapter.getConfigurationsAsConfiguration("POST").getBodyFormat();
 }
  /**
   * Queries for <tt>CapitalMarketsOrder</tt> with the provided <tt>scope</tt> in the background.
   *
   * <p>Note: the scopes were defined from the Designer.
   *
   * <p>The {@link import com.anypresence.sdk.callbacks.APCallback} callback allows you to run code
   * in the main UI thread.
   *
   * <p>Example:
   *
   * <pre>{@code
   * Map<String,String> params = new HashMap<String,String>();
   * params.put("name", "test");
   * CapitalMarketsOrder.queryInBackground("all", params, 0, 25, new APCallback<List<CapitalMarketsOrder>>() {
   *
   *     public void finished(List<CapitalMarketsOrder> objects, Throwable ex) {
   *         if (ex == null) {
   *             Log.i(TAG, "Objects fetched!");
   *         } else {
   *             Log.e(TAG, "Unable to fetch objects!", ex);
   *         }
   *     }
   * });
   * }</pre>
   *
   * @see import com.anypresence.sdk.callbacks.APCallback
   * @param scope the scope to perform the query.
   * @param params a map of parameters.
   * @param offset offset for the query, or <tt>null</tt> for no offset.
   * @param limit max results to return back, <tt>null</tt> for no limit.
   * @param futureCallback the callback that handles the result of the request.
   */
  public static List<CapitalMarketsOrder> queryInBackground(
      final String scope,
      Map<String, String> params,
      Integer offset,
      Integer limit,
      IAPFutureCallback<List<CapitalMarketsOrder>> futureCallback) {

    RemoteRequest.RemoteRequestBuilder remoteRequestBuilder =
        RemoteRailsConfig.getRouterAdapterByClassOrUseDefaultAdapter(CapitalMarketsOrder.class)
            .createRemoteRequestBuilder(scope, RequestMethod.GET, CapitalMarketsOrder.class);
    remoteRequestBuilder.query(scope);
    if (params != null) {
      remoteRequestBuilder.addToParameters(params);
      remoteRequestBuilder.context(params);
    }

    if (offset != null) {
      remoteRequestBuilder.addToParameters("offset", offset.toString());
    }

    if (limit != null) {
      remoteRequestBuilder.addToParameters("limit", limit.toString());
    }

    RemoteRequest remoteRequest = remoteRequestBuilder.createRemoteRequest();

    if (DEBUG_MODE) {
      System.out.println(ReflectionToStringBuilder.reflectionToString(remoteRequest));
    }

    return com.anypresence.sdk.APObject.queryInBackground(
        remoteRequest, CapitalMarketsOrder.class, futureCallback);
  }
  /**
   * Fetches a <tt>CapitalMarketsOrder</tt> with the provided <tt>objectId</tt>.
   *
   * @param objectId the id of the object
   * @return an instance of <tt>CapitalMarketsOrder</tt>
   * @throws RemoteRequestException If there are other issues with the request.
   */
  public static CapitalMarketsOrder fetch(String objectId) throws RemoteRequestException {
    RemoteRequest.RemoteRequestBuilder remoteRequestBuilder =
        RemoteRailsConfig.getRouterAdapterByClassOrUseDefaultAdapter(CapitalMarketsOrder.class)
            .createRemoteRequestBuilder(RequestMethod.GET, CapitalMarketsOrder.class);

    CapitalMarketsOrder object = new CapitalMarketsOrder();
    object.setObjectIdAsString(objectId);

    remoteRequestBuilder.context(object);

    RemoteRequest remoteRequest = remoteRequestBuilder.createRemoteRequest();
    remoteRequest = Utility.interpolateRemoteRequest(remoteRequest);

    return com.anypresence.sdk.APObject.fetch(remoteRequest, CapitalMarketsOrder.class);
  }
  @Test
  public void shouldBeAbleDeleteFromCache()
      throws com.anypresence.sdk.acl.UnauthorizedException, RemoteRequestException,
          InterruptedException, ClassNotFoundException {
    APSetup.setBaseUrl("http://localhost");
    APSetup.setup();
    Config.getInstance().setStrictQueryFieldCheck(false);
    APSDKSetup.setDBStoreType(DBStoreType.IN_MEMORY);
    Config.DEBUG_MODE = true;

    RemoteRailsConfig.getInstance().setInlineMode(true);

    lock = new CountDownLatch(1);
    IRestClient client = getTestRestClient();
    RestClientFactory.registerJSONRestClientImplementation(client);
    com.anypresence.sdk.citi_mobile_challenge.models.RetailBankingLogin.queryInBackground(
        "all",
        null,
        new APFutureCallback<
            List<com.anypresence.sdk.citi_mobile_challenge.models.RetailBankingLogin>>() {
          @Override
          public void finished(
              List<com.anypresence.sdk.citi_mobile_challenge.models.RetailBankingLogin> arg0,
              Throwable ex) {
            objects = arg0;
            lock.countDown();
          }
        });
    lock.await();
    Assert.assertTrue(objects.size() == 2);

    // Delete one object in cache
    objects.get(0).deleteInCache();
    Assert.assertTrue(
        com.anypresence.sdk.citi_mobile_challenge.models.RetailBankingLogin.loadAllInCache().size()
            == 1);

    // Delete all objects in the cache
    com.anypresence.sdk.APObject.deleteAllInCache();
    Assert.assertTrue(
        com.anypresence.sdk.citi_mobile_challenge.models.RetailBankingLogin.loadAllInCache().size()
            == 0);
  }
  /**
   * Queries for <tt>CapitalMarketsOrder</tt> with the provided <tt>scope</tt> and other params.
   *
   * <p>Note: the scopes were defined from the Designer.
   *
   * @param scope the scope to perform the query.
   * @param params a map of parameters.
   * @param offset offset for the query, or <tt>null</tt> for no offset.
   * @param limit max results to return back, <tt>null</tt> for no limit.
   * @throws RemoteRequestException If there are other issues with the request.
   */
  public static List<CapitalMarketsOrder> query(
      String scope, Map<String, String> params, Integer offset, Integer limit)
      throws RemoteRequestException {
    RemoteRequest.RemoteRequestBuilder remoteRequestBuilder =
        RemoteRailsConfig.getRouterAdapterByClassOrUseDefaultAdapter(CapitalMarketsOrder.class)
            .createRemoteRequestBuilder(scope, RequestMethod.GET, CapitalMarketsOrder.class);
    remoteRequestBuilder.query(scope);
    if (params != null) {
      remoteRequestBuilder.addToParameters(params);
      remoteRequestBuilder.context(params);
    }
    if (offset != null) {
      remoteRequestBuilder.addToParameters("offset", offset.toString());
    }

    if (limit != null) {
      remoteRequestBuilder.addToParameters("limit", limit.toString());
    }
    RemoteRequest remoteRequest = remoteRequestBuilder.createRemoteRequest();

    return com.anypresence.sdk.APObject.query(
        remoteRequest, offset, limit, CapitalMarketsOrder.class);
  }
  @Override
  public String serialize() {
    String bodyFormat = getSerializationBodyFormat();
    SerializerBuilder builder = null;

    String recordSelector = HttpAdapter.createResponseWrangling(this).getRecordSelector();
    if (XML.equals(bodyFormat)) {
      builder = new XmlSerializer.XmlSerializerBuilder(recordSelector);
    } else if (JSON.equals(bodyFormat)) {
      builder = new JsonSerializer.JsonSerializerBuilder(recordSelector);
    } else if (FORM_ENCODED.equals(bodyFormat)) {
      builder = new FormEncodedSerializer.FormEncodedSerializerBuilder(recordSelector);
    }

    final RouterAdapter routerAdapter = RemoteRailsConfig.getRouterAdapterByClass(this.getClass());
    if (builder != null && routerAdapter != null) {

      builder.addFieldMapping(
          ((HttpAdapter) routerAdapter)
              .getConfigurationsAsConfiguration("POST")
              .getResponseRemoteFieldName("id"),
          "id");

      builder.addFieldMapping(
          ((HttpAdapter) routerAdapter)
              .getConfigurationsAsConfiguration("POST")
              .getResponseRemoteFieldName("comments"),
          "comments");

      builder.addFieldMapping(
          ((HttpAdapter) routerAdapter)
              .getConfigurationsAsConfiguration("POST")
              .getResponseRemoteFieldName("currency"),
          "currency");

      builder.addFieldMapping(
          ((HttpAdapter) routerAdapter)
              .getConfigurationsAsConfiguration("POST")
              .getResponseRemoteFieldName("last_price"),
          "last_price");

      builder.addFieldMapping(
          ((HttpAdapter) routerAdapter)
              .getConfigurationsAsConfiguration("POST")
              .getResponseRemoteFieldName("last_qty"),
          "last_qty");

      builder.addFieldMapping(
          ((HttpAdapter) routerAdapter)
              .getConfigurationsAsConfiguration("POST")
              .getResponseRemoteFieldName("price"),
          "price");

      builder.addFieldMapping(
          ((HttpAdapter) routerAdapter)
              .getConfigurationsAsConfiguration("POST")
              .getResponseRemoteFieldName("quantity"),
          "quantity");

      builder.addFieldMapping(
          ((HttpAdapter) routerAdapter)
              .getConfigurationsAsConfiguration("POST")
              .getResponseRemoteFieldName("side"),
          "side");

      builder.addFieldMapping(
          ((HttpAdapter) routerAdapter)
              .getConfigurationsAsConfiguration("POST")
              .getResponseRemoteFieldName("status"),
          "status");

      builder.addFieldMapping(
          ((HttpAdapter) routerAdapter)
              .getConfigurationsAsConfiguration("POST")
              .getResponseRemoteFieldName("symbol"),
          "symbol");

      builder.addFieldMapping(
          ((HttpAdapter) routerAdapter)
              .getConfigurationsAsConfiguration("POST")
              .getResponseRemoteFieldName("transact_time"),
          "transact_time");

      return builder.create().serialize(this);
    } else {
      return super.serialize();
    }
  }
  public RemoteObject deserialize(String queryScope, Parser parser) {
    final RouterAdapter routerAdapter = RemoteRailsConfig.getRouterAdapterByClass(this.getClass());
    CapitalMarketsOrder object = new CapitalMarketsOrder();
    HttpAdapter.Configuration configuration = null;
    if ((routerAdapter != null) && (routerAdapter instanceof HttpAdapter)) {
      if (queryScope != null && !queryScope.isEmpty()) {
        configuration =
            ((HttpAdapter) routerAdapter).getScopedConfigurationsByNameAsConfiguration(queryScope);
      } else {
        configuration = ((HttpAdapter) routerAdapter).getConfigurationsAsConfiguration("GET");
      }
    }
    if (configuration != null) {

      try {

        object.id = parser.parse(configuration.getResponseRemoteFieldName("id"), ObjectId.class);

      } catch (PathNotFoundException e) {
        log.info("Unable to deserialize field: " + e.getMessage());
      }

      try {

        object.comments =
            parser.parse(configuration.getResponseRemoteFieldName("comments"), String.class);

      } catch (PathNotFoundException e) {
        log.info("Unable to deserialize field: " + e.getMessage());
      }

      try {

        object.currency =
            parser.parse(configuration.getResponseRemoteFieldName("currency"), String.class);

      } catch (PathNotFoundException e) {
        log.info("Unable to deserialize field: " + e.getMessage());
      }

      try {

        object.lastPrice =
            parser.parse(configuration.getResponseRemoteFieldName("last_price"), Float.class);

      } catch (PathNotFoundException e) {
        log.info("Unable to deserialize field: " + e.getMessage());
      }

      try {

        object.lastQty =
            parser.parse(configuration.getResponseRemoteFieldName("last_qty"), Integer.class);

      } catch (PathNotFoundException e) {
        log.info("Unable to deserialize field: " + e.getMessage());
      }

      try {

        object.price = parser.parse(configuration.getResponseRemoteFieldName("price"), Float.class);

      } catch (PathNotFoundException e) {
        log.info("Unable to deserialize field: " + e.getMessage());
      }

      try {

        object.quantity =
            parser.parse(configuration.getResponseRemoteFieldName("quantity"), Integer.class);

      } catch (PathNotFoundException e) {
        log.info("Unable to deserialize field: " + e.getMessage());
      }

      try {

        object.side = parser.parse(configuration.getResponseRemoteFieldName("side"), String.class);

      } catch (PathNotFoundException e) {
        log.info("Unable to deserialize field: " + e.getMessage());
      }

      try {

        object.status =
            parser.parse(configuration.getResponseRemoteFieldName("status"), String.class);

      } catch (PathNotFoundException e) {
        log.info("Unable to deserialize field: " + e.getMessage());
      }

      try {

        object.symbol =
            parser.parse(configuration.getResponseRemoteFieldName("symbol"), String.class);

      } catch (PathNotFoundException e) {
        log.info("Unable to deserialize field: " + e.getMessage());
      }

      try {

        object.transactTime =
            parser.parse(configuration.getResponseRemoteFieldName("transact_time"), String.class);

      } catch (PathNotFoundException e) {
        log.info("Unable to deserialize field: " + e.getMessage());
      }

    } else {
      // Default to use gson deserializer
      String json = "";
      if (parser instanceof JsonParser) {
        json = ((JsonParser) parser).getRootElement().toString();
      }

      object = (CapitalMarketsOrder) Utilities.transformJsonToObject(json, this.getClass());
    }

    return object;
  }