/*
 * 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.Attributes;
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;
import javax.xml.namespace.QName;

public class DocumentControlModel implements XmlMarshallable {
    
    public static final transient String TAG = "document";
    public static final transient QName QN = new QName(TAG);
    private 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(QName 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, QName tagName) throws SAXException {
        if(ParamModel.QN.equals(tagName)) {
            params.add((ParamModel)child);
        } else if(ControlModel.QN.equals(tagName)) {
            ControlModel cm = (ControlModel)child;
            if(indexedControls.containsKey(cm.getId())) {
                ControlModel old = indexedControls.get(cm.getId());
                controls.remove(old);
                indexedControls.remove(old.getId());
            }
            controls.add(cm);
            indexedControls.put(cm.getId(),cm);
        } else if(ControlModelTech.QN.equals(tagName)) {
            ControlModelTech cm = (ControlModelTech)child;
            if(indexedControlsTech.contains(cm.getId())) {
                ControlModelTech old = indexedControlsTech.get(cm.getId());
                controlsTech.remove(old);
                indexedControlsTech.remove(old.getId());
            }
            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 getDocumentId() {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(QN);
        dcm.id = this.id;
        for(ControlModel cm:getControls()){
        	try{
        		addChild(cm.clone(),ControlModel.QN);
        	} catch(SAXException ignore){}
        }
        for(ControlModelTech cmt:getControlsTech()) {
        	try {
        		addChild(cmt.clone(), ControlModelTech.QN);
        	} catch(SAXException ignore) {}
        }
        for(ParamModel pm:getParams()) {
        	try {
        		addChild(pm.clone(), ParamModel.QN);
        	} catch(SAXException ignore) {}
        }
        return dcm;
    }

    public XmlMarshallable getChildToModify(String uri, String localName, String qName, Attributes atts) {
        QName tagName = new QName(uri,localName);
        if(ControlModel.QN.equals(tagName)) {
            return indexedControls.get(atts.getValue("id"));
        } else if(ControlModelTech.QN.equals(tagName)) {
            return indexedControlsTech.get(atts.getValue("id"));
        }
        return null;
   }

    public QName getQName() {
        return QN;
    }
}
