/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2007 JP.Tessier - jean-philippe.tessier@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS 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 3 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package fr.gouv.finances.cp.xemelios.controls.models;

import java.util.Hashtable;
import java.util.Vector;

import org.xml.sax.SAXException;

import fr.gouv.finances.cp.utils.xml.marshal.InvalidXmlDefinition;
import fr.gouv.finances.cp.utils.xml.marshal.XmlAttributes;
import fr.gouv.finances.cp.utils.xml.marshal.XmlMarshallable;
import fr.gouv.finances.cp.utils.xml.marshal.XmlOutputter;

public class DocumentControlModel implements XmlMarshallable {
    
    public static final transient String TAG = "document";
    public String id;
    private Vector<ParamModel> params;
    private Vector<ControlModelTech> controlsTech;
    private Vector<ControlModel> controls;
    private Hashtable<String,ControlModel> indexedControls;
    private Hashtable<String,ControlModelTech> indexedControlsTech;
    
    public DocumentControlModel(String tagName) {
        super();
        params=new Vector<ParamModel>();
        controls=new Vector<ControlModel>();
        controlsTech=new Vector<ControlModelTech>();
        indexedControls = new Hashtable<String,ControlModel>();
        indexedControlsTech = new Hashtable<String,ControlModelTech>();
    }
    
    public void addCharacterData(String cData) throws SAXException { }
    public void addChild(XmlMarshallable child, String tagName) throws SAXException {
        if(ParamModel.TAG.equals(tagName)) {
            params.add((ParamModel)child);
        } else if(ControlModel.TAG.equals(tagName)) {
            ControlModel cm = (ControlModel)child;
            controls.add(cm);
            indexedControls.put(cm.getId(),cm);
        } else if(ControlModelTech.TAG.equals(tagName)) {
        	ControlModelTech cm = (ControlModelTech)child;
            controlsTech.add(cm);
            indexedControlsTech.put(cm.getId(),cm);
        }
    }
    public XmlMarshallable getAttributes(XmlAttributes attributes) throws SAXException {
        id=attributes.getValue("id");
        return this;
    }
    public void validate() throws InvalidXmlDefinition {
        for(ParamModel pcm:params) pcm.validate();
        for(ControlModel cm:controls) cm.validate();
        for(ControlModelTech cm:controlsTech) cm.validate();
    }
    public void marshall(XmlOutputter output) {
        output.startTag (TAG);
        output.addAttribute("id",id);
        for(ParamModel pm:params) pm.marshall(output);
        for(ControlModelTech cm:controlsTech) cm.marshall(output);
        for(ControlModel cm:controls) cm.marshall(output);
        output.endTag(TAG);
    }
    
    public String getDocumentById() {return id;}
    
    public ParamModel getParamById(String etatId) {
        for(ParamModel param:params) {
            if(param.getId().equals(etatId)) return param;
        }
        return null;
    }
    
    public ControlModel getControlById(String ctrlId) {
        return indexedControls.get(ctrlId);
    }
    
    public ControlModelTech getControlTechById(String ctrlId) {
        return indexedControlsTech.get(ctrlId);
    }
    
    public Vector<ControlModelTech> getControlsTech(){
        return controlsTech;
    }
        
    public Vector<ControlModel> getControls(){
        return controls;
    }
    public Vector<ParamModel> getParams(){
        return params;
    }
    
    public DocumentControlModel clone() {
        DocumentControlModel dcm = new DocumentControlModel(TAG);
        dcm.id = this.id;
        for(ControlModel cm:getControls()){
        	try{
        		addChild(cm.clone(),ControlModel.TAG);
        	} catch(SAXException ignore){}
        }
        for(ControlModelTech cmt:getControlsTech()) {
        	try {
        		addChild(cmt.clone(), ControlModelTech.TAG);
        	} catch(SAXException ignore) {}
        }
        for(ParamModel pm:getParams()) {
        	try {
        		addChild(pm.clone(), ParamModel.TAG);
        	} catch(SAXException ignore) {}
        }
        return dcm;
    }
}
