From 84d90f17c48a9daa363549785c1d594ce30b8e16 Mon Sep 17 00:00:00 2001 From: y72wvh Date: Thu, 4 Apr 2024 10:09:20 +0200 Subject: [PATCH 1/7] wip: source open close --- pom.xml | 10 +++ .../config/cache/CacheConfig.java | 25 +++++++ .../config/cache/CacheName.java | 10 +++ .../constants/Constants.java | 5 ++ .../metadata/controller/SourceController.java | 67 ++++++++++++++----- .../metadata/controller/SurveyController.java | 1 + .../metadata/domain/Source.java | 3 + .../metadata/dto/OpenDto.java | 13 ++++ .../metadata/dto/SourceCompleteDto.java | 4 ++ .../metadata/dto/SourceDto.java | 2 + src/main/resources/application.properties | 5 +- 11 files changed, 127 insertions(+), 18 deletions(-) create mode 100644 src/main/java/fr/insee/survey/datacollectionmanagement/config/cache/CacheConfig.java create mode 100644 src/main/java/fr/insee/survey/datacollectionmanagement/config/cache/CacheName.java create mode 100644 src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/OpenDto.java diff --git a/pom.xml b/pom.xml index 9796a691..6e0bf364 100644 --- a/pom.xml +++ b/pom.xml @@ -18,6 +18,7 @@ 21 2.2.0 3.1.0 + 3.1.8 3.1.0 5.0.0 1.0.2 @@ -41,6 +42,10 @@ org.springframework.boot spring-boot-starter-validation + + org.springframework.boot + spring-boot-starter-cache + org.modelmapper modelmapper @@ -66,6 +71,11 @@ springdoc-openapi-starter-webmvc-ui ${springdoc-version} + + com.github.ben-manes.caffeine + caffeine + ${caffeine.version} + jakarta.ws.rs jakarta.ws.rs-api diff --git a/src/main/java/fr/insee/survey/datacollectionmanagement/config/cache/CacheConfig.java b/src/main/java/fr/insee/survey/datacollectionmanagement/config/cache/CacheConfig.java new file mode 100644 index 00000000..a50f8f57 --- /dev/null +++ b/src/main/java/fr/insee/survey/datacollectionmanagement/config/cache/CacheConfig.java @@ -0,0 +1,25 @@ +package fr.insee.survey.datacollectionmanagement.config.cache; + +import com.github.benmanes.caffeine.cache.Caffeine; +import lombok.extern.slf4j.Slf4j; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.cache.caffeine.CaffeineCache; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.concurrent.TimeUnit; + +@Configuration +@EnableCaching +@Slf4j +public class CacheConfig { + @Bean + protected CaffeineCache habilitationCache() { + return new CaffeineCache(CacheName.SOURCE_OPENED, + Caffeine.newBuilder() + .initialCapacity(2000) + .maximumSize(20000) + .expireAfterAccess(10, TimeUnit.MINUTES) + .build()); + } +} diff --git a/src/main/java/fr/insee/survey/datacollectionmanagement/config/cache/CacheName.java b/src/main/java/fr/insee/survey/datacollectionmanagement/config/cache/CacheName.java new file mode 100644 index 00000000..98123bb6 --- /dev/null +++ b/src/main/java/fr/insee/survey/datacollectionmanagement/config/cache/CacheName.java @@ -0,0 +1,10 @@ +package fr.insee.survey.datacollectionmanagement.config.cache; + +public class CacheName { + + private CacheName() { + throw new IllegalArgumentException("Utility class"); + } + + public static final String SOURCE_OPENED= "source-opened"; +} diff --git a/src/main/java/fr/insee/survey/datacollectionmanagement/constants/Constants.java b/src/main/java/fr/insee/survey/datacollectionmanagement/constants/Constants.java index 7053e06b..f420a4d6 100644 --- a/src/main/java/fr/insee/survey/datacollectionmanagement/constants/Constants.java +++ b/src/main/java/fr/insee/survey/datacollectionmanagement/constants/Constants.java @@ -2,6 +2,7 @@ public class Constants { + private Constants() { throw new IllegalStateException("Constants class"); } @@ -45,11 +46,15 @@ private Constants() { public static final String API_SOURCES_ID = "/api/sources/{id}"; public static final String API_SOURCES_ID_SURVEYS = "/api/sources/{id}/surveys"; public static final String API_SURVEYS = "/api/surveys"; + public static final String API_SOURCE_ID_OPENED = "/api/source/{id}/is-opened"; + public static final String API_SURVEYS_SEARCH = "/api/surveys/search"; public static final String API_SURVEYS_ID = "/api/surveys/{id}"; public static final String API_SURVEYS_ID_CAMPAIGNS = "/api/surveys/{id}/campaigns"; + + public static final String API_SURVEYS_ID_CAMPAIGNS_PARTITIONINGS = "/api/surveys/{id}/campaigns-partitionings"; public static final String API_CAMPAIGNS = "/api/campaigns"; public static final String API_CAMPAIGNS_ID = "/api/campaigns/{id}"; diff --git a/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/controller/SourceController.java b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/controller/SourceController.java index 6a6f1367..14f66b63 100644 --- a/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/controller/SourceController.java +++ b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/controller/SourceController.java @@ -1,13 +1,13 @@ package fr.insee.survey.datacollectionmanagement.metadata.controller; +import fr.insee.survey.datacollectionmanagement.config.cache.CacheName; import fr.insee.survey.datacollectionmanagement.constants.Constants; import fr.insee.survey.datacollectionmanagement.exception.NotFoundException; import fr.insee.survey.datacollectionmanagement.exception.NotMatchException; -import fr.insee.survey.datacollectionmanagement.metadata.domain.Campaign; -import fr.insee.survey.datacollectionmanagement.metadata.domain.Owner; -import fr.insee.survey.datacollectionmanagement.metadata.domain.Partitioning; -import fr.insee.survey.datacollectionmanagement.metadata.domain.Source; +import fr.insee.survey.datacollectionmanagement.metadata.domain.*; +import fr.insee.survey.datacollectionmanagement.metadata.dto.OpenDto; import fr.insee.survey.datacollectionmanagement.metadata.dto.SourceCompleteDto; +import fr.insee.survey.datacollectionmanagement.metadata.service.CampaignService; import fr.insee.survey.datacollectionmanagement.metadata.service.OwnerService; import fr.insee.survey.datacollectionmanagement.metadata.service.SourceService; import fr.insee.survey.datacollectionmanagement.metadata.service.SupportService; @@ -20,6 +20,8 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.modelmapper.ModelMapper; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; import org.springframework.data.domain.*; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; @@ -55,6 +57,8 @@ public class SourceController { private final QuestioningService questioningService; + private final CampaignService campaignService; + @Operation(summary = "Search for sources, paginated") @GetMapping(value = Constants.API_SOURCES, produces = "application/json") public ResponseEntity getSources( @@ -71,15 +75,15 @@ public ResponseEntity getSources( @GetMapping(value = Constants.API_SOURCES_ID, produces = "application/json") public ResponseEntity getSource(@PathVariable("id") String id) { Source source = sourceService.findById(StringUtils.upperCase(id)); - source = sourceService.findById(StringUtils.upperCase(id)); return ResponseEntity.ok().body(convertToDto(source)); } @Operation(summary = "Update or create a source") + @CacheEvict(CacheName.SOURCE_OPENED) @PutMapping(value = Constants.API_SOURCES_ID, produces = "application/json", consumes = "application/json") - public ResponseEntity putSource(@PathVariable("id") String id, @RequestBody @Valid SourceCompleteDto SourceCompleteDto) { - if (!SourceCompleteDto.getId().equalsIgnoreCase(id)) { + public ResponseEntity putSource(@PathVariable("id") String id, @RequestBody @Valid SourceCompleteDto sourceCompleteDto) { + if (!sourceCompleteDto.getId().equalsIgnoreCase(id)) { throw new NotMatchException("id and source id don't match"); } @@ -87,19 +91,19 @@ public ResponseEntity putSource(@PathVariable("id") String id Source source; HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.set(HttpHeaders.LOCATION, - ServletUriComponentsBuilder.fromCurrentRequest().buildAndExpand(SourceCompleteDto.getId()).toUriString()); + ServletUriComponentsBuilder.fromCurrentRequest().buildAndExpand(sourceCompleteDto.getId()).toUriString()); HttpStatus httpStatus; try { sourceService.findById(id); - log.warn("Update source with the id {}", SourceCompleteDto.getId()); + log.warn("Update source with the id {}", sourceCompleteDto.getId()); httpStatus = HttpStatus.OK; } catch (NotFoundException e) { - log.info("Create source with the id {}", SourceCompleteDto.getId()); + log.info("Create source with the id {}", sourceCompleteDto.getId()); httpStatus = HttpStatus.CREATED; } - source = sourceService.insertOrUpdateSource(convertToEntity(SourceCompleteDto)); + source = sourceService.insertOrUpdateSource(convertToEntity(sourceCompleteDto)); if (source.getOwner() != null && httpStatus.equals(HttpStatus.CREATED)) ownerService.addSourceFromOwner(source.getOwner(), source); if (source.getSupport() != null && httpStatus.equals(HttpStatus.CREATED)) @@ -111,9 +115,11 @@ public ResponseEntity putSource(@PathVariable("id") String id @Operation(summary = "Delete a source, its surveys, campaigns, partitionings, questionings ...") @DeleteMapping(value = Constants.API_SOURCES_ID) @ResponseStatus(HttpStatus.NO_CONTENT) + @CacheEvict(CacheName.SOURCE_OPENED) @Transactional public void deleteSource(@PathVariable("id") String id) { - int nbQuestioningDeleted = 0, nbViewDeleted = 0; + int nbQuestioningDeleted = 0; + int nbViewDeleted = 0; Source source = sourceService.findById(id); if (source.getOwner() != null) @@ -141,9 +147,40 @@ public void deleteSource(@PathVariable("id") String id) { } + @Operation(summary = "Check if a source is opened") + @GetMapping(value = Constants.API_SOURCE_ID_OPENED, produces = "application/json") + @Cacheable(CacheName.SOURCE_OPENED) + public ResponseEntity isSourceOpened(@PathVariable("id") String id) { + + + try { + Source source = sourceService.findById(id.toUpperCase()); + if (Boolean.TRUE.equals(source.getForceClose())){ + return ResponseEntity.ok().body(new OpenDto(false,source.getMessageSurveyOffline(),source.getMessageInfoSurveyOffline())); + + } + + if(source.getSurveys().isEmpty()) + return ResponseEntity.ok().body(new OpenDto(true,source.getMessageSurveyOffline(),source.getMessageInfoSurveyOffline())); + + for (Survey survey : source.getSurveys()) { + for (Campaign campaign : survey.getCampaigns()) { + if (campaignService.isCampaignOngoing(campaign.getId())) { + return ResponseEntity.ok().body(new OpenDto(true,source.getMessageSurveyOffline(),source.getMessageInfoSurveyOffline())); + } + } + } + + return ResponseEntity.ok().body(new OpenDto(false,source.getMessageSurveyOffline(),source.getMessageInfoSurveyOffline())); + } catch (NotFoundException e) { + return ResponseEntity.ok().body(new OpenDto(true,null,null)); + + } + } + @Operation(summary = "Search for surveys by the owner id") @GetMapping(value = Constants.API_OWNERS_ID_SOURCES, produces = "application/json") - public ResponseEntity getSourcesByOwner(@PathVariable("id") String id) { + public ResponseEntity> getSourcesByOwner(@PathVariable("id") String id) { Owner owner = ownerService.findById(id); return ResponseEntity.ok() .body(owner.getSources().stream().map(this::convertToDto).toList()); @@ -155,8 +192,8 @@ private SourceCompleteDto convertToDto(Source source) { return modelmapper.map(source, SourceCompleteDto.class); } - private Source convertToEntity(SourceCompleteDto SourceCompleteDto) { - return modelmapper.map(SourceCompleteDto, Source.class); + private Source convertToEntity(SourceCompleteDto sourceCompleteDto) { + return modelmapper.map(sourceCompleteDto, Source.class); } class SourcePage extends PageImpl { diff --git a/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/controller/SurveyController.java b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/controller/SurveyController.java index 74dd99a5..0fc337c4 100644 --- a/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/controller/SurveyController.java +++ b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/controller/SurveyController.java @@ -76,6 +76,7 @@ public ResponseEntity getSurveys( List listSurveys = pageSurvey.stream().map(this::convertToDto).toList(); return ResponseEntity.ok().body(new SurveyController.SurveyPage(listSurveys, pageable, pageSurvey.getTotalElements())); } + @Operation(summary = "Search for surveys by the source id") @GetMapping(value = Constants.API_SOURCES_ID_SURVEYS, produces = "application/json") public ResponseEntity> getSurveysBySource(@PathVariable("id") String id) { diff --git a/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/domain/Source.java b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/domain/Source.java index 8e47519a..c730b59a 100644 --- a/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/domain/Source.java +++ b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/domain/Source.java @@ -17,6 +17,9 @@ public class Source { private String id; private String longWording; private String shortWording; + private Boolean forceClose; + private String messageSurveyOffline; + private String messageInfoSurveyOffline; @NonNull @Enumerated(EnumType.STRING) private PeriodicityEnum periodicity; diff --git a/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/OpenDto.java b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/OpenDto.java new file mode 100644 index 00000000..76dfa0ab --- /dev/null +++ b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/OpenDto.java @@ -0,0 +1,13 @@ +package fr.insee.survey.datacollectionmanagement.metadata.dto; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class OpenDto { + private boolean surveyOnline; + private String messageSurveyOffline; + private String messageInfoSurveyOffline; + +} diff --git a/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/SourceCompleteDto.java b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/SourceCompleteDto.java index 806a40e0..c7f33eca 100644 --- a/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/SourceCompleteDto.java +++ b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/SourceCompleteDto.java @@ -1,6 +1,7 @@ package fr.insee.survey.datacollectionmanagement.metadata.dto; import fr.insee.survey.datacollectionmanagement.metadata.util.PeriodicityEnum; +import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.constraints.NotBlank; import lombok.Getter; import lombok.Setter; @@ -14,7 +15,10 @@ public class SourceCompleteDto { private String longWording; private String shortWording; private PeriodicityEnum periodicity; + @Schema(description = "Indicates whether or not you need to use the my surveys portal", defaultValue = "false") private boolean mandatoryMySurveys; + @Schema(description = "Indicates if the source should be force closed", defaultValue = "false") + private boolean forceClose = false; private String ownerId; private String supportId; diff --git a/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/SourceDto.java b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/SourceDto.java index 9c9da516..fa99adce 100644 --- a/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/SourceDto.java +++ b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/SourceDto.java @@ -13,5 +13,7 @@ public class SourceDto { private String shortWording; private PeriodicityEnum periodicity; private boolean mandatoryMySurveys; + private boolean forceClose; + } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 0e52ee54..0a93e8a0 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -74,10 +74,9 @@ fr.insee.datacollectionmanagement.roles.respondent.role=respondent fr.insee.datacollectionmanagement.roles.internal.user.role=internalUser fr.insee.datacollectionmanagement.roles.admin.role=admin fr.insee.datacollectionmanagement.roles.webclient.role=webclient -S + # Questioning url -fr.insee.datacollectionmanagement.api.questioning.url=http://localhost:8081 +fr.insee.datacollectionmanagement.api.questioning.url=http://localhost:8090 fr.insee.datacollectionmanagement.public.urls=/swagger-ui/**,/swagger-ui.html,/v3/api-docs/**,/csrf, /,/webjars/**,/swagger-resources/**,/environnement,/healthcheck,/actuator/** - From 84284907d191aebe34b99ffdd5bf804d5859f0d4 Mon Sep 17 00:00:00 2001 From: y72wvh Date: Mon, 22 Apr 2024 08:59:56 +0200 Subject: [PATCH 2/7] fix: remove cache --- pom.xml | 7 +----- .../config/cache/CacheConfig.java | 25 ------------------- .../config/cache/CacheName.java | 10 -------- .../metadata/controller/SourceController.java | 6 ----- 4 files changed, 1 insertion(+), 47 deletions(-) delete mode 100644 src/main/java/fr/insee/survey/datacollectionmanagement/config/cache/CacheConfig.java delete mode 100644 src/main/java/fr/insee/survey/datacollectionmanagement/config/cache/CacheName.java diff --git a/pom.xml b/pom.xml index 6e0bf364..a9194fab 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,6 @@ 21 2.2.0 3.1.0 - 3.1.8 3.1.0 5.0.0 1.0.2 @@ -71,11 +70,7 @@ springdoc-openapi-starter-webmvc-ui ${springdoc-version} - - com.github.ben-manes.caffeine - caffeine - ${caffeine.version} - + jakarta.ws.rs jakarta.ws.rs-api diff --git a/src/main/java/fr/insee/survey/datacollectionmanagement/config/cache/CacheConfig.java b/src/main/java/fr/insee/survey/datacollectionmanagement/config/cache/CacheConfig.java deleted file mode 100644 index a50f8f57..00000000 --- a/src/main/java/fr/insee/survey/datacollectionmanagement/config/cache/CacheConfig.java +++ /dev/null @@ -1,25 +0,0 @@ -package fr.insee.survey.datacollectionmanagement.config.cache; - -import com.github.benmanes.caffeine.cache.Caffeine; -import lombok.extern.slf4j.Slf4j; -import org.springframework.cache.annotation.EnableCaching; -import org.springframework.cache.caffeine.CaffeineCache; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import java.util.concurrent.TimeUnit; - -@Configuration -@EnableCaching -@Slf4j -public class CacheConfig { - @Bean - protected CaffeineCache habilitationCache() { - return new CaffeineCache(CacheName.SOURCE_OPENED, - Caffeine.newBuilder() - .initialCapacity(2000) - .maximumSize(20000) - .expireAfterAccess(10, TimeUnit.MINUTES) - .build()); - } -} diff --git a/src/main/java/fr/insee/survey/datacollectionmanagement/config/cache/CacheName.java b/src/main/java/fr/insee/survey/datacollectionmanagement/config/cache/CacheName.java deleted file mode 100644 index 98123bb6..00000000 --- a/src/main/java/fr/insee/survey/datacollectionmanagement/config/cache/CacheName.java +++ /dev/null @@ -1,10 +0,0 @@ -package fr.insee.survey.datacollectionmanagement.config.cache; - -public class CacheName { - - private CacheName() { - throw new IllegalArgumentException("Utility class"); - } - - public static final String SOURCE_OPENED= "source-opened"; -} diff --git a/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/controller/SourceController.java b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/controller/SourceController.java index 14f66b63..546dc7db 100644 --- a/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/controller/SourceController.java +++ b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/controller/SourceController.java @@ -1,6 +1,5 @@ package fr.insee.survey.datacollectionmanagement.metadata.controller; -import fr.insee.survey.datacollectionmanagement.config.cache.CacheName; import fr.insee.survey.datacollectionmanagement.constants.Constants; import fr.insee.survey.datacollectionmanagement.exception.NotFoundException; import fr.insee.survey.datacollectionmanagement.exception.NotMatchException; @@ -20,8 +19,6 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.modelmapper.ModelMapper; -import org.springframework.cache.annotation.CacheEvict; -import org.springframework.cache.annotation.Cacheable; import org.springframework.data.domain.*; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; @@ -80,7 +77,6 @@ public ResponseEntity getSource(@PathVariable("id") String id } @Operation(summary = "Update or create a source") - @CacheEvict(CacheName.SOURCE_OPENED) @PutMapping(value = Constants.API_SOURCES_ID, produces = "application/json", consumes = "application/json") public ResponseEntity putSource(@PathVariable("id") String id, @RequestBody @Valid SourceCompleteDto sourceCompleteDto) { if (!sourceCompleteDto.getId().equalsIgnoreCase(id)) { @@ -115,7 +111,6 @@ public ResponseEntity putSource(@PathVariable("id") String id @Operation(summary = "Delete a source, its surveys, campaigns, partitionings, questionings ...") @DeleteMapping(value = Constants.API_SOURCES_ID) @ResponseStatus(HttpStatus.NO_CONTENT) - @CacheEvict(CacheName.SOURCE_OPENED) @Transactional public void deleteSource(@PathVariable("id") String id) { int nbQuestioningDeleted = 0; @@ -149,7 +144,6 @@ public void deleteSource(@PathVariable("id") String id) { @Operation(summary = "Check if a source is opened") @GetMapping(value = Constants.API_SOURCE_ID_OPENED, produces = "application/json") - @Cacheable(CacheName.SOURCE_OPENED) public ResponseEntity isSourceOpened(@PathVariable("id") String id) { From 231a92dccb6f4545fef5c7275eeba68902c37a00 Mon Sep 17 00:00:00 2001 From: y72wvh Date: Mon, 22 Apr 2024 09:12:34 +0200 Subject: [PATCH 3/7] Update pom.xml build: version 2.1.5 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 32355ecd..53588d76 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ fr.insee.survey platine-management - 2.1.4 + 2.1.5 platine-management REST API for communication between DB and Platine-Management UI and Platine-My-Surveys UI From 4ca32557edf5e1fda183fc6a6d4ed1b2d03dc9a0 Mon Sep 17 00:00:00 2001 From: y72wvh Date: Mon, 22 Apr 2024 09:29:48 +0200 Subject: [PATCH 4/7] Update pom.xml --- pom.xml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pom.xml b/pom.xml index 53588d76..905ce505 100644 --- a/pom.xml +++ b/pom.xml @@ -41,10 +41,6 @@ org.springframework.boot spring-boot-starter-validation - - org.springframework.boot - spring-boot-starter-cache - org.modelmapper modelmapper From 8e77379848d1a831486a9217e0391d0b9a43ca58 Mon Sep 17 00:00:00 2001 From: y72wvh Date: Mon, 22 Apr 2024 12:25:28 +0200 Subject: [PATCH 5/7] refactor: improve source Dto management --- .../metadata/controller/SourceController.java | 34 +++++++++++-------- .../metadata/dto/SourceCompleteDto.java | 4 ++- .../metadata/dto/SourceDto.java | 2 -- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/controller/SourceController.java b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/controller/SourceController.java index 546dc7db..b6046c30 100644 --- a/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/controller/SourceController.java +++ b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/controller/SourceController.java @@ -6,6 +6,7 @@ import fr.insee.survey.datacollectionmanagement.metadata.domain.*; import fr.insee.survey.datacollectionmanagement.metadata.dto.OpenDto; import fr.insee.survey.datacollectionmanagement.metadata.dto.SourceCompleteDto; +import fr.insee.survey.datacollectionmanagement.metadata.dto.SourceDto; import fr.insee.survey.datacollectionmanagement.metadata.service.CampaignService; import fr.insee.survey.datacollectionmanagement.metadata.service.OwnerService; import fr.insee.survey.datacollectionmanagement.metadata.service.SourceService; @@ -64,7 +65,7 @@ public ResponseEntity getSources( @RequestParam(defaultValue = "id") String sort) { Pageable pageable = PageRequest.of(page, size, Sort.by(sort)); Page pageSource = sourceService.findAll(pageable); - List listSources = pageSource.stream().map(this::convertToDto).toList(); + List listSources = pageSource.stream().map(this::convertToDto).toList(); return ResponseEntity.ok().body(new SourcePage(listSources, pageable, pageSource.getTotalElements())); } @@ -72,7 +73,7 @@ public ResponseEntity getSources( @GetMapping(value = Constants.API_SOURCES_ID, produces = "application/json") public ResponseEntity getSource(@PathVariable("id") String id) { Source source = sourceService.findById(StringUtils.upperCase(id)); - return ResponseEntity.ok().body(convertToDto(source)); + return ResponseEntity.ok().body(convertToCompleteDto(source)); } @@ -105,7 +106,7 @@ public ResponseEntity putSource(@PathVariable("id") String id if (source.getSupport() != null && httpStatus.equals(HttpStatus.CREATED)) supportService.addSourceFromSupport(source.getSupport(), source); - return ResponseEntity.status(httpStatus).headers(responseHeaders).body(convertToDto(source)); + return ResponseEntity.status(httpStatus).headers(responseHeaders).body(convertToCompleteDto(source)); } @Operation(summary = "Delete a source, its surveys, campaigns, partitionings, questionings ...") @@ -149,32 +150,32 @@ public ResponseEntity isSourceOpened(@PathVariable("id") String id) { try { Source source = sourceService.findById(id.toUpperCase()); - if (Boolean.TRUE.equals(source.getForceClose())){ - return ResponseEntity.ok().body(new OpenDto(false,source.getMessageSurveyOffline(),source.getMessageInfoSurveyOffline())); + if (Boolean.TRUE.equals(source.getForceClose())) { + return ResponseEntity.ok().body(new OpenDto(false, source.getMessageSurveyOffline(), source.getMessageInfoSurveyOffline())); } - if(source.getSurveys().isEmpty()) - return ResponseEntity.ok().body(new OpenDto(true,source.getMessageSurveyOffline(),source.getMessageInfoSurveyOffline())); + if (source.getSurveys().isEmpty()) + return ResponseEntity.ok().body(new OpenDto(true, source.getMessageSurveyOffline(), source.getMessageInfoSurveyOffline())); for (Survey survey : source.getSurveys()) { for (Campaign campaign : survey.getCampaigns()) { if (campaignService.isCampaignOngoing(campaign.getId())) { - return ResponseEntity.ok().body(new OpenDto(true,source.getMessageSurveyOffline(),source.getMessageInfoSurveyOffline())); + return ResponseEntity.ok().body(new OpenDto(true, source.getMessageSurveyOffline(), source.getMessageInfoSurveyOffline())); } } } - return ResponseEntity.ok().body(new OpenDto(false,source.getMessageSurveyOffline(),source.getMessageInfoSurveyOffline())); + return ResponseEntity.ok().body(new OpenDto(false, source.getMessageSurveyOffline(), source.getMessageInfoSurveyOffline())); } catch (NotFoundException e) { - return ResponseEntity.ok().body(new OpenDto(true,null,null)); + return ResponseEntity.ok().body(new OpenDto(true, null, null)); } } @Operation(summary = "Search for surveys by the owner id") @GetMapping(value = Constants.API_OWNERS_ID_SOURCES, produces = "application/json") - public ResponseEntity> getSourcesByOwner(@PathVariable("id") String id) { + public ResponseEntity> getSourcesByOwner(@PathVariable("id") String id) { Owner owner = ownerService.findById(id); return ResponseEntity.ok() .body(owner.getSources().stream().map(this::convertToDto).toList()); @@ -182,17 +183,22 @@ public ResponseEntity> getSourcesByOwner(@PathVariable(" } - private SourceCompleteDto convertToDto(Source source) { + private SourceDto convertToDto(Source source) { + return modelmapper.map(source, SourceDto.class); + } + + private SourceCompleteDto convertToCompleteDto(Source source) { return modelmapper.map(source, SourceCompleteDto.class); } + private Source convertToEntity(SourceCompleteDto sourceCompleteDto) { return modelmapper.map(sourceCompleteDto, Source.class); } - class SourcePage extends PageImpl { + class SourcePage extends PageImpl { - public SourcePage(List content, Pageable pageable, long total) { + public SourcePage(List content, Pageable pageable, long total) { super(content, pageable, total); } } diff --git a/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/SourceCompleteDto.java b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/SourceCompleteDto.java index c7f33eca..5a63a5fa 100644 --- a/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/SourceCompleteDto.java +++ b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/SourceCompleteDto.java @@ -16,9 +16,11 @@ public class SourceCompleteDto { private String shortWording; private PeriodicityEnum periodicity; @Schema(description = "Indicates whether or not you need to use the my surveys portal", defaultValue = "false") - private boolean mandatoryMySurveys; + private boolean mandatoryMySurveys = false; @Schema(description = "Indicates if the source should be force closed", defaultValue = "false") private boolean forceClose = false; + private String messageInfoSurveyOffline = ""; + private String messageSurveyOffline = ""; private String ownerId; private String supportId; diff --git a/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/SourceDto.java b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/SourceDto.java index fa99adce..9c9da516 100644 --- a/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/SourceDto.java +++ b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/SourceDto.java @@ -13,7 +13,5 @@ public class SourceDto { private String shortWording; private PeriodicityEnum periodicity; private boolean mandatoryMySurveys; - private boolean forceClose; - } From 5f1326a26b179a6641e6460487c3db551808c2e4 Mon Sep 17 00:00:00 2001 From: y72wvh Date: Mon, 22 Apr 2024 16:17:45 +0200 Subject: [PATCH 6/7] refactor: changes from code review --- .../metadata/controller/SourceController.java | 34 ++++++++----------- ...eteDto.java => SourceOnlineStatusDto.java} | 2 +- 2 files changed, 15 insertions(+), 21 deletions(-) rename src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/{SourceCompleteDto.java => SourceOnlineStatusDto.java} (96%) diff --git a/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/controller/SourceController.java b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/controller/SourceController.java index b6046c30..da8b3b89 100644 --- a/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/controller/SourceController.java +++ b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/controller/SourceController.java @@ -5,8 +5,8 @@ import fr.insee.survey.datacollectionmanagement.exception.NotMatchException; import fr.insee.survey.datacollectionmanagement.metadata.domain.*; import fr.insee.survey.datacollectionmanagement.metadata.dto.OpenDto; -import fr.insee.survey.datacollectionmanagement.metadata.dto.SourceCompleteDto; import fr.insee.survey.datacollectionmanagement.metadata.dto.SourceDto; +import fr.insee.survey.datacollectionmanagement.metadata.dto.SourceOnlineStatusDto; import fr.insee.survey.datacollectionmanagement.metadata.service.CampaignService; import fr.insee.survey.datacollectionmanagement.metadata.service.OwnerService; import fr.insee.survey.datacollectionmanagement.metadata.service.SourceService; @@ -71,7 +71,7 @@ public ResponseEntity getSources( @Operation(summary = "Search for a source by its id") @GetMapping(value = Constants.API_SOURCES_ID, produces = "application/json") - public ResponseEntity getSource(@PathVariable("id") String id) { + public ResponseEntity getSource(@PathVariable("id") String id) { Source source = sourceService.findById(StringUtils.upperCase(id)); return ResponseEntity.ok().body(convertToCompleteDto(source)); @@ -79,8 +79,8 @@ public ResponseEntity getSource(@PathVariable("id") String id @Operation(summary = "Update or create a source") @PutMapping(value = Constants.API_SOURCES_ID, produces = "application/json", consumes = "application/json") - public ResponseEntity putSource(@PathVariable("id") String id, @RequestBody @Valid SourceCompleteDto sourceCompleteDto) { - if (!sourceCompleteDto.getId().equalsIgnoreCase(id)) { + public ResponseEntity putSource(@PathVariable("id") String id, @RequestBody @Valid SourceOnlineStatusDto sourceOnlineStatusDto) { + if (!sourceOnlineStatusDto.getId().equalsIgnoreCase(id)) { throw new NotMatchException("id and source id don't match"); } @@ -88,19 +88,19 @@ public ResponseEntity putSource(@PathVariable("id") String id Source source; HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.set(HttpHeaders.LOCATION, - ServletUriComponentsBuilder.fromCurrentRequest().buildAndExpand(sourceCompleteDto.getId()).toUriString()); + ServletUriComponentsBuilder.fromCurrentRequest().buildAndExpand(sourceOnlineStatusDto.getId()).toUriString()); HttpStatus httpStatus; try { sourceService.findById(id); - log.warn("Update source with the id {}", sourceCompleteDto.getId()); + log.warn("Update source with the id {}", sourceOnlineStatusDto.getId()); httpStatus = HttpStatus.OK; } catch (NotFoundException e) { - log.info("Create source with the id {}", sourceCompleteDto.getId()); + log.info("Create source with the id {}", sourceOnlineStatusDto.getId()); httpStatus = HttpStatus.CREATED; } - source = sourceService.insertOrUpdateSource(convertToEntity(sourceCompleteDto)); + source = sourceService.insertOrUpdateSource(convertToEntity(sourceOnlineStatusDto)); if (source.getOwner() != null && httpStatus.equals(HttpStatus.CREATED)) ownerService.addSourceFromOwner(source.getOwner(), source); if (source.getSupport() != null && httpStatus.equals(HttpStatus.CREATED)) @@ -158,15 +158,9 @@ public ResponseEntity isSourceOpened(@PathVariable("id") String id) { if (source.getSurveys().isEmpty()) return ResponseEntity.ok().body(new OpenDto(true, source.getMessageSurveyOffline(), source.getMessageInfoSurveyOffline())); - for (Survey survey : source.getSurveys()) { - for (Campaign campaign : survey.getCampaigns()) { - if (campaignService.isCampaignOngoing(campaign.getId())) { - return ResponseEntity.ok().body(new OpenDto(true, source.getMessageSurveyOffline(), source.getMessageInfoSurveyOffline())); - } - } - } + boolean isOpened = source.getSurveys().stream().flatMap(survey -> survey.getCampaigns().stream()).anyMatch(campaign -> campaignService.isCampaignOngoing(campaign.getId())); - return ResponseEntity.ok().body(new OpenDto(false, source.getMessageSurveyOffline(), source.getMessageInfoSurveyOffline())); + return ResponseEntity.ok().body(new OpenDto(isOpened, source.getMessageSurveyOffline(), source.getMessageInfoSurveyOffline())); } catch (NotFoundException e) { return ResponseEntity.ok().body(new OpenDto(true, null, null)); @@ -187,13 +181,13 @@ private SourceDto convertToDto(Source source) { return modelmapper.map(source, SourceDto.class); } - private SourceCompleteDto convertToCompleteDto(Source source) { - return modelmapper.map(source, SourceCompleteDto.class); + private SourceOnlineStatusDto convertToCompleteDto(Source source) { + return modelmapper.map(source, SourceOnlineStatusDto.class); } - private Source convertToEntity(SourceCompleteDto sourceCompleteDto) { - return modelmapper.map(sourceCompleteDto, Source.class); + private Source convertToEntity(SourceOnlineStatusDto sourceOnlineStatusDto) { + return modelmapper.map(sourceOnlineStatusDto, Source.class); } class SourcePage extends PageImpl { diff --git a/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/SourceCompleteDto.java b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/SourceOnlineStatusDto.java similarity index 96% rename from src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/SourceCompleteDto.java rename to src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/SourceOnlineStatusDto.java index 5a63a5fa..d3b4d5c5 100644 --- a/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/SourceCompleteDto.java +++ b/src/main/java/fr/insee/survey/datacollectionmanagement/metadata/dto/SourceOnlineStatusDto.java @@ -8,7 +8,7 @@ @Getter @Setter -public class SourceCompleteDto { +public class SourceOnlineStatusDto { @NotBlank private String id; From f4c03e143715c1da0777251d19173f764feb1b62 Mon Sep 17 00:00:00 2001 From: y72wvh Date: Tue, 23 Apr 2024 10:17:41 +0200 Subject: [PATCH 7/7] Update pom.xml --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 905ce505..9ef1093f 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ fr.insee.survey platine-management - 2.1.5 + 2.2.0 platine-management REST API for communication between DB and Platine-Management UI and Platine-My-Surveys UI