/*
 * 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 General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU 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.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 ControlModel implements XmlMarshallable{
    public static final transient String TAG = "control";
    public static final transient QName QN = new QName(TAG);
    public String id, libelle, severity, classe;
    public RegleModel regle;
    public Vector<RefIdModel> params;
    public MessageModel message;
    //public Vector<ElementLieModel> elementsLies;
    
    
    public ControlModel(QName tagName) {
        super();
        //regles=new Vector<RegleModel>();
        params=new Vector<RefIdModel>();
        //messages=new Vector<MessageModel>();
        //elementsLies=new Vector<ElementLieModel>();
    }
    
    public void addCharacterData(String cData) throws SAXException { }
    public void addChild(XmlMarshallable child, QName tagName) throws SAXException {
        if(RegleModel.QN.equals(tagName)) {
            regle = (RegleModel)child;
        } else if(RefIdModel.QN.equals(tagName)) {
            params.add((RefIdModel)child);
        } else if(MessageModel.QN.equals(tagName)) {
            message=(MessageModel)child;
//        } else if(ElementLieModel.TAG.equals(tagName)) {
//            elementsLies.add((ElementLieModel)child);
        }
    }
    
    public XmlMarshallable getAttributes(XmlAttributes attributes) throws SAXException {
        id=attributes.getValue("id");
        libelle=attributes.getValue("libelle");
        severity=attributes.getValue("severity");
        classe=attributes.getValue("class");
        return this;
    }
    public void validate() throws InvalidXmlDefinition {
        if(regle==null) throw new InvalidXmlDefinition("<regle/> element is required in <control/>");
        regle.validate();
        for(RefIdModel pcm:params) pcm.validate();
        if(message==null) throw new InvalidXmlDefinition("<message/> element is required in <control/>");
        message.validate();
        //for(ElementLieModel elm:elementsLies) elm.validate();
    }
    public void marshall(XmlOutputter output) {
        output.startTag(TAG);
        output.addAttribute("id",id);
        output.addAttribute("libelle",libelle);
        output.addAttribute("severity",severity);
        output.addAttribute("class",classe);
        if(regle!=null) regle.marshall(output);
        for(RefIdModel pcm:params) pcm.marshall(output);
        message.marshall(output);
        output.endTag(TAG);
    }
    
    public String getId() {return id;}
    public String getLibelle() {return libelle;}
    public String getSeverity() {return severity;}
    public String getClasse() {return classe;}
    public RegleModel getRegle() {return regle;}
    public RefIdModel getRefIdModel() {return params.firstElement();}
    public MessageModel getMessage() {return message;}
    
    public RegleModel getRegleFonctionnelle(String etatId) { return regle;}
    
    public ControlModel clone() {
        ControlModel cm = new ControlModel(QN);
        
        cm.id = this.id;
        cm.libelle = this.libelle;
        cm.severity = this.severity;
        cm.classe = this.classe;
        cm.regle=regle.clone();
        for(RefIdModel pcm:params) {
            try {
                cm.addChild(pcm.clone(),RefIdModel.QN);
            }catch(Throwable t) {}
        }
        return cm;
    }

    public XmlMarshallable getChildToModify(String uri, String localName, String qName, Attributes atts) {
        QName tagName = new QName(uri,localName);
        if(RegleModel.QN.equals(tagName)) {
            return regle;
        } else if(RefIdModel.QN.equals(tagName)) {
            return null;
        } else if(MessageModel.TAG.equals(tagName)) {
            return message;
        }
        return null;
    }

    public QName getQName() {
        return QN;
    }
}
