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

import java.util.List;

import javax.persistence.Query;

import com.fp.armas.rules.query.webservices.util.PersonaUtil;
import com.fp.dto.query.QueryRequest;
import com.fp.dto.rules.QueryRule;
import com.fp.persistence.commondb.PersistenceHelper;
import com.fp.persistence.commondb.helper.FormatDates;
import com.fp.persistence.parmas.dto.ws.ImagenDto;
import com.fp.persistence.pcustomer.gene.TcustPersonDetail;

/**
 * Componente que retorna la foto o logo de una persona sea esta natural o jur&iacute;dica
 * @author dcruz
 *
 */
public class ImagenPersona extends QueryRule {

	private static final long serialVersionUID = -1019985182946646396L;
	public static final String QUERY_PERSONA_IDENT = "SELECT o FROM TcustPersonDetail o WHERE o.identification = :identification AND o.pk.dateto=:dateto";

	@SuppressWarnings("unchecked")
	@Override
	public QueryRequest process(QueryRequest pRequest) throws Exception {
		try {
			System.out.println("Query ImagenPersona....................................................................");
			String numeroidentificacion = (String) pRequest.get("NUMEROIDENTIFICACION");
			System.out.println(QUERY_PERSONA_IDENT+".........."+numeroidentificacion+" ..... "+FormatDates.getDefaultExpiryTimestamp());
			Query query = PersistenceHelper.getEntityManager().createQuery(QUERY_PERSONA_IDENT);
			query.setParameter("identification", numeroidentificacion);
			query.setParameter("dateto", FormatDates.getDefaultExpiryTimestamp());
			List<TcustPersonDetail> ltcustPersonDetails = query.getResultList();
			System.out.println("tamanio ......."+ltcustPersonDetails.size());
			if(ltcustPersonDetails != null && !ltcustPersonDetails.isEmpty()){
				TcustPersonDetail tcustPersonDetail = ltcustPersonDetails.iterator().next();
				ImagenDto imagen = PersonaUtil.getInstancia().obtenerImagen(tcustPersonDetail.getPk().getPersoncode());
				pRequest.getResponse().put("IMAGENPERSONA", imagen);
			}
		} catch (Throwable e) {
			
		}
		return pRequest;
	}

}
