package com.fp.person.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.pcustomer.gene.TcustPersonAddress;
import com.fp.persistence.pcustomer.gene.TcustPersonPhone;
import com.fp.person.db.DataHelper;
import java.util.List;

/**
 * Clase que se encarga de completar la direccion del telefono.
 * @author Wilson Sigcho
 * @version 2.1
 */
public class PhonesPerClient extends QueryRule {

    /**
     * Metodo que agrega la direccion asociada a un telefono, cuando una consulta es requerida.
     * @param pQueryRequest Objeto que posee la consulta requerida.
     * @return pQueryRequest
     * @throws Exception
     */
    public QueryRequest process(QueryRequest pQueryRequest) throws Exception {
        Response response = pQueryRequest.getResponse();
        Query qry = new Query();
        qry.process(pQueryRequest);
        List<Object> lobjects = (List<Object>) response.get("TCUSTPERSONPHONELOV");
        if (lobjects == null) {
            return pQueryRequest;
        }
        for (Object obj : lobjects) {
            TcustPersonPhone tcustPersonPhone = (TcustPersonPhone) obj;
            if (tcustPersonPhone.getPk().getPersoncode() != null
                    && tcustPersonPhone.getAddressnumber() != null) {
                TcustPersonAddress tcustPersonAddress = DataHelper.getInstance().getTcustPersonAddress(tcustPersonPhone.getPk().getPersoncode(), tcustPersonPhone.getAddressnumber());
                tcustPersonPhone.addAddtionalInfo("addressdesc", tcustPersonAddress.getAddress());
            } else {
                tcustPersonPhone.addAddtionalInfo("addressdesc", null);
            }
        }
        return pQueryRequest;
    }
}
