Android Open Source - loopback-sdk-android Model






From Project

Back to project page loopback-sdk-android.

License

The source code is released under:

MIT License

If you think the Android project loopback-sdk-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

// Copyright (c) 2013 StrongLoop. All rights reserved.
/* w  w w. jav  a  2  s.c o m*/
package com.strongloop.android.loopback;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import com.strongloop.android.loopback.callbacks.VoidCallback;
import com.strongloop.android.remoting.Repository;
import com.strongloop.android.remoting.VirtualObject;
import com.strongloop.android.remoting.adapters.Adapter;

/**
 * A local representative of a single model instance on the server. The data is
 * immediately accessible locally, but can be saved, destroyed, etc. from the
 * server easily.
 */
public class Model extends VirtualObject {

    /**
     * @deprecated Use {link VoidCallback} instead.
     */
    public static interface Callback extends VoidCallback {
    }

    private Object id;
    private Map<String, Object> overflow = new HashMap<String, Object>();

    public Model(Repository repository,
            Map<String, ? extends Object> creationParameters) {
        super(repository, creationParameters);
    }

    public Model() {
        this(null, null);
    }

    /**
     * Gets the model's id field.
     * @return The id.
     */
    public Object getId() {
        return id;
    }

    /* package private */ void setId(Object id) {
        this.id = id;
    }

    /**
     * Gets the value associated with a given key.
     * @param key The key for which to return the corresponding value.
     * @return The value associated with the key, or <code>null</code> if no
     * value is associated with the key.
     */
    public Object get(String key) {
        return overflow.get(key);
    }

    /**
     * Adds a given key-value pair to the dictionary.
     *
     * @param key The key for value. If the key already exists
     * in the dictionary, the specified value takes its place.
     * @param value The value for the key. The value may be <code>null</code>.
     */
    public void put(String key, Object value) {
        overflow.put(key, value);
    }

    /**
     * Adds all the specified params to the dictionary.
     * @param params The params to add.
     */
    public void putAll(Map<String, ? extends Object> params) {
        overflow.putAll(params);
    }

    /**
     * Converts the Model (and all of its Java Bean properties) into a
     * {@link java.util.Map}.
     */
    public Map<String, ? extends Object> toMap() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.putAll(overflow);
        map.put("id", getId());
        map.putAll(super.toMap());
        return map;
    }

    /**
     * Saves the Model to the server.
     * <p>
     * This method calls {@link #toMap()} to determine which fields should be
     * saved.
     * @param callback The callback to be executed when finished.
     */
    public void save(final VoidCallback callback) {
        invokeMethod(id == null ? "create" : "save", toMap(),
                new Adapter.JsonObjectCallback() {

            @Override
            public void onError(Throwable t) {
                callback.onError(t);
            }

            @Override
            public void onSuccess(JSONObject response) {
                Object id = response.opt("id");
                if (id != null) {
                    setId(id);
                }
                callback.onSuccess();
            }
        });
    }

    /**
     * Destroys the Model from the server.
     * @param callback The callback to be executed when finished.
     */
    public void destroy(final VoidCallback callback) {
        invokeMethod("remove", toMap(), new Adapter.Callback() {

            @Override
            public void onError(Throwable t) {
                callback.onError(t);
            }

            @Override
            public void onSuccess(String response) {
                callback.onSuccess();
            }
        });
    }
}




Java Source Code List

com.strongloop.android.loopback.AccessTokenRepository.java
com.strongloop.android.loopback.AccessToken.java
com.strongloop.android.loopback.ContainerRepository.java
com.strongloop.android.loopback.Container.java
com.strongloop.android.loopback.FileRepository.java
com.strongloop.android.loopback.File.java
com.strongloop.android.loopback.LocalInstallation.java
com.strongloop.android.loopback.ModelRepository.java
com.strongloop.android.loopback.Model.java
com.strongloop.android.loopback.RestAdapter.java
com.strongloop.android.loopback.RestRepository.java
com.strongloop.android.loopback.UserRepository.java
com.strongloop.android.loopback.User.java
com.strongloop.android.loopback.callbacks.EmptyResponseParser.java
com.strongloop.android.loopback.callbacks.JsonArrayParser.java
com.strongloop.android.loopback.callbacks.JsonObjectParser.java
com.strongloop.android.loopback.callbacks.ListCallback.java
com.strongloop.android.loopback.callbacks.ObjectCallback.java
com.strongloop.android.loopback.callbacks.VoidCallback.java
com.strongloop.android.remoting.BeanUtil.java
com.strongloop.android.remoting.JsonUtil.java
com.strongloop.android.remoting.Repository.java
com.strongloop.android.remoting.Transient.java
com.strongloop.android.remoting.VirtualObject.java
com.strongloop.android.remoting.adapters.Adapter.java
com.strongloop.android.remoting.adapters.RestAdapter.java
com.strongloop.android.remoting.adapters.RestContractItem.java
com.strongloop.android.remoting.adapters.RestContract.java
com.strongloop.android.remoting.adapters.StreamParam.java