/*
 * 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.updater.config;

import java.util.ArrayList;
import java.util.Iterator;

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 ComponentModel implements XmlMarshallable {
	public static final transient String TAG = "component";
	
	/**
	 * XEMELIOS type component
	 */
	public static final int XEMELIOS = 1;
	/**
	 * CONFIG type component
	 */
	public static final int CONFIG = 2;
	/**
	 * FEATURE type component
	 */
	public static final int FEATURE = 3;
	
	
	private String id, name;
	private ReleaseModel release;
	private TexteModel description;
	private RequireModel requires;
	private ArrayList<FileListModel> fileLists;
	private ConfigurationModel configuration;
	private boolean visible = true;
	
	// for UI behavior only
	public boolean selected = false;
	public int type = 0;

	public ComponentModel(String tag) {
		super();
		fileLists = new ArrayList<FileListModel>();
	}

	public void addCharacterData(String cData) throws SAXException {
	}

	public void addChild(XmlMarshallable child, String tagName) throws SAXException {
		if(ReleaseModel.TAG.equals(tagName)) release = (ReleaseModel)child;
		else if("description".equals(tagName)) description = (TexteModel)child;
		else if(RequireModel.TAG.equals(tagName)) requires=(RequireModel)child;
		else if(FileListModel.TAG.equals(tagName)) fileLists.add((FileListModel)child);
		else if(ConfigurationModel.TAG.equals(tagName)) configuration=(ConfigurationModel)child;
	}

	public XmlMarshallable getAttributes(XmlAttributes attributes) throws SAXException {
		id = attributes.getValue("cid");
		name = attributes.getValue("name");
		String sTmp = attributes.getValue("visible");
		if(sTmp!=null) visible=attributes.getBooleanValue("visible");
		return this;
	}

	public void marshall(XmlOutputter output) {
		output.startTag(TAG);
		output.addAttribute("cid",id);
		output.addAttribute("name",name);
		if(!visible) output.addAttribute("visible","no");
		release.marshall(output);
		description.marshall(output);
		if(requires!=null) requires.marshall(output);
		for(Iterator<FileListModel> it=fileLists.iterator();it.hasNext();)
			it.next().marshall(output);
		// TODO
		output.endTag(TAG);
	}

	public void validate() throws InvalidXmlDefinition {
		if(id==null) throw new InvalidXmlDefinition("cid attribute is mandatory for element "+TAG);
		if(name==null) throw new InvalidXmlDefinition("name attribute is mandatory for element "+TAG);
	}
	
	public ComponentModel clone() {
		ComponentModel cm = new ComponentModel(TAG);
		cm.id=this.id;
		cm.name=this.name;
		cm.release=release.clone();
		cm.description=this.description.clone();
		for(Iterator<FileListModel> it=fileLists.iterator();it.hasNext();)
			cm.fileLists.add(it.next().clone());
		return cm;
	}

	public TexteModel getDescription() {
		return description;
	}

	public void setDescription(TexteModel description) {
		this.description = description;
	}
	public void setDescription(String s) {
		TexteModel desc = new TexteModel("description");
		try { desc.addCharacterData(s); } catch(Throwable t) {}
		setDescription(desc);
	}

	public ArrayList<FileListModel> getFileLists() {
		return fileLists;
	}

	public void setFileLists(ArrayList<FileListModel> fileLists) {
		this.fileLists = fileLists;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public ReleaseModel getRelease() {
		return release;
	}

	public void setRelease(ReleaseModel release) {
		this.release = release;
	}
	public void setRelease(String s) {
		ReleaseModel rm = new ReleaseModel(ReleaseModel.TAG);
		rm.setV(s);
		setRelease(rm);
	}

	public RequireModel getRequires() {
		return requires;
	}

	public void setRequires(RequireModel requires) {
		this.requires = requires;
	}
	public String toString() {
		return getId()+" ("+getRelease().getV()+")";
	}
	public boolean isVisible() {
		return visible;
	}

	@Override
	public boolean equals(Object o) {
		if(o instanceof ComponentModel) {
			ComponentModel cm = (ComponentModel)o;
			return cm.getId().equals(id) && cm.getRelease().getV().equals(getRelease().getV());
		} else if(o instanceof ComponentRefModel) {
			ComponentRefModel cm = (ComponentRefModel)o;
			return cm.getId().equals(id) && cm.getVersion().equals(getRelease().getV());
		} else if(o instanceof String) {
			return getId().equals((String)o);
		}
		return false;
	}

	public ConfigurationModel getConfiguration() {
		return configuration;
	}

	public void setConfiguration(ConfigurationModel configuration) {
		this.configuration = configuration;
	}
}
