Android Open Source - Android-SDK Baas Link






From Project

Back to project page Android-SDK.

License

The source code is released under:

Apache License

If you think the Android project Android-SDK 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) 2014./* w  w w . j  a  v a2 s.co m*/
 *
 * BaasBox - info@baasbox.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 */

package com.baasbox.android;

import android.os.Parcel;
import android.os.Parcelable;

import com.baasbox.android.impl.Util;
import com.baasbox.android.json.JsonArray;
import com.baasbox.android.json.JsonObject;
import com.baasbox.android.net.HttpRequest;

import org.apache.http.HttpResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * Represents a BaasBox Link between objects
 *
 * Created by Andrea Tortorella on 13/08/14.
 */
public final class BaasLink implements Parcelable{

    private String label;
    private BaasObject source;
    private BaasObject destination;
    private String id;
    private String author;

    private String creationDate;
    private long version;

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        Util.writeOptString(dest,id);
        Util.writeOptString(dest,label);
        Util.writeOptString(dest,author);
        Util.writeOptString(dest,creationDate);
        dest.writeLong(version);
        writeObject(dest,source);
        writeObject(dest,destination);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    private BaasLink(Parcel parcel){
        id = Util.readOptString(parcel);
        label = Util.readOptString(parcel);
        author = Util.readOptString(parcel);
        creationDate = Util.readOptString(parcel);
        version = parcel.readLong();
        source = readObject(parcel);
        destination = readObject(parcel);
    }

    public static final Creator<BaasLink> CREATOR = new Creator<BaasLink>() {
        @Override
        public BaasLink createFromParcel(Parcel source) {
            return new BaasLink(source);
        }

        @Override
        public BaasLink[] newArray(int size) {

            return new BaasLink[size];
        }
    };

    private static BaasObject readObject(Parcel parcel){
        byte b = parcel.readByte();
        if (b == 0) return null;
        if (b==1) {
            BaasDocument d = parcel.readParcelable(BaasDocument.class.getClassLoader());
            return d;
        } else if (b == 2){
            BaasFile f = parcel.readParcelable(BaasFile.class.getClassLoader());
            return f;
        }
        return null;
    }
    private static void writeObject(Parcel p,BaasObject o){
        if (o == null){
            p.writeByte((byte)0);
        } else {
            if (o instanceof BaasDocument){
                p.writeByte((byte)1);
            } else if (o instanceof BaasFile){
                p.writeByte((byte)2);
            }
            p.writeParcelable(o,0);
        }
    }

    private BaasLink(String id,String label){
        this.id = id;
        this.label = label;
    }

    public BaasObject in(){
        return source;
    }

    public BaasObject out(){
        return destination;
    }

    public String getLabel(){
        return label;
    }

    public String getId(){
        return id;
    }

    public String getAuthor(){ return author;}

    public String getCreationDate(){return creationDate;}

    public long getVersion(){return version;}

    static RequestToken makeLink(String label, String sourceId, String destinationId, int flags, BaasHandler<BaasLink> handler){
        if (sourceId==null||sourceId.length()==0) throw new IllegalArgumentException("invalid source");
        if (destinationId==null||destinationId.length()==0) throw new IllegalArgumentException("invalid destination");
        if (label==null||label.length()==0) throw new IllegalArgumentException("invalid label");
        BaasBox cli = BaasBox.getDefaultChecked();
        Create create = new Create(cli,sourceId,destinationId,label,flags,handler);
        return cli.submitAsync(create);
    }

    static BaasResult<BaasLink> makeLinkSync(String label, String sourceId, String destinationId){
        if (sourceId==null||sourceId.length()==0) throw new IllegalArgumentException("invalid source");
        if (destinationId==null||destinationId.length()==0) throw new IllegalArgumentException("invalid destination");
        if (label==null||label.length()==0) throw new IllegalArgumentException("invalid label");
        BaasBox cli = BaasBox.getDefaultChecked();
        Create create = new Create(cli,sourceId,destinationId,label,RequestOptions.DEFAULT,null);
        return cli.submitSync(create);
    }


    public static BaasResult<BaasLink> createSync(String label,String source,String destination){
        return makeLinkSync(label,source,destination);
    }


    public static BaasResult<BaasLink> createSync(String label,BaasObject source,BaasObject destination){
        return makeLinkSync(label,source.getId(),destination.getId());
    }

