Bladeren bron

Ajout autocompletion ville

Arthur Brandao 5 jaren geleden
bovenliggende
commit
bf5ebca3fd

+ 25 - 5
WebContent/inscription.jsp

@@ -2,6 +2,12 @@
 	pageEncoding="UTF-8"%>
 <%@ taglib prefix="s" uri="/struts-tags"%>
 <%@ taglib prefix="tag" tagdir="/WEB-INF/tags" %>
+<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
+<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
+
+<c:set var="req" value="${pageContext.request}" />
+<c:set var="url">${req.requestURL}</c:set>
+<c:set var="base" value="${fn:substring(url, 0, fn:length(url) - fn:length(req.requestURI))}${req.contextPath}/" />
 
 <!DOCTYPE html>
 <html>
@@ -24,7 +30,7 @@
         </div>
         <div class="row">
         	<s:debug />
-            <s:form action="inscription" validate="true" class="col s12">
+            <s:form action="inscription" validate="true" class="col s12" autocomplete="off">
                 <div class="row">
                     <div class="input-field col m6 s12">
                     	<i class="material-icons prefix">person</i>
@@ -59,7 +65,7 @@
                 <div class="row">
                     <div class="input-field col m6 s12">
                         <i class="material-icons prefix">location_city</i>
-                        <s:textfield id="ville" name="ville" class="validate"  required="required" />
+                        <s:textfield id="ville" name="ville" class="validate autocomplete"  required="required" />
                         <label for="ville">Ville*</label>
                         <span class="helper-text"></span>
                         <s:fielderror fieldName="ville" class="field-error hide" />
@@ -127,9 +133,23 @@
     	$(document).ready(() => {
     		const loader = M.Modal.getInstance($('#loader'));
     		
-    		$('#btn-valid').on('click', function() {
-    			loader.open();
-    		});
+    		// Chargement autcomplete
+    		loader.open();
+    		$.ajax({
+                type: "GET",
+                url: "${base}api/ville/list/all/autocomplete",
+                error: () => {
+                	loader.close();
+					console.error("Impossible de charger l'autocomplete");
+                },
+                success: (result) => {
+                	loader.close();
+                	$('input.autocomplete').autocomplete({
+                		data: result.data,
+                		limit: 5
+                	});
+                },
+            });
     	});
     </script>
 </body>

+ 3 - 0
sql/insert.sql

@@ -5,6 +5,9 @@ Insert into Espace(LIBELLE, CODE) Values('Imprimante 3D', 'imprimante-3d');
 Insert into Ville(LIBELLE, CODEPOSTAL) Values('Troyes', '10000');
 Insert into Ville(LIBELLE, CODEPOSTAL) Values('Reims', '51000');
 Insert into Ville(LIBELLE, CODEPOSTAL) Values('Lens', '62300');
+Insert into Ville(LIBELLE, CODEPOSTAL) Values('Villechetif', '10410');
+Insert into Ville(LIBELLE, CODEPOSTAL) Values('Roubaix', '59100');
+Insert into Ville(LIBELLE, CODEPOSTAL) Values('Douai', '59500');
 Insert into Niveau(LIBELLE) Values('A');
 Insert into Niveau(LIBELLE) Values('B');
 Insert into Ecole(LIBELLE, VILLE, NIVEAU) Values('Camille claudel', 1, 2);

+ 46 - 0
src/microfolie/entry/rest/VilleController.java

@@ -0,0 +1,46 @@
+package microfolie.entry.rest;
+
+import java.util.List;
+import java.util.logging.Logger;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+import microfolie.service.VilleService;
+import microfolie.service.dto.VilleDTO;
+import microfolie.utils.JsonUtils;
+
+@Path("/ville")
+@Produces("application/json")
+public class VilleController {
+	
+private static final Logger LOGGER = Logger.getLogger(VilleController.class.getName());
+	
+	private VilleService service = VilleService.getInstance();
+	
+	@GET
+	@Path("/list/all")
+	public String listAll() {
+		LOGGER.info("Begin: Ville list all");
+		List<VilleDTO> villes = service.getAll();
+		JSONArray data = new JSONArray(villes);
+		LOGGER.info("End: Ville list all");
+		return JsonUtils.success(data).toString();
+	}
+	
+	@GET
+	@Path("/list/all/autocomplete")
+	public String listAllAutocomplete() {
+		LOGGER.info("Begin: Ville list all");
+		List<VilleDTO> villes = service.getAll();
+		JSONObject data = new JSONObject();
+		villes.forEach(elt -> data.put(elt.getLibelle(), ""));
+		LOGGER.info("End: Ville list all");
+		return JsonUtils.success(data).toString();
+	}
+
+}

+ 32 - 0
src/microfolie/service/VilleService.java

@@ -0,0 +1,32 @@
+package microfolie.service;
+
+import java.util.List;
+
+import microfolie.persistance.entity.Ville;
+import microfolie.persistance.table.VilleTable;
+import microfolie.service.dto.VilleDTO;
+import microfolie.service.transformer.VilleTransformer;
+
+public class VilleService {
+	
+	private static VilleService instance;
+	
+	private VilleTable table = VilleTable.getInstance();
+
+	private VilleService() {
+		// Constructeur privé pour singleton
+	}
+	
+	public List<VilleDTO> getAll() {
+		List<Ville> villes = table.getAll();
+		return VilleTransformer.entityToDto(villes);
+	}
+	
+	public static VilleService getInstance() {
+		if (instance == null) {
+			instance = new VilleService();
+		}
+		return instance;
+	}
+	
+}

+ 34 - 0
src/microfolie/service/dto/VilleDTO.java

@@ -0,0 +1,34 @@
+package microfolie.service.dto;
+
+public class VilleDTO {
+	
+	private String libelle;
+	private String cp;
+	
+	public VilleDTO() {
+		// Constructeur par defaut pour contruire un objet vide
+	}
+	
+	public VilleDTO(String libelle, String cp) {
+		super();
+		this.libelle = libelle;
+		this.cp = cp;
+	}
+
+	public String getLibelle() {
+		return libelle;
+	}
+
+	public void setLibelle(String libelle) {
+		this.libelle = libelle;
+	}
+
+	public String getCp() {
+		return cp;
+	}
+
+	public void setCp(String cp) {
+		this.cp = cp;
+	}
+	
+}

+ 37 - 0
src/microfolie/service/transformer/VilleTransformer.java

@@ -0,0 +1,37 @@
+package microfolie.service.transformer;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import microfolie.persistance.entity.Ville;
+import microfolie.service.dto.VilleDTO;
+
+public class VilleTransformer {
+	
+	public static Ville dtoToEntity(VilleDTO dto) {
+		Ville ville = new Ville();
+		ville.cp = dto.getCp();
+		ville.libelle = dto.getLibelle();
+		return ville;
+	}
+	
+	public static List<Ville> dtoToEntity(List<VilleDTO> dto) {
+		List<Ville> result = new ArrayList<>();
+		dto.forEach(elt -> result.add(dtoToEntity(elt)));
+		return result;
+	}
+	
+	public static VilleDTO entityToDto(Ville ville) {
+		VilleDTO dto = new VilleDTO();
+		dto.setCp(ville.cp);
+		dto.setLibelle(ville.libelle);
+		return dto;
+	}
+	
+	public static List<VilleDTO> entityToDto(List<Ville> ville) {
+		List<VilleDTO> result = new ArrayList<>();
+		ville.forEach(elt -> result.add(entityToDto(elt)));
+		return result;
+	}
+
+}