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

import com.fp.dto.query.QueryBean;
import com.fp.dto.query.QueryRequest;
import com.fp.dto.rules.QueryRule;
import com.fp.persistence.commondb.GeneralQuery;
import com.fp.persistence.commondb.PersistenceHelper;
import com.fp.persistence.commondb.helper.FormatDates;
import com.fp.persistence.pcustomer.gene.TcustPersonDetail;
import com.fp.persistence.pgeneral.gene.TgeneParameters;
import com.fp.persistence.pgeneral.gene.TgeneParametersKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.Query;

/**
 *
 * @author BPTWPA
 */
public class LovOfficial extends QueryRule {

    private static final long serialVersionUID = 1L;
    /**
     * JPQL que presenta los funcionarions del BEV
     */
    private static final String JPQL_OFFICIAL = "from TcustPersonDetail t "
            + "where t.pk.personcode in ("
            + "select f.pk.personcode "
            + "from TcustPeopleJob f "
            + "where f.employerpersoncode = :bevcode "
            + "and f.pk.dateto = :datetojob "
            + "and f.departuredate = null)";

    @Override
    public QueryRequest process(QueryRequest pQueryRequest) throws Exception {
        StringBuilder jpql = new StringBuilder();
        QueryBean qb = (QueryBean) pQueryRequest.get("OFFICIALLOV");
        Map<String, Object> mcriteria = new HashMap<String, Object>();
        TgeneParametersKey tgeneParametersKey = new TgeneParametersKey("BEVCODE", Integer.valueOf(pQueryRequest.get("COMPANY").toString()));
        TgeneParameters tgeneParameters = TgeneParameters.find(PersistenceHelper.getEntityManager(), tgeneParametersKey);
        if (tgeneParameters.getNumbervalue() == null) {
            return pQueryRequest;
        }
        jpql.append(LovOfficial.JPQL_OFFICIAL);
        GeneralQuery.addParameters(qb, jpql, mcriteria);
        Query qry = PersistenceHelper.getEntityManager().createQuery(String.valueOf(jpql));
        qry.setParameter("datetojob", FormatDates.getDefaultExpiryTimestamp());
        qry.setParameter("bevcode", Integer.valueOf(tgeneParameters.getNumbervalue().intValue()));
        GeneralQuery.setParameters(qb, mcriteria, qry);
        List<TcustPersonDetail> ldata = qry.getResultList();
        if (!ldata.isEmpty()) {
            List<Map<String, Object>> lresp = new ArrayList<Map<String, Object>>();
            for (TcustPersonDetail obj : ldata) {
                Map<String, Object> m = new HashMap<String, Object>();
                m.put("pk.personcode", obj.getPk().getPersoncode());
                m.put("pk.dateto", obj.getPk().getDateto());
                m.put("identification", obj.getIdentification());
                m.put("name", obj.getName());
                lresp.add(m);
            }
            pQueryRequest.getResponse().put("OFFICIALLOV", lresp);
        }
        return pQueryRequest;
    }
}
