From 8f684dec39f953a7bdd0e83f452ef9c82311852e Mon Sep 17 00:00:00 2001
From: Bruno Nocera Zanette <brunonzanette@gmail.com>
Date: Wed, 4 Jun 2014 15:10:21 -0300
Subject: [PATCH] Update Municipio option's list based on the Estado

Update Municipio option's list based on the Estado chosen.

Steps:
1) Client sends a request to the server using estado as parameter.

2) Server receives the request, generates the list based on the Estado
and sends as response a complete view/page ("orgaoexecutores/create")
containing that list

3) Client receive the response, processes it to get only the list,
and exchanges the list of the page for the response list.
---
 .../ufpr/c3sl/estacaojuventude/Municipio.java |  4 ++
 .../web/OrgaoExecutorController.java          | 30 +++++++++++---
 .../WEB-INF/i18n/application.properties       |  2 +
 .../views/municipios/selectmunicipios.jspx    |  9 ++++
 .../webapp/WEB-INF/views/municipios/views.xml |  3 ++
 .../WEB-INF/views/orgaoexecutores/create.jspx | 41 +++++++++++++++++++
 6 files changed, 83 insertions(+), 6 deletions(-)
 create mode 100644 src/main/webapp/WEB-INF/views/municipios/selectmunicipios.jspx

diff --git a/src/main/java/br/ufpr/c3sl/estacaojuventude/Municipio.java b/src/main/java/br/ufpr/c3sl/estacaojuventude/Municipio.java
index 0b99bc1..2886458 100644
--- a/src/main/java/br/ufpr/c3sl/estacaojuventude/Municipio.java
+++ b/src/main/java/br/ufpr/c3sl/estacaojuventude/Municipio.java
@@ -29,4 +29,8 @@ public class Municipio {
 	public static List<Municipio> findAllMunicipios() {
         return entityManager().createQuery("SELECT o FROM Municipio o ORDER BY nome", Municipio.class).getResultList();
     }
+	
+	public static List<Municipio> findAllMunicipiosByUf(String uf) {
+        return entityManager().createQuery("SELECT o FROM Municipio o WHERE UF="+uf+" ORDER BY nome", Municipio.class).getResultList();
+    }
 }
diff --git a/src/main/java/br/ufpr/c3sl/estacaojuventude/web/OrgaoExecutorController.java b/src/main/java/br/ufpr/c3sl/estacaojuventude/web/OrgaoExecutorController.java
index bf1ed34..9451fae 100644
--- a/src/main/java/br/ufpr/c3sl/estacaojuventude/web/OrgaoExecutorController.java
+++ b/src/main/java/br/ufpr/c3sl/estacaojuventude/web/OrgaoExecutorController.java
@@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 
+import br.ufpr.c3sl.estacaojuventude.Estado;
 import br.ufpr.c3sl.estacaojuventude.Municipio;
 import br.ufpr.c3sl.estacaojuventude.OrgaoExecutor;
 
@@ -63,16 +64,33 @@ public class OrgaoExecutorController {
         return "redirect:/orgaoexecutores";
     }
 	
+	@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "text/html")
+	public String get(@PathVariable(value = "id") String uf, @Valid OrgaoExecutor orgaoExecutor, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
+		repopulateMunicipios(uf, uiModel, orgaoExecutor);
+		return "municipios/selectmunicipios";
+	}
+	
+	void repopulateMunicipios(String uf, Model uiModel, OrgaoExecutor orgaoExecutor) {
+		List<Municipio> lm = Municipio.findAllMunicipiosByUf(uf);
+        uiModel.addAttribute("municipios", lm);
+	}
+	
 	void populateEditForm(Model uiModel, OrgaoExecutor orgaoExecutor) {
         uiModel.addAttribute("orgaoExecutor", orgaoExecutor);
         addDateTimeFormatPatterns(uiModel);
         
-        List<Municipio> lm = Municipio.findAllMunicipios();
-        Municipio m = new Municipio();
-        m.setId((long) 0);
-        m.setNome("");
-        lm.add(0, m);
-        uiModel.addAttribute("municipios", lm);
+        List<Estado> le = Estado.findAllEstadoes();
+        Estado e = new Estado();
+        e.setNome("");
+        le.add(0, e);
+        uiModel.addAttribute("estadoes", le);
+        
+//	        List<Municipio> lm = Municipio.findAllMunicipios();
+//	        Municipio m = new Municipio();
+//	        m.setId((long) 0);
+//	        m.setNome("");
+//	        lm.add(0, m);
+//	        uiModel.addAttribute("municipios", lm);
     }
 	
 }
