diff --git a/src/main/java/br/ufpr/c3sl/estacaojuventude/web/OcorrenciaMunicipalController.java b/src/main/java/br/ufpr/c3sl/estacaojuventude/web/OcorrenciaMunicipalController.java index 305e6edd688482b7289f8f2be0d1739b42398298..b0d89488ee9a15bcc0eb8fb5ebcb40ce058ef157 100644 --- a/src/main/java/br/ufpr/c3sl/estacaojuventude/web/OcorrenciaMunicipalController.java +++ b/src/main/java/br/ufpr/c3sl/estacaojuventude/web/OcorrenciaMunicipalController.java @@ -1,5 +1,6 @@ package br.ufpr.c3sl.estacaojuventude.web; +import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.HashSet; @@ -20,6 +21,7 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.SessionAttributes; import br.ufpr.c3sl.estacaojuventude.Localizacao; import br.ufpr.c3sl.estacaojuventude.Municipio; @@ -33,10 +35,20 @@ import br.ufpr.c3sl.estacaojuventude.enums.TipoStatus; @RequestMapping("/ocorrenciamunicipais") @Controller +@SessionAttributes( { "ocorrenciaMunicipal" }) @RooWebScaffold(path = "ocorrenciamunicipais", formBackingObject = OcorrenciaMunicipal.class) public class OcorrenciaMunicipalController { OcorrenciaMunicipal ocorrenciaMunicipalGlobal; + private int getIndexOrgao(OrgaoExecutor orgao, List<OrgaoExecutor> orgaos){ + for(int i=0; i < orgaos.size();i++){ + if(orgao.getId().equals(orgaos.get(i).getId())){ + return i; + } + } + return -1; + } + private void refreshRelations() { if (ocorrenciaMunicipalGlobal.getProgramaVinculado() != null) ocorrenciaMunicipalGlobal.setProgramaVinculado(Programa.findPrograma(ocorrenciaMunicipalGlobal.getProgramaVinculado().getId())); @@ -59,23 +71,29 @@ public class OcorrenciaMunicipalController { ocorrenciaMunicipalGlobal.setUsuario(Usuario.findUsuario(ocorrenciaMunicipalGlobal.getUsuario().getId())); } - @RequestMapping(value = "/form/{id}", produces = "text/html") - public String treatSelected(@PathVariable("id") Long id, Model uiModel, HttpServletRequest httpServletRequest) { + @RequestMapping(value = "/form/{ids}", method = {RequestMethod.POST,RequestMethod.PUT}, produces = "text/html") + public String treatSelected(@Valid OcorrenciaMunicipal ocorrenciaMunicipal, @PathVariable("ids") String ids, Model uiModel, HttpServletRequest httpServletRequest) { + + ocorrenciaMunicipalGlobal = ocorrenciaMunicipal; refreshRelations(); - if (OrgaoExecutor.findOrgaoExecutor(id) != null) { - if (ocorrenciaMunicipalGlobal.getOrgaosExecutores() == null) { - Set<OrgaoExecutor> orgaosExecutores = new HashSet<OrgaoExecutor>(); - orgaosExecutores.add(OrgaoExecutor.findOrgaoExecutor(id)); - - ocorrenciaMunicipalGlobal.setOrgaosExecutores(orgaosExecutores); - } - else { - Set<OrgaoExecutor> orgaosExecutoresNovo = ocorrenciaMunicipalGlobal.getOrgaosExecutores(); - if (orgaosExecutoresNovo.contains(OrgaoExecutor.findOrgaoExecutor(id))) - orgaosExecutoresNovo.remove(OrgaoExecutor.findOrgaoExecutor(id)); - else - orgaosExecutoresNovo.add(OrgaoExecutor.findOrgaoExecutor(id)); - ocorrenciaMunicipalGlobal.setOrgaosExecutores(orgaosExecutoresNovo); + for(String idString : ids.split("\\+")){ + Long id = Long.parseLong(idString); + if (OrgaoExecutor.findOrgaoExecutor(id) != null) { + if (ocorrenciaMunicipalGlobal.getOrgaosExecutores() == null) { + Set<OrgaoExecutor> orgaosExecutores = new HashSet<OrgaoExecutor>(); + orgaosExecutores.add(OrgaoExecutor.findOrgaoExecutor(id)); + + ocorrenciaMunicipalGlobal.setOrgaosExecutores(orgaosExecutores); + } + else { + List<OrgaoExecutor> orgaosExecutoresNovo = new ArrayList<OrgaoExecutor>(ocorrenciaMunicipalGlobal.getOrgaosExecutores()); + int index = getIndexOrgao(OrgaoExecutor.findOrgaoExecutor(id), orgaosExecutoresNovo); + if (index != -1) + orgaosExecutoresNovo.remove(index); + else + orgaosExecutoresNovo.add(OrgaoExecutor.findOrgaoExecutor(id)); + ocorrenciaMunicipalGlobal.setOrgaosExecutores(new HashSet<OrgaoExecutor>(orgaosExecutoresNovo)); + } } } populateEditForm(uiModel, ocorrenciaMunicipalGlobal); @@ -89,9 +107,12 @@ public class OcorrenciaMunicipalController { } } - @RequestMapping(value = "selecionar", method = {RequestMethod.POST, RequestMethod.PUT}, produces = "text/html") + @RequestMapping(value = "selecionar", method = {RequestMethod.GET, RequestMethod.PUT}, produces = "text/html") public String selectForm(@Valid OcorrenciaMunicipal ocorrenciaMunicipal, @RequestParam(value = "tipo", required = false) String type, @RequestParam(value = "op", required = false) Integer op, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) { - ocorrenciaMunicipalGlobal = ocorrenciaMunicipal; + + if(ocorrenciaMunicipalGlobal == null){ + ocorrenciaMunicipalGlobal = ocorrenciaMunicipal; + } if (op == 0) { if (type.equals("orgaosExecutores")) { @@ -99,6 +120,12 @@ public class OcorrenciaMunicipalController { uiModel.addAttribute("orgaoexecutors", OrgaoExecutor.findAllOrgaoExecutors()); else { List<OrgaoExecutor> oe = OrgaoExecutor.findAllOrgaoExecutors(); + for(OrgaoExecutor orgao : ocorrenciaMunicipal.getOrgaosExecutores()){ + int index = getIndexOrgao(orgao, oe); + if(index != -1){ + oe.remove(index); + } + } oe.removeAll(ocorrenciaMunicipal.getOrgaosExecutores()); uiModel.addAttribute("orgaoexecutors", oe); } diff --git a/src/main/java/br/ufpr/c3sl/estacaojuventude/web/ProgramaController.java b/src/main/java/br/ufpr/c3sl/estacaojuventude/web/ProgramaController.java index 1c301169e74bb32070845169fa5f5126f5b507ba..12e705b86309b0e4f41707f2752cf8974ecd4bfb 100644 --- a/src/main/java/br/ufpr/c3sl/estacaojuventude/web/ProgramaController.java +++ b/src/main/java/br/ufpr/c3sl/estacaojuventude/web/ProgramaController.java @@ -45,12 +45,6 @@ import br.ufpr.c3sl.estacaojuventude.enums.TipoTemporalidade; public class ProgramaController { private Programa programaGlobal; - public void safePrintln(String s) { - synchronized (System.out) { - System.out.println(s); - } - } - private int getIndexTematica(Tematica tematica, List<Tematica> tematicas){ for(int i=0; i < tematicas.size();i++){ if(tematica.getId().equals(tematicas.get(i).getId())){ @@ -96,24 +90,12 @@ public class ProgramaController { programaGlobal.setUsuario(Usuario.findUsuario(programaGlobal.getUsuario().getId())); } - /*@RequestMapping(value = "?form", method = RequestMethod.GET, produces = "text/html") - public String resetProgramaSession(@Valid Programa programa, Model uiModel, HttpServletRequest httpServletRequest){ - safePrintln(" -- resetando --"); - uiModel.addAttribute("programa", null); - programaGlobal=null; - programa=null; - safePrintln(" -- programa resetado: "+uiModel.asMap().get("programa")); - return "programas/create"; - }*/ - @RequestMapping(value = "/form/{ids}", method = {RequestMethod.POST,RequestMethod.PUT}, produces = "text/html") public String treatSelected(@Valid Programa programa, @PathVariable("ids") String ids, Model uiModel, HttpServletRequest httpServletRequest) { programaGlobal = programa; refreshRelations(); - safePrintln(" -- programa global treat -- "); - safePrintln(" -- tematicas treat antes :"+programaGlobal.toString()); for(String idString : ids.split("\\+")){ Long id = Long.parseLong(idString); if (Tematica.findTematica(id) != null) { @@ -128,11 +110,9 @@ public class ProgramaController { List<Tematica> tematicasNova = new ArrayList<Tematica>(programaGlobal.getTematicas()); int index = getIndexTematica(Tematica.findTematica(id), tematicasNova); if (index != -1){ - safePrintln(" -- removendo id "+id); tematicasNova.remove(index); } else{ - safePrintln(" -- adicionando id "+id); tematicasNova.add(Tematica.findTematica(id)); } programaGlobal.setTematicas(new HashSet<Tematica>(tematicasNova)); @@ -157,18 +137,7 @@ public class ProgramaController { } } - - safePrintln(" -- tematicas treat depois : "+programaGlobal.getTematicas().toString()); populateEditForm(uiModel, programaGlobal); - safePrintln(" -- model treat -- "); - if(uiModel.asMap().isEmpty()){ - safePrintln(" -- vazio --"); - }else{ - for(Entry entry : uiModel.asMap().entrySet()){ - if(entry.getKey().equals("programa")) - safePrintln(" -- "+entry.getKey()+" treat = "+entry.getValue()+" -- "); - } - } if (programaGlobal.getId() == null) { programaGlobal = null; @@ -182,27 +151,11 @@ public class ProgramaController { @RequestMapping(value = "selecionar", method = {RequestMethod.GET, RequestMethod.PUT}, produces = "text/html") public String selectForm(@Valid Programa programa, @RequestParam(value = "tipo", required = false) String type, @RequestParam(value = "op", required = false) Integer op, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) { - safePrintln("-------------------------------------------------------"); if(programaGlobal == null){ - safePrintln(" -- programa global selecionar null -- "); programaGlobal = programa; } - safePrintln(" -- programa global selecionar : "+programaGlobal.toString()); - safePrintln(" -- programa selecionar : "+programa.toString()); - - safePrintln(" -- model selecionar -- "); - if(uiModel.asMap().isEmpty()){ - safePrintln(" -- vazio --"); - }else{ - for(Entry entry : uiModel.asMap().entrySet()){ - safePrintln(" -- "+entry.getKey()+" selecionar = "+entry.getValue()+" -- "); - } - } - - - if (op == 0) { if (type.equals("tematicas")) { if (programa.getTematicas() == null){ @@ -221,8 +174,6 @@ public class ProgramaController { TematicaController tc = new TematicaController(); tc.addDateTimeFormatPatterns(uiModel); - safePrintln("-------------------------------------------------------"); - return "tematicas/select"; } else if (type.equals("orgaosExecutores")) { @@ -246,7 +197,6 @@ public class ProgramaController { } else if (op == 1) { if (type.equals("tematicas")) { - safePrintln("-------------------------------------------------------"); if (programa.getTematicas() != null) { uiModel.addAttribute("tematicas", programa.getTematicas()); TematicaController tc = new TematicaController(); @@ -511,23 +461,14 @@ public class ProgramaController { @RequestMapping(value = "pesquisa", method = RequestMethod.GET) public String searchProgramas(@Valid Programa programa, Model uiModel) { - Object lol = uiModel.asMap().get("create"); - safePrintln(" -- create search: "+lol.toString()); if((Boolean)uiModel.asMap().get("create")){ - safePrintln(" -- entrou --"); programa = new Programa(); - //uiModel.asMap().put("programa", new Programa()); uiModel.addAttribute("programa", new Programa()); uiModel.addAttribute("create", false); - }else{ - safePrintln(" -- não entrou --"); } - safePrintln(" -- programa search: "+uiModel.asMap().get("programa").toString()); - safePrintln(" -- create search novo: "+uiModel.asMap().get("create").toString()); //System.out.println("\n\n"); //System.out.println(":tematicas: "+tematicas+" :nomeOficial: "+nomeOficial+" :nomeMunicipio: "+nomeMunicipio); //System.out.println("\n\n"); - //safePrintln(" -- programa pesquisa: "+uiModel.asMap().get("programa")); uiModel.addAttribute("tematicas", programa.getTematicas()); if (programa.getNomeOficial() != null) @@ -545,8 +486,6 @@ public class ProgramaController { void populateEditForm(Model uiModel, Programa programa) { uiModel.addAttribute("programa", programa); uiModel.addAttribute("create", true); - Object lol = uiModel.asMap().get("create"); - safePrintln(" -- create populate: "+lol.toString()); addDateTimeFormatPatterns(uiModel); //List<Programa> lp = Programa.findAllProgramas(); diff --git a/src/main/webapp/WEB-INF/tags/form/fields/table.tagx b/src/main/webapp/WEB-INF/tags/form/fields/table.tagx index d5fde476a594d8beed05610f40add5718db0d86a..bac35908a5379723af221c22f872118e74866403 100644 --- a/src/main/webapp/WEB-INF/tags/form/fields/table.tagx +++ b/src/main/webapp/WEB-INF/tags/form/fields/table.tagx @@ -290,16 +290,11 @@ } function post(){ - var form = document.getElementById("programa"); + var form = document.getElementById("programa") == null ? document.getElementById("ocorrenciaMunicipal") : document.getElementById("programa"); console.log(form); form.action = selectRedirect(); form.submit(); - /* var originalAction = $("#programa").attr('action'); - var action = selectRedirect(); - console.log(originalAction); - console.log(action); - $("#programa").attr("action", action); - $("#programa").submit(); */ + } function selectRedirect(){ @@ -315,14 +310,17 @@ selected = selected.substring(0,selected.lastIndexOf("+")); var formIndex = window.location.pathname.indexOf("/form"); var progIndex = window.location.pathname.indexOf("/programas"); - var pathHeader = window.location.pathname.substring(0,progIndex)+"/programas"; - //var pathHeader = formIndex != -1 ? window.location.pathname.substring(0,formIndex) : window.location.pathname; - console.log(pathHeader); + if(progIndex == -1){ + progIndex = window.location.pathname.indexOf("/ocorrenciamunicipais"); + var pathHeader = window.location.pathname.substring(0,progIndex)+"/ocorrenciamunicipais"; + }else{ + var pathHeader = window.location.pathname.substring(0,progIndex)+"/programas"; + } + pathHeader = pathHeader.charAt(pathHeader.length - 1) == "/" ? pathHeader.slice(0,pathHeader.length-1) : pathHeader; if(window.location.pathname.indexOf("pesquisa") != -1){ pathHeader += "/pesquisa"; } - //window.location.href=pathHeader+"/form/"+selected; return pathHeader+"/form/"+selected; }