/*
 * blancoIg
 * Copyright (C) 2004-2005 Yasuo Nakanishi
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 */
package blanco.struts.expander.action;

import blanco.ig.expander.Scope;
import blanco.ig.expander.Type;
import blanco.ig.expander.Value;
import blanco.ig.expander.method.AbstractMethod;
import blanco.struts.definition.form.Form;
import blanco.struts.definition.form.FormEvent;

/**
 * @author Yasuo Nakanishi
 */
public class EventMethod extends AbstractMethod {
    private Type _formType = null;

    private Form _form = null;

    private FormEvent _event = null;

    public EventMethod(FormEvent event, Type formType, Form form) {
        super(createEventName(event.getName()));
        _event = event;
        _formType = formType;
        _form = form;
    }

    public static String createEventName(String event) {
        String result = "";
        if (event.equals("init")) {
            result = event;
        } else if (event.equals("load")) {
            result = "onLoad";
        } else {
            result = "onClick" + getNameAdjuster().toTitleCase(event);
        }
        return result;
    }

    public void setupSignature() {

        setScope(Scope.PROTECTED);

        if (getName().equals("init")) {
            setReturnType(new Type(void.class));
        } else {
            setReturnType(new Type("org.apache.struts.action.ActionForward"));
        }

        getJavaDoc().addParameter("mapping", "ActionMappingオブジェクト。");
        addArgument(new Value(
                new Type("org.apache.struts.action.ActionMapping"), "mapping"));
        // フォームだけは自分が関連する具象クラスをセットします。
        getJavaDoc().addParameter("form",
                "Strutsから渡される Form [" + _formType.getFullName() + "] オブジェクト。");
        addArgument(new Value(_formType, "form"));
        getJavaDoc().addParameter("request", "Servletから得られるrequestオブジェクト。");
        addArgument(new Value(
                new Type("javax.servlet.http.HttpServletRequest"), "request"));
        getJavaDoc().addParameter("response", "Servletから得られるresponseオブジェクト。");
        addArgument(new Value(
                new Type("javax.servlet.http.HttpServletResponse"), "response"));
        if (_form.isUsingDb()) {
            getJavaDoc().addParameter("connection", "データベース接続オブジェクト。");
            addArgument(new Value(new Type("java.sql.Connection"), "connection"));
        }

        addException(Exception.class);

        String doc = "";
        if (!_event.getDescription().equals("")) {
            doc = _event.getDescription() + "イベントを処理するためのメソッド。";
        } else {
            doc = getName() + " イベントを処理するためのメソッド。";
        }
        getJavaDoc().addLine(doc);

        getJavaDoc().addLine("このメソッドは抽象メソッドです。実際にはサブクラス側で実装してください。");
    }

    public static String getParameterName(Class parameter) {
        String className = new Type(parameter).getName();
        String[] names = className.replaceAll("[A-Z]", " $0").split("\\s");
        return names[names.length - 1].toLowerCase();
    }

}
