/*
 * blancoCsv Copyright (C) 2005 Tosiki Iga
 * 
 * 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.csv.expand;

import java.io.File;
import java.io.IOException;

import javax.xml.transform.TransformerException;

import blanco.commons.util.BlancoNameAdjuster;
import blanco.csv.valueobject.BlancoCsvFieldStructure;
import blanco.csv.valueobject.BlancoCsvStructure;
import blanco.valueobject.BlancoValueObjectXml2JavaClass;
import blanco.valueobject.valueobject.BlancoValueObjectClassStructure;
import blanco.valueobject.valueobject.BlancoValueObjectFieldStructure;

public class BlancoCsvExpandRecord {
    /**
     * 自動生成するソースファイルの文字エンコーディング。
     */
    private String fEncoding = null;

    public void setEncoding(final String argEncoding) {
        fEncoding = argEncoding;
    }

    /**
     * レコードを展開します。
     * 
     * @param processStructure
     * @param direcotryValueObjectWork
     * @param directoryTarget
     * @throws IOException
     * @throws TransformerException
     */
    public void expand(final BlancoCsvStructure processStructure,
            final File direcotryValueObjectWork, final File directoryTarget)
            throws IOException, TransformerException {
        final String className = BlancoNameAdjuster
                .toClassName(processStructure.getFileDefinitionId())
                + "CsvRecord";
        final String packageName = processStructure.getPackageName()
                + ".record";

        final BlancoValueObjectClassStructure voClass = new BlancoValueObjectClassStructure();
        voClass.setName(className);
        voClass.setPackage(packageName);
        voClass.setDescription("blancoCsvファイル定義書から作成されたレコードクラス\nファイル定義["
                + processStructure.getFileDefinitionId()
                + (processStructure.getFileDescription() == null ? "" : "/"
                        + processStructure.getFileDescription())
                + "]のレコードクラス\n");

        for (int indexField = 0; indexField < processStructure.getFieldList()
                .size(); indexField++) {
            final BlancoCsvFieldStructure field = (BlancoCsvFieldStructure) processStructure
                    .getFieldList().get(indexField);

            boolean isQuoted = false;
            String actualType = field.getType();
            if (field.getType().endsWith("(quote)")) {
                // クオート指定がある場合には、型名称から(quote)を除去します。
                actualType = field.getType().substring(0,
                        field.getType().length() - "(quote)".length());
                isQuoted = true;
            }
            if (field.getRequired() == false) {
                // 必要に応じてラッパー型に対応します。
                if (actualType.equals("int")) {
                    actualType = "java.lang.Integer";
                } else if (actualType.equals("long")) {
                    actualType = "java.lang.Long";
                }
            }

            final BlancoValueObjectFieldStructure voField = new BlancoValueObjectFieldStructure();
            voClass.getFieldList().add(voField);

            if (field.getNo() != null) {
                voField.setNo(field.getNo());
            }

            voField.setName(field.getName());
            voField.setType(actualType);

            StringBuffer strbuf = new StringBuffer();
            if (field.getNo() != null) {
                strbuf.append("項目番号: " + field.getNo() + "\n");
            }
            if (field.getDescription() != null) {
                strbuf.append("項目の説明: " + field.getDescription() + "\n");
            }
            if (isQuoted) {
                strbuf.append("文字列はダブルクオート処理されて入出力されます。\n");
            }

            if (field.getMinLength() != null) {
                strbuf.append("MIN長さ: " + field.getMinLength() + "\n");
            }
            if (field.getMaxLength() != null) {
                strbuf.append("MAX長さ: " + field.getMaxLength() + "\n");
            }
            if (field.getRequired()) {
                strbuf.append("必須項目\n");
            }
            voField.setDescription(strbuf.toString());

            if (field.getDefault() != null) {
                voField.setDefault(field.getDefault());
            }
        }

        final BlancoValueObjectXml2JavaClass xml2source = new BlancoValueObjectXml2JavaClass();
        xml2source.setEncoding(fEncoding);
        xml2source.structure2Source(voClass, directoryTarget);
    }
}
