/*
 * $Id: DataConfig.java,v 1.4 2006/05/15 07:02:33 sugimotokenichi Exp $
 * Copyright (C) 2005 SUGIMOTO Ken-ichi
 * 作成日: 2006/02/20
 */
package feat2.config;

import feat2.Data;
import feat2.FeatError;
import feat2.FeatErrors;
import feat2.Response;
import feat2.config.type.Identifier;
import feat2.config.type.JavaIdentifier;
import feat2.impl.DataResponse;

/**
 * データレスポンスの設定。
 * @author SUGIMOTO Ken-ichi
 */
public class DataConfig extends ResponseConfigBase implements DIComponentConfig, ValidatableComponentConfig {

    private JavaIdentifier className;
    private InputSupport inputs;
    private ValidationSupport validations;
    private Identifier errorResponse;

    private Class responseClass;

    public DataConfig() {
        inputs = new InputSupport(this);
        validations = new ValidationSupport(this);
    }


    // プロパティ -------------------------------------------------------------

    public JavaIdentifier getClassName() {
        return className;
    }
    public void setClassName(JavaIdentifier className) {
        this.className = className;
    }

    public Identifier getErrorResponse() {
        return errorResponse;
    }
    public void setErrorResponse(Identifier errorResponse) {
        this.errorResponse = errorResponse;
    }


    // DIComponentConfigのメソッド --------------------------------------------

    public void addInput(InputConfigBase input) {
        inputs.addInput(input);
        input.setParent(this);
    }
    public InputConfigBase[] getInputs() {
        return inputs.getInputs();
    }

    public InputConfigBase getInputConfig(String propertyName) {
        return inputs.getInputConfig(propertyName);
    }


    // ValidatableComponentConfigのメソッド -----------------------------------

    public void addValidation(ValidationConfig validation) {
        validations.addValidation(validation);
    }

    public ValidationConfig[] getValidations() {
        return validations.getValidations();
    }

    public ValidationConfig getValidationConfig(String name) {
        return validations.getValidationConfig(name);
    }

    public String getPropertyNameId(String propertyName) {
        return getInputConfig(propertyName).getDisplayName();
    }


    // ResponseConfigBaseのメソッド -------------------------------------------

    public Response getResponseInstance() {
        try {
            Data data = (Data)responseClass.newInstance();
            DataResponse response = new DataResponse(data, this);
            return response;
        }
        catch (InstantiationException ex) {
            throw new ConfigurationException(ex);
        }
        catch (IllegalAccessException ex) {
            throw new ConfigurationException(ex);
        }
    }


    // Configのメソッド -------------------------------------------------------

    public String getElementName() {
        return "data";
    }

    public boolean validate(FeatErrors errs) {

        boolean ret = super.validate(errs);

        responseClass = ConfigUtil.validateClass(className, "class", this, errs);
        if ( responseClass == null )
            ret = false;
        if ( !Data.class.isAssignableFrom(responseClass) ) {
            errs.add(new ConfigError(className.getValue(), "class", ConfigError.TYPE_DATA_INTERFACE, FeatError.ERROR, this) );
        }

        if ( validations.count() > 0 && errorResponse == null ) {
            errs.add( new ConfigError(null, "error-response", ConfigError.TYPE_ERROR_RESPONSE, FeatError.ERROR, this) );
        }

        ret = ret && inputs.validate(errs);
        ret = ret && validations.validate(errs);

        return ret;
    }



}
