/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2005 C.Marchand - christophe.marchand@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.common.config;

import java.util.Vector;

import org.apache.commons.lang.StringEscapeUtils;
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 fr.gouv.finances.cp.xemelios.ui.ImageResources;
import fr.gouv.finances.cp.xemelios.ui.ListDisplayable;
import java.util.Hashtable;
import javax.xml.namespace.QName;

/**
 * Represente l'enregistrement d'une recherche utilisateur
 * @author chm
 */
public class RechercheModel implements XmlMarshallable, Comparable, ListDisplayable {
    public static final transient String TAG = "recherche";
    public static final transient QName QN = new QName(TAG);
    private String name;
    private boolean writable = true;
    private Vector<CritereModel> criteres;
//    private Hashtable<String,CritereModel> hCriteres;

    public RechercheModel(QName tagName) {
        super();
        this.criteres = new Vector<CritereModel>();
//        hCriteres = new Hashtable<String,CritereModel>();
    }
    public RechercheModel(QName tagName,String name) {
        this(tagName);
        this.name=name;
    }

    public void addCharacterData(String cData) throws SAXException {}
    public void addChild(XmlMarshallable child, QName tagName) throws SAXException {
        if(CritereModel.QN.equals(tagName)) {
            CritereModel cm = (CritereModel)child;
//            if(hCriteres.containsKey(cm.getId())) {
//                CritereModel old = hCriteres.get(cm.getId());
//                criteres.remove(old);
//                hCriteres.remove(old.getId());
//            }
            criteres.add(cm);
//            hCriteres.put(cm.getId(),cm);
        }
    }
    public XmlMarshallable getAttributes(XmlAttributes attributes) throws SAXException {
        name = attributes.getValue("name");
        return this;
    }
    public void marshall(XmlOutputter output) {
        if(!writable) return;
        output.startTag(TAG);
        output.addAttribute("name",StringEscapeUtils.escapeXml(name));
        for(CritereModel cm:criteres) cm.marshall(output);
        output.endTag(TAG);
    }
    public void validate() throws InvalidXmlDefinition {}
    @Override
    public RechercheModel clone() {
        RechercheModel rm = new RechercheModel(QN,name);
        for(CritereModel cm:this.criteres) rm.criteres.add(cm);
        return rm;
    }
    public String getName() { return name; }
    @Override
    public String toString() { return getName(); }
    public int compareTo(Object o) {
        if(o==this) return 0;
        if(o instanceof RechercheModel) {
            RechercheModel other = (RechercheModel)o;
            return getName().compareTo(other.getName());
        } else if(o instanceof PluginModel) {
            PluginModel other = (PluginModel)o;
            return getName().compareTo(other.getPluginTitle());
        }
        return 0;
    }
    @Override
    public boolean equals(Object o) { return compareTo(o)==0; }
    public Vector<CritereModel> getCriteres() {
        return criteres;
    }
    public String getDisplayName() {
        return toString();
    }
    public String getResource() {
        return ImageResources.LST_SEARCH_SAV;
    }
    public boolean isDeletable() {
        return true;
    }

    public XmlMarshallable getChildToModify(String uri, String localName, String qName, Attributes atts) {
        //return hCriteres.get(atts.getValue("id"));
        return null;
    }

    public QName getQName() {
        return QN;
    }
    public void setUnWritable(String name) {
        writable = false;
        this.name = this.name+" ("+name+")";
    }
}
