package com.fp.armas.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;

import org.apache.commons.lang.StringUtils;

import com.fp.armas.rules.exception.ControlArmasException;
import com.fp.persistence.commondb.PersistenceHelper;
import com.fp.persistence.pbpm.gene.TbpmGroups;
import com.fp.persistence.pbpm.gene.TbpmGroupsKey;
import com.fp.persistence.pgeneral.gene.TgeneProvince;
import com.fp.persistence.pgeneral.gene.TgeneProvinceKey;
import com.fp.simple.action.TaskAction;

/**
 * Clase que se encarga de buscar el grupo para asignar la tarea
 * una tarea.
 * 
 * @version 2.1
 */
public class ValidacionGrupo extends TaskAction {

    /*
     * (non-Javadoc)
     * 
     * @see com.fp.simple.action.TaskAction#assignOwner(com.fp.dto.Request)
     */
    @SuppressWarnings("unchecked")
    @Override
    public void assignOwner(Object pParam) throws Exception {
    	EntityManager em=PersistenceHelper.getEntityManager();
    	System.out.println("Ingresa a validar el grupo");
        HashMap<String, Object> m = (HashMap<String, Object>) pParam;
        Map<String, Object> mrequest = (Map<String, Object>) m.get("request");
        if (mrequest.get("cprovincia") == null) {
        	m.put("grupo",null);
        	throw new ControlArmasException("BPM-0014", "SIN PROVINCIA", "");
        }
        String cprovince=mrequest.get("cprovincia").toString();
        TgeneProvince province= TgeneProvince.find(em, new TgeneProvinceKey("EC", cprovince));
        
        if(province!=null && province.getPk()!=null){
        	 System.out.println(" PROVINCIA "+obtieneNombreLimpio(province.getDescription()));
        	 //se setea al grupo asignado
             m.put("grupo",m.get("grupo")+obtieneNombreLimpio(province.getDescription()));
             TbpmGroups groups= TbpmGroups.find(em, new TbpmGroupsKey(m.get("grupo").toString(), 1));
             if(groups!=null && groups.getPk()!=null ){
            	 System.out.println("...................GRUPO . "+m.get("grupo"));
            	 System.out.println("csolicitud  "+mrequest.get("csolicitud"));
            	 System.out.println("cdecomiso  "+mrequest.get("cdecomiso"));
             }else{
            	 m.put("grupo",null);
            	 throw new ControlArmasException("BPM-0012", "NO EXISTE EL GRUPO {0}", m.get("grupo"));
             	// throw new Exception("NO EXISTE EL GRUPO "+m.get("grupo")) ;
             }
        }else{
        	 throw new ControlArmasException("BPM-0013", "NO TIENE CENTRO DE CONTROL ASIGNADO LA  PROVINCIA {0}", cprovince);
        }
    }
    
    /* Reemplazo un string los caracteres en blanco y reemplazo caracteres especiales
    * @param toConvert
    * @return la cadena convertida
    */
   private String obtieneNombreLimpio(String toConvert){
   	return StringUtils.replaceEach(StringUtils.deleteWhitespace(toConvert), 
   			new String[]{"\u00e1","\u00e9","\u00ed","\u00f3","\u00fa","\u00c1","\u00c9","\u00cd","\u00d3","\u00da","\u00F1","\u00d1"}, 
   			new String[]{"a","e","i","o","u","A","E","I","O","U","n","N"});
   }
}
