com.echopf.contents.ECHOContentsCategoryObject.java Source code

Java tutorial

Introduction

Here is the source code for com.echopf.contents.ECHOContentsCategoryObject.java

Source

/*******
 Copyright 2015 NeuroBASE,Inc. All Rights Reserved.
     
 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.echopf.contents;

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

import com.echopf.*;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * {@.en An ECHOContentsCategoryObject is a particular category object.}
 * {@.ja {@link com.echopf.contents.ECHOContentsCategoriesMap}???}
 */
public class ECHOContentsCategoryObject extends ECHODataObject<ECHOContentsCategoryObject>
        implements Fetchable<ECHOContentsCategoryObject>, Pushable<ECHOContentsCategoryObject>,
        Deletable<ECHOContentsCategoryObject>, TreeNodeable<ECHOContentsCategoryObject> {

    private ECHOContentsCategoryObject newParent = null;

    /* Begin constructors */

    /**
     * {@.en Constructs a new ECHOContentsCategoryObject.}
     * {@.ja ?????????}
     * 
     * @param instanceId
     *       {@.en the reference ID of the instance to which this object belongs}
     *       {@.ja ?????ID}
     */
    public ECHOContentsCategoryObject(String instanceId) {
        super(instanceId, "categories");
    }

    /**
     * {@.en Constructs a new ECHOContentsCategoryObject based on an existing one on the remote server.}
     * {@.ja ????????}
     * 
     * @param instanceId
     *       {@.en the reference ID of the instance to which this object has belonged}
     *       {@.ja ???ID}
     * @param refid
     *       {@.en the reference ID of the existing one}
     *       {@.ja ?ID}
     */
    public ECHOContentsCategoryObject(String instanceId, String refid) {
        super(instanceId, "categories", refid);
    }

    /**
     * Constructs a new ECHOContentsCategoryObject based on an existing one on the remote server.
     * @param instanceId the reference ID of the instance to which this object has belonged
     * @param refid the reference ID of the existing one
     * @param source : a source JSONObject to copy
     */
    public ECHOContentsCategoryObject(String instanceId, String refid, JSONObject source) {
        this(instanceId, refid);
        copyData(source);
    }

    /* End constructors */

    /*
     * Implement Fetchable
     * @see com.echopf.Fetchable#fetch()
     */
    public ECHOContentsCategoryObject fetch() throws ECHOException {
        doFetch(true, null);
        return this;
    }

    /*
     * Implement Fetchable
     * @see com.echopf.Fetchable#fetchInBackground()
     */
    public void fetchInBackground(FetchCallback<ECHOContentsCategoryObject> callback) {
        try {
            doFetch(false, callback);
        } catch (ECHOException e) {
            throw new InternalError();
        }
    }

    /*
     * Implement Pushable
     * @see com.echopf.Pushable#push()
     */
    public ECHOContentsCategoryObject push() throws ECHOException {
        doPush(true, null);
        return this;
    }

    /*
     * Implement Pushable
     * @see com.echopf.Pushable#pushInBackground()
     */
    public void pushInBackground(PushCallback<ECHOContentsCategoryObject> callback) {
        try {
            doPush(false, callback);
        } catch (ECHOException e) {
            throw new InternalError();
        }
    }

    /*
     * Implement Deleteable
     * @see com.echopf.Deleteable#delete()
     */
    public ECHOContentsCategoryObject delete() throws ECHOException {
        doDelete(true, null);
        return this;
    }

    /*
     * Implement Deleteable
     * @see com.echopf.Deleteable#deleteInBackground()
     */
    public void deleteInBackground(DeleteCallback<ECHOContentsCategoryObject> callback) {
        try {
            doDelete(false, callback);
        } catch (ECHOException e) {
            throw new InternalError();
        }
    }

    /*
     * Implement TreeNodeable
     * @see com.echopf.TreeNodeable#setNewParent()
     */
    public void setNewParent(ECHOContentsCategoryObject newParent) {
        this.newParent = newParent;
    }

    @Override
    protected JSONObject buildRequestContents() {
        JSONObject obj = super.buildRequestContents();

        try {

            // parent_refid
            obj.remove("parent_refid");
            if (this.newParent != null) {
                String new_parent_refid = this.newParent.getRefid();

                if (!new_parent_refid.isEmpty()) {
                    obj.put("parent_refid", new_parent_refid);
                    this.newParent = null;
                }
            }

        } catch (JSONException e) {
            throw new RuntimeException(e);
        }

        return obj;
    }

    @Override
    protected void copyData(JSONObject source) {
        if (source == null)
            throw new IllegalArgumentException("Argument `source` must not be null.");

        JSONArray categories = source.optJSONArray("categories");

        JSONObject category = null;
        if (categories != null) { // if the data is a tree format
            category = categories.optJSONObject(0);
        } else { // the data is a category object
            category = source;
        }

        if (category == null)
            return; //skip

        category.remove("children"); // remove children

        // super
        super.copyData(category);
    }

    /* Begin Parcel methods */

    public static final Parcelable.Creator<ECHOContentsCategoryObject> CREATOR = new Parcelable.Creator<ECHOContentsCategoryObject>() {
        public ECHOContentsCategoryObject createFromParcel(Parcel in) {
            return new ECHOContentsCategoryObject(in);
        }

        public ECHOContentsCategoryObject[] newArray(int size) {
            return new ECHOContentsCategoryObject[size];
        }
    };

    public ECHOContentsCategoryObject(Parcel in) {
        super(in);
    }

    /* End Parcel methods */
}