package com.fp.frontend.controller.armas.solicitud; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import javax.annotation.PostConstruct; import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; import javax.faces.event.AjaxBehaviorEvent; import com.fp.common.logger.APPLogger; import com.fp.dto.Request; import com.fp.dto.Response; import com.fp.dto.query.DtoQuery; import com.fp.dto.query.SubQuery; import com.fp.dto.save.DtoSave; import com.fp.frontend.controller.AbstractController; import com.fp.frontend.controller.pgeneral.gene.CantonController; import com.fp.frontend.controller.pgeneral.gene.CatalogDetailController; import com.fp.frontend.controller.pgeneral.gene.CityController; import com.fp.frontend.controller.pgeneral.gene.ParroquiaController; import com.fp.frontend.controller.pgeneral.gene.ProvinceController; import com.fp.frontend.helper.MessageHelper; import com.fp.persistence.parmas.soli.TarmAgenciaSucursal; import com.fp.persistence.pgeneral.gene.TgeneCanton; import com.fp.persistence.pgeneral.gene.TgeneCatalogDetail; import com.fp.persistence.pgeneral.gene.TgeneCity; import com.fp.persistence.pgeneral.gene.TgeneParroquia; import com.fp.persistence.pgeneral.gene.TgeneProvince; /** * Controlador que maneja la entidad de {@link TarmAgenciaSucursal} * @author dcruz * */ @ManagedBean @ViewScoped public class AgenciaSucursalController extends AbstractController { private List estadoAgencias; private List tipoAgencias; private List lprovincias; private List lcantones; private List lparroquias; private List lcities; private static final long serialVersionUID = 4621335239110508027L; public AgenciaSucursalController() throws Exception{ super(TarmAgenciaSucursal.class); } @PostConstruct public void postConstruct() { this.init(); super.startQuery(); } private void init(){ this.beanalias = "AGENCIASUCURSAL"; this.lrecord = new ArrayList<>(); this.recperpage = 15; this.estadoAgencias = CatalogDetailController.find("ESTADOAGENCIA"); this.tipoAgencias = CatalogDetailController.find("TIPOAGENCIA"); } @SuppressWarnings("unchecked") @Override protected void querydatabase() { try { DtoQuery dto = this.getDtoQuery(); HashMap mtables = new HashMap(); mtables.put(beanalias, dto);//se puede aniadir mas tablas Request request = callerhelper.getRequest(); request.setQueryTables(mtables); Response response = callerhelper.executeQuery(request); if(response.getResponseCode().compareTo(Response.RESPONSE_OK) != 0){ this.lrecord = new ArrayList<>(); MessageHelper.setMessageError(response); } else{ this.lrecord = (List) response.get(beanalias); this.postQuery(lrecord); } } catch (Throwable e) { MessageHelper.setMessageError(e); } } @Override public void save() { try { DtoSave dto = super.getDtoSave(); if(!dto.pendingProcess()){ return; } dto.setReturnpk(Boolean.TRUE); HashMap msaves = new HashMap(); msaves.put(beanalias, dto); Request request = callerhelper.getRequest(); request.setSaveTables(msaves); Response response = callerhelper.executeSave(request); if(response.getResponseCode().compareTo(Response.RESPONSE_OK) == 0){ this.postCommit(response); MessageHelper.setMessageInfo(response); } else{ MessageHelper.setMessageError(response); } } catch (Throwable e) { MessageHelper.setMessageError(e); } } public DtoQuery getDtoQuery() throws Exception { DtoQuery dto = super.getDtoQuery(Boolean.TRUE); SubQuery subQueryTipo = new SubQuery("TgeneCatalogDetail", "description", "ntipo", "i.pk.catalog=t.tipo and i.pk.catalogcode=t.tipocodigo"); dto.addSubQuery(subQueryTipo); SubQuery subQueryEstado = new SubQuery("TgeneCatalogDetail", "description", "nestado", "i.pk.catalog=t.estado and i.pk.catalogcode=t.estadocodigo"); dto.addSubQuery(subQueryEstado); SubQuery subQueryProvincia = new SubQuery("TgeneProvince", "description", "nprovincia", "i.pk.provincecode=t.provincecode and i.pk.countrycode=t.countrycode"); dto.addSubQuery(subQueryProvincia); SubQuery subQueryCanton = new SubQuery("TgeneCanton", "description", "ncanton", "i.pk.countrycode=t.countrycode and i.pk.provincecode=t.provincecode and i.pk.cantoncode=t.cantoncode"); dto.addSubQuery(subQueryCanton); SubQuery subQueryParroquia = new SubQuery("TgeneParroquia", "description", "nparroquia", "i.pk.countrycode=t.countrycode and i.pk.provincecode=t.provincecode and i.pk.cantoncode=t.cantoncode and i.pk.parroquiacode=t.parroquiacode"); dto.addSubQuery(subQueryParroquia); SubQuery subQueryCiudad = new SubQuery("TgeneCity", "description", "nciudad", "i.pk.countrycode=t.countrycode and i.pk.provincecode=t.provincecode and i.pk.cantoncode=t.cantoncode and i.pk.citycode=t.citycode"); dto.addSubQuery(subQueryCiudad); return dto; } public void loadEdit() { lprovincias = ProvinceController.find(record.getCountrycode()); lcantones = CantonController.find(record.getCountrycode(), record.getProvincecode()); lparroquias = ParroquiaController.find(record.getCountrycode(), record.getProvincecode(), record.getCantoncode()); lcities = CityController.find(record.getCountrycode(), record.getProvincecode(), record.getCantoncode()); } @Override public void create() throws Exception { super.create(); this.record.setCountrycode("EC"); this.record.setTipocodigo("TIPOAGENCIA"); this.record.setEstadocodigo("ESTADOAGENCIA"); this.record.setFautorizacion(new Date()); if(this.record.isnew){ this.record.setEstado("REG"); } this.lprovincias = ProvinceController.find(record.getCountrycode()); this.executeProvince(); } /** * Método para el evento change del combo provincias */ public void executeProvince() { if ((record.getCountrycode() != null) && (record.getCountrycode().compareTo("") != 0)) { lcantones = CantonController.find(record.getCountrycode(), record.getProvincecode()); lparroquias = new ArrayList<>(); lcities = new ArrayList<>(); } } /** * Método para el evento change del combo cantones */ public void executeCanton() { if ((record.getCountrycode() != null) && (record.getCountrycode().compareTo("") != 0)) { lparroquias = ParroquiaController.find(record.getCountrycode(), record.getProvincecode(), record.getCantoncode()); lcities = CityController.find(record.getCountrycode(), record.getProvincecode(), record.getCantoncode()); } } /** * Seleccionado todos los registros de la tabla */ public void seleccionarTodos(AjaxBehaviorEvent event) { try { Boolean valorColocar = Boolean.FALSE; if(Boolean.valueOf(event.getComponent().getAttributes().get("value") != null ? event.getComponent().getAttributes().get("value").toString() : "false")){ valorColocar = Boolean.TRUE; } for (TarmAgenciaSucursal agenciaSucursal : lrecord) { agenciaSucursal.getModifiedData().put("seleccionado", valorColocar); } } catch (Throwable e) { MessageHelper.setMessageError(e); } } public static TarmAgenciaSucursal find(String cagencia) { TarmAgenciaSucursal agenciaSucursal = null; try { AgenciaSucursalController cc = new AgenciaSucursalController(); cc.addFilter("pk", cagencia); cc.init(); cc.recperpage = 300; cc.query(); if ((cc.lrecord != null) && !cc.lrecord.isEmpty()) { agenciaSucursal = cc.lrecord.get(0); } return agenciaSucursal; } catch (Throwable e) { MessageHelper.setMessageError(e); return agenciaSucursal; } } public List getEstadoAgencias() { return estadoAgencias; } public void setEstadoAgencias(List estadoAgencias) { this.estadoAgencias = estadoAgencias; } public List getTipoAgencias() { return tipoAgencias; } public void setTipoAgencias(List tipoAgencias) { this.tipoAgencias = tipoAgencias; } public List getLprovincias() { return lprovincias; } public void setLprovincias(List lprovincias) { this.lprovincias = lprovincias; } public List getLcantones() { return lcantones; } public void setLcantones(List lcantones) { this.lcantones = lcantones; } public List getLparroquias() { return lparroquias; } public void setLparroquias(List lparroquias) { this.lparroquias = lparroquias; } public List getLcities() { return lcities; } public void setLcities(List lcities) { this.lcities = lcities; } }