/*
 * $Id: OutputConfig.java,v 1.4 2006/04/26 16:59:43 sugimotokenichi Exp $
 * Copyright (C) 2005 SUGIMOTO Ken-ichi
 * 作成日: 2006/02/20
 */
package feat2.config;

import java.util.ArrayList;

import feat2.FeatError;
import feat2.FeatErrors;
import feat2.config.type.Identifier;
import feat2.config.type.IdentifierList;
import feat2.config.type.Scope;

/**
 * 出力の遷移の設定。
 * @author SUGIMOTO Ken-ichi
 */
public class OutputConfig extends Config {

    private Identifier name;
    private Identifier response;
    private ArrayList outputObjects;
    private Scope scope;

    public OutputConfig() {
        outputObjects = new ArrayList();
    }


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

    public Identifier getResponse() {
        return response;
    }
    public void setResponse(Identifier response) {
        this.response = response;
    }

    public void setOutputObjects(IdentifierList nameList) {

        for(int i=0; i<nameList.size(); i++) {

            String name = nameList.get(i).getValue();
            Mapping mp = new Mapping(name, name, scope);
            outputObjects.add(mp);

        }

    }

    public Scope getScope() {
        return scope;
    }
    public void setScope(Scope scope) {
        this.scope = scope;
    }


    // インスタンスメソッド ---------------------------------------------------

    public void addOutputMapping(Identifier localName, Identifier outputName, Scope scope) {
        Mapping o = new Mapping(localName.getValue(), outputName.getValue(), scope);
        outputObjects.add(o);
    }

    public Mapping[] getOutputMapping() {
        return (Mapping[])outputObjects.toArray(new Mapping[0]);
    }



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

    public Identifier getName() {
        return name;
    }

    public void setName(Identifier name) {
        this.name = name;
    }

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

    public boolean validate(FeatErrors errs) {

        boolean ret = true;

        ret = ret && ConfigUtil.validateRequired(name, "name", this, errs);
        ret = ret && ConfigUtil.validateRequired(response, "response", this, errs);

        String responseName = response.getValue();
        if ( responseName != null && getFeatureConfig().getResponseConfig(responseName) == null )
            errs.add( new ConfigError(responseName, "response", ConfigError.TYPE_REFERRENCE_RESPONSE, FeatError.ERROR, this) );

        CommandConfig commandConf = (CommandConfig)getParent();
        for(int i=0; i<outputObjects.size(); i++) {

            Mapping m = (Mapping)outputObjects.get(i);
            if ( ConfigUtil.validateRequired(m.getLocalName(), "local-name", this, errs)
                    && ConfigUtil.validateRequired(m.getOutputName(), "output-name", this, errs) ) {

                if ( commandConf.getActionComponentConfig(m.getLocalName()) == null ) {

                    errs.add(new ConfigError(m.getLocalName(), "local-name", ConfigError.TYPE_REFERRENCE, FeatError.ERROR, this) );

                }

            }
            else {
                ret = false;
            }

        }

        return ret;
    }



    // 内部クラス -------------------------------------------------------------

    public class Mapping {

        String localName;
        String outputName;
        Scope scope;

        private Mapping(String localName, String outputName, Scope scope) {
            this.localName = localName;
            this.outputName = outputName;
            this.scope = scope;
            if ( scope == null )
                this.scope = new Scope("request");
        }

        public String getLocalName() {
            return localName;
        }

        public String getOutputName() {
            return outputName;
        }

        public Scope getScope() {
            return scope;
        }


    }

}