package com.fp.armas.rules.query.webservices.util;

import com.fp.common.logger.APPLogger;
import com.fp.persistence.commondb.PersistenceHelper;
import com.fp.persistence.commondb.exception.CommondbException;
import com.fp.persistence.pgeneral.gene.TgeneCatalogDetail;
import com.fp.persistence.pgeneral.gene.TgeneCatalogDetailKey;

public class CatalogoUtil {

	private static CatalogoUtil INSTANCIA = new CatalogoUtil();
	
	private CatalogoUtil(){}
	
	/**
	 * Retorna una instancia de la clase
	 * @return
	 */
	public static CatalogoUtil getInstancia() {
		return INSTANCIA;
	}
	
	/**
	 * Retorna la descripci&oacute;n del cat&aacute;logo
	 * @param catalogo
	 * @param catalogocodigo
	 * @return
	 */
	public String getDescripcion(String catalogo, String catalogocodigo) {
		String descripcion = null;
		try {
			if(catalogo != null && catalogocodigo != null){
				TgeneCatalogDetail tgeneCatalogDetail = TgeneCatalogDetail.find(PersistenceHelper.getEntityManager(), new TgeneCatalogDetailKey(catalogo, catalogocodigo));
				if(tgeneCatalogDetail != null){
					descripcion = tgeneCatalogDetail.getDescription();
				}
			}
		} catch (CommondbException e) {
			APPLogger.getLogger("WEBSERVICES").error(e.getMessage(), e);
		} catch (Exception e) {
			APPLogger.getLogger("WEBSERVICES").error(e.getMessage(), e);
		}
		return descripcion;
	}
	
	/**
	 * Devuelve el legalcode del cat&aacute;logo
	 * @param catalogo
	 * @param catalogocodigo
	 * @return
	 */
	public String getLegalCode(String catalogo, String catalogocodigo) {
		String legalcode = null;
		try {
			if(catalogo != null && catalogocodigo != null){
				TgeneCatalogDetail tgeneCatalogDetail = TgeneCatalogDetail.find(PersistenceHelper.getEntityManager(), new TgeneCatalogDetailKey(catalogo, catalogocodigo));
				if(tgeneCatalogDetail != null){
					legalcode = tgeneCatalogDetail.getLegalcode();
				}
			}
		} catch (Throwable e) {
			APPLogger.getLogger("WEBSERVICES").error(e.getMessage(), e);
		}
		return legalcode;
	}
}
