rsanchez
2017-03-24 4f5711b8ec555ab8307516ce178b454445d3833f
securis/src/main/java/net/curisit/securis/services/LicenseTypeResource.java
....@@ -4,6 +4,7 @@
44 import java.util.HashSet;
55 import java.util.List;
66 import java.util.Set;
7
+import java.util.stream.Collectors;
78
89 import javax.annotation.security.RolesAllowed;
910 import javax.inject.Inject;
....@@ -24,6 +25,9 @@
2425 import javax.ws.rs.core.Response;
2526 import javax.ws.rs.core.Response.Status;
2627
28
+import org.apache.logging.log4j.LogManager;
29
+import org.apache.logging.log4j.Logger;
30
+
2731 import net.curisit.integrity.commons.Utils;
2832 import net.curisit.securis.DefaultExceptionHandler;
2933 import net.curisit.securis.SeCurisException;
....@@ -35,10 +39,8 @@
3539 import net.curisit.securis.security.Securable;
3640 import net.curisit.securis.services.exception.SeCurisServiceException;
3741 import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes;
42
+import net.curisit.securis.services.helpers.MetadataHelper;
3843 import net.curisit.securis.utils.TokenHelper;
39
-
40
-import org.apache.logging.log4j.LogManager;
41
-import org.apache.logging.log4j.Logger;
4244
4345 /**
4446 * LicenseType resource, this service will provide methods to create, modify and
....@@ -49,206 +51,204 @@
4951 @Path("/licensetype")
5052 public class LicenseTypeResource {
5153
52
- private static final Logger LOG = LogManager.getLogger(LicenseTypeResource.class);
54
+ private static final Logger LOG = LogManager.getLogger(LicenseTypeResource.class);
5355
54
- @Inject
55
- TokenHelper tokenHelper;
56
+ @Inject
57
+ TokenHelper tokenHelper;
5658
57
- @Context
58
- EntityManager em;
59
+ @Inject
60
+ MetadataHelper metadataHelper;
5961
60
- public LicenseTypeResource() {
61
- }
62
+ @Context
63
+ EntityManager em;
6264
63
- /**
64
- *
65
- * @return the server version in format majorVersion.minorVersion
66
- */
67
- @GET
68
- @Path("/")
69
- @Produces({
70
- MediaType.APPLICATION_JSON
71
- })
72
- @Securable
73
- public Response index() {
74
- LOG.info("Getting license types list ");
65
+ public LicenseTypeResource() {
66
+ }
7567
76
- // EntityManager em = emProvider.get();
77
- em.clear();
78
- TypedQuery<LicenseType> q = em.createNamedQuery("list-license_types", LicenseType.class);
79
- List<LicenseType> list = q.getResultList();
68
+ /**
69
+ *
70
+ * @return the server version in format majorVersion.minorVersion
71
+ */
72
+ @GET
73
+ @Path("/")
74
+ @Produces({ MediaType.APPLICATION_JSON })
75
+ @Securable
76
+ public Response index() {
77
+ LOG.info("Getting license types list ");
8078
81
- return Response.ok(list).build();
82
- }
79
+ // EntityManager em = emProvider.get();
80
+ em.clear();
81
+ TypedQuery<LicenseType> q = em.createNamedQuery("list-license_types", LicenseType.class);
82
+ List<LicenseType> list = q.getResultList();
8383
84
- /**
85
- *
86
- * @return the server version in format majorVersion.minorVersion
87
- * @throws SeCurisServiceException
88
- */
89
- @GET
90
- @Path("/{ltid}")
91
- @Produces({
92
- MediaType.APPLICATION_JSON
93
- })
94
- @Securable
95
- public Response get(@PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) throws SeCurisServiceException {
96
- LOG.info("Getting license type data for id: {}: ", ltid);
97
- if (ltid == null || "".equals(ltid)) {
98
- LOG.error("LicenseType ID is mandatory");
99
- return Response.status(Status.NOT_FOUND).build();
100
- }
84
+ return Response.ok(list).build();
85
+ }
10186
102
- // EntityManager em = emProvider.get();
103
- em.clear();
104
- LicenseType lt = em.find(LicenseType.class, Integer.parseInt(ltid));
105
- if (lt == null) {
106
- LOG.error("LicenseType with id {} not found in DB", ltid);
107
- throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "LicenseType was not found in DB");
108
- }
109
- return Response.ok(lt).build();
110
- }
87
+ /**
88
+ *
89
+ * @return the server version in format majorVersion.minorVersion
90
+ * @throws SeCurisServiceException
91
+ */
92
+ @GET
93
+ @Path("/{ltid}")
94
+ @Produces({ MediaType.APPLICATION_JSON })
95
+ @Securable
96
+ public Response get(@PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) throws SeCurisServiceException {
97
+ LOG.info("Getting license type data for id: {}: ", ltid);
98
+ if (ltid == null || "".equals(ltid)) {
99
+ LOG.error("LicenseType ID is mandatory");
100
+ return Response.status(Status.NOT_FOUND).build();
101
+ }
111102
112
- @POST
113
- @Path("/")
114
- @Consumes(MediaType.APPLICATION_JSON)
115
- @Produces({
116
- MediaType.APPLICATION_JSON
117
- })
118
- @EnsureTransaction
119
- @Securable
120
- @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
121
- public Response create(LicenseType lt, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
122
- LOG.info("Creating new license type");
123
- // EntityManager em = emProvider.get();
103
+ // EntityManager em = emProvider.get();
104
+ em.clear();
105
+ LicenseType lt = em.find(LicenseType.class, Integer.parseInt(ltid));
106
+ if (lt == null) {
107
+ LOG.error("LicenseType with id {} not found in DB", ltid);
108
+ throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "LicenseType was not found in DB");
109
+ }
110
+ return Response.ok(lt).build();
111
+ }
124112
125
- try {
126
- setApplication(lt, lt.getApplicationId(), em);
127
- } catch (SeCurisException e) {
128
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
129
- }
113
+ @POST
114
+ @Path("/")
115
+ @Consumes(MediaType.APPLICATION_JSON)
116
+ @Produces({ MediaType.APPLICATION_JSON })
117
+ @EnsureTransaction
118
+ @Securable
119
+ @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
120
+ public Response create(LicenseType lt, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
121
+ LOG.info("Creating new license type");
122
+ // EntityManager em = emProvider.get();
130123
131
- if (lt.getApplicationId() == null) {
132
- LOG.error("Application is missing for current license type data");
133
- return Response.status(Status.NOT_FOUND)
134
- .header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application is missing for current license type data").build();
135
- }
124
+ try {
125
+ setApplication(lt, lt.getApplicationId(), em);
126
+ } catch (SeCurisException e) {
127
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
128
+ }
136129
137
- lt.setCreationTimestamp(new Date());
138
- em.persist(lt);
139
- Set<LicenseTypeMetadata> newMD = lt.getMetadata();
130
+ if (lt.getApplicationId() == null) {
131
+ LOG.error("Application is missing for current license type data");
132
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Application is missing for current license type data").build();
133
+ }
140134
141
- if (newMD != null) {
142
- for (LicenseTypeMetadata md : newMD) {
143
- md.setLicenseType(lt);
144
- em.persist(md);
145
- }
146
- }
147
- lt.setMetadata(newMD);
135
+ lt.setCreationTimestamp(new Date());
136
+ em.persist(lt);
137
+ Set<LicenseTypeMetadata> newMD = lt.getMetadata();
148138
149
- return Response.ok(lt).build();
150
- }
139
+ if (newMD != null) {
140
+ for (LicenseTypeMetadata md : newMD) {
141
+ md.setLicenseType(lt);
142
+ em.persist(md);
143
+ }
144
+ }
145
+ lt.setMetadata(newMD);
151146
152
- private Set<String> getMdKeys(Set<LicenseTypeMetadata> mds) {
153
- Set<String> ids = new HashSet<String>();
154
- if (mds != null) {
155
- for (LicenseTypeMetadata md : mds) {
156
- ids.add(md.getKey());
157
- }
158
- }
159
- return ids;
160
- }
147
+ return Response.ok(lt).build();
148
+ }
161149
162
- @PUT
163
- @POST
164
- @Path("/{ltid}")
165
- @EnsureTransaction
166
- @Consumes(MediaType.APPLICATION_JSON)
167
- @Produces({
168
- MediaType.APPLICATION_JSON
169
- })
170
- @Securable
171
- @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
172
- public Response modify(LicenseType lt, @PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
173
- LOG.info("Modifying license type with id: {}", ltid);
174
- // EntityManager em = emProvider.get();
175
- LicenseType currentlt = em.find(LicenseType.class, Integer.parseInt(ltid));
176
- if (currentlt == null) {
177
- LOG.error("LicenseType with id {} not found in DB", ltid);
178
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License type not found with ID: " + ltid)
179
- .build();
180
- }
150
+ private Set<String> getMdKeys(Set<LicenseTypeMetadata> mds) {
151
+ Set<String> ids = new HashSet<String>();
152
+ if (mds != null) {
153
+ for (LicenseTypeMetadata md : mds) {
154
+ ids.add(md.getKey());
155
+ }
156
+ }
157
+ return ids;
158
+ }
181159
182
- try {
183
- setApplication(currentlt, lt.getApplicationId(), em);
184
- } catch (SeCurisException e) {
185
- return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
186
- }
160
+ @PUT
161
+ @POST
162
+ @Path("/{ltid}")
163
+ @EnsureTransaction
164
+ @Consumes(MediaType.APPLICATION_JSON)
165
+ @Produces({ MediaType.APPLICATION_JSON })
166
+ @Securable
167
+ @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
168
+ public Response modify(LicenseType lt, @PathParam("ltid") String ltid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
169
+ LOG.info("Modifying license type with id: {}", ltid);
170
+ // EntityManager em = emProvider.get();
171
+ LicenseType currentlt = em.find(LicenseType.class, Integer.parseInt(ltid));
172
+ if (currentlt == null) {
173
+ LOG.error("LicenseType with id {} not found in DB", ltid);
174
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License type not found with ID: " + ltid).build();
175
+ }
187176
188
- currentlt.setCode(lt.getCode());
189
- currentlt.setName(lt.getName());
190
- currentlt.setDescription(lt.getDescription());
177
+ try {
178
+ setApplication(currentlt, lt.getApplicationId(), em);
179
+ } catch (SeCurisException e) {
180
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
181
+ }
191182
192
- Set<LicenseTypeMetadata> newMD = lt.getMetadata();
193
- Set<String> newMdKeys = getMdKeys(newMD);
194
- for (LicenseTypeMetadata currentMd : currentlt.getMetadata()) {
195
- if (!newMdKeys.contains(currentMd.getKey())) {
196
- em.remove(currentMd);
197
- LOG.info("Removing MD: {}", currentMd);
198
- }
199
- }
183
+ currentlt.setCode(lt.getCode());
184
+ currentlt.setName(lt.getName());
185
+ currentlt.setDescription(lt.getDescription());
200186
201
- if (newMD != null) {
202
- Set<LicenseTypeMetadata> oldMD = currentlt.getMetadata();
203
- Set<String> oldMdKeys = getMdKeys(oldMD);
187
+ Set<LicenseTypeMetadata> newMD = lt.getMetadata();
188
+ Set<LicenseTypeMetadata> oldMD = currentlt.getMetadata();
189
+ boolean metadataChanges = !metadataHelper.match(newMD, oldMD);
190
+ if (metadataChanges) {
191
+ Set<String> newMdKeys = getMdKeys(newMD);
192
+ for (LicenseTypeMetadata currentMd : oldMD) {
193
+ if (!newMdKeys.contains(currentMd.getKey())) {
194
+ em.remove(currentMd);
195
+ LOG.info("Removing MD: {}", currentMd);
196
+ }
197
+ }
204198
205
- for (LicenseTypeMetadata md : newMD) {
206
- if (oldMdKeys.contains(md.getKey())) {
207
- em.merge(md);
208
- } else {
209
- md.setLicenseType(currentlt);
210
- em.persist(md);
211
- }
212
- }
213
- }
214
- currentlt.setMetadata(newMD);
215
- em.merge(currentlt);
199
+ if (newMD != null) {
200
+ Set<String> oldMdKeys = getMdKeys(oldMD);
216201
217
- return Response.ok(currentlt).build();
218
- }
202
+ for (LicenseTypeMetadata md : newMD) {
203
+ if (oldMdKeys.contains(md.getKey())) {
204
+ em.merge(md);
205
+ } else {
206
+ md.setLicenseType(currentlt);
207
+ em.persist(md);
208
+ }
209
+ }
210
+ }
211
+ currentlt.setMetadata(newMD);
212
+ }
213
+ em.merge(currentlt);
214
+ if (metadataChanges) {
215
+ Set<String> keys = newMD.parallelStream().map(md -> md.getKey()).collect(Collectors.toSet());
216
+ metadataHelper.propagateMetadata(em, currentlt, keys);
217
+ }
219218
220
- private void setApplication(LicenseType licType, Integer applicationId, EntityManager em) throws SeCurisException {
221
- Application app = null;
222
- if (applicationId != null) {
223
- app = em.find(Application.class, applicationId);
224
- if (app == null) {
225
- LOG.error("LicenseType application with id {} not found in DB", applicationId);
219
+ return Response.ok(currentlt).build();
220
+ }
226221
227
- throw new SecurityException("License type's app not found with ID: " + applicationId);
228
- }
229
- }
230
- licType.setApplication(app);
231
- }
222
+ private void setApplication(LicenseType licType, Integer applicationId, EntityManager em) throws SeCurisException {
223
+ Application app = null;
224
+ if (applicationId != null) {
225
+ app = em.find(Application.class, applicationId);
226
+ if (app == null) {
227
+ LOG.error("LicenseType application with id {} not found in DB", applicationId);
232228
233
- @DELETE
234
- @Path("/{ltid}")
235
- @EnsureTransaction
236
- @Produces({
237
- MediaType.APPLICATION_JSON
238
- })
239
- @Securable
240
- @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
241
- public Response delete(@PathParam("ltid") String ltid, @Context HttpServletRequest request) {
242
- LOG.info("Deleting app with id: {}", ltid);
243
- // EntityManager em = emProvider.get();
244
- LicenseType app = em.find(LicenseType.class, Integer.parseInt(ltid));
245
- if (app == null) {
246
- LOG.error("LicenseType with id {} can not be deleted, It was not found in DB", ltid);
247
- return Response.status(Status.NOT_FOUND).build();
248
- }
229
+ throw new SecurityException("License type's app not found with ID: " + applicationId);
230
+ }
231
+ }
232
+ licType.setApplication(app);
233
+ }
249234
250
- em.remove(app);
251
- return Response.ok(Utils.createMap("success", true, "id", ltid)).build();
252
- }
235
+ @DELETE
236
+ @Path("/{ltid}")
237
+ @EnsureTransaction
238
+ @Produces({ MediaType.APPLICATION_JSON })
239
+ @Securable
240
+ @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
241
+ public Response delete(@PathParam("ltid") String ltid, @Context HttpServletRequest request) {
242
+ LOG.info("Deleting app with id: {}", ltid);
243
+ // EntityManager em = emProvider.get();
244
+ LicenseType app = em.find(LicenseType.class, Integer.parseInt(ltid));
245
+ if (app == null) {
246
+ LOG.error("LicenseType with id {} can not be deleted, It was not found in DB", ltid);
247
+ return Response.status(Status.NOT_FOUND).build();
248
+ }
249
+
250
+ em.remove(app);
251
+ return Response.ok(Utils.createMap("success", true, "id", ltid)).build();
252
+ }
253253
254254 }