/*
 * blancoDb Enterprise Edition
 * Copyright (C) 2004-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.db.expander.query.iterator;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import blanco.db.definition.QueryIterator;
import blanco.db.properties.GenerationProperties;
import blanco.db.util.BlancoDbObjectStorage;
import blanco.ig.expander.Type;
import blanco.ig.expander.Value;
import blanco.ig.expander.implementor.Implementor;
import blanco.ig.expander.method.MethodExpander;

/**
 * @author Tosiki Iga
 */
public class GetListMethod extends MethodExpander {
    private GenerationProperties _properties = null;

    private QueryIterator _iterator = null;

    public GetListMethod(QueryIterator iterator) {
        super("getList");
        _iterator = iterator;
        _properties = BlancoDbObjectStorage.getInstance()
                .getGenerationProperties();
    }

    public void setupSignature() {
        setReturnType(new Type(List.class));
        if (_iterator.getScrollInterface().equals("forward_only") == false) {
            addArgument(new Value(int.class, "absoluteStartPoint"));
        }
        addArgument(new Value(int.class, "size"));
        addException(SQLException.class);
        addUsingType(new Type(ArrayList.class));

        if (_properties.isGenerateJavaDoc()) {
            getJavaDoc().addLine("検索結果をリストで取得します。<br>");
            getJavaDoc()
                    .addLine("リストには " + _iterator.getName() + "クラスが格納されます。");
            if (_iterator.getScrollInterface().equals("forward_only")) {
                getJavaDoc()
                        .addLine(
                                "FORWARD_ONLY(順方向カーソル)のgetListの利用はなるべく避けてください. FORWARD_ONLY(順方向カーソル)以外の値を利用して再生成するか nextの呼び出しを行うかしてください.");
            }
            if (_iterator.getScrollInterface().equals("forward_only") == false) {
                getJavaDoc().addParameter("absoluteStartPoint",
                        "読み出しを開始する行。最初の行から読み出したい場合には 1 を指定します。");
            }
            getJavaDoc().addParameter("size", "読み出しを行う行数。");
            getJavaDoc().addReturn(
                    _iterator.getName() + "クラスのList。検索結果が0件の場合には空のリスト。");
        }
    }

    public void implement() {
        Implementor i = new Implementor(getData());

        i.addLine("List result = new ArrayList(size);");
        if (_iterator.getScrollInterface().equals("forward_only") == false) {
            i.addLine("if (absolute(absoluteStartPoint) == false) {");
            i.addLine("return result;");
            i.addLine("}");
        }
        i.addLine("for (int count = 1; count <= size; count++) {");
        if (_iterator.getScrollInterface().equals("forward_only")) {
            i.addLine("if (next() == false) {");
            i.addLine("break;");
            i.addLine("}");
        } else {
            i.addLine("if (count != 1) {");
            i.addLine("if (next() == false) {");
            i.addLine("break;");
            i.addLine("}");
            i.addLine("}");
        }
        i.addLine("result.add(getRow());");
        i.addLine("}");
        i.addLine("return result;");
    }
}