2015年7月19日日曜日

[メタデータエディタ]TextConvertDefRepository

package domain;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import util.CnvUtil;
import util.CopyUtil;
import util.PropUtil;
import dao.SubstanceDao;
import dao.TextConvertDefDao;
import entity.TextConvertDef;
import entity.TextConvertLine;
import exception.ApplicationInternalException;

/**
 * テキスト変換定義リポジトリ
 */
public class TextConvertDefRepository {

    /** サブスタンスDAO */
    private SubstanceDao substanceDao = null;

    public SubstanceDao getSubstanceDao() {
        return substanceDao;
    }

    public void setSubstanceDao(SubstanceDao substanceDao) {
        this.substanceDao = substanceDao;
    }

    /** テキスト変換定義DAO */
    private TextConvertDefDao textConvertDefDao = null;

    public TextConvertDefDao getTextConvertDefDao() {
        return textConvertDefDao;
    }

    public void setTextConvertDefDao(TextConvertDefDao textConvertDefDao) {
        this.textConvertDefDao = textConvertDefDao;
    }

    /** テキスト変換定義ドメイン */
    private TextConvertDefDomain textConvertDefDomain = null;

    public TextConvertDefDomain getTextConvertDefDomain() {
        return textConvertDefDomain;
    }

    public void setTextConvertDefDomain(
            TextConvertDefDomain textConvertDefDomain) {
        this.textConvertDefDomain = textConvertDefDomain;
    }

    /** テキスト変換行リポジトリ */
    private TextConvertLineRepository textConvertLineRepository = null;

    public TextConvertLineRepository getTextConvertLineRepository() {
        return textConvertLineRepository;
    }

    public void setTextConvertLineRepository(
            TextConvertLineRepository textConvertLineRepository) {
        this.textConvertLineRepository = textConvertLineRepository;
    }

    /**
     * ドメインを生成します。
     *
     * @return 生成したドメイン
     */
    public TextConvertDefDomain createDomain(TextConvertDef textConvertDef) {

        // 指定されたデータで登録を行う
        textConvertDefDao.insert(textConvertDef);

        // ドメインを取得し、呼び出し側に戻す
        return getDomain(textConvertDef.getTextConvertDefId());
    }

    /**
     * ドメインを取得します。
     *
     * @param textConvertDefId
     *            メタデータID
     * @return 取得したドメイン
     */
    public TextConvertDefDomain getDomain(Integer textConvertDefId) {

        // 引数をキーとして、データベースから該当データを取得する
        TextConvertDef entity = textConvertDefDao.get(textConvertDefId);

        // データが見つからなかった場合は、戻り値nullで呼び出し側に復帰する
        if (entity == null) {
            return null;
        }

        // ドメインを生成し、エンティティを設定する
        TextConvertDefDomain textConvertDefDomain = new TextConvertDefDomain();
        CopyUtil.copy(this.textConvertDefDomain, textConvertDefDomain);
        textConvertDefDomain.setEntity(entity);

        // 生成したドメインを呼び出し側に戻す
        return textConvertDefDomain;
    }

    /**
     * テンプレートファイルの内容を解析して、テキスト変換定義を作成します。
     *
     * @param textConvertDef
     *            テキスト変換定義
     * @return 新規作成したテキスト変換定義ドメイン
     */
    public List<TextConvertDefDomain> importTemplateFile(
            TextConvertDef textConvertDef) {

        // インポートするファイル(フルパス名)を取得する
        List<String> filePathList = getFileNameList(textConvertDef);

        // 全てのファイルをインポートし、テキスト変換定義ドメインのリストを作成する
        List<TextConvertDefDomain> textConvertDefDomainList = new ArrayList<TextConvertDefDomain>();
        for (String filePath : filePathList) {
            TextConvertDefDomain textConvertDefDomain = importTargetFile(
                    textConvertDef, filePath);
            if (textConvertDefDomain != null) {
                textConvertDefDomainList.add(textConvertDefDomain);
            }
        }

        // 作成したテキスト変換定義ドメインのリストを呼び出し側に戻す
        return textConvertDefDomainList;
    }

    /**
     * インポートするファイルの存在確認を行います。
     *
     * @param textConvertDef
     */
    private List<String> getFileNameList(TextConvertDef textConvertDef) {

        // パス区切り文字を取得する
        String pathDelm = PropUtil.get("path.delm");
        String pathDelmPkg = PropUtil.get("path.delm.pkg");

        // ファイルパスを作成する(パッケージはドットをパス区切り文字に変えてパス名に追加する)
        String path = textConvertDef.getBaseDir() + pathDelm
                + textConvertDef.getPkg().replace(pathDelmPkg, pathDelm);

        // パスが存在しなければエラーとする
        File dir = new File(path);
        if (!dir.exists()) {
            throw new ApplicationInternalException(
                    PropUtil.get("msg.err.dirNotFound") + path);
        }

        // ファイル名のみ、テキスト変換による複数指定を認める
        CnvUtil cnvUtil = new CnvUtil(substanceDao,
                textConvertDef.getFileName());
        List<String> fileNameList = cnvUtil.getAfterConvertList();

        // フルパスファイル名を作成し、存在するファイルのみ戻り値のリストに追加する
        List<String> filePathList = new ArrayList<String>();
        for (String fileName : fileNameList) {
            String filePath = path + pathDelm + fileName;
            File file = new File(filePath);
            if (file.exists()) {
                filePathList.add(filePath);
            }
        }

        // 作成したファイルパスリストを呼び出し側に戻す
        return filePathList;
    }

    // TODO 実装が完了。次回はインポートを実際に行ってみる。
    // インポートが成功したら、テキスト変換定義に基づきテキストを変換するサービスを実装する。

    /**
     * ファイルパスで示されるファイルを読み込み、テキスト変換定義をインポートします。
     *
     * @param filePath
     *            ファイルパス
     * @return テキスト変換定義ドメイン
     */
    private TextConvertDefDomain importTargetFile(
            TextConvertDef textConvertDef, String filePath) {

        BufferedReader br = null;
        try {
            // ファイルを開く
            br = new BufferedReader(new FileReader(filePath));

            // テキスト変換定義ドメインを作成する
            TextConvertDefDomain textConvertDefDomain = createDomain(textConvertDef);

            // ファイルを全て読み込むまでループ
            String line = "";
            int lineNo = 0;
            while ((line = br.readLine()) != null) {

                // テキスト変換行ドメインを作成する
                TextConvertLine textConvertLine = new TextConvertLine();
                textConvertLine.setLineNo(Integer.valueOf(lineNo));
                textConvertLine.setLine(line);
                textConvertLine.setTextConvertDefId(textConvertDefDomain
                        .getEntity().getTextConvertDefId());
                textConvertLineRepository.createDomain(textConvertLine);
            }

        } catch (FileNotFoundException e) {
            throw new ApplicationInternalException(
                    PropUtil.get("msg.err.fileNotFound") + filePath);
        } catch (IOException e) {
            throw new ApplicationInternalException(e);
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    throw new ApplicationInternalException(e);
                }
            }
        }

        // 作成したテキスト変換定義を呼び出し側に戻す
        return textConvertDefDomain;
    }
}

0 件のコメント:

コメントを投稿