/*
 * Copyright 2004,2005 The Apache Software Foundation.
 *
 * 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.
 */

#include <axis2_svc_skeleton.h>
#include <axis2_log_default.h>
#include <axis2_error_default.h>
#include <axiom_text.h>
#include <axiom_node.h>
#include <axiom_element.h>
#include <axis2_array_list.h>
#include <stdio.h>

axiom_node_t *axis2_hello_greet(const axis2_env_t *env,
        axiom_node_t *node);

int AXIS2_CALL
hello_free(axis2_svc_skeleton_t *svc_skeleton,
        const axis2_env_t *env);

axiom_node_tAXIS2_CALL
hello_invoke(axis2_svc_skeleton_t *svc_skeleton,
        const axis2_env_t *env,
        axiom_node_t *node,
        axis2_msg_ctx_t *msg_ctx);


int AXIS2_CALL
hello_init(axis2_svc_skeleton_t *svc_skeleton,
        const axis2_env_t *env);

axiom_node_tAXIS2_CALL
hello_on_fault(axis2_svc_skeleton_t *svc_skeli,
        const axis2_env_t *envaxiom_node_t *node);


axiom_node_t *
build_greeting_response(const axis2_env_t *env
        axis2_char_t *greeting);

axiom_node_t *
axis2_hello_greet(const axis2_env_t *envaxiom_node_t *node)
{
    axiom_node_t *client_greeting_node = NULL;
    axiom_node_t *return_node = NULL;

    AXIS2_ENV_CHECK(envNULL);

    if (node)
    {
        client_greeting_node = AXIOM_NODE_GET_FIRST_CHILD(nodeenv);
        if (client_greeting_node &&
                AXIOM_NODE_GET_NODE_TYPE(client_greeting_nodeenv) == AXIOM_TEXT)
        {
            axiom_text_t *greeting = (axiom_text_t *)AXIOM_NODE_GET_DATA_ELEMENT(client_greeting_nodeenv);
            if (greeting && AXIOM_TEXT_GET_VALUE(greeting , env))
            {
                axis2_char_t *greeting_str = AXIOM_TEXT_GET_VALUE(greetingenv);
                printf("Client greeted saying \"%s\" \n"greeting_str);
                return_node = build_greeting_response(env"Hello Client!");
            }
        }
    }
    else
    {
        AXIS2_ERROR_SET(env->errorAXIS2_ERROR_SVC_SKEL_INVALID_XML_FORMAT_IN_REQUESTAXIS2_FAILURE);
        printf("ERROR: invalid XML in request\n");
        return_node = build_greeting_response(env"Client! Who are you?");
    }

    return return_node;
}

axiom_node_t *
build_greeting_response(const axis2_env_t *envaxis2_char_t *greeting)
{
    axiom_node_tgreeting_om_node = NULL;
    axiom_element_t * greeting_om_ele = NULL;

    greeting_om_ele = axiom_element_create(envNULL"greetResponse"NULL, &greeting_om_node);

    AXIOM_ELEMENT_SET_TEXT(greeting_om_eleenvgreetinggreeting_om_node);

    return greeting_om_node;
}


axis2_svc_skeleton_t *
axis2_hello_create(const axis2_env_t *env)
{
    axis2_svc_skeleton_t *svc_skeleton = NULL;
    svc_skeleton = AXIS2_MALLOC(env->allocator,
            sizeof(axis2_svc_skeleton_t));

    svc_skeleton->ops = AXIS2_MALLOC(
                env->allocatorsizeof(axis2_svc_skeleton_ops_t));

    svc_skeleton->func_array = NULL;
    svc_skeleton->ops->free = hello_free;
    svc_skeleton->ops->init = hello_init;
    svc_skeleton->ops->invoke = hello_invoke;
    svc_skeleton->ops->on_fault = hello_on_fault;

    return svc_skeleton;
}

int AXIS2_CALL
hello_init(axis2_svc_skeleton_t *svc_skeleton,
        const axis2_env_t *env)
{
    svc_skeleton->func_array = axis2_array_list_create(env0);
    AXIS2_ARRAY_LIST_ADD(svc_skeleton->func_arrayenv"helloString");
    return AXIS2_SUCCESS;
}

axiom_node_tAXIS2_CALL
hello_invoke(axis2_svc_skeleton_t *svc_skeleton,
        const axis2_env_t *env,
        axiom_node_t *node,
        axis2_msg_ctx_t *msg_ctx)
{
    return axis2_hello_greet(envnode);
}

axiom_node_tAXIS2_CALL
hello_on_fault(axis2_svc_skeleton_t *svc_skeli,
        const axis2_env_t *envaxiom_node_t *node)
{
    axiom_node_t *error_node = NULL;
    axiom_node_ttext_node = NULL;
    axiom_element_t *error_ele = NULL;
    error_ele = axiom_element_create(envnode"EchoServiceError"NULL,
            &error_node);
    AXIOM_ELEMENT_SET_TEXT(error_eleenv"Echo service failed ",
            text_node);
    return error_node;
}

int AXIS2_CALL
hello_free(axis2_svc_skeleton_t *svc_skeleton,
        const axis2_env_t *env)
{
    if (svc_skeleton->func_array)
    {
        AXIS2_ARRAY_LIST_FREE(svc_skeleton->func_arrayenv);
        svc_skeleton->func_array = NULL;
    }

    if (svc_skeleton->ops)
    {
        AXIS2_FREE(env->allocatorsvc_skeleton->ops);
        svc_skeleton->ops = NULL;
    }

    if (svc_skeleton)
    {
        AXIS2_FREE(env->allocatorsvc_skeleton);
        svc_skeleton = NULL;
    }

    return AXIS2_SUCCESS;
}


AXIS2_EXPORT int
axis2_get_instance(axis2_svc_skeleton_t **inst,
        const axis2_env_t *env)
{
    *inst = axis2_hello_create(env);
    if (!(*inst))
    {
        return AXIS2_FAILURE;
    }

    return AXIS2_SUCCESS;
}

AXIS2_EXPORT int
axis2_remove_instance(axis2_svc_skeleton_t *inst,
        const axis2_env_t *env)
{
    axis2_status_t status = AXIS2_FAILURE;
    if (inst)
    {
        status = AXIS2_SVC_SKELETON_FREE(instenv);
    }
    return status;
}