Commit ec214b7d3dd1970eeff4f1cca8730e4ee95cba70
1 parent
f85d84b0
Implémentation JSON : Sous objets
Showing
8 changed files
with
197 additions
and
37 deletions
Show diff stats
.gitignore
src/main/java/etunicorn/BaseController.java
@@ -3,14 +3,18 @@ package etunicorn; | @@ -3,14 +3,18 @@ package etunicorn; | ||
3 | import com.fasterxml.jackson.annotation.JsonProperty; | 3 | import com.fasterxml.jackson.annotation.JsonProperty; |
4 | import net.minidev.json.JSONObject; | 4 | import net.minidev.json.JSONObject; |
5 | import org.springframework.beans.factory.annotation.Autowired; | 5 | import org.springframework.beans.factory.annotation.Autowired; |
6 | +import org.springframework.data.repository.Repository; | ||
6 | import org.springframework.http.HttpStatus; | 7 | import org.springframework.http.HttpStatus; |
7 | import org.springframework.http.ResponseEntity; | 8 | import org.springframework.http.ResponseEntity; |
8 | import org.springframework.web.bind.annotation.RestController; | 9 | import org.springframework.web.bind.annotation.RestController; |
9 | 10 | ||
11 | +import javax.persistence.Entity; | ||
10 | import javax.servlet.http.HttpServletRequest; | 12 | import javax.servlet.http.HttpServletRequest; |
11 | import java.lang.reflect.Constructor; | 13 | import java.lang.reflect.Constructor; |
12 | import java.lang.reflect.InvocationTargetException; | 14 | import java.lang.reflect.InvocationTargetException; |
13 | import java.lang.reflect.Method; | 15 | import java.lang.reflect.Method; |
16 | +import java.util.LinkedHashMap; | ||
17 | +import java.util.Map; | ||
14 | 18 | ||
15 | /** | 19 | /** |
16 | * etunicorn-server | 20 | * etunicorn-server |
@@ -20,6 +24,9 @@ import java.lang.reflect.Method; | @@ -20,6 +24,9 @@ import java.lang.reflect.Method; | ||
20 | @RestController | 24 | @RestController |
21 | public class BaseController { | 25 | public class BaseController { |
22 | 26 | ||
27 | + // Utilités pour merger un request dans une entity | ||
28 | + @Autowired | ||
29 | + Map<String, Repository> repositories; | ||
23 | // Permettent la vérification de permissions dans les méthodes de controlleur | 30 | // Permettent la vérification de permissions dans les méthodes de controlleur |
24 | @Autowired | 31 | @Autowired |
25 | private HttpServletRequest request; | 32 | private HttpServletRequest request; |
@@ -66,12 +73,35 @@ public class BaseController { | @@ -66,12 +73,35 @@ public class BaseController { | ||
66 | return generateError(HttpStatus.INTERNAL_SERVER_ERROR, exception, exception.getLocalizedMessage()); | 73 | return generateError(HttpStatus.INTERNAL_SERVER_ERROR, exception, exception.getLocalizedMessage()); |
67 | } | 74 | } |
68 | 75 | ||
69 | - // Utilités pour merger un request dans une entity | ||
70 | - void mergeRequestInEntity(Object request, Object entity) throws EntityRequestMismatchException { | 76 | + protected Object getEntityFromObject(String className, LinkedHashMap object) throws NotEnoughDataException { |
77 | + Object repository = repositories.get(className + "Repository"); | ||
78 | + for (Method findMethod : repository.getClass().getMethods()) { | ||
79 | + String findMethodName = findMethod.getName(); | ||
80 | + if (findMethodName.startsWith("findBy")) { | ||
81 | + String key = Character.toLowerCase(findMethodName.charAt(6)) + findMethodName.substring(7); | ||
82 | + if (object.containsKey(key)) { | ||
83 | + try { | ||
84 | + Object data = object.get(key); | ||
85 | + return findMethod.invoke(repository, data); | ||
86 | + } catch (IllegalAccessException e) { | ||
87 | + continue; | ||
88 | + } catch (InvocationTargetException e) { | ||
89 | + continue; | ||
90 | + } | ||
91 | + } else { | ||
92 | + continue; | ||
93 | + } | ||
94 | + } | ||
95 | + } | ||
96 | + throw new NotEnoughDataException(); | ||
97 | + } | ||
98 | + | ||
99 | + protected void mergeRequestInEntity(Object request, Object entity) throws EntityRequestMismatchException, NotEnoughDataException, ObjectNotFoundException { | ||
71 | for (Method getMethode : request.getClass().getMethods()) { | 100 | for (Method getMethode : request.getClass().getMethods()) { |
72 | String getMethodName = getMethode.getName(); | 101 | String getMethodName = getMethode.getName(); |
73 | JsonProperty annotation = getMethode.getAnnotation(JsonProperty.class); | 102 | JsonProperty annotation = getMethode.getAnnotation(JsonProperty.class); |
74 | if (getMethodName.startsWith("get") && annotation != null) { | 103 | if (getMethodName.startsWith("get") && annotation != null) { |
104 | + String fieldName = annotation.value(); | ||
75 | String setMethodName = "s" + getMethodName.substring(1); | 105 | String setMethodName = "s" + getMethodName.substring(1); |
76 | Method setMethode; | 106 | Method setMethode; |
77 | Class fieldClass; | 107 | Class fieldClass; |
@@ -85,9 +115,16 @@ public class BaseController { | @@ -85,9 +115,16 @@ public class BaseController { | ||
85 | if (getMethode.invoke(request) != null) { | 115 | if (getMethode.invoke(request) != null) { |
86 | Object data = getMethode.invoke(request); | 116 | Object data = getMethode.invoke(request); |
87 | if (data.getClass() != fieldClass) { | 117 | if (data.getClass() != fieldClass) { |
88 | - Constructor constructor = fieldClass.getConstructor(data.getClass()); | ||
89 | - if (constructor != null) { | ||
90 | - data = constructor.newInstance(data); | 118 | + if (fieldClass.getAnnotation(Entity.class) != null) { |
119 | + data = getEntityFromObject(fieldName, (LinkedHashMap) data); | ||
120 | + if (data == null) { | ||
121 | + throw new ObjectNotFoundException(); | ||
122 | + } | ||
123 | + } else { | ||
124 | + Constructor constructor = fieldClass.getConstructor(data.getClass()); | ||
125 | + if (constructor != null) { | ||
126 | + data = constructor.newInstance(data); | ||
127 | + } | ||
91 | } | 128 | } |
92 | } | 129 | } |
93 | setMethode.invoke(entity, data); | 130 | setMethode.invoke(entity, data); |
src/main/java/etunicorn/NotEnoughDataException.java
0 → 100644
src/main/java/etunicorn/ObjectNotFoundException.java
0 → 100644
src/main/java/etunicorn/PersonneController.java
@@ -74,6 +74,10 @@ public class PersonneController extends BaseController implements etunicorn.gene | @@ -74,6 +74,10 @@ public class PersonneController extends BaseController implements etunicorn.gene | ||
74 | mergeRequestInEntity(updatePersonneRequest, personne); | 74 | mergeRequestInEntity(updatePersonneRequest, personne); |
75 | } catch (EntityRequestMismatchException e) { | 75 | } catch (EntityRequestMismatchException e) { |
76 | return generateError(e); | 76 | return generateError(e); |
77 | + } catch (NotEnoughDataException e) { | ||
78 | + return generateError(HttpStatus.BAD_REQUEST, e, "Il n'y a pas suffisament de données pour identifier un sous-objet"); | ||
79 | + } catch (ObjectNotFoundException e) { | ||
80 | + return generateError(HttpStatus.NOT_FOUND, e, "Sous-objet non trouvé"); | ||
77 | } | 81 | } |
78 | return new ResponseEntity<Object>(personne, HttpStatus.CREATED); | 82 | return new ResponseEntity<Object>(personne, HttpStatus.CREATED); |
79 | } | 83 | } |
src/main/java/etunicorn/generated/TransactionController.java
@@ -2,7 +2,7 @@ | @@ -2,7 +2,7 @@ | ||
2 | package etunicorn.generated; | 2 | package etunicorn.generated; |
3 | 3 | ||
4 | import etunicorn.generated.model.UpdateConsommationRequest; | 4 | import etunicorn.generated.model.UpdateConsommationRequest; |
5 | -import etunicorn.generated.model.UpdateEvenementCreditRequest; | 5 | +import etunicorn.generated.model.UpdateCreditRequest; |
6 | import etunicorn.generated.model.UpdateEvenementRequest; | 6 | import etunicorn.generated.model.UpdateEvenementRequest; |
7 | import org.springframework.http.ResponseEntity; | 7 | import org.springframework.http.ResponseEntity; |
8 | import org.springframework.web.bind.annotation.PathVariable; | 8 | import org.springframework.web.bind.annotation.PathVariable; |
@@ -26,54 +26,54 @@ public interface TransactionController { | @@ -26,54 +26,54 @@ public interface TransactionController { | ||
26 | * | 26 | * |
27 | */ | 27 | */ |
28 | @RequestMapping(value = "", method = RequestMethod.GET) | 28 | @RequestMapping(value = "", method = RequestMethod.GET) |
29 | - public ResponseEntity<?> getTransaction(); | 29 | + ResponseEntity<?> getTransaction(); |
30 | 30 | ||
31 | /** | 31 | /** |
32 | * Achat d'une consomation (id) par un participant à un acteur | 32 | * Achat d'une consomation (id) par un participant à un acteur |
33 | * | 33 | * |
34 | */ | 34 | */ |
35 | @RequestMapping(value = "/consommation", method = RequestMethod.POST) | 35 | @RequestMapping(value = "/consommation", method = RequestMethod.POST) |
36 | - public ResponseEntity<?> updateConsommation( | ||
37 | - @javax.validation.Valid | ||
38 | - @org.springframework.web.bind.annotation.RequestBody | ||
39 | - UpdateConsommationRequest updateConsommationRequest); | 36 | + ResponseEntity<?> updateConsommation( |
37 | + @javax.validation.Valid | ||
38 | + @org.springframework.web.bind.annotation.RequestBody | ||
39 | + UpdateConsommationRequest updateConsommationRequest); | ||
40 | 40 | ||
41 | /** | 41 | /** |
42 | * No description | 42 | * No description |
43 | * | 43 | * |
44 | */ | 44 | */ |
45 | @RequestMapping(value = "/evenement", method = RequestMethod.POST) | 45 | @RequestMapping(value = "/evenement", method = RequestMethod.POST) |
46 | - public ResponseEntity<?> updateEvenement( | ||
47 | - @javax.validation.Valid | ||
48 | - @org.springframework.web.bind.annotation.RequestBody | ||
49 | - UpdateEvenementRequest updateEvenementRequest); | 46 | + ResponseEntity<?> updateEvenement( |
47 | + @javax.validation.Valid | ||
48 | + @org.springframework.web.bind.annotation.RequestBody | ||
49 | + UpdateEvenementRequest updateEvenementRequest); | ||
50 | 50 | ||
51 | /** | 51 | /** |
52 | * Permet de recharger ou debiter un compte | 52 | * Permet de recharger ou debiter un compte |
53 | * | 53 | * |
54 | */ | 54 | */ |
55 | - @RequestMapping(value = "/evenement/credit", method = RequestMethod.POST) | ||
56 | - public ResponseEntity<?> updateEvenementCredit( | ||
57 | - @javax.validation.Valid | ||
58 | - @org.springframework.web.bind.annotation.RequestBody | ||
59 | - UpdateEvenementCreditRequest updateEvenementCreditRequest); | 55 | + @RequestMapping(value = "/credit", method = RequestMethod.POST) |
56 | + ResponseEntity<?> updateCredit( | ||
57 | + @javax.validation.Valid | ||
58 | + @org.springframework.web.bind.annotation.RequestBody | ||
59 | + UpdateCreditRequest updateCreditRequest); | ||
60 | 60 | ||
61 | /** | 61 | /** |
62 | * Permet de recuperer la liste des transaction d'une personne | 62 | * Permet de recuperer la liste des transaction d'une personne |
63 | * | 63 | * |
64 | */ | 64 | */ |
65 | @RequestMapping(value = "/{idPersonne}", method = RequestMethod.GET) | 65 | @RequestMapping(value = "/{idPersonne}", method = RequestMethod.GET) |
66 | - public ResponseEntity<?> getTransactionByIdPersonne( | ||
67 | - @PathVariable | ||
68 | - Long idPersonne); | 66 | + ResponseEntity<?> getTransactionByIdPersonne( |
67 | + @PathVariable | ||
68 | + Long idPersonne); | ||
69 | 69 | ||
70 | /** | 70 | /** |
71 | * Permet de recuperer la liste des transaction d'une personne | 71 | * Permet de recuperer la liste des transaction d'une personne |
72 | * | 72 | * |
73 | */ | 73 | */ |
74 | @RequestMapping(value = "/acteur/{idPersonne}", method = RequestMethod.GET) | 74 | @RequestMapping(value = "/acteur/{idPersonne}", method = RequestMethod.GET) |
75 | - public ResponseEntity<?> getActeurByIdPersonne( | ||
76 | - @PathVariable | ||
77 | - Long idPersonne); | 75 | + ResponseEntity<?> getActeurByIdPersonne( |
76 | + @PathVariable | ||
77 | + Long idPersonne); | ||
78 | 78 | ||
79 | } | 79 | } |
src/main/java/etunicorn/generated/model/UpdateCreditRequest.java
0 → 100644
@@ -0,0 +1,104 @@ | @@ -0,0 +1,104 @@ | ||
1 | + | ||
2 | +package etunicorn.generated.model; | ||
3 | + | ||
4 | +import com.fasterxml.jackson.annotation.*; | ||
5 | +import org.apache.commons.lang.builder.EqualsBuilder; | ||
6 | +import org.apache.commons.lang.builder.HashCodeBuilder; | ||
7 | +import org.apache.commons.lang.builder.ToStringBuilder; | ||
8 | + | ||
9 | +import java.util.HashMap; | ||
10 | +import java.util.Map; | ||
11 | + | ||
12 | +@JsonInclude(JsonInclude.Include.NON_NULL) | ||
13 | +@JsonPropertyOrder({ | ||
14 | + "participant", | ||
15 | + "prix" | ||
16 | +}) | ||
17 | +public class UpdateCreditRequest { | ||
18 | + | ||
19 | + /** | ||
20 | + * (Required) | ||
21 | + */ | ||
22 | + @JsonProperty("participant") | ||
23 | + private Object participant; | ||
24 | + /** | ||
25 | + * (Required) | ||
26 | + */ | ||
27 | + @JsonProperty("prix") | ||
28 | + private Float prix; | ||
29 | + @JsonIgnore | ||
30 | + private Map<String, Object> additionalProperties = new HashMap<String, Object>(); | ||
31 | + | ||
32 | + /** | ||
33 | + * (Required) | ||
34 | + * | ||
35 | + * @return The participant | ||
36 | + */ | ||
37 | + @JsonProperty("participant") | ||
38 | + public Object getParticipant() { | ||
39 | + return participant; | ||
40 | + } | ||
41 | + | ||
42 | + /** | ||
43 | + * (Required) | ||
44 | + * | ||
45 | + * @param participant The participant | ||
46 | + */ | ||
47 | + @JsonProperty("participant") | ||
48 | + public void setParticipant(Object participant) { | ||
49 | + this.participant = participant; | ||
50 | + } | ||
51 | + | ||
52 | + /** | ||
53 | + * (Required) | ||
54 | + * | ||
55 | + * @return The prix | ||
56 | + */ | ||
57 | + @JsonProperty("prix") | ||
58 | + public Float getPrix() { | ||
59 | + return prix; | ||
60 | + } | ||
61 | + | ||
62 | + /** | ||
63 | + * (Required) | ||
64 | + * | ||
65 | + * @param prix The prix | ||
66 | + */ | ||
67 | + @JsonProperty("prix") | ||
68 | + public void setPrix(Float prix) { | ||
69 | + this.prix = prix; | ||
70 | + } | ||
71 | + | ||
72 | + @Override | ||
73 | + public String toString() { | ||
74 | + return ToStringBuilder.reflectionToString(this); | ||
75 | + } | ||
76 | + | ||
77 | + @JsonAnyGetter | ||
78 | + public Map<String, Object> getAdditionalProperties() { | ||
79 | + return this.additionalProperties; | ||
80 | + } | ||
81 | + | ||
82 | + @JsonAnySetter | ||
83 | + public void setAdditionalProperty(String name, Object value) { | ||
84 | + this.additionalProperties.put(name, value); | ||
85 | + } | ||
86 | + | ||
87 | + @Override | ||
88 | + public int hashCode() { | ||
89 | + return new HashCodeBuilder().append(participant).append(prix).append(additionalProperties).toHashCode(); | ||
90 | + } | ||
91 | + | ||
92 | + @Override | ||
93 | + public boolean equals(Object other) { | ||
94 | + if (other == this) { | ||
95 | + return true; | ||
96 | + } | ||
97 | + if ((other instanceof UpdateCreditRequest) == false) { | ||
98 | + return false; | ||
99 | + } | ||
100 | + UpdateCreditRequest rhs = ((UpdateCreditRequest) other); | ||
101 | + return new EqualsBuilder().append(participant, rhs.participant).append(prix, rhs.prix).append(additionalProperties, rhs.additionalProperties).isEquals(); | ||
102 | + } | ||
103 | + | ||
104 | +} |
src/main/java/etunicorn/generated/model/UpdatePersonneRequest.java
1 | 1 | ||
2 | package etunicorn.generated.model; | 2 | package etunicorn.generated.model; |
3 | 3 | ||
4 | -import java.util.HashMap; | ||
5 | -import java.util.Map; | ||
6 | -import com.fasterxml.jackson.annotation.JsonAnyGetter; | ||
7 | -import com.fasterxml.jackson.annotation.JsonAnySetter; | ||
8 | -import com.fasterxml.jackson.annotation.JsonIgnore; | ||
9 | -import com.fasterxml.jackson.annotation.JsonInclude; | ||
10 | -import com.fasterxml.jackson.annotation.JsonProperty; | ||
11 | -import com.fasterxml.jackson.annotation.JsonPropertyOrder; | 4 | +import com.fasterxml.jackson.annotation.*; |
12 | import org.apache.commons.lang.builder.EqualsBuilder; | 5 | import org.apache.commons.lang.builder.EqualsBuilder; |
13 | import org.apache.commons.lang.builder.HashCodeBuilder; | 6 | import org.apache.commons.lang.builder.HashCodeBuilder; |
14 | import org.apache.commons.lang.builder.ToStringBuilder; | 7 | import org.apache.commons.lang.builder.ToStringBuilder; |
15 | 8 | ||
9 | +import java.util.HashMap; | ||
10 | +import java.util.Map; | ||
11 | + | ||
16 | @JsonInclude(JsonInclude.Include.NON_NULL) | 12 | @JsonInclude(JsonInclude.Include.NON_NULL) |
17 | @JsonPropertyOrder({ | 13 | @JsonPropertyOrder({ |
18 | "carte", | 14 | "carte", |
@@ -36,7 +32,7 @@ public class UpdatePersonneRequest { | @@ -36,7 +32,7 @@ public class UpdatePersonneRequest { | ||
36 | * | 32 | * |
37 | */ | 33 | */ |
38 | @JsonProperty("login") | 34 | @JsonProperty("login") |
39 | - private Object login; | 35 | + private String login; |
40 | /** | 36 | /** |
41 | * | 37 | * |
42 | */ | 38 | */ |
@@ -91,7 +87,7 @@ public class UpdatePersonneRequest { | @@ -91,7 +87,7 @@ public class UpdatePersonneRequest { | ||
91 | * The login | 87 | * The login |
92 | */ | 88 | */ |
93 | @JsonProperty("login") | 89 | @JsonProperty("login") |
94 | - public Object getLogin() { | 90 | + public String getLogin() { |
95 | return login; | 91 | return login; |
96 | } | 92 | } |
97 | 93 | ||
@@ -101,7 +97,7 @@ public class UpdatePersonneRequest { | @@ -101,7 +97,7 @@ public class UpdatePersonneRequest { | ||
101 | * The login | 97 | * The login |
102 | */ | 98 | */ |
103 | @JsonProperty("login") | 99 | @JsonProperty("login") |
104 | - public void setLogin(Object login) { | 100 | + public void setLogin(String login) { |
105 | this.login = login; | 101 | this.login = login; |
106 | } | 102 | } |
107 | 103 |