/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.fp.person.rules.save;

import com.fp.common.helper.Constant;
import com.fp.dto.save.SaveBean;
import com.fp.dto.save.SaveRequest;
import com.fp.persistence.commondb.PersistenceHelper;
import com.fp.persistence.pcustomer.company.TcustCompany;
import com.fp.persistence.pcustomer.company.TcustCompanyKey;
import com.fp.persistence.pcustomer.company.TcustCompanyManagers;
import com.fp.persistence.pcustomer.gene.TcustPersonDetail;
import com.fp.persistence.pgeneral.gene.OptmListaNegra;
import java.util.List;

/**
 * Clase que verifica si la compania se encuentra registrada en el CONSEP
 *
 * @author hinga
 */
public class VerifyInConsepCompany extends VerifyInConsep {

    /**
     * Ejecuta la transaccion en modo normal.<br> Verifica que la persona que se intenta regidtrar se encuentra o no en
     * la lista negra (CONSEP).
     *
     * @param pSaveRequest Objeto que almacena referencia a las tablas de que intervienen en el proceso con sus datos.
     * @throws Exception
     * @return pSaveRequest Objeto que almacena referencia a las tablas de que intervienen en el proceso con sus datos.
     */
    @Override
    public SaveRequest normalProcess(SaveRequest pSaveRequest) throws Exception {
        String firsname = null, transaction = null, identificationConsep = null;
        identificationConsep = (String) pSaveRequest.get("identification");
        if (pSaveRequest.get("personcode") != null) {
            this.verifyChangeIdentificationPeople(pSaveRequest, identificationConsep);
        }
        SaveBean saveBean = (SaveBean) pSaveRequest.get("TCUSTCOMPANYMANAGERS");
        if (saveBean != null) {
            List<Object> lTcustCompanyManagers = saveBean.getModifiedRecords();
            for (int i = 0; i < lTcustCompanyManagers.size(); i++) {
                TcustCompanyManagers tcustCompanyManagers = (TcustCompanyManagers) lTcustCompanyManagers.get(i);
                if (tcustCompanyManagers.getPk() != null && tcustCompanyManagers.getPk().getManagerpersoncode() != null) {
                    TcustPersonDetail tcustPersonDetail = TcustPersonDetail.find(PersistenceHelper.getEntityManager(),
                            tcustCompanyManagers.getPk().getManagerpersoncode());
                    if (tcustPersonDetail != null && tcustPersonDetail.getIdentification() != null) {
                        List<OptmListaNegra> ldata = OptmListaNegra.find(PersistenceHelper.getEntityManager(), tcustPersonDetail.getIdentification());
                        String typeItem;
                        String isnew = (String) tcustCompanyManagers.get("isnew");
                        if (isnew != null && isnew.compareTo("Y") == 0) {
                            typeItem = "N";
                        } else {
                            typeItem = "U";
                        }
                        if (identificationConsep.compareTo(tcustPersonDetail.getIdentification()) != 0) {
                            this.fillConsep(pSaveRequest, ldata, tcustPersonDetail.getIdentification(), tcustPersonDetail.getName(), null, null, null, typeItem, true);
                        }
                    }
                }
            }
        }
        if (pSaveRequest.get("personcode") == null || "".equals(((Integer) pSaveRequest.get("personcode")).toString())) {
            transaction = "N";
        } else {
            transaction = "U";
        }
        firsname = (String) pSaveRequest.get("socialreason");
        if (identificationConsep != null) {
            List<OptmListaNegra> ldata = OptmListaNegra.find(PersistenceHelper.getEntityManager(), identificationConsep);
            this.fillConsep(pSaveRequest, ldata, identificationConsep, firsname, null, null, null, transaction, false);
        }
        return pSaveRequest;
    }

    /**
     * Ejecuta la transaccion en modo reverso.<br>
     *
     * @param pSaveRequest Objeto que almacena referencia a las tablas de que intervienen en el proceso con sus datos.
     * @throws Exception
     */
    @Override
    public SaveRequest reverseProcess(SaveRequest pSaveRequest) throws Exception {
        return pSaveRequest;
    }
}
