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

import com.fp.base.persistence.util.helper.GeneralDescriptions;
import com.fp.dto.Response;
import com.fp.dto.query.QueryRequest;
import com.fp.dto.rules.QueryRule;
import com.fp.general.db.DataHelper;
import com.fp.general.exception.GeneralException;
import com.fp.persistence.pgeneral.gene.TgeneSolicitude;

/**
 * Clase que se encarge de completar los datos de cabecera de una solicitud
 *
 * @author scastillo
 */
public class HeaderSolicitude extends QueryRule {

    String solicitudnumber;
    Integer solicitudsequence;

    @Override
    public QueryRequest process(QueryRequest pQueryRequest) throws Exception {
        Response response = pQueryRequest.getResponse();
        this.solicitudnumber = (String) pQueryRequest.get("solicitudnumber");
        this.solicitudsequence = (Integer) pQueryRequest.get("solicitudsequence");
        TgeneSolicitude tgeneSolicitude = DataHelper.getInstance().getTgeneSolicitude(this.solicitudnumber, this.solicitudsequence);
        if ((tgeneSolicitude == null) || (tgeneSolicitude.getModulecode().compareTo(pQueryRequest.getTransactionModule()) != 0)) {
            throw new GeneralException("GENE-0049", "SOLICITUD NO EXISTE");
        }
        if (tgeneSolicitude != null && tgeneSolicitude.getStatuscode().compareTo("COM") == 0) {
            /*TgeneSolicitudeStatusKey tgeneSolicitudeStatusKey = new TgeneSolicitudeStatusKey(tgeneSolicitude.getModulecode(), 
                    tgeneSolicitude.getStatuscode());
            TgeneSolicitudeStatus tgeneSolicitudeStatus = TgeneSolicitudeStatus.find(PersistenceHelper.getEntityManager(), 
                    tgeneSolicitudeStatusKey);
            throw new GeneralException("GENE-0046", "SOLICITUD EN ESTADO {0}", tgeneSolicitudeStatus.getDescription());*/
            throw new GeneralException("GENE-0049", "SOLICITUD NO EXISTE");
        }
        response.put("m", tgeneSolicitude.getModulecode());
        response.put("md", GeneralDescriptions.getModuledesc(tgeneSolicitude.getModulecode()));
        response.put("p", tgeneSolicitude.getProductcode());
        response.put("pd", GeneralDescriptions.getProductdesc(tgeneSolicitude.getModulecode(),
                tgeneSolicitude.getProductcode()));
        response.put("s", tgeneSolicitude.getSubproductcode());
        response.put("sd", GeneralDescriptions.getSubProductdesc(tgeneSolicitude.getModulecode(),
                tgeneSolicitude.getProductcode(), tgeneSolicitude.getSubproductcode()));
        response.put("c", tgeneSolicitude.getCurrencycode());
        return pQueryRequest;
    }
}