diff --git a/src/main/webapp/WEB-INF/i18n/application.properties b/src/main/webapp/WEB-INF/i18n/application.properties
index dc49d27..26ebf93 100644
--- a/src/main/webapp/WEB-INF/i18n/application.properties
+++ b/src/main/webapp/WEB-INF/i18n/application.properties
@@ -69,6 +69,8 @@ label_br_ufpr_c3sl_estacaojuventude_orgaoexecutor_sigla=Sigla
 label_br_ufpr_c3sl_estacaojuventude_orgaoexecutor_site=Site
 label_br_ufpr_c3sl_estacaojuventude_orgaoexecutor_telefonecontato=Telefone do contato
 label_br_ufpr_c3sl_estacaojuventude_orgaoexecutor_version=Versão
+label_br_ufpr_c3sl_estacaojuventude_orgaoexecutor_municipio.uf=UF
+label_br_ufpr_c3sl_estacaojuventude_orgaoexecutor_estado=Estado
 
 label_br_ufpr_c3sl_estacaojuventude_programa=Programa
 label_br_ufpr_c3sl_estacaojuventude_programa_beneficios=Benefícios Associados
diff --git a/src/main/webapp/WEB-INF/views/municipios/selectmunicipios.jspx b/src/main/webapp/WEB-INF/views/municipios/selectmunicipios.jspx
new file mode 100644
index 0000000..fb84e60
--- /dev/null
+++ b/src/main/webapp/WEB-INF/views/municipios/selectmunicipios.jspx
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<div xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields" xmlns:form="urn:jsptagdir:/WEB-INF/tags/form" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:spring="http://www.springframework.org/tags" version="2.0">
+    <jsp:directive.page contentType="text/html;charset=UTF-8"/>
+    <jsp:output omit-xml-declaration="yes"/>
+    <form:create id="fc_br_ufpr_c3sl_estacaojuventude_OrgaoExecutor" modelAttribute="orgaoExecutor" path="/orgaoexecutores" render="${empty dependencies}" z="ySSBiOgc68ijzLxGB8mEHSqYtyU=">
+        <field:select field="municipio" id="c_br_ufpr_c3sl_estacaojuventude_OrgaoExecutor_municipio" itemValue="id" items="${municipios}" path="/municipios" z="TsmVkI/NlGQHwdhUUjgjQ8bprz4="/>
+    </form:create>
+    <form:dependency dependencies="${dependencies}" id="d_br_ufpr_c3sl_estacaojuventude_OrgaoExecutor" render="${not empty dependencies}" z="AAjeOEHmudaMpa7oWwH71C1aVn0="/>
+</div>
diff --git a/src/main/webapp/WEB-INF/views/municipios/views.xml b/src/main/webapp/WEB-INF/views/municipios/views.xml
index 18109b6..1f1c7ff 100644
--- a/src/main/webapp/WEB-INF/views/municipios/views.xml
+++ b/src/main/webapp/WEB-INF/views/municipios/views.xml
@@ -13,4 +13,7 @@
 <definition extends="default" name="municipios/update">
         <put-attribute name="body" value="/WEB-INF/views/municipios/update.jspx"/>
     </definition>
+    <definition extends="default" name="municipios/selectmunicipios">
+        <put-attribute name="body" value="/WEB-INF/views/municipios/selectmunicipios.jspx"/>
+    </definition>
 </tiles-definitions>
