com.sepgil.ral.CreateNodeTask.java Source code

Java tutorial

Introduction

Here is the source code for com.sepgil.ral.CreateNodeTask.java

Source

package com.sepgil.ral;

/**
 * This file is part of RestWS Android Library (RAL).
 * RAL is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * RAL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with RAL.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.UnsupportedEncodingException;

import org.apache.http.StatusLine;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

/**
 * Creates a new node.
 * @author Sebastian Gilits
 *
 */
public class CreateNodeTask extends DrupalOperationTask {
    public enum ErrorState {
        ERROR_PARSE, ERROR_HTTP, ERROR_WRONG_RESOURCE
    }

    private OnNodeCreatedListener mListener = null;
    private Node mNode;

    /**
     * Constructor.
     * @param node which should be updated.
     * @throws JSONException
     */
    public CreateNodeTask(Node node) throws JSONException {
        mNode = node;
    }

    /**
     * Register a callback to be invoked when the node was created (or not).
     * @param listener The callback that will run.
     */
    public void setOnNodeCreatedListener(OnNodeCreatedListener listener) {
        mListener = listener;
    }

    @Override
    public String getPath() {
        return "/node";
    }

    @Override
    public HttpRequestBase getHttpRequest(String url) {
        HttpPut put = new HttpPut(url);

        try {
            StringEntity enity = new StringEntity(mNode.getJSON());
            put.setEntity(enity);
        } catch (JSONException e) {
            mListener.onError(ErrorState.ERROR_PARSE);
        } catch (UnsupportedEncodingException e) {
            mListener.onError(ErrorState.ERROR_PARSE);
        }
        return put;
    }

    @Override
    public void onResponse(JSONObject response) {
        if (mListener == null)
            return;
        try {
            if (response.getString("resource").equals("node")) {
                mListener.onNodeCreated(response.getInt("id"));
            } else {
                mListener.onError(ErrorState.ERROR_WRONG_RESOURCE);
            }
        } catch (JSONException e) {
            mListener.onError(ErrorState.ERROR_PARSE);
        }
    }

    @Override
    public void onJSONError(JSONException e) {
        if (mListener == null)
            return;
        mListener.onError(ErrorState.ERROR_PARSE);
    }

    @Override
    public void onHttpError(StatusLine statusLine) {
        if (mListener == null)
            return;
        mListener.onError(ErrorState.ERROR_HTTP);
        Log.e("RESTWS_HTTP", statusLine.toString());
    }

    @Override
    public void onParserError(Exception e) {
        if (mListener == null)
            return;
        mListener.onError(ErrorState.ERROR_PARSE);
    }

    /**
     * Interface definition for a callback to be invoked when the node was updated.
     * @author Sebastian Gilits
     *
     */
    public interface OnNodeCreatedListener {
        /**
         * The node was created.
         * @param nid The node id that was generated by Drupal.
         */
        public void onNodeCreated(int nid);

        /**
         * Called when there was an error while trying to create the node.
         * @param error
         */
        public void onError(ErrorState error);
    }
}