Commit 5596f9d9b2a5ad91fe173fa982526f87ab5c7203

Authored by Geoffrey PREUD'HOMME
1 parent 7d30cc53

Début de l'implémentation pour le JSON

src/main/java/etunicorn/BaseController.java
1 package etunicorn; 1 package etunicorn;
2 2
  3 +import com.fasterxml.jackson.annotation.JsonProperty;
  4 +import net.minidev.json.JSONObject;
3 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.beans.factory.annotation.Autowired;
  6 +import org.springframework.http.HttpStatus;
  7 +import org.springframework.http.ResponseEntity;
4 import org.springframework.web.bind.annotation.RestController; 8 import org.springframework.web.bind.annotation.RestController;
5 9
6 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletRequest;
  11 +import java.lang.reflect.Constructor;
  12 +import java.lang.reflect.InvocationTargetException;
  13 +import java.lang.reflect.Method;
7 14
8 /** 15 /**
9 * etunicorn-server 16 * etunicorn-server
@@ -33,4 +40,69 @@ public class BaseController { @@ -33,4 +40,69 @@ public class BaseController {
33 Permission permission = permissionRepository.findByNom(nomPermission); 40 Permission permission = permissionRepository.findByNom(nomPermission);
34 return hasPermission(permission); 41 return hasPermission(permission);
35 } 42 }
  43 +
  44 + // Utilités pour générer des erreurs
  45 + ResponseEntity generateError(HttpStatus status, String message) {
  46 + JSONObject json = new JSONObject();
  47 + json.put("status", status.value());
  48 + json.put("message", message);
  49 + return new ResponseEntity(json, status);
  50 + }
  51 +
  52 + ResponseEntity generateError(HttpStatus status) {
  53 + return generateError(status, String.format("Erreur de type %d", status.value()));
  54 + }
  55 +
  56 + ResponseEntity generateError(HttpStatus status, Exception exception, String message) {
  57 + JSONObject json = new JSONObject();
  58 + json.put("status", status.value());
  59 + json.put("message", message);
  60 + json.put("errorMessage", exception.getLocalizedMessage());
  61 + json.put("stacktrace", exception.getStackTrace().toString());
  62 + return new ResponseEntity(json, status);
  63 + }
  64 +
  65 + ResponseEntity generateError(Exception exception) {
  66 + return generateError(HttpStatus.INTERNAL_SERVER_ERROR, exception, exception.getLocalizedMessage());
  67 + }
  68 +
  69 + // Utilités pour merger un request dans une entity
  70 + void mergeRequestInEntity(Object request, Object entity) throws EntityRequestMismatchException {
  71 + for (Method getMethode : request.getClass().getMethods()) {
  72 + String getMethodName = getMethode.getName();
  73 + JsonProperty annotation = getMethode.getAnnotation(JsonProperty.class);
  74 + if (getMethodName.startsWith("get") && annotation != null) {
  75 + String setMethodName = "s" + getMethodName.substring(1);
  76 + Method setMethode;
  77 + Class fieldClass;
  78 + try {
  79 + fieldClass = entity.getClass().getMethod(getMethodName).getReturnType();
  80 + setMethode = entity.getClass().getMethod(setMethodName, fieldClass);
  81 + } catch (NoSuchMethodException e) {
  82 + throw new EntityRequestMismatchException();
  83 + }
  84 + try {
  85 + if (getMethode.invoke(request) != null) {
  86 + Object data = getMethode.invoke(request);
  87 + if (data.getClass() != fieldClass) {
  88 + Constructor constructor = fieldClass.getConstructor(data.getClass());
  89 + if (constructor != null) {
  90 + data = constructor.newInstance(data);
  91 + }
  92 + }
  93 + setMethode.invoke(entity, data);
  94 + }
  95 + } catch (IllegalAccessException e) {
  96 + throw new EntityRequestMismatchException();
  97 + } catch (InvocationTargetException e) {
  98 + throw new EntityRequestMismatchException();
  99 + } catch (NoSuchMethodException e) {
  100 + throw new EntityRequestMismatchException();
  101 + } catch (InstantiationException e) {
  102 + throw new EntityRequestMismatchException();
  103 + }
  104 + }
  105 +
  106 + }
  107 + }
36 } 108 }
src/main/java/etunicorn/EntityRequestMismatchException.java 0 → 100644
@@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
  1 +package etunicorn;
  2 +
  3 +/**
  4 + * etunicorn-server
  5 + * Copyright © 2017 Le Club Info Polytech Lille
  6 + * Tous droits réservés
  7 + */
  8 +public class EntityRequestMismatchException extends Exception {
  9 +}