    public static RequestToken create(String label,String source,String destination,int flags,BaasHandler<BaasLink> handler){
        return makeLink(label, source, destination, flags, handler);
    }

    public static RequestToken create(String label,BaasObject source,BaasObject destination,int flags,BaasHandler<BaasLink> handler){
        return  makeLink(label,source.getId(),destination.getId(),flags,handler);
    }


    public RequestToken refresh(int flags,BaasHandler<BaasLink> handler){
        if (id==null) throw new IllegalStateException("this link is not stored on the server");
        BaasBox cli = BaasBox.getDefaultChecked();
        Refresh refresh = new Refresh(cli,this,flags,handler);
        return cli.submitAsync(refresh);
    }

    public BaasResult<BaasLink> refreshSync(){
        if (id==null) throw new IllegalStateException("this link is not stored on the server");
        BaasBox cli = BaasBox.getDefaultChecked();
        Refresh refresh = new Refresh(cli,this,RequestOptions.DEFAULT,null);
        return cli.submitSync(refresh);
    }

    public static RequestToken fetch(String id,int flags,BaasHandler<BaasLink> handler){
        if (id == null) throw new IllegalArgumentException("id cannot be null");
        BaasLink link = new BaasLink(id,null);
        return link.refresh(flags,handler);
    }

    public static BaasResult<BaasLink> fetchSync(String id){
        if (id == null) throw new IllegalArgumentException("id cannot be null");
        BaasLink link = new BaasLink(id,null);
        return link.refreshSync();
    }


    public RequestToken delete(int flags,BaasHandler<Void> handler){
        if (id == null) throw new IllegalStateException("this link is not bound on the server");
        BaasBox cli  = BaasBox.getDefaultChecked();
        Delete delete = new Delete(cli,this,flags,handler);
        return cli.submitAsync(delete);
    }

    public BaasResult<Void> deleteSync(){
        if (id==null) throw new IllegalArgumentException("this link is not bound on the server");
        BaasBox cli = BaasBox.getDefaultChecked();
        Delete delete = new Delete(cli,this,RequestOptions.DEFAULT,null);
        return cli.submitSync(delete);
    }


    public static RequestToken fetchAll(String label,BaasQuery.Criteria criteria,int flags,BaasHandler<List<BaasLink>> handler){
        if (label!=null){
            if (criteria == null){
                criteria = BaasQuery.builder().where("label = ?").whereParams(label).criteria();
            } else {
                criteria = criteria.buildUpon().and("label = ?").whereParams(label).criteria();
            }
        }
        return fetchAll(criteria,flags,handler);
    }

    public static BaasResult<List<BaasLink>> fetchAllSync(String label,BaasQuery.Criteria criteria){
        if (label!=null){
            if (criteria == null){
                criteria = BaasQuery.builder().where("label = ?").whereParams(label).criteria();
            } else {
                criteria = criteria.buildUpon().and("label = ?").whereParams(label).criteria();
            }
        }
        return fetchAllSync(criteria);
    }


    public static RequestToken fetchAll(BaasQuery.Criteria criteria,int flags,BaasHandler<List<BaasLink>> handler){
        if (criteria == null){
            criteria = BaasQuery.Criteria.ANY;
        }
        BaasBox cli  = BaasBox.getDefaultChecked();
        FetchAll all = new FetchAll(cli,criteria,flags,handler);
        return cli.submitAsync(all);
    }

    public static BaasResult<List<BaasLink>> fetchAllSync(BaasQuery.Criteria criteria){
        if (criteria == null) {
            criteria = BaasQuery.Criteria.ANY;
        }
        BaasBox cli = BaasBox.getDefaultChecked();
        FetchAll all = new FetchAll(cli,criteria,RequestOptions.DEFAULT,null);
        return cli.submitSync(all);
    }

    public static BaasLink withId(String id) {
        return new BaasLink(id,null);
    }


    private static class FetchAll extends NetworkTask<List<BaasLink>>{
        BaasQuery.Criteria criteria;
        protected FetchAll(BaasBox box,BaasQuery.Criteria criteria, int flags, BaasHandler<List<BaasLink>> handler) {
            super(box, flags, handler, true);
            this.criteria =criteria;
        }

        @Override
        protected List<BaasLink> onOk(int status, HttpResponse response, BaasBox box) throws BaasException {
            JsonArray data = parseJson(response,box).getArray("data");
            ArrayList<BaasLink> ret = new ArrayList<BaasLink>();
            for (Object o:data){
                JsonObject object =(JsonObject)o;
                BaasLink link = new BaasLink(null,null);
                link.update(object);
                ret.add(link);
            }
            return ret;
        }

