package com.fp.general.security.rules.query;

import com.fp.bpm.query.Query;
import com.fp.dto.Response;
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.pcustomer.gene.TcustPersonDetail;
import com.fp.persistence.pcustomer.gene.TcustPersonDetailKey;
import com.fp.persistence.pgeneral.acco.TgeneAccount;
import com.fp.persistence.pgeneral.acco.TgeneAccountKey;
import com.fp.persistence.pgeneral.gene.TgeneSolicitude;
import com.fp.persistence.pgeneral.gene.TgeneSolicitudeKey;
import com.fp.persistence.pgeneral.safe.TsafeAudit;
import java.util.List;

/**
 * Clase que se encarga de completar datos como el nombre de cliente o nobre de
 * cuanta o prestamo
 *
 * @author scastillo
 */
public class CompleteAudit extends QueryRule {

    private static final String name = "name";

    /**
     * Metodo que se encarga de completar datos como el nombre de cliente nombre
     * de prestamo o cuenta
     *
     * @param qr
     * @return
     * @throws Exception
     */
    @Override
    public QueryRequest process(QueryRequest qr) throws Exception {
        Response response = qr.getResponse();
        Query query = new Query();
        query.process(qr);
        List<Object> lTsafeAudit = (List<Object>) response.get("TSAFEAUDIT");
        if (lTsafeAudit != null) {
            for (Object obj : lTsafeAudit) {
                TsafeAudit tsafeAudit = (TsafeAudit) obj;
                if (tsafeAudit.getPersoncode() != null) {
                    TcustPersonDetailKey tcustPersonDetailKey =
                            new TcustPersonDetailKey(
                            tsafeAudit.getPersoncode(),
                            FormatDates.getDefaultExpiryTimestamp());
                    TcustPersonDetail tcustPersonDetail = TcustPersonDetail.find(
                            PersistenceHelper.getEntityManager(),
                            tcustPersonDetailKey);
                    if (tcustPersonDetail != null) {
                        tsafeAudit.addAddtionalInfo(name,
                                tcustPersonDetail.getName());
                    } else {
                        tsafeAudit.addAddtionalInfo(name, null);
                    }
                } else if (tsafeAudit.getSolicitudenumber() != null) {
                    TgeneSolicitudeKey tgeneSolicitudeKey =
                            new TgeneSolicitudeKey();
                    TgeneSolicitude tgeneSolicitude = TgeneSolicitude.find(
                            PersistenceHelper.getEntityManager(),
                            tgeneSolicitudeKey);
                    if (tgeneSolicitude != null) {
                        tsafeAudit.addAddtionalInfo(name,
                                tgeneSolicitude.getAccountname());
                    } else {
                        tsafeAudit.addAddtionalInfo(name, null);
                    }
                } else if (tsafeAudit.getAccount() != null) {
                    TgeneAccountKey tgeneAccountKey = new TgeneAccountKey(
                            tsafeAudit.getAccount(), qr.getCompany(),
                            FormatDates.getDefaultExpiryTimestamp());
                    TgeneAccount tgeneAccount = TgeneAccount.find(
                            PersistenceHelper.getEntityManager(),
                            tgeneAccountKey);
                    if (tgeneAccount != null) {
                        tsafeAudit.addAddtionalInfo(name,
                                tgeneAccount.getAccountname());
                    } else {
                        tsafeAudit.addAddtionalInfo(name, null);
                    }
                } else {
                    tsafeAudit.addAddtionalInfo(name, null);
                }
            }
        }
        return qr;
    }
}
