package com.ejie.m62fb.util;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpSession;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import com.ejie.m62fb.model.M62fbr49s0s00;
import com.ejie.m62fb.service.registroVPP.M62fbr49s0s00Service;
import com.ejie.m62fb.utils.constants.AppConstantes;

/**
 * * Utilidades
 * 
XXXX
 */
public class UtilsXML {

	private UtilsXML() {
	}

	@Autowired
	private static M62fbr49s0s00Service M62fbr49s0s00Service;

	/**
	 * isTecnicoAutenticado comprueba si en la session del navegador existe
	 * técnico autenticado
	 * 
	 * @param session
	 * @return
	 */
	public static boolean isTecnicoAutenticado(HttpSession session) {
		// Técnico Autenticado?¿
		/*
		 * LoginForm loginForm = (LoginForm) session.getAttribute("loginForm");
		 * 
		 * if (loginForm == null) { return false; } if
		 * (!loginForm.isAutentificado()) { return false; }
		 */
		return true;
	}

	/**
	 * 
	 */
	public static boolean isValidXml(
			ReloadableResourceBundleMessageSource messageSource,
			String contextPath, String filenameXML, String tipoSchemaXML,
			ArrayList<String> listaErrores, M62fbr49s0s00 ficheroR49) {

		List errors = new LinkedList();
		errors = new ArrayList();
		List warnings = new LinkedList();
		warnings = new ArrayList();
		// Creando la factoria e indicando que hay validacion
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
				.newInstance();
		documentBuilderFactory.setNamespaceAware(true);
		documentBuilderFactory.setValidating(true);
		StringBuffer sbError = new StringBuffer();

		try {

			// Configurando el Schema de validacion
			documentBuilderFactory.setAttribute(
					AppConstantes.JAXP_SCHEMA_LANGUAGE,
					AppConstantes.W3C_XML_SCHEMA);
			// establecer el esquema SegSemanal.xsd
			// documentBuilderFactory.setAttribute(
			// ConstantsApp.JAXP_SCHEMA_SOURCE, contextPath
			// + ConstantsApp.ALOKABIDE_SCHEMA);

			documentBuilderFactory.setAttribute(
					AppConstantes.JAXP_SCHEMA_SOURCE, contextPath
							+ "/xsd/fichero" + tipoSchemaXML + ".xsd");

			// Parseando el documento
			DocumentBuilder documentBuilder = documentBuilderFactory
					.newDocumentBuilder();
			documentBuilder.setErrorHandler(new ParserErrorHandler(warnings,
					errors, messageSource));

			Document parse = documentBuilder.parse(new File(filenameXML));

		} catch (SAXException saxEx) {
			listaErrores.add(saxEx.getMessage());
			ficheroR49.setESTADO("VALIDACION XML FALLIDA");
			if (ficheroR49.getERRORES() != ""
					&& ficheroR49.getERRORES() != null) {
				ficheroR49.setERRORES(ficheroR49.getERRORES()
						+ saxEx.getMessage().replaceAll("[^a-zA-Z]+", " "));
			} else {
				ficheroR49.setERRORES(saxEx.getMessage().replaceAll(
						"[^a-zA-Z]+", " "));
			}
			return false;
			// throw new MethodFailureException(saxEx.getMessage());
		} catch (Exception ex) {
			listaErrores.add(ex.getMessage());
			ficheroR49.setESTADO("VALIDACION XML FALLIDA");
			if (ficheroR49.getERRORES() != ""
					&& ficheroR49.getERRORES() != null) {
				ficheroR49.setERRORES(ficheroR49.getERRORES()
						+ ex.getMessage().replaceAll("[^a-zA-Z]+", " "));
			} else {
				ficheroR49.setERRORES(ex.getMessage().replaceAll("[^a-zA-Z]+",
						" "));
			}
			return false;
			// throw new MethodFailureException(ex.getMessage());
		}
		return true;
	}

}
