/*
 * Copyright (c) 2007, Going Dot Com Inc. All rights reserved.
 */

package jp.co.going.xbrl.instance;

import java.sql.Connection;
import java.util.ArrayList;

import jp.co.going.xbrl.common.XbContextIdList;
import jp.co.going.xbrl.common.XbRef;
import jp.co.going.xbrl.common.XbRefList;
import jp.co.going.xbrl.common.XbException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * @author Going Dot Com Inc.
 */
class XbInstance21 extends XbInstance {

    private XbRootElement root;
    private XbInstanceRef ref;
    private XbItem item;
    private XbContext21 context;
    private XbUnit unit;

    protected XbInstance21(Connection conn) throws XbException {
        super(conn);
        root = new XbRootElement21();
        ref = new XbInstanceRef(conn, super.doc);
        item = new XbItem21(conn, super.doc);
        context = new XbContext21(conn, super.doc);
        unit = new XbUnit21(conn, super.doc);

        mappingDAO = XbMappingDAO21.getInstance(conn);
    }

    /**
     * インスタンスを作成する
     * @param taxGroupId
     * @param contextIdList
     * @param refList
     * @return
     * @throws XbException
     */
    protected Document createInstance(String taxGroupId, XbContextIdList contextIdList, XbRefList refList) throws XbException {

        //interfaceテーブルクリア
        super.clearInterface();
        // contextList
        ArrayList<Element> contextList = new ArrayList<Element>();
        // unitList
        ArrayList<Element> unitList = new ArrayList<Element>();
        // root作成
        Element rootElement = root.createRootElement(super.conn, super.doc);
        // ref作成
        for (int i = 0;i < refList.size();i ++) {
            XbRef obj = refList.get(i);
            rootElement.appendChild(ref.createInstanceRef(obj.getRefType(), obj.getRefId()));
        }
        // item・context作成
        for (int i = 0;i < contextIdList.size();i ++) {
            // context情報取得
            XbContextObj obj = contextDAO.getContext(contextIdList.get(i));
            // mappingInfo
            // mappingInfo
            super.getSearchInfo(obj);
            // item取得
            mappongList = mappingDAO.getMappingInfo(obj, taxGroupId, super.entity, super.unit.getUnitName());
            // context作成
            if (mappongList.size() != 0) {
                // insrtItem
                super.setContextItem(mappongList, obj);
                // tupleData取得
                super.getTupleData(taxGroupId, obj);

                // footnoteIdList作成
                super.addFootNoteId();
                // context作成
                Element contextTag = context.createContext(obj, rootElement);
                // unit作成
                Element unitTag = unit.createUnit(obj.getContextId(), rootElement);
                // context配列格納
                contextList.add(contextTag);
                // unit配列格納
                unitList.add(unitTag);
            }
        }

        // アイテム作成(並べ替え)
        item.createItem(rootElement);

        // context追加
        for (int i = 0;i < contextList.size();i ++) {
            if (contextList.get(i) != null) {
                rootElement.appendChild(contextList.get(i));
            }
        }
        // unit追加
        for (int i = 0;i < unitList.size();i ++) {
            if (unitList.get(i) != null) {
                rootElement.appendChild(unitList.get(i));
            }
        }
        // footnote作成
        super.createFootNote(taxGroupId, rootElement);

        super.doc.appendChild(rootElement);
        return super.doc;
    }
}