        @Override
        protected HttpRequest request(BaasBox box) {
            String endpoint = box.requestFactory.getEndpoint("link");
            return box.requestFactory.get(endpoint,criteria.toParams());
        }
    }

    private static class Delete extends NetworkTask<Void>{
        private final BaasLink link;
        private final String id;
        protected Delete(BaasBox box,BaasLink link, int flags, BaasHandler<Void> handler) {
            super(box, flags, handler);
            this.link = link;
            this.id=link.id;
        }

        @Override
        protected Void onOk(int status, HttpResponse response, BaasBox box) throws BaasException {
            link.id =null;
            link.label=null;
            link.version=0;
            link.destination=null;
            link.source=null;
            link.author=null;
            link.creationDate=null;
            return null;
        }

        @Override
        protected HttpRequest request(BaasBox box) {
            String endpoint = box.requestFactory.getEndpoint("link/{}",id);
            return box.requestFactory.delete(endpoint);
        }
    }

    private static class Create extends NetworkTask<BaasLink>{
        private final String label;
        private final String source;
        private final String destination;

        protected Create(BaasBox box,String source,String destination,String label, int flags, BaasHandler<BaasLink> handler) {
            super(box, flags, handler,true);
            this.label=label;
            this.source=source;
            this.destination=destination;
        }

        @Override
        protected BaasLink onOk(int status, HttpResponse response, BaasBox box) throws BaasException {
            JsonObject data = parseJson(response,box).getObject("data");
            BaasLink l = new BaasLink(null,null);
            l.update(data);
            return l;
        }

        @Override
        protected HttpRequest request(BaasBox box) {
            String endpoint = box.requestFactory.getEndpoint("link/{}/{}/{}",source,label,destination);
            return box.requestFactory.post(endpoint);
        }
    }

    private static class Refresh extends NetworkTask<BaasLink>{
        private final BaasLink link;

        protected Refresh(BaasBox box,BaasLink link, int flags, BaasHandler<BaasLink> handler) {
            super(box, flags, handler, true);
            this.link = link;
        }

        @Override
        protected BaasLink onOk(int status, HttpResponse response, BaasBox box) throws BaasException {
            JsonObject data = parseJson(response, box).getObject("data");
            link.update(data);
            return link;
        }

        @Override
        protected HttpRequest request(BaasBox box) {
            String endpoint = box.requestFactory.getEndpoint("link/{}", link.id);
            return box.requestFactory.get(endpoint);
        }
    }

    private void update(JsonObject data) {
        this.label=data.getString("label");
        this.id=data.getString("id");
        this.author=data.getString("_author");
        this.creationDate=data.getString("_creation_date");
        this.version = data.getLong("@version");
        JsonObject in = data.getObject("out");
        JsonObject out = data.getObject("in");
        this.source = parseObject(in);
        this.destination = parseObject(out);
    }

    private BaasObject parseObject(JsonObject object){
        BaasObject ret;
        if (object.contains("@class")){
            ret = new BaasDocument(object);
        } else {
            BaasFile f = new BaasFile();
            f.update(object);
            ret = f;
        }
        return ret;
    }


}




Java Source Code List

