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

package jp.co.going.xbrl.instance;

import java.sql.Connection;

import jp.co.going.xbrl.common.XbException;
import jp.co.going.xbrl.common.XbConstants;

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

/**
 * @author Going Dot Com Inc.
 */
class XbContext21 extends XbContext {

    private Document doc;
    private XbEntity entity;
    private XbPeriod period;
    private XbScenario scenario;
    private String contextId;

    protected XbContext21(Connection conn, Document doc) throws XbException {
        this.doc = doc;

        entity = new XbEntity(conn, doc);
        period = new XbPeriod(doc);
        scenario = new XbScenario(conn, doc);
        contextId = "";
    }

    protected Element createContext(XbContextObj obj, Element rootElement) throws XbException {
        Element context = null;

        if ("".equals(contextId) || !contextId.equals(obj.getName())) {
            // contextタグ作成
            context = super.createElement(this.doc, XbConstants.TAG_CONTEXT);
            // id属性追加
            super.setAttribute(context, XbConstants.ATTRIBUTE_ID, obj.getName());

            // entityタグ作成
            Element entityTag = entity.createEntity(obj.getEntityId(), rootElement);
            // periodタグ作成
            Element periodTag = period.createPeriod(obj.getPeriodStartDate(), obj.getPeriodEndDate());
            // scenarioタグ作成
            Element scenarioTag = scenario.createScenario(obj.getContextId(), rootElement);

            // contextタグに追加
            super.appendChild(context, entityTag); // entity
            super.appendChild(context, periodTag); // period
            super.appendChild(context, scenarioTag); // scenario

            contextId = obj.getName();
        }

        return context;
    }
}