src/main/java/etunicorn/LoginController.java
1 package etunicorn; 1 package etunicorn;
2 2
3 -import net.minidev.json.JSONObject; 3 +import etunicorn.generated.model.UpdateLoginRequest;
4 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.beans.factory.annotation.Autowired;
5 import org.springframework.http.HttpStatus; 5 import org.springframework.http.HttpStatus;
6 import org.springframework.http.ResponseEntity; 6 import org.springframework.http.ResponseEntity;
7 import org.springframework.web.bind.annotation.PathVariable; 7 import org.springframework.web.bind.annotation.PathVariable;
  8 +import org.springframework.web.bind.annotation.RequestBody;
8 import org.springframework.web.bind.annotation.RequestParam; 9 import org.springframework.web.bind.annotation.RequestParam;
9 import org.springframework.web.bind.annotation.RestController; 10 import org.springframework.web.bind.annotation.RestController;
10 11
  12 +import javax.validation.Valid;
  13 +
11 /** 14 /**
12 * etunicorn-server 15 * etunicorn-server
13 * Copyright © 2017 Le Club Info Polytech Lille 16 * Copyright © 2017 Le Club Info Polytech Lille
@@ -23,6 +26,9 @@ public class LoginController implements etunicorn.generated.LoginController { @@ -23,6 +26,9 @@ public class LoginController implements etunicorn.generated.LoginController {
23 26
24 @Override 27 @Override
25 @RestrictedTo(authentifie = false) 28 @RestrictedTo(authentifie = false)
  29 + public ResponseEntity<?> updateLogin(@Valid @RequestBody UpdateLoginRequest updateLoginRequest) {
  30 + return null;
  31 + }
26 public ResponseEntity<?> updateLogin(@RequestParam String login, @RequestParam String password) { 32 public ResponseEntity<?> updateLogin(@RequestParam String login, @RequestParam String password) {
27 Personne personne = personneRepository.findByLogin(login); 33 Personne personne = personneRepository.findByLogin(login);
28 if (personne == null) { 34 if (personne == null) {
@@ -39,6 +45,7 @@ public class LoginController implements etunicorn.generated.LoginController { @@ -39,6 +45,7 @@ public class LoginController implements etunicorn.generated.LoginController {
39 return new ResponseEntity<Object>(session, HttpStatus.OK); 45 return new ResponseEntity<Object>(session, HttpStatus.OK);
40 } 46 }
41 47
  48 +
42 @Override 49 @Override
43 public ResponseEntity<?> deleteLoginByToken(@PathVariable String token) { 50 public ResponseEntity<?> deleteLoginByToken(@PathVariable String token) {
44 return null; 51 return null;
src/main/java/etunicorn/Personne.java
@@ -12,21 +12,17 @@ import java.util.Date; @@ -12,21 +12,17 @@ import java.util.Date;
12 public class Personne { 12 public class Personne {
13 13
14 14
  15 + @ManyToOne
  16 + public Role role;
15 @Id 17 @Id
16 @GeneratedValue(strategy = GenerationType.AUTO) 18 @GeneratedValue(strategy = GenerationType.AUTO)
17 private int id; 19 private int id;
18 -  
19 @Column(unique = true) 20 @Column(unique = true)
20 private String carte; 21 private String carte;
21 -  
22 private Date naissance; 22 private Date naissance;
23 -  
24 @Column(unique = true) 23 @Column(unique = true)
25 private String login; 24 private String login;
26 25
27 - @ManyToOne  
28 - private Role role;  
29 -  
30 26
31 public Personne() { 27 public Personne() {
32 } 28 }
src/main/java/etunicorn/PersonneController.java
1 package etunicorn; 1 package etunicorn;
2 2
  3 +import etunicorn.generated.model.UpdatePersonneByIdRequest;
  4 +import etunicorn.generated.model.UpdatePersonneRequest;
3 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.dao.DataIntegrityViolationException; 6 import org.springframework.dao.DataIntegrityViolationException;
5 import org.springframework.http.HttpStatus; 7 import org.springframework.http.HttpStatus;
6 import org.springframework.http.ResponseEntity; 8 import org.springframework.http.ResponseEntity;
7 import org.springframework.web.bind.annotation.PathVariable; 9 import org.springframework.web.bind.annotation.PathVariable;
  10 +import org.springframework.web.bind.annotation.RequestBody;
8 import org.springframework.web.bind.annotation.RequestParam; 11 import org.springframework.web.bind.annotation.RequestParam;
9 import org.springframework.web.bind.annotation.RestController; 12 import org.springframework.web.bind.annotation.RestController;
10 13
  14 +import javax.validation.Valid;
11 import java.math.BigDecimal; 15 import java.math.BigDecimal;
12 import java.util.Date; 16 import java.util.Date;
13 import java.util.List; 17 import java.util.List;
@@ -32,7 +36,6 @@ public class PersonneController extends BaseController implements etunicorn.gene @@ -32,7 +36,6 @@ public class PersonneController extends BaseController implements etunicorn.gene
32 return new ResponseEntity<List>((List) this.personneRepository.findAll(), HttpStatus.OK); 36 return new ResponseEntity<List>((List) this.personneRepository.findAll(), HttpStatus.OK);
33 } 37 }
34 38
35 -  
36 private ResponseEntity<?> mergePersonne(Personne personne, String carte, Date naissance, String login, String role) { 39 private ResponseEntity<?> mergePersonne(Personne personne, String carte, Date naissance, String login, String role) {
37 if (carte != null) { 40 if (carte != null) {
38 personne.setCarte(carte); 41 personne.setCarte(carte);
@@ -65,6 +68,15 @@ public class PersonneController extends BaseController implements etunicorn.gene @@ -65,6 +68,15 @@ public class PersonneController extends BaseController implements etunicorn.gene
65 68
66 @Override 69 @Override
67 @RestrictedTo("PERSONNE_ADD") 70 @RestrictedTo("PERSONNE_ADD")
  71 + public ResponseEntity<?> updatePersonne(@Valid @RequestBody UpdatePersonneRequest updatePersonneRequest) {
  72 + Personne personne = new Personne();
  73 + try {
  74 + mergeRequestInEntity(updatePersonneRequest, personne);
  75 + } catch (EntityRequestMismatchException e) {
  76 + return generateError(e);
  77 + }
  78 + return new ResponseEntity<Object>(personne, HttpStatus.CREATED);
  79 + }
68 public ResponseEntity<?> updatePersonne(@RequestParam(required = false) String carte, @RequestParam(required = false) Date naissance, @RequestParam(required = false) String login, @RequestParam(required = false) String role) { 80 public ResponseEntity<?> updatePersonne(@RequestParam(required = false) String carte, @RequestParam(required = false) Date naissance, @RequestParam(required = false) String login, @RequestParam(required = false) String role) {
69 Personne personne = new Personne(); 81 Personne personne = new Personne();
70 return mergePersonne(personne, carte, naissance, login, role); 82 return mergePersonne(personne, carte, naissance, login, role);
@@ -82,6 +94,10 @@ public class PersonneController extends BaseController implements etunicorn.gene @@ -82,6 +94,10 @@ public class PersonneController extends BaseController implements etunicorn.gene
82 94
83 @Override 95 @Override
84 @RestrictedTo("PERSONNE_EDIT") 96 @RestrictedTo("PERSONNE_EDIT")
  97 + public ResponseEntity<?> updatePersonneById(@PathVariable BigDecimal idPersonne, @Valid @RequestBody UpdatePersonneByIdRequest updatePersonneByIdRequest) {
  98 + return null;
  99 + }
  100 +
85 public ResponseEntity<?> updatePersonneById(@PathVariable BigDecimal idPersonne, @RequestParam(required = false) String carte, @RequestParam(required = false) Date naissance, @RequestParam(required = false) String login, @RequestParam(required = false) String role) { 101 public ResponseEntity<?> updatePersonneById(@PathVariable BigDecimal idPersonne, @RequestParam(required = false) String carte, @RequestParam(required = false) Date naissance, @RequestParam(required = false) String login, @RequestParam(required = false) String role) {
86 Personne personne = personneRepository.findById(idPersonne.intValueExact()); 102 Personne personne = personneRepository.findById(idPersonne.intValueExact());
87 if (personne == null) { 103 if (personne == null) {
src/main/java/etunicorn/RoleController.java
1 package etunicorn; 1 package etunicorn;
2 2
3 3
  4 +import etunicorn.generated.model.UpdateRoleByIdRequest;
  5 +import etunicorn.generated.model.UpdateRoleRequest;
4 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.beans.factory.annotation.Autowired;
5 import org.springframework.dao.DataIntegrityViolationException; 7 import org.springframework.dao.DataIntegrityViolationException;
6 import org.springframework.http.HttpStatus; 8 import org.springframework.http.HttpStatus;
7 import org.springframework.http.ResponseEntity; 9 import org.springframework.http.ResponseEntity;
8 import org.springframework.web.bind.annotation.PathVariable; 10 import org.springframework.web.bind.annotation.PathVariable;
  11 +import org.springframework.web.bind.annotation.RequestBody;
9 import org.springframework.web.bind.annotation.RequestParam; 12 import org.springframework.web.bind.annotation.RequestParam;
10 import org.springframework.web.bind.annotation.RestController; 13 import org.springframework.web.bind.annotation.RestController;
11 14
  15 +import javax.validation.Valid;
  16 +import java.math.BigDecimal;
12 import java.util.List; 17 import java.util.List;
13 18
14 /** 19 /**
@@ -31,6 +36,9 @@ public class RoleController extends BaseController implements etunicorn.generate @@ -31,6 +36,9 @@ public class RoleController extends BaseController implements etunicorn.generate
31 36
32 @Override 37 @Override
33 @RestrictedTo("ROLE_ADD") 38 @RestrictedTo("ROLE_ADD")
  39 + public ResponseEntity<?> updateRole(@Valid @RequestBody UpdateRoleRequest updateRoleRequest) {
  40 + return null;
  41 + }
34 public ResponseEntity<?> updateRole(@RequestParam String nom) { 42 public ResponseEntity<?> updateRole(@RequestParam String nom) {
35 Role oldRole = roleRepository.findByNom(nom); 43 Role oldRole = roleRepository.findByNom(nom);
36 if (oldRole != null) { 44 if (oldRole != null) {
@@ -48,6 +56,9 @@ public class RoleController extends BaseController implements etunicorn.generate @@ -48,6 +56,9 @@ public class RoleController extends BaseController implements etunicorn.generate
48 56
49 @Override 57 @Override
50 @RestrictedTo("ROLE_DELETE") 58 @RestrictedTo("ROLE_DELETE")
  59 + public ResponseEntity<?> deleteRoleById(@PathVariable BigDecimal nomRole) {
  60 + return null;
  61 + }
51 public ResponseEntity<?> deleteRoleById(@PathVariable String nomRole) { 62 public ResponseEntity<?> deleteRoleById(@PathVariable String nomRole) {
52 Role role = roleRepository.findByNom(nomRole); 63 Role role = roleRepository.findByNom(nomRole);
53 if (role == null) { 64 if (role == null) {
@@ -59,6 +70,9 @@ public class RoleController extends BaseController implements etunicorn.generate @@ -59,6 +70,9 @@ public class RoleController extends BaseController implements etunicorn.generate
59 70
60 @Override 71 @Override
61 @RestrictedTo("ROLE_PERMISSION_ADD") 72 @RestrictedTo("ROLE_PERMISSION_ADD")
  73 + public ResponseEntity<?> updateRoleById(@PathVariable BigDecimal nomRole, @Valid @RequestBody UpdateRoleByIdRequest updateRoleByIdRequest) {
  74 + return null;
  75 + }
62 public ResponseEntity<?> updateRoleById(@PathVariable String nomRole, @RequestParam String nom) { 76 public ResponseEntity<?> updateRoleById(@PathVariable String nomRole, @RequestParam String nom) {
63 Role role = roleRepository.findByNom(nomRole); 77 Role role = roleRepository.findByNom(nomRole);
64 if (role == null) { 78 if (role == null) {
@@ -79,6 +93,9 @@ public class RoleController extends BaseController implements etunicorn.generate @@ -79,6 +93,9 @@ public class RoleController extends BaseController implements etunicorn.generate
79 93
80 @Override 94 @Override
81 @RestrictedTo("ROLE_PERMISSION_REMOVE") 95 @RestrictedTo("ROLE_PERMISSION_REMOVE")
  96 + public ResponseEntity<?> deleteRoleByNomPermission(@PathVariable String nomPermission, @PathVariable BigDecimal nomRole) {
  97 + return null;
  98 + }
82 public ResponseEntity<?> deleteRoleByNomPermission(@PathVariable String nomPermission, @PathVariable String nomRole) { 99 public ResponseEntity<?> deleteRoleByNomPermission(@PathVariable String nomPermission, @PathVariable String nomRole) {
83 Role role = roleRepository.findByNom(nomRole); 100 Role role = roleRepository.findByNom(nomRole);
84 if (role == null) { 101 if (role == null) {