com.baasbox.android.AsyncStream.java
com.baasbox.android.BaasACL.java
com.baasbox.android.BaasApiException.java
com.baasbox.android.BaasAssetId.java
com.baasbox.android.BaasAsset.java
com.baasbox.android.BaasBox.java
com.baasbox.android.BaasCancellationException.java
com.baasbox.android.BaasClientException.java
com.baasbox.android.BaasCloudMessagingService.java
com.baasbox.android.BaasCredentialManager.java
com.baasbox.android.BaasDocument.java
com.baasbox.android.BaasException.java
com.baasbox.android.BaasFile.java
com.baasbox.android.BaasHandler.java
com.baasbox.android.BaasIOException.java
com.baasbox.android.BaasInvalidSessionException.java
com.baasbox.android.BaasLink.java
com.baasbox.android.BaasObject.java
com.baasbox.android.BaasQuery.java
com.baasbox.android.BaasResult.java
com.baasbox.android.BaasRuntimeException.java
com.baasbox.android.BaasServerException.java
com.baasbox.android.BaasStream.java
com.baasbox.android.BaasUser.java
com.baasbox.android.Cache.java
com.baasbox.android.DataStreamHandler.java
com.baasbox.android.ExceptionHandler.java
com.baasbox.android.FixedByteArrayOutputStream.java
com.baasbox.android.Grant.java
com.baasbox.android.HttpUrlConnectionClient.java
com.baasbox.android.JsonWrapper.java
com.baasbox.android.NetworkTask.java
com.baasbox.android.Plugin.java
com.baasbox.android.RequestFactory.java
com.baasbox.android.RequestOptions.java
com.baasbox.android.RequestToken.java
com.baasbox.android.RestImpl.java
com.baasbox.android.Rest.java
com.baasbox.android.Role.java
com.baasbox.android.SaveMode.java
com.baasbox.android.StreamBody.java
com.baasbox.android.StreamRequest.java
com.baasbox.android.impl.Base64.java
com.baasbox.android.impl.Constants.java
com.baasbox.android.impl.DiskLruCache.java
com.baasbox.android.impl.Dispatcher.java
com.baasbox.android.impl.GCMWrapper.java
com.baasbox.android.impl.ImmediateDispatcher.java
com.baasbox.android.impl.Logger.java
com.baasbox.android.impl.StrictLineReader.java
com.baasbox.android.impl.Task.java
com.baasbox.android.impl.Util.java
com.baasbox.android.impl.package-info.java
com.baasbox.android.json.JsonArray.java
com.baasbox.android.json.JsonException.java
com.baasbox.android.json.JsonObject.java
com.baasbox.android.json.JsonReader.java
com.baasbox.android.json.JsonScope.java
com.baasbox.android.json.JsonStructure.java
com.baasbox.android.json.JsonToken.java
com.baasbox.android.json.JsonWriter.java
com.baasbox.android.json.StringPool.java
com.baasbox.android.json.package-info.java
com.baasbox.android.net.HttpRequest.java
com.baasbox.android.net.OkClient.java
com.baasbox.android.net.RestClient.java
com.baasbox.android.plugins.glide.BaasAssetModelLoader.java
com.baasbox.android.plugins.glide.BaasFileModelLoader.java
com.baasbox.android.plugins.glide.BaasImageFetcher.java
com.baasbox.android.plugins.glide.GlidePlugin.java
com.baasbox.android.samples.aloa.Aloa.java
com.baasbox.android.samples.aloa.ApplicationTest.java
com.baasbox.android.samples.aloa.Configuration.java
com.baasbox.android.samples.aloa.Data.java
com.baasbox.android.samples.aloa.activities.CreateChannelFragment.java
com.baasbox.android.samples.aloa.activities.LoginActivity.java
com.baasbox.android.samples.aloa.activities.MainActivity.java
com.baasbox.android.samples.aloa.activities.OnTargetSelectedListener.java
com.baasbox.android.samples.aloa.activities.SendMessageActivity.java
com.baasbox.android.samples.aloa.activities.SendTargetFragment.java
com.baasbox.android.samples.aloa.activities.UserStatusFragment.java
com.baasbox.android.samples.aloa.activities.loaders.BaasUsersLoader.java
com.baasbox.android.samples.aloa.activities.loaders.ChannelsLoader.java
com.baasbox.android.samples.aloa.receive.NewMessageNotification.java
com.baasbox.android.samples.aloa.receive.PushReceiver.java
com.baasbox.android.samples.aloa.utils.BaasLoader.java
com.baasbox.android.samples.aloa.utils.BaseActivity.java
com.baasbox.android.samples.aloa.utils.BaseBaasLoader.java
com.baasbox.android.samples.aloa.utils.BaseFragment.java
com.baasbox.android.samples.phrasebook.Configuration.java
com.baasbox.android.samples.phrasebook.Phrasebook.java
com.baasbox.android.samples.phrasebook.ServerContract.java
com.baasbox.android.samples.phrasebook.common.BaseActivity.java
com.baasbox.android.samples.phrasebook.common.BaseFragment.java
com.baasbox.android.samples.phrasebook.loaders.BaasDocumentsLoader.java
com.baasbox.android.samples.phrasebook.ui.HomeActivity.java
com.baasbox.android.samples.phrasebook.ui.PhraseOfTheDay.java
com.baasbox.android.samples.phrasebook.utils.ThreadLocalRandom.java
com.baasbox.android.package-info.java