/*
 * Copyright 
 *   2007 axYus - www.axyus.com
 *   2007 N.Le Corre - nicolas.lecorre@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.paye;

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

import org.apache.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

import fr.gouv.finances.cp.xemelios.controls.AbstractUnitControl;
import fr.gouv.finances.cp.xemelios.controls.Anomalie;
import fr.gouv.finances.cp.xemelios.controls.IdGenerator;
import fr.gouv.finances.cp.xemelios.controls.Node;

public class PAY20 extends AbstractUnitControl {
	private final static Logger logger = Logger.getLogger(PAY20.class);
    public static final transient String CTRL_ID = PAY20.class.getSimpleName();
	private Vector<Anomalie> anos = new Vector<Anomalie>();
	private String docId = null;

	
	private static final String ON_RETURN_ANOMALIES = "DocumentPaye/DonneesIndiv/Etablissement/";
	private static final String CHEMIN_PK_ANNEE = "DocumentPaye/Annee/";
	private static final String CHEMIN_PK_MOIS = "DocumentPaye/Mois/";
	private static final String CHEMIN_ETABLISSEMENT = "DocumentPaye/DonneesIndiv/Etablissement/";
	private static final String CHEMIN_PK_MATRICULE = "DocumentPaye/DonneesIndiv/PayeIndivMensuel/Agent/Matricule/";
	private String pk_annee = "", pk_mois = "", pk_matricule = "";
	private String refNodeId = "";

	private static final String	CHEMIN_ADRESSE_1_ETABLISSEMENT = "DocumentPaye/DonneesIndiv/Etablissement/Adresse/Adr1/";
	private static final String CHEMIN_ADRESSE_2_ETABLISSEMENT = "DocumentPaye/DonneesIndiv/Etablissement/Adresse/Adr2/";
	private static final String CHEMIN_ADRESSE_3_ETABLISSEMENT = "DocumentPaye/DonneesIndiv/Etablissement/Adresse/Adr3/";
	private static final String CHEMIN_ADRESSE_CODE_POSTAL_ETABLISSEMENT = "DocumentPaye/DonneesIndiv/Etablissement/Adresse/CP/";
	private static final String CHEMIN_ADRESSE_VILLE_ETABLISSEMENT = "DocumentPaye/DonneesIndiv/Etablissement/Adresse/Ville/";

	private static final String REPLACE_PAYE = "##PAYE##";
	private String adr1 = "", adr2 = "", adr3 = "", cp = "", ville = "";
	private String adr1NodeId = "", adr2NodeId = "", adr3NodeId = "", cpNodeId = "", villeNodeId = "";
	
	public void startDocument() throws SAXException {
		logger.info("In "+CTRL_ID);
	}
	
	public void setDocId(String docID) {
		this.docId = docID;		
	}

	public void startElement(String uri, String localName, String qName, Attributes atts, String xpath) throws SAXException {
		if (xpath.endsWith(CHEMIN_ETABLISSEMENT)) {
			refNodeId = atts.getValue("ano:node-id");
			adr1 = "";
			adr2 = "";
			adr3 = "";
			cp = "";
			ville = "";
		} else if (xpath.endsWith(CHEMIN_PK_ANNEE)) {
			pk_annee = atts.getValue("V");
		} else if (xpath.endsWith(CHEMIN_PK_MOIS)) {
			pk_mois = atts.getValue("V");
		} else if (xpath.endsWith(CHEMIN_PK_MATRICULE)) {
			pk_matricule = atts.getValue("V");
		} else if (xpath.endsWith(CHEMIN_ADRESSE_1_ETABLISSEMENT)) {
			adr1 = atts.getValue("V");
			adr1NodeId = atts.getValue("ano:node-id");
		} else if (xpath.endsWith(CHEMIN_ADRESSE_2_ETABLISSEMENT)) {
			adr2 = atts.getValue("V");
			adr2NodeId = atts.getValue("ano:node-id");
		} else if (xpath.endsWith(CHEMIN_ADRESSE_3_ETABLISSEMENT)) {
			adr3 = atts.getValue("V");
			adr3NodeId = atts.getValue("ano:node-id");
		} else if (xpath.endsWith(CHEMIN_ADRESSE_CODE_POSTAL_ETABLISSEMENT)) {
			cp = atts.getValue("V");
			cpNodeId = atts.getValue("ano:node-id");
		} else if (xpath.endsWith(CHEMIN_ADRESSE_VILLE_ETABLISSEMENT)) {
			ville = atts.getValue("V");
			villeNodeId = atts.getValue("ano:node-id");
		}
	}

	public Vector<Anomalie> endElement(String uri, String localName, String qName, String xpath) throws SAXException {
		if (xpath.endsWith(ON_RETURN_ANOMALIES)) {
			// TEST
			Vector<Node>nodes = new Vector<Node>();
			if (!"".equals(adr1) && adr1.length()<2) nodes.add(new Node(adr1NodeId));
			if (!"".equals(adr2) && adr2.length()<2) nodes.add(new Node(adr2NodeId));
			if (!"".equals(adr3) && adr3.length()<2) nodes.add(new Node(adr3NodeId));
			if (!"".equals(cp) && cp.length()<5) nodes.add(new Node(cpNodeId));
			if (!"".equals(ville) && ville.length()<2) nodes.add(new Node(villeNodeId));
			// ANOMALIE
			if (nodes.size()>0) {
				String msg = getDocumentModel().getControlById(CTRL_ID).getMessage().getHtmlContent();
				msg = msg.replaceAll(REPLACE_PAYE, pk_annee+" / "+pk_mois+" / "+pk_matricule);
				String path="";
				String anoId = IdGenerator.nextId();
				String libelleLien = "";
				Hashtable<String, Object> xslParams = new Hashtable<String, Object>();
				xslParams.put("elementId", "Paye");
				xslParams.put("browser-destination", "internal");
				xslParams.put("anoId", anoId);
				Anomalie ano = new Anomalie(anoId,
											getDocumentModel().getControlById(CTRL_ID).getId(),
											getDocumentModel().getControlById(CTRL_ID).getLibelle(),
											getDocumentModel().getDocumentId(),
											"etatPaye",
											"payeIndivMensuel", // élément importable
											refNodeId, // id élément importable
											localName,
											libelleLien,
											msg,
											getDocumentModel().getControlById(CTRL_ID).getRegle().getHtmlContent(),
											getDocumentModel().getControlById(CTRL_ID).getSeverity(),
											path, // xpath vers le bordereau sous lequel se trouve l'anomalie
											xslParams); // paramètres supplémentaires pour accéder à l'élément en anomalie
				ano.addAll(nodes);
				anos.add(ano);
			}
			return anos;
		} else {
			return null;
		}
	}

	public void characters(char[] ch, int start, int length, String xpath)
			throws SAXException {
		// TODO Raccord de méthode auto-généré

	}

	public void endDocument() throws SAXException {
		// TODO Raccord de méthode auto-généré

	}

	public void endPrefixMapping(String prefix, String xpath)
			throws SAXException {
		// TODO Raccord de méthode auto-généré

	}

	public void ignorableWhitespace(char[] ch, int start, int length,
			String xpath) throws SAXException {
		// TODO Raccord de méthode auto-généré

	}

	public void processingInstruction(String target, String data, String xpath)
			throws SAXException {
		// TODO Raccord de méthode auto-généré

	}

	public void setDocumentLocator(Locator locator) {
		// TODO Raccord de méthode auto-généré

	}

	public void setParameters(Hashtable<String, Object> params) {
		// TODO Raccord de méthode auto-généré

	}

	public void skippedEntity(String name, String xpath) throws SAXException {
		// TODO Raccord de méthode auto-généré

	}

	public void startPrefixMapping(String prefix, String uri, String xpath)
			throws SAXException {
		// TODO Raccord de méthode auto-généré

	}

}