diff --git a/src/main/webapp/WEB-INF/views/orgaoexecutores/create.jspx b/src/main/webapp/WEB-INF/views/orgaoexecutores/create.jspx
index d337c41..980cd2b 100644
--- a/src/main/webapp/WEB-INF/views/orgaoexecutores/create.jspx
+++ b/src/main/webapp/WEB-INF/views/orgaoexecutores/create.jspx
@@ -10,11 +10,52 @@
         <field:input field="logradouro" id="c_br_ufpr_c3sl_estacaojuventude_OrgaoExecutor_logradouro" z="k/1XFT6R2/obR+osepiqSAJabF8="/>
         <field:input field="bairro" id="c_br_ufpr_c3sl_estacaojuventude_OrgaoExecutor_bairro" z="C0AhYYllc9NQKj6P/OsWQ/cbgBs="/>
         <field:input field="cep" id="c_br_ufpr_c3sl_estacaojuventude_OrgaoExecutor_cep" widthcss="65px" z="user-managed"/>
+        
+        <field:select field="uf"
+			id="c_br_ufpr_c3sl_estacaojuventude_Municipio_uf"
+			itemValue="id" items="${estadoes}" path="/estados"
+			z="user-managed"
+			disableFormBinding="true" />
+        
         <field:select field="municipio" id="c_br_ufpr_c3sl_estacaojuventude_OrgaoExecutor_municipio" itemValue="id" items="${municipios}" path="/municipios" z="TsmVkI/NlGQHwdhUUjgjQ8bprz4="/>
+
         <field:input field="contato" id="c_br_ufpr_c3sl_estacaojuventude_OrgaoExecutor_contato" z="N464inkZDRkVnlMA8hpmvvyzs7g="/>
         <field:datetime dateTimePattern="${orgaoExecutor_ultimaalteracao_date_format}" field="ultimaAlteracao" id="c_br_ufpr_c3sl_estacaojuventude_OrgaoExecutor_ultimaAlteracao" render="false" z="user-managed"/>
         <field:input field="emailContato" id="c_br_ufpr_c3sl_estacaojuventude_OrgaoExecutor_emailContato" validationMessageCode="field_invalid_email" validationRegex="[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}" z="user-managed"/>
         <field:input field="telefoneContato" id="c_br_ufpr_c3sl_estacaojuventude_OrgaoExecutor_telefoneContato" mask="(##)####-####" max="13" validationMessageCode="field_invalid_integer" validationRegex="[(][0-9]*[)][0-9]*-[0-9]*" widthcss="85px" z="user-managed"/>
     </form:create>
     <form:dependency dependencies="${dependencies}" id="d_br_ufpr_c3sl_estacaojuventude_OrgaoExecutor" render="${not empty dependencies}" z="AAjeOEHmudaMpa7oWwH71C1aVn0="/>
+    
+    <script type="text/javascript">
+
+    	Spring.addDecoration(new Spring.ElementDecoration({
+			elementId : '_uf_id',
+			widgetType : "dijit.form.FilteringSelect",
+			widgetAttrs : {
+				onChange : function() {
+
+					//ID of the chosen "estado"
+ 					uf=document.getElementsByName("uf")[0].value;
+					
+					//Create and send to server a request of 
+					//the list of "Municipios" that belongs to the "Estado" chosen 
+					xmlhttp = new XMLHttpRequest();
+					xmlhttp.open("GET", "orgaoexecutores/"+uf ,false);
+					xmlhttp.send();
+
+					//Create a temporary html document to store the html code
+					//sent by the server containing the list of "Municipios"
+					var responseDoc = document.implementation.createHTMLDocument("XMLHttpResponse");
+					responseDoc.documentElement.innerHTML = xmlhttp.responseText;
+					
+					//Copy the list of the response to the real page 
+					var PageElem = document.getElementById("_c_br_ufpr_c3sl_estacaojuventude_OrgaoExecutor_municipio_id");
+					var RespElem = responseDoc.getElementById("_c_br_ufpr_c3sl_estacaojuventude_OrgaoExecutor_municipio_id");
+					PageElem.innerHTML = RespElem.innerHTML;
+				}
+			}
+		}));
+
+ 	</script>
+ 	
 </div>
-- 
GitLab