Skip to content

Commit

Permalink
Merge pull request #62 from InseeFr/source_open_close
Browse files Browse the repository at this point in the history
Source open close
  • Loading branch information
BettyB979 authored Apr 23, 2024
2 parents 3faab51 + f4c03e1 commit cdddf40
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 49 deletions.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</parent>
<groupId>fr.insee.survey</groupId>
<artifactId>platine-management</artifactId>
<version>2.1.4</version>
<version>2.2.0</version>
<name>platine-management</name>
<description>REST API for communication between DB and Platine-Management UI and Platine-My-Surveys UI</description>
<properties>
Expand Down Expand Up @@ -66,6 +66,7 @@
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${springdoc-version}</version>
</dependency>

<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public class Constants {


private Constants() {
throw new IllegalStateException("Constants class");
}
Expand Down Expand Up @@ -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}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
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.dto.SourceCompleteDto;
import fr.insee.survey.datacollectionmanagement.metadata.domain.*;
import fr.insee.survey.datacollectionmanagement.metadata.dto.OpenDto;
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;
import fr.insee.survey.datacollectionmanagement.metadata.service.SupportService;
Expand Down Expand Up @@ -55,6 +55,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<SourcePage> getSources(
Expand All @@ -63,57 +65,57 @@ public ResponseEntity<SourcePage> getSources(
@RequestParam(defaultValue = "id") String sort) {
Pageable pageable = PageRequest.of(page, size, Sort.by(sort));
Page<Source> pageSource = sourceService.findAll(pageable);
List<SourceCompleteDto> listSources = pageSource.stream().map(this::convertToDto).toList();
List<SourceDto> listSources = pageSource.stream().map(this::convertToDto).toList();
return ResponseEntity.ok().body(new SourcePage(listSources, pageable, pageSource.getTotalElements()));
}

@Operation(summary = "Search for a source by its id")
@GetMapping(value = Constants.API_SOURCES_ID, produces = "application/json")
public ResponseEntity<SourceCompleteDto> getSource(@PathVariable("id") String id) {
public ResponseEntity<SourceOnlineStatusDto> getSource(@PathVariable("id") String id) {
Source source = sourceService.findById(StringUtils.upperCase(id));
source = sourceService.findById(StringUtils.upperCase(id));
return ResponseEntity.ok().body(convertToDto(source));
return ResponseEntity.ok().body(convertToCompleteDto(source));

}

@Operation(summary = "Update or create a source")
@PutMapping(value = Constants.API_SOURCES_ID, produces = "application/json", consumes = "application/json")
public ResponseEntity<SourceCompleteDto> putSource(@PathVariable("id") String id, @RequestBody @Valid SourceCompleteDto SourceCompleteDto) {
if (!SourceCompleteDto.getId().equalsIgnoreCase(id)) {
public ResponseEntity<SourceOnlineStatusDto> putSource(@PathVariable("id") String id, @RequestBody @Valid SourceOnlineStatusDto sourceOnlineStatusDto) {
if (!sourceOnlineStatusDto.getId().equalsIgnoreCase(id)) {
throw new NotMatchException("id and source id don't match");

}

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))
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 ...")
@DeleteMapping(value = Constants.API_SOURCES_ID)
@ResponseStatus(HttpStatus.NO_CONTENT)
@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)
Expand Down Expand Up @@ -141,27 +143,56 @@ 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")
public ResponseEntity<OpenDto> 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()));

boolean isOpened = source.getSurveys().stream().flatMap(survey -> survey.getCampaigns().stream()).anyMatch(campaign -> campaignService.isCampaignOngoing(campaign.getId()));

return ResponseEntity.ok().body(new OpenDto(isOpened, 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<List<SourceDto>> getSourcesByOwner(@PathVariable("id") String id) {
Owner owner = ownerService.findById(id);
return ResponseEntity.ok()
.body(owner.getSources().stream().map(this::convertToDto).toList());


}

private SourceCompleteDto convertToDto(Source source) {
return modelmapper.map(source, SourceCompleteDto.class);
private SourceDto convertToDto(Source source) {
return modelmapper.map(source, SourceDto.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<SourceCompleteDto> {
class SourcePage extends PageImpl<SourceDto> {

public SourcePage(List<SourceCompleteDto> content, Pageable pageable, long total) {
public SourcePage(List<SourceDto> content, Pageable pageable, long total) {
super(content, pageable, total);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public ResponseEntity<SurveyController.SurveyPage> getSurveys(
List<SurveyDto> 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<List<SurveyDto>> getSurveysBySource(@PathVariable("id") String id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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;

@Getter
@Setter
public class SourceOnlineStatusDto {

@NotBlank
private String id;
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 = 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;

}
5 changes: 2 additions & 3 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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/**

0 comments on commit cdddf40

Please sign in to comment.