/*
 * 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.form;

import java.util.Iterator;

import blanco.ig.expander.NameAdjuster;
import blanco.ig.expander.Type;
import blanco.ig.expander.Value;
import blanco.struts.definition.form.FieldValidation;
import blanco.struts.definition.form.FormField;
import blanco.struts.definition.form.FormTable;
import blanco.struts.expander.TypeFactory;
import blanco.struts.expander.form.base.BaseEventValidationMethod;
import blanco.struts.runtime.validator.Validator;
import blanco.struts.runtime.validator.message.ErrorMessageKey;

/**
 * @author Yasuo Nakanishi
 */
public class ValidateFormatEventMethod extends BaseEventValidationMethod {

    public ValidateFormatEventMethod(String event, Iterator eventFields,
            Iterator tableIterator, TypeFactory typeFactory) {
        super(createName(event), event, eventFields, tableIterator, typeFactory);
    }

    public static String createName(String event) {
        NameAdjuster na = getNameAdjuster();
        return createName("validate", "FormatOn" + na.toTitleCase(event));
    }

    public void setupSignature() {
        super.setupSignature();

        String doc = "";

        doc = getName() + "イベントフォーマット検証メソッド。";
        getJavaDoc().addLine(doc);

        doc = "このメソッドはActionクラスから呼び出してください。";
        getJavaDoc().addLine(doc);
    }

    protected void implemntFieldValidation(FormField f, Value result) {
        Value fieldValue = getField(f.getValue().getName());

        Type keyType = getRuntimeType(ErrorMessageKey.class);

        boolean formatCheck = f.getValidation().isCheckFormat();
        if (formatCheck) {
            getImplementor().checkFormat(f, fieldValue, result, keyType);
        }

        boolean sizeCheck = f.getValidation().isCheckSize();
        if (sizeCheck) {
            getImplementor().checkSize(f, fieldValue, result, keyType);
        }

        boolean regexCheck = f.getValidation().isCheckRegex();
        if (regexCheck) {
            getImplementor().checkRegex(f, fieldValue, result, keyType);
        }

        if (formatCheck || sizeCheck || regexCheck) {
            getImplementor().emptyLine();

			addUsingType(getRuntimeType(Validator.class));
	        addUsingType(getRuntimeType(ErrorMessageKey.class));
	        addUsingType(new Type("org.apache.struts.action.ActionError"));
        }
    }

    protected void implementTableValidation(Value item, FormField field,
            Value errors) {
        FieldValidation validation = field.getValidation();

        Type keyType = getRuntimeType(ErrorMessageKey.class);
        Type validatorType = getRuntimeType(Validator.class);

        boolean formatCheck = validation.isCheckFormat();
        if (formatCheck) {
            getImplementor().checkTableFormat(item, field, errors, keyType, validatorType);
        }

        boolean sizeCheck = validation.isCheckSize();
        if (sizeCheck) {
            getImplementor().checkTableSize(item, field, errors, keyType);
        }

        boolean regexCheck = validation.isCheckRegex();
        if (regexCheck) {
            getImplementor().checkTableRegex(item, field, errors, keyType, validatorType);
        }

        if (formatCheck || sizeCheck || regexCheck) {
            getImplementor().emptyLine();

			addUsingType(getRuntimeType(Validator.class));
	        addUsingType(getRuntimeType(ErrorMessageKey.class));
	        addUsingType(new Type("org.apache.struts.action.ActionError"));
		}
    }

    protected boolean existTableValidation(String event, FormTable table) {
        return table.existEventFormValidationField(event);
    }
}
