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 1 package etunicorn;
2 2  
  3 +import com.fasterxml.jackson.annotation.JsonProperty;
  4 +import net.minidev.json.JSONObject;
3 5 import org.springframework.beans.factory.annotation.Autowired;
  6 +import org.springframework.http.HttpStatus;
  7 +import org.springframework.http.ResponseEntity;
4 8 import org.springframework.web.bind.annotation.RestController;
5 9  
6 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 16 * etunicorn-server
... ... @@ -33,4 +40,69 @@ public class BaseController {
33 40 Permission permission = permissionRepository.findByNom(nomPermission);
34 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 @@
  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 1 package etunicorn;
2 2  
3   -import net.minidev.json.JSONObject;
  3 +import etunicorn.generated.model.UpdateLoginRequest;
4 4 import org.springframework.beans.factory.annotation.Autowired;
5 5 import org.springframework.http.HttpStatus;
6 6 import org.springframework.http.ResponseEntity;
7 7 import org.springframework.web.bind.annotation.PathVariable;
  8 +import org.springframework.web.bind.annotation.RequestBody;
8 9 import org.springframework.web.bind.annotation.RequestParam;
9 10 import org.springframework.web.bind.annotation.RestController;
10 11  
  12 +import javax.validation.Valid;
  13 +
11 14 /**
12 15 * etunicorn-server
13 16 * Copyright © 2017 Le Club Info Polytech Lille
... ... @@ -23,6 +26,9 @@ public class LoginController implements etunicorn.generated.LoginController {
23 26  
24 27 @Override
25 28 @RestrictedTo(authentifie = false)
  29 + public ResponseEntity<?> updateLogin(@Valid @RequestBody UpdateLoginRequest updateLoginRequest) {
  30 + return null;
  31 + }
26 32 public ResponseEntity<?> updateLogin(@RequestParam String login, @RequestParam String password) {
27 33 Personne personne = personneRepository.findByLogin(login);
28 34 if (personne == null) {
... ... @@ -39,6 +45,7 @@ public class LoginController implements etunicorn.generated.LoginController {
39 45 return new ResponseEntity<Object>(session, HttpStatus.OK);
40 46 }
41 47  
  48 +
42 49 @Override
43 50 public ResponseEntity<?> deleteLoginByToken(@PathVariable String token) {
44 51 return null;
... ...
src/main/java/etunicorn/Personne.java
... ... @@ -12,21 +12,17 @@ import java.util.Date;
12 12 public class Personne {
13 13  
14 14  
  15 + @ManyToOne
  16 + public Role role;
15 17 @Id
16 18 @GeneratedValue(strategy = GenerationType.AUTO)
17 19 private int id;
18   -
19 20 @Column(unique = true)
20 21 private String carte;
21   -
22 22 private Date naissance;
23   -
24 23 @Column(unique = true)
25 24 private String login;
26 25  
27   - @ManyToOne
28   - private Role role;
29   -
30 26  
31 27 public Personne() {
32 28 }
... ...
src/main/java/etunicorn/PersonneController.java
1 1 package etunicorn;
2 2  
  3 +import etunicorn.generated.model.UpdatePersonneByIdRequest;
  4 +import etunicorn.generated.model.UpdatePersonneRequest;
3 5 import org.springframework.beans.factory.annotation.Autowired;
4 6 import org.springframework.dao.DataIntegrityViolationException;
5 7 import org.springframework.http.HttpStatus;
6 8 import org.springframework.http.ResponseEntity;
7 9 import org.springframework.web.bind.annotation.PathVariable;
  10 +import org.springframework.web.bind.annotation.RequestBody;
8 11 import org.springframework.web.bind.annotation.RequestParam;
9 12 import org.springframework.web.bind.annotation.RestController;
10 13  
  14 +import javax.validation.Valid;
11 15 import java.math.BigDecimal;
12 16 import java.util.Date;
13 17 import java.util.List;
... ... @@ -32,7 +36,6 @@ public class PersonneController extends BaseController implements etunicorn.gene
32 36 return new ResponseEntity<List>((List) this.personneRepository.findAll(), HttpStatus.OK);
33 37 }
34 38  
35   -
36 39 private ResponseEntity<?> mergePersonne(Personne personne, String carte, Date naissance, String login, String role) {
37 40 if (carte != null) {
38 41 personne.setCarte(carte);
... ... @@ -65,6 +68,15 @@ public class PersonneController extends BaseController implements etunicorn.gene
65 68  
66 69 @Override
67 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 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 81 Personne personne = new Personne();
70 82 return mergePersonne(personne, carte, naissance, login, role);
... ... @@ -82,6 +94,10 @@ public class PersonneController extends BaseController implements etunicorn.gene
82 94  
83 95 @Override
84 96 @RestrictedTo("PERSONNE_EDIT")
  97 + public ResponseEntity<?> updatePersonneById(@PathVariable BigDecimal idPersonne, @Valid @RequestBody UpdatePersonneByIdRequest updatePersonneByIdRequest) {
  98 + return null;
  99 + }
  100 +
85 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 102 Personne personne = personneRepository.findById(idPersonne.intValueExact());
87 103 if (personne == null) {
... ...
src/main/java/etunicorn/RoleController.java
1 1 package etunicorn;
2 2  
3 3  
  4 +import etunicorn.generated.model.UpdateRoleByIdRequest;
  5 +import etunicorn.generated.model.UpdateRoleRequest;
4 6 import org.springframework.beans.factory.annotation.Autowired;
5 7 import org.springframework.dao.DataIntegrityViolationException;
6 8 import org.springframework.http.HttpStatus;
7 9 import org.springframework.http.ResponseEntity;
8 10 import org.springframework.web.bind.annotation.PathVariable;
  11 +import org.springframework.web.bind.annotation.RequestBody;
9 12 import org.springframework.web.bind.annotation.RequestParam;
10 13 import org.springframework.web.bind.annotation.RestController;
11 14  
  15 +import javax.validation.Valid;
  16 +import java.math.BigDecimal;
12 17 import java.util.List;
13 18  
14 19 /**
... ... @@ -31,6 +36,9 @@ public class RoleController extends BaseController implements etunicorn.generate
31 36  
32 37 @Override
33 38 @RestrictedTo("ROLE_ADD")
  39 + public ResponseEntity<?> updateRole(@Valid @RequestBody UpdateRoleRequest updateRoleRequest) {
  40 + return null;
  41 + }
34 42 public ResponseEntity<?> updateRole(@RequestParam String nom) {
35 43 Role oldRole = roleRepository.findByNom(nom);
36 44 if (oldRole != null) {
... ... @@ -48,6 +56,9 @@ public class RoleController extends BaseController implements etunicorn.generate
48 56  
49 57 @Override
50 58 @RestrictedTo("ROLE_DELETE")
  59 + public ResponseEntity<?> deleteRoleById(@PathVariable BigDecimal nomRole) {
  60 + return null;
  61 + }
51 62 public ResponseEntity<?> deleteRoleById(@PathVariable String nomRole) {
52 63 Role role = roleRepository.findByNom(nomRole);
53 64 if (role == null) {
... ... @@ -59,6 +70,9 @@ public class RoleController extends BaseController implements etunicorn.generate
59 70  
60 71 @Override
61 72 @RestrictedTo("ROLE_PERMISSION_ADD")
  73 + public ResponseEntity<?> updateRoleById(@PathVariable BigDecimal nomRole, @Valid @RequestBody UpdateRoleByIdRequest updateRoleByIdRequest) {
  74 + return null;
  75 + }
62 76 public ResponseEntity<?> updateRoleById(@PathVariable String nomRole, @RequestParam String nom) {
63 77 Role role = roleRepository.findByNom(nomRole);
64 78 if (role == null) {
... ... @@ -79,6 +93,9 @@ public class RoleController extends BaseController implements etunicorn.generate
79 93  
80 94 @Override
81 95 @RestrictedTo("ROLE_PERMISSION_REMOVE")
  96 + public ResponseEntity<?> deleteRoleByNomPermission(@PathVariable String nomPermission, @PathVariable BigDecimal nomRole) {
  97 + return null;
  98 + }
82 99 public ResponseEntity<?> deleteRoleByNomPermission(@PathVariable String nomPermission, @PathVariable String nomRole) {
83 100 Role role = roleRepository.findByNom(nomRole);
84 101 if (role == null) {
... ...