/*
 * blanco Framework
 * Copyright (C) 2004-2005 IGA Tosiki
 * 
 * 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.db.expander.query.iterator;

import blanco.db.definition.QueryIteratorDotNet;
import blanco.db.util.BlancoDbObjectStorageDotNet;
import blanco.ig.expander.IgType;
import blanco.ig.expander.IgValue;
import blanco.ig.expander.implementor.Implementor;
import blanco.ig.expander.method.MethodExpander;

/**
 * Single属性がtrueの場合にのみ、このクラスは利用されます
 * 
 * @author Tosiki Iga
 */
public class GetSingleRowMethodDotNet extends MethodExpander {
    private BlancoDbObjectStorageDotNet storage = null;

    private String runtimePackage = null;

    private IgValue _result = null;

    public GetSingleRowMethodDotNet(final BlancoDbObjectStorageDotNet storage,
            final IgType rowObjectType, final QueryIteratorDotNet iterator) {
        super("GetSingleRow");
        this.storage = storage;

        runtimePackage = storage.getSetting().getRootNameSpace();
        if (storage.getSetting().getRuntimePackage() != null) {
            runtimePackage = storage.getSetting().getRuntimePackage();
        }

        _result = new IgValue(rowObjectType, "result");
    }

    public void setupSignature() {
        setReturnType(_result.getType());

        final IgType noRowFoundException = new IgType(runtimePackage
                + ".exception.NoRowFoundException");
        addUsingType(noRowFoundException);
        final IgType tooManyRowsFoundException = new IgType(runtimePackage
                + ".exception.TooManyRowsFoundException");
        addUsingType(tooManyRowsFoundException);

        getJavaDoc().addLine("現在の行のデータをオブジェクトとして取得します。");
        getJavaDoc().addLine(
                "このメソッドを利用する場合には、Next()などのカーソルを操作するメソッドとは併用しないでください。");
        getJavaDoc().addLine("");
        getJavaDoc().addLine("single属性が有効");
        getJavaDoc().addLine("検索結果が1件以外の場合には、NotSingleRowExceptionクラスを");
        getJavaDoc().addLine("派生したクラスの例外が発生します。");
        getJavaDoc().addReturn("行オブジェクト");
    }

    public void implement() {
        final Implementor implementor = new Implementor(getData());

        if (storage.getSetting().isLogging()) {
            implementor.addLine("if (fLog.isDebugEnabled()) {");
            implementor.addLine("fLog.debug(\"" + getName() + "\");");
            implementor.addLine("}");
            implementor.addLine("");
        }

        getData().addLine("if (Next() == false) {");
        getData().addLine("throw new NoRowFoundException(\"行が検索できませんでした。\");");
        getData().addLine("}");
        getData().addLine("");
        getData().addLine(_result.getType().getName() + " result = GetRow();");
        getData().addLine("");
        getData().addLine("if (Next()) {");
        getData().addLine(
                "throw new TooManyRowsFoundException(\"1件以上の行が検索されました。\");");
        getData().addLine("}");
        getData().addLine("");
        getData().addLine("return result;");
    }
}