/**
 * adb_Predicate.c
 *
 * This file was auto-generated from WSDL schema
 * by the Apache Axis2/C Native version: 1.0.0
 */

#include "adb_Predicate.h"

struct adb_Predicate {
    axutil_array_list_t* nodes;
};

adb_Predicate_t* AXIS2_CALL
adb_Predicate_create(const axutil_env_t *env)
{
    adb_Predicate_t *obj = NULL;
    AXIS2_ENV_CHECK(env, NULL);
    obj = AXIS2_MALLOC(env->allocator, sizeof(adb_Predicate_t));
    if (!obj) {
        AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE);
        return NULL;
    }
    memset(obj, 0, sizeof(adb_Predicate_t));
    return obj;
}

axis2_status_t AXIS2_CALL
adb_Predicate_free(adb_Predicate_t* _this, const axutil_env_t *env)
{
    AXIS2_ENV_CHECK(env, AXIS2_FAILURE);
    if (!_this) {
        return AXIS2_SUCCESS;
    }
    if (_this->nodes) {
        int _arr_i, _arr_size = axutil_array_list_size(_this->nodes, env);
        for (_arr_i = 0; _arr_i < _arr_size; _arr_i++) {
            void *_arr_item = axutil_array_list_get(_this->nodes, env, _arr_i);
            if (_arr_item) {
                AXIS2_FREE(env->allocator, _arr_item);
            }
        }
        axutil_array_list_free(_this->nodes, env);
    }
    AXIS2_FREE(env->allocator, _this);
    return AXIS2_SUCCESS;
}

axutil_array_list_t* AXIS2_CALL
adb_Predicate_get_nodes(adb_Predicate_t* _this, const axutil_env_t *env)
{
    AXIS2_ENV_CHECK(env, NULL);
    return _this ? _this->nodes : NULL;
}

axis2_status_t AXIS2_CALL
adb_Predicate_set_nodes(adb_Predicate_t* _this, const axutil_env_t *env, axutil_array_list_t* value)
{
    AXIS2_ENV_CHECK(env, AXIS2_FAILURE);
    AXIS2_PARAM_CHECK(env->error, _this, AXIS2_FAILURE);
    _this->nodes = value;
    return AXIS2_SUCCESS;
}

axis2_status_t AXIS2_CALL
adb_Predicate_add_nodes(adb_Predicate_t* _this, const axutil_env_t *env, axis2_char_t* value)
{
    AXIS2_ENV_CHECK(env, AXIS2_FAILURE);
    AXIS2_PARAM_CHECK(env->error, _this, AXIS2_FAILURE);
    if (!_this->nodes) {
        _this->nodes = axutil_array_list_create(env, 4);
        if (!_this->nodes) {
            AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE);
            return AXIS2_FAILURE;
        }
    }
    return axutil_array_list_add(_this->nodes, env, value);
}

axis2_char_t* AXIS2_CALL
adb_Predicate_get_nodes_at(adb_Predicate_t* _this, const axutil_env_t *env, int index)
{
    AXIS2_ENV_CHECK(env, NULL);
    if (!_this || !_this->nodes) return NULL;
    return (axis2_char_t*)axutil_array_list_get(_this->nodes, env, index);
}

int AXIS2_CALL
adb_Predicate_sizeof_nodes(adb_Predicate_t* _this, const axutil_env_t *env)
{
    AXIS2_ENV_CHECK(env, 0);
    if (!_this || !_this->nodes) return 0;
    return axutil_array_list_size(_this->nodes, env);
}

axiom_node_t* AXIS2_CALL
adb_Predicate_serialize(adb_Predicate_t* _this, const axutil_env_t *env,
                axiom_node_t* parent, axiom_element_t *parent_element,
                int parent_tag_closed, axutil_hash_t* namespaces,
                int *next_ns_index)
{
    axiom_node_t *current_node = NULL;
    axiom_element_t *current_element = NULL;
    axiom_namespace_t *ns = NULL;
    axis2_char_t *text_value = NULL;
    axis2_char_t tmp_buf[64];

    AXIS2_ENV_CHECK(env, NULL);
    AXIS2_PARAM_CHECK(env->error, _this, NULL);

    /* Serialize nodes */
    if (_this->nodes) {
        int _ser_i, _ser_size = axutil_array_list_size(_this->nodes, env);
        for (_ser_i = 0; _ser_i < _ser_size; _ser_i++) {
            axis2_char_t *_arr_item = (axis2_char_t*)axutil_array_list_get(_this->nodes, env, _ser_i);
            if (_arr_item) {
                ns = axiom_namespace_create(env, "", NULL);
                current_element = axiom_element_create(env, parent, "nodes", ns, &current_node);
                if (current_element) {
                    axiom_element_set_text(current_element, env, _arr_item, current_node);
                }
            }
        }
    }

    return parent;
}

adb_Predicate_t* AXIS2_CALL
adb_Predicate_create_from_node(const axutil_env_t *env, axiom_node_t *node,
                       int dont_care_minoccurs)
{
    adb_Predicate_t *obj = NULL;
    axiom_node_t *current_node = NULL;
    axiom_element_t *current_element = NULL;
    const axis2_char_t *text_value = NULL;

    AXIS2_ENV_CHECK(env, NULL);

    obj = adb_Predicate_create(env);
    if (!obj) {
        return NULL;
    }

    /* Iterate through child elements */
    current_node = axiom_node_get_first_child(node, env);
    while (current_node) {
        if (axiom_node_get_node_type(current_node, env) == AXIOM_ELEMENT) {
            current_element = (axiom_element_t*)axiom_node_get_data_element(current_node, env);
            if (current_element) {
                const axis2_char_t *local_name = axiom_element_get_localname(current_element, env);
                if (axutil_strcmp(local_name, "nodes") == 0) {
                    text_value = axiom_element_get_text(current_element, env, current_node);
                    if (text_value) {
                        if (!obj->nodes) {
                            obj->nodes = axutil_array_list_create(env, 4);
                        }
                        if (obj->nodes) {
                            axutil_array_list_add(obj->nodes, env, axutil_strdup(env, text_value));
                        }
                    }
                }
            }
        }
        current_node = axiom_node_get_next_sibling(current_node, env);
    }

    /* AXIS2C-936: Verify XML was completely parsed */
    if (!axiom_node_is_complete(node, env)) {
        AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "XML node is not complete, possible truncated response");
        adb_Predicate_free(obj, env);
        return NULL;
    }

    return obj;
}
