diff --git a/src/main/java/fr/istic/web/rest/CommentsResource.java b/src/main/java/fr/istic/web/rest/CommentsResource.java index cc524b9..344f855 100644 --- a/src/main/java/fr/istic/web/rest/CommentsResource.java +++ b/src/main/java/fr/istic/web/rest/CommentsResource.java @@ -4,6 +4,7 @@ import fr.istic.domain.Authority; import fr.istic.domain.Comments; +import fr.istic.domain.ExamSheet; import fr.istic.domain.Student; import fr.istic.domain.User; import fr.istic.security.AuthoritiesConstants; @@ -13,6 +14,7 @@ import fr.istic.web.util.HeaderUtil; import fr.istic.web.util.ResponseUtil; import fr.istic.service.dto.CommentsDTO; +import fr.istic.service.dto.ExamSheetDTO; import org.eclipse.microprofile.config.inject.ConfigProperty; import org.slf4j.Logger; @@ -33,6 +35,9 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.StringTokenizer; +import java.util.stream.Collector; +import java.util.stream.Collectors; /** * REST controller for managing {@link fr.istic.domain.Comments}. @@ -50,7 +55,6 @@ public class CommentsResource { @ConfigProperty(name = "application.name") String applicationName; - @Inject CommentsService commentsService; @@ -61,10 +65,12 @@ public class CommentsResource { * {@code POST /comments} : Create a new comments. * * @param commentsDTO the commentsDTO to create. - * @return the {@link Response} with status {@code 201 (Created)} and with body the new commentsDTO, or with status {@code 400 (Bad Request)} if the comments has already an ID. + * @return the {@link Response} with status {@code 201 (Created)} and with body + * the new commentsDTO, or with status {@code 400 (Bad Request)} if the + * comments has already an ID. */ @POST - @RolesAllowed({AuthoritiesConstants.USER, AuthoritiesConstants.ADMIN}) + @RolesAllowed({ AuthoritiesConstants.USER, AuthoritiesConstants.ADMIN }) public Response createComments(CommentsDTO commentsDTO, @Context UriInfo uriInfo) { log.debug("REST request to save Comments : {}", commentsDTO); if (commentsDTO.id != null) { @@ -72,7 +78,8 @@ public Response createComments(CommentsDTO commentsDTO, @Context UriInfo uriInfo } var result = commentsService.persistOrUpdate(commentsDTO); var response = Response.created(fromPath(uriInfo.getPath()).path(result.id.toString()).build()).entity(result); - HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, result.id.toString()).forEach(response::header); + HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, result.id.toString()) + .forEach(response::header); return response.build(); } @@ -80,25 +87,28 @@ public Response createComments(CommentsDTO commentsDTO, @Context UriInfo uriInfo * {@code PUT /comments} : Updates an existing comments. * * @param commentsDTO the commentsDTO to update. - * @return the {@link Response} with status {@code 200 (OK)} and with body the updated commentsDTO, - * or with status {@code 400 (Bad Request)} if the commentsDTO is not valid, - * or with status {@code 500 (Internal Server Error)} if the commentsDTO couldn't be updated. + * @return the {@link Response} with status {@code 200 (OK)} and with body the + * updated commentsDTO, + * or with status {@code 400 (Bad Request)} if the commentsDTO is not + * valid, + * or with status {@code 500 (Internal Server Error)} if the commentsDTO + * couldn't be updated. */ @PUT - @RolesAllowed({AuthoritiesConstants.USER, AuthoritiesConstants.ADMIN}) + @RolesAllowed({ AuthoritiesConstants.USER, AuthoritiesConstants.ADMIN }) public Response updateComments(CommentsDTO commentsDTO, @Context SecurityContext ctx) { // log.error("REST request to update Comments : {}", commentsDTO); if (commentsDTO.id == null) { throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull"); } - if (!securityService.canAccess(ctx, commentsDTO.id, Comments.class )){ + if (!securityService.canAccess(ctx, commentsDTO.id, Comments.class)) { return Response.status(403, "Current user cannot access to this ressource").build(); } - var result = commentsService.persistOrUpdate(commentsDTO); var response = Response.ok().entity(result); - HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, commentsDTO.id.toString()).forEach(response::header); + HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, commentsDTO.id.toString()) + .forEach(response::header); return response.build(); } @@ -109,17 +119,19 @@ public Response updateComments(CommentsDTO commentsDTO, @Context SecurityContext * @return the {@link Response} with status {@code 204 (NO_CONTENT)}. */ @DELETE - @RolesAllowed({AuthoritiesConstants.USER, AuthoritiesConstants.ADMIN}) + @RolesAllowed({ AuthoritiesConstants.USER, AuthoritiesConstants.ADMIN }) @Path("/{id}") public Response deleteComments(@PathParam("id") Long id, @Context SecurityContext ctx) { log.debug("REST request to delete Comments : {}", id); - if (!securityService.canAccess(ctx, id, Comments.class )){ + if (!securityService.canAccess(ctx, id, Comments.class)) { return Response.status(403, "Current user cannot access to this ressource").build(); - }; + } + ; commentsService.delete(id); var response = Response.noContent(); - HeaderUtil.createEntityDeletionAlert(applicationName, true, ENTITY_NAME, id.toString()).forEach(response::header); + HeaderUtil.createEntityDeletionAlert(applicationName, true, ENTITY_NAME, id.toString()) + .forEach(response::header); return response.build(); } @@ -127,60 +139,118 @@ public Response deleteComments(@PathParam("id") Long id, @Context SecurityContex * {@code GET /comments} : get all the comments. * * @param pageRequest the pagination information. - * @return the {@link Response} with status {@code 200 (OK)} and the list of comments in body. + * @return the {@link Response} with status {@code 200 (OK)} and the list of + * comments in body. */ @GET - public Response getAllComments(@BeanParam PageRequestVM pageRequest, @BeanParam SortRequestVM sortRequest, @Context UriInfo uriInfo, @Context SecurityContext ctx) { + public Response getAllComments(@BeanParam PageRequestVM pageRequest, @BeanParam SortRequestVM sortRequest, + @Context UriInfo uriInfo, @Context SecurityContext ctx) { log.debug("REST request to get a page of Comments"); var page = pageRequest.toPage(); var sort = sortRequest.toSort(); - Paged result = new Paged<>(0, 0, 0,0, new ArrayList<>()); + Paged result = new Paged<>(0, 0, 0, 0, new ArrayList<>()); MultivaluedMap param = uriInfo.getQueryParameters(); if (param.containsKey("zonegeneratedid")) { List zonegeneratedid = (List) param.get("zonegeneratedid"); - result = commentsService.findCommentsbyZonegeneratedid(page, zonegeneratedid.get(0)); - } - else { - if (ctx.getUserPrincipal().getName()!= null){ + result = commentsService.findCommentsbyZonegeneratedid(page, zonegeneratedid.get(0)); + } else { + if (ctx.getUserPrincipal().getName() != null) { - var userLogin = Optional - .ofNullable(ctx.getUserPrincipal().getName()); - if (!userLogin.isPresent()){ - throw new AccountResourceException("Current user login not found"); - } - var user = User.findOneByLogin(userLogin.get()); - if (!user.isPresent()) { - throw new AccountResourceException("User could not be found"); - } - else if (user.get().authorities.size() >= 1 && user.get().authorities.stream().anyMatch(e1-> e1.equals(new Authority("ROLE_ADMIN")))){ - result =commentsService.findAll(page); + var userLogin = Optional + .ofNullable(ctx.getUserPrincipal().getName()); + if (!userLogin.isPresent()) { + throw new AccountResourceException("Current user login not found"); + } + var user = User.findOneByLogin(userLogin.get()); + if (!user.isPresent()) { + throw new AccountResourceException("User could not be found"); + } else if (user.get().authorities.size() >= 1 + && user.get().authorities.stream().anyMatch(e1 -> e1.equals(new Authority("ROLE_ADMIN")))) { + result = commentsService.findAll(page); - } else { - return Response.status(403, "Current user cannot access to this ressource").build(); + } else { + return Response.status(403, "Current user cannot access to this ressource").build(); + } } } - } var response = Response.ok().entity(result.content); response = PaginationUtil.withPaginationInfo(response, uriInfo, result); return response.build(); } - /** * {@code GET /comments/:id} : get the "id" comments. * * @param id the id of the commentsDTO to retrieve. - * @return the {@link Response} with status {@code 200 (OK)} and with body the commentsDTO, or with status {@code 404 (Not Found)}. + * @return the {@link Response} with status {@code 200 (OK)} and with body the + * commentsDTO, or with status {@code 404 (Not Found)}. */ @GET @Path("/{id}") public Response getComments(@PathParam("id") Long id, @Context SecurityContext ctx) { log.debug("REST request to get Comments : {}", id); - if (!securityService.canAccess(ctx, id, Comments.class )){ + if (!securityService.canAccess(ctx, id, Comments.class)) { return Response.status(403, "Current user cannot access to this ressource").build(); - }; + } + ; Optional commentsDTO = commentsService.findOne(id); return ResponseUtil.wrapOrNotFound(commentsDTO); } + + @GET + @Path("/migrate/{exId}") + @Produces(MediaType.TEXT_PLAIN) + public String migrateExamSheets4ExamId(@PathParam("exId") Long exId, @Context SecurityContext ctx) { + log.debug("REST request to migrate Comment : {}", exId); + List toMigrate = Comments.findCommentByExamId("" + exId).list(); + List sheets = ExamSheet.getAll4ExamId(exId).list(); + StringBuffer result = new StringBuffer(); + List newIds = new ArrayList<>(); + for (Comments comment : toMigrate) { + + StringTokenizer t = new StringTokenizer(comment.zonegeneratedid, "_"); + if (t.countTokens() > 1) { + String examId = t.nextToken(); + String studentId = t.nextToken(); + String questiono = t.nextToken(); + String index = t.nextToken(); + + List sheetsf = new ArrayList<>(); + for (ExamSheet sh : sheets){ + if (sh.students != null && sh.students.size() > 0){ + Student s = (Student) sh.students.toArray()[0]; + if (Long.valueOf(studentId).equals(s.id)){ + sheetsf.add(sh); + } + } + } + /* + * '' + this.exam!.id + '_' + this.selectionStudents![0].id + '_' + + * this.questionno + '_' + index + * + */ + if (sheetsf.size() > 0) { + + var zonegeneratedid = "" + examId + "_" + + sheetsf.get(0).id + "_" + questiono + "_" + + index; + if (newIds.contains(comment.zonegeneratedid)){ + result.append("--CLASH for " + comment.zonegeneratedid + "\n"); + + } + result.append("update comments set zonegeneratedid=\"" + zonegeneratedid + + "\" WHERE zonegeneratedid=\"" + comment.zonegeneratedid + "\";\n"); + newIds.add(zonegeneratedid); + + } else { + result.append("--no match for " + comment.zonegeneratedid + "\n"); + } + } + + } + + return result.toString(); + } + } diff --git a/src/main/java/fr/istic/web/rest/ExamSheetResource.java b/src/main/java/fr/istic/web/rest/ExamSheetResource.java index ed5d2a1..a534175 100644 --- a/src/main/java/fr/istic/web/rest/ExamSheetResource.java +++ b/src/main/java/fr/istic/web/rest/ExamSheetResource.java @@ -33,6 +33,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.StringTokenizer; /** * REST controller for managing {@link fr.istic.domain.ExamSheet}. @@ -272,4 +273,5 @@ public Response getExamSheet(@PathParam("id") Long id, @Context SecurityContext Optional examSheetDTO = examSheetService.findOne(id); return ResponseUtil.wrapOrNotFound(examSheetDTO); } + }