Android Open Source - Android-SDK Baas Object






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. BaasBox/*from   ww  w.j a v  a2s .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.Parcelable;

import com.baasbox.android.json.JsonObject;
import com.baasbox.android.net.HttpRequest;
import org.apache.http.HttpResponse;


/**
 * Base class for remote resources that can be saved and retrieved from
 * BaasBox.
 *
 * @author Andrea Tortorella
 * @since 0.7.3
 */
public abstract class BaasObject implements Parcelable {

// --------------------------- CONSTRUCTORS ---------------------------
    // todo this should provide common interface among remote objects
    //      such as dirty tracking timestamps ecc
    BaasObject() {
    }

// -------------------------- OTHER METHODS --------------------------

    /**
     * The username of the owner of this object, or null if this object is new.
     *
     * @return a username as String or null
     */
    public abstract String getAuthor();

    /**
     * The date this object was created on the server, or null if the object is new.
     *
     * @return a string representation of a date.
     */
    public abstract String getCreationDate();

    /**
     * The version number of this object.
     *
     * @return a long version number
     */
    public abstract long getVersion();


//    public abstract JsonObject toJson();

    /**
     * Asynchronously grants the access <code>grant</code> to this object to <code>username</code>.
     * The outcome of the request is handed to the provided <code>handler</code>.
     *
     * @param grant    a non null {@link com.baasbox.android.Grant}
     * @param username a non null username
     * @param handler  an handler that will receive the outcome of the request.
     * @return a {@link com.baasbox.android.RequestToken} to manage the asynchronous request
     */
    public final RequestToken grant(Grant grant, String username, BaasHandler<Void> handler) {
        return grant(grant, username, RequestOptions.DEFAULT, handler);
    }

    /**
     * Asynchronously grants the access <code>grant</code> to this object to users with <code>role</code>.
     * The outcome of the request is handed to the provided <code>handler</code>.
     *
     * @param grant   a non null {@link com.baasbox.android.Grant}
     * @param role    a non null username
     * @param handler an handler that will receive the outcome of the request.
     * @return a {@link com.baasbox.android.RequestToken} to manage the asynchronous request
     */
    public final RequestToken grantAll(Grant grant, String role, BaasHandler<Void> handler) {
        return grantAll(grant, role, RequestOptions.DEFAULT, handler);
    }

    /**
     * Synchronously grants the acces <code>grant</code> to this object to all users in <code>role</code>
     *
     * @param grant a non null {@link com.baasbox.android.Grant}
     * @param role  a non null role
     * @return the outcome of the request wrapped in a {@link com.baasbox.android.BaasResult}
     */
    public abstract BaasResult<Void> grantAllSync(Grant grant, String role);

    /**
     * Synchronously grants the access <code>grant</code> to this object to <code>username</code>.
     *
     * @param grant a non null {@link com.baasbox.android.Grant}
     * @param user  a non null username
     * @return the outcome of the request wrapped in a {@link com.baasbox.android.BaasResult}
     */
    public abstract BaasResult<Void> grantSync(Grant grant, String user);

    /**
     * Returns true if this object has no conunterpart on the BaasBox server.
     *
     * @return true if this object has no conunterpart on the BaasBox server
     */
    public final boolean isNew() {
        return getId() == null;
    }

    public abstract boolean isDirty();

    /**
     * The id of this object on the server. New objects created locally have no id.
     *
     * @return a uuid as a String or null.
     */
    public abstract String getId();

    /**
     * Asynchronously revoke the access <code>grant</code> to this object from <code>username</code>.
     * The outcome of the request is handed to the provided <code>handler</code>.
     *
     * @param grant    a non null {@link com.baasbox.android.Grant}
     * @param username a non null username
     * @param handler  an handler that will receive the outcome of the request.
     * @return a {@link com.baasbox.android.RequestToken} to manage the asynchronous request
     */
    public final RequestToken revoke(Grant grant, String username, BaasHandler<Void> handler) {
        return grant(grant, username, RequestOptions.DEFAULT, handler);
    }

    /**
     * Asynchronously grants the access <code>grant</code> to this object to <code>username</code>.
     * The outcome of the request is handed to the provided <code>handler</code>.
     *
     * @param grant    a non null {@link com.baasbox.android.Grant}
     * @param username a non null username
     * @param flags    {@link RequestOptions}
     * @param handler  an handler that will receive the outcome of the request.
     * @return a {@link com.baasbox.android.RequestToken} to manage the asynchronous request
     */
    public abstract RequestToken grant(Grant grant, String username, int flags, BaasHandler<Void> handler);

    /**
     * Asynchronously revoke the access <code>grant</code> to this object to <code>username</code>.
     * The outcome of the request is handed to the provided <code>handler</code>.
     *
     * @param grant    a non null {@link com.baasbox.android.Grant}
     * @param username a non null username
     * @param flags    {@link RequestOptions}
     * @param handler  an handler that will receive the outcome of the request.
     * @return a {@link com.baasbox.android.RequestToken} to manage the asynchronous request
     */
    public abstract RequestToken revoke(Grant grant, String username, int flags, BaasHandler<Void> handler);


    /**
     * Asynchronously revokes the access <code>grant</code> to this object from users with <code>role</code>.
     * The outcome of the request is handed to the provided <code>handler</code>.
     *
     * @param grant   a non null {@link com.baasbox.android.Grant}
     * @param role    a non null username
     * @param handler an handler that will receive the outcome of the request.
     * @return a {@link com.baasbox.android.RequestToken} to manage the asynchronous request
     */
    public final RequestToken revokeAll(Grant grant, String role, BaasHandler<Void> handler) {
        return grantAll(grant, role, RequestOptions.DEFAULT, handler);
    }

    /**
     * Asynchronously grants the access <code>grant</code> to this object to users with <code>role</code>.
     * The outcome of the request is handed to the provided <code>handler</code>.
     *
     * @param grant    a non null {@link com.baasbox.android.Grant}
     * @param role     a non null username
     * @param flags    {@link RequestOptions}
     * @param handler  an handler that will receive the outcome of the request.
     * @return a {@link com.baasbox.android.RequestToken} to manage the asynchronous request
     */
    public abstract RequestToken grantAll(Grant grant, String role, int flags, BaasHandler<Void> handler);

    /**
     * Asynchronously revokes the access <code>grant</code> to this object from users with <code>role</code>.
     * The outcome of the request is handed to the provided <code>handler</code>.
     *
     * @param grant    a non null {@link com.baasbox.android.Grant}
     * @param role     a non null username
     * @param flags    {@link RequestOptions}
     * @param handler  an handler that will receive the outcome of the request.
     * @return a {@link com.baasbox.android.RequestToken} to manage the asynchronous request
     */
    public abstract RequestToken revokeAll(Grant grant, String role, int flags, BaasHandler<Void> handler);

    /**
     * Synchronously revokes the acces <code>grant</code> to this object from all users in <code>role</code>
     *
     * @param grant a non null {@link com.baasbox.android.Grant}
     * @param role  a non null role
     * @return the outcome of the request wrapped in a {@link com.baasbox.android.BaasResult}
     */
    public abstract BaasResult<Void> revokeAllSync(Grant grant, String role);

    /**
     * Synchronously revokes the access <code>grant</code> to this object from <code>username</code>.
     *
     * @param grant    a non null {@link com.baasbox.android.Grant}
     * @param username a non null username
     * @return the outcome of the request wrapped in a {@link com.baasbox.android.BaasResult}
     */
    public abstract BaasResult<Void> revokeSync(Grant grant, String username);

    public final BaasDocument asDocument(){
        return (BaasDocument)this;
    }

    public abstract boolean isDocument();

    public final BaasFile asFile(){
        return (BaasFile)this;
    }

    public abstract boolean isFile();


// -------------------------- INNER CLASSES --------------------------

    abstract static class Access extends NetworkTask<Void> {
        private final boolean isRole;
        private final boolean add;
        private final Grant grant;
        private final String id;
        private final String collection;
        private final String to;

        protected Access(BaasBox box, boolean add, boolean isRole, String collection, String id, String to, Grant grant, int  flags, BaasHandler<Void> handler) {
            super(box, flags, handler);
            this.isRole = isRole;
            this.add = add;
            this.grant = grant;
            this.id = id;
            this.to = to;

            this.collection = collection;
        }

        @Override
        protected Void onOk(int status, HttpResponse response, BaasBox box) throws BaasException {
            return null;
        }

        @Override
        protected final HttpRequest request(BaasBox box) {
            String endpoint;
            RequestFactory factory = box.requestFactory;
            if (isRole) {
                endpoint = roleGrant(factory, grant, collection, id, to);
            } else {
                endpoint = userGrant(factory, grant, collection, id, to);
            }
            if (add) {
                return factory.put(endpoint);
            } else {
                return factory.delete(endpoint);
            }
        }

        protected abstract String userGrant(RequestFactory factory, Grant grant, String collection, String id, String to);

        protected abstract String roleGrant(RequestFactory factory, Grant grant, String collection, String id, String to);
    }
}




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