package com.ejie.ab61.adaptadores.helper;

import com.ejie.t65.jaxb.beans.fsd.StringListDocument;
import com.ejie.t65.jaxb.beans.fsd.T65ContentListType;
import com.ejie.t65.jaxb.beans.fsd.T65ContentType;
import com.ejie.t65.jaxb.beans.fsd.T65DocumentIDListType;
import com.ejie.t65.jaxb.beans.fsd.T65DocumentIDType;
import com.ejie.t65.jaxb.beans.fsd.T65StringListType;
import com.ejie.ab61.comun.utilidades.Constructores;
import com.ejie.ab61.model.dokusi.Metadato;
import java.util.List;

/**
 * 
 *  
 * 
 */
public class DokusiHelper {

	/**
	 * Constructor
	 */
	private DokusiHelper() {
		super();
	}

	public static final String CONTENT_TYPE_MSEXCEL = "application/vnd.ms-excel";
	public static final String CONTENT_TYPE_MSEXCELX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
	public static final String EXTENSION_XLS = ".xls";
	public static final String EXTENSION_XLSX = ".xlsx";

	/**
	 * étodo que crea el xml documentID
	 * 
	 * @param oidDokusi
	 *            id del documento en dokusi
	 * @return String documentIDRetornado
	 */
	public static String crearXMLdocumentID(String oidDokusi) {

		T65DocumentIDListType t65DocumentIDListType = T65DocumentIDListType.Factory.newInstance();
		T65DocumentIDType t65DocumentIDType = t65DocumentIDListType.addNewDocumentID();
		t65DocumentIDType.setId(oidDokusi);

		return t65DocumentIDListType.xmlText();
	}

	/**
	 * MÃ©todo que crea el xml content
	 * 
	 * @param pathPif
	 *            String
	 * @return saveTask String
	 */
	public static String crearXMLContentPif(String pathPif) {

		T65ContentListType contentToStore = T65ContentListType.Factory.newInstance();
		T65ContentType content = contentToStore.addNewContent();

		content.setPifId(pathPif);

		return contentToStore.xmlText();
	}

	/**
	 * Metodo que crea el xml ReturnedAttributeKeysList
	 * 
	 * @param metadatos
	 *            List<Metadato>
	 * 
	 * @return xmlReturnedAttributeKeysList String
	 * @throws Exception
	 *             Exception
	 */
	public static String crearXMLReturnedAttributeKeysList(List<Metadato> metadatos) throws Exception {
		StringListDocument listaAtributos = StringListDocument.Factory.newInstance();

		T65StringListType t65BStringListType = listaAtributos.addNewStringList();

		// A�adimos los metadatos al documento
		if (metadatos != null && metadatos.size() > 0) {
			for (Metadato metadato : metadatos) {

				t65BStringListType.addElement(metadato.getNombre());
			}
		}
		return DokusiHelper.quitarNameSpacesString(listaAtributos);
	}

	/**
	 * quitarNameSpacesString(com.bea.xml.XmlObject oXmlEntrada)
	 * 
	 * @param oXmlEntrada
	 *            XmlObject
	 * @return String
	 * @exception Exception
	 *                e
	 */
	public static String quitarNameSpacesString(com.bea.xml.XmlObject oXmlEntrada) throws Exception {

		com.bea.xml.XmlObject xmlO = DokusiHelper.quitarNameSpaces(oXmlEntrada);

		return (xmlO != null) ? xmlO.xmlText() : "";
	}

	/**
	 * quitarNameSpaces(com.bea.xml.XmlObject oXmlEntrada)
	 * 
	 * @param oXmlEntrada
	 *            XmlObject
	 * @return XmlObject
	 * @exception Exception
	 *                e
	 */
	public static com.bea.xml.XmlObject quitarNameSpaces(com.bea.xml.XmlObject oXmlEntrada) throws Exception {

		String sNuevoNameSpace;

		com.bea.xml.XmlObject oXmlRetorno = oXmlEntrada;
		com.bea.xml.XmlCursor oXmlCursor = oXmlRetorno.newCursor();

		oXmlCursor.toNextToken();

		while (oXmlCursor.hasNextToken()) {

			if (oXmlCursor.isNamespace()) {
				oXmlCursor.removeXml();
			} else {

				if (oXmlCursor.isStart()) {
					sNuevoNameSpace = oXmlCursor.getName().getLocalPart();
					oXmlCursor.setName(Constructores.newQName(sNuevoNameSpace));
				}
				oXmlCursor.toNextToken();
			}
		}
		oXmlCursor.dispose();

		return oXmlRetorno;

	}

}
