Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add parameters to customise redirection #66

Merged
merged 14 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.4</version>
<version>3.3.0</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>fr.insee.survey</groupId>
<artifactId>platine-management</artifactId>
<version>2.2.3</version>
<version>2.3.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 @@ -202,4 +202,4 @@
</build>


</project>
</project>
1 change: 0 additions & 1 deletion renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,3 @@
"schedule": ["every weekend"],
"timezone": "Europe/Paris"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package fr.insee.survey.datacollectionmanagement.constants;

public class CheckHabilitationsRoles {
public class UserRoles {

private UserRoles() {
throw new IllegalStateException("Utility class");
}
public static final String INTERVIEWER = "interviewer";
public static final String REVIEWER = "reviewer";
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
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.Parameters;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Partitioning;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Survey;
import fr.insee.survey.datacollectionmanagement.metadata.dto.CampaignDto;
import fr.insee.survey.datacollectionmanagement.metadata.dto.CampaignPartitioningsDto;
import fr.insee.survey.datacollectionmanagement.metadata.dto.OnGoingDto;
import fr.insee.survey.datacollectionmanagement.metadata.dto.ParamsDto;
import fr.insee.survey.datacollectionmanagement.metadata.service.CampaignService;
import fr.insee.survey.datacollectionmanagement.metadata.service.SurveyService;
import fr.insee.survey.datacollectionmanagement.questioning.domain.Upload;
Expand Down Expand Up @@ -37,8 +39,13 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static fr.insee.survey.datacollectionmanagement.questioning.util.UrlTypeEnum.values;
import static java.util.stream.Collectors.joining;

@RestController
@PreAuthorize("@AuthorizeMethodDecider.isInternalUser() "
Expand Down Expand Up @@ -109,6 +116,38 @@ public ResponseEntity<CampaignDto> getCampaign(@PathVariable("id") String id) {

}


@Operation(summary = "Get campaign parameters")
@GetMapping(value = "/api/campaigns/{id}/params", produces = "application/json")
public ResponseEntity<List<ParamsDto>> getParams(@PathVariable("id") String id) {
Campaign campaign = campaignService.findById(StringUtils.upperCase(id));
List<ParamsDto> listParams = campaign.getParams().stream().map(this::convertToDto).toList();
return ResponseEntity.ok().body(listParams);
}


@Operation(summary = "Create a parameter for a campaign")
@PutMapping(value = "/api/campaigns/{id}/params", produces = "application/json")
public void putParams(@PathVariable("id") String id, @RequestBody @Valid ParamsDto paramsDto) {
Campaign campaign = campaignService.findById(StringUtils.upperCase(id));

if (paramsDto.getParamId().equalsIgnoreCase(Parameters.ParameterEnum.URL_TYPE.name())
&& Arrays.stream(values()).noneMatch(p -> p.name().equalsIgnoreCase(paramsDto.getParamValue()))) {

throw new NotMatchException(String.format("Only %s are valid values for URL_TYPE", Arrays.stream(values()).map(item -> item.name())
.collect(joining(" "))));
}
Parameters param = convertToEntity(paramsDto);
param.setMetadataId(StringUtils.upperCase(id));
Set<Parameters> setParams = campaign.getParams().stream()
.filter(parameter -> !parameter.getParamId().equals(param.getParamId()))
.collect(Collectors.toSet());
setParams.add(param);
campaign.setParams(setParams);
campaignService.insertOrUpdateCampaign(campaign);
}


@Operation(summary = "Update or create a campaign")
@PutMapping(value = Constants.API_CAMPAIGNS_ID, produces = "application/json", consumes = "application/json")
@ApiResponses(value = {
Expand Down Expand Up @@ -189,6 +228,10 @@ private CampaignDto convertToDto(Campaign campaign) {
return modelmapper.map(campaign, CampaignDto.class);
}

private ParamsDto convertToDto(Parameters params) {
return modelmapper.map(params, ParamsDto.class);
}

private CampaignPartitioningsDto convertToCampaignPartitioningsDto(Campaign campaign) {
return modelmapper.map(campaign, CampaignPartitioningsDto.class);
}
Expand All @@ -197,6 +240,13 @@ private Campaign convertToEntity(CampaignDto campaignDto) {
return modelmapper.map(campaignDto, Campaign.class);
}

private Parameters convertToEntity(ParamsDto paramsDto) {

Parameters params = modelmapper.map(paramsDto, Parameters.class);
params.setParamId(Parameters.ParameterEnum.valueOf(paramsDto.getParamId()));
return params;
}

class CampaignPage extends PageImpl<CampaignDto> {

public CampaignPage(List<CampaignDto> content, Pageable pageable, long total) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public class Campaign {
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<Partitioning> partitionings;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@Enumerated(EnumType.STRING)
private Set<Parameters> params;

@ManyToOne
private Survey survey;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package fr.insee.survey.datacollectionmanagement.metadata.domain;

import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Id;
import jakarta.persistence.*;
import lombok.Data;
import lombok.Getter;

@Entity
@Data
public class Parameters {

@Getter
public enum ParameterEnum {
URL_REDIRECTION,URL_TYPE;
}

@Id
private String metadataId;

@Id
@Enumerated(EnumType.STRING)
private ParameterEnum paramId;

@Column(length = 2000)
private String paramValue;

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.Setter;

import java.util.Date;
import java.util.Set;

@Entity
@Getter
Expand All @@ -24,4 +25,8 @@ public class Partitioning {
@ManyToOne
private Campaign campaign;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@Enumerated(EnumType.STRING)
private Set<Parameters> params;

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ public class Source {
@NonNull
private Support support;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@Enumerated(EnumType.STRING)
private Set<Parameters> params;

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public class Survey {
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<Campaign> campaigns;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@Enumerated(EnumType.STRING)
private Set<Parameters> params;

@ManyToOne
private Source source;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package fr.insee.survey.datacollectionmanagement.metadata.dto;

import fr.insee.survey.datacollectionmanagement.metadata.validation.ParameterEnumValid;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ParamsDto {
@ParameterEnumValid
private String paramId;
private String paramValue;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.insee.survey.datacollectionmanagement.metadata.service;

import fr.insee.survey.datacollectionmanagement.metadata.domain.Parameters;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Partitioning;

import java.util.Date;
Expand All @@ -14,4 +15,8 @@ public interface PartitioningService {

boolean isOnGoing(Partitioning part, Date date);

String findSuitableParameterValue(Partitioning part, Parameters.ParameterEnum paramValue);



}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.insee.survey.datacollectionmanagement.metadata.service.impl;

import fr.insee.survey.datacollectionmanagement.exception.NotFoundException;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Parameters;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Partitioning;
import fr.insee.survey.datacollectionmanagement.metadata.repository.PartitioningRepository;
import fr.insee.survey.datacollectionmanagement.metadata.service.PartitioningService;
Expand All @@ -9,6 +10,8 @@
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;
import java.util.Set;

@Service
@Slf4j
Expand Down Expand Up @@ -40,4 +43,23 @@ public void deletePartitioningById(String id) {
public boolean isOnGoing(Partitioning part, Date date) {
return part.getClosingDate().compareTo(date) > 0 && part.getOpeningDate().compareTo(date) < 0;
}

@Override
public String findSuitableParameterValue(Partitioning part, Parameters.ParameterEnum paramValue) {
return findParameterValueInSet(part.getParams(), paramValue)
.orElse(findParameterValueInSet(part.getCampaign().getParams(), paramValue)
.orElse(findParameterValueInSet(part.getCampaign().getSurvey().getParams(), paramValue)
.orElse(findParameterValueInSet(part.getCampaign().getSurvey().getSource().getParams(), paramValue)
.orElse(""))));
}

private Optional<String> findParameterValueInSet(Set<Parameters> params, Parameters.ParameterEnum paramValue) {
return params.stream()
.filter(param -> param.getParamId().equals(paramValue))
.map(Parameters::getParamValue)
.findFirst();
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package fr.insee.survey.datacollectionmanagement.metadata.validation;

import jakarta.validation.Constraint;
import jakarta.validation.Payload;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;

@Target({FIELD, PARAMETER, METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = ParameterEnumValidator.class)
public @interface ParameterEnumValid {
//error message
String message() default "Type missing or not recognized. Only URL_REDIRECTION,URL_TYPE are valid";

//represents group of constraints
Class<?>[] groups() default {};

//represents additional information about annotation
Class<? extends Payload>[] payload() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package fr.insee.survey.datacollectionmanagement.metadata.validation;

import fr.insee.survey.datacollectionmanagement.metadata.domain.Parameters;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.util.Arrays;

public class ParameterEnumValidator implements ConstraintValidator<ParameterEnumValid, String> {


@Override
public void initialize(ParameterEnumValid constraintAnnotation) {
ConstraintValidator.super.initialize(constraintAnnotation);
}

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (value == null)
return false;
return Arrays.stream(Parameters.ParameterEnum.values()).anyMatch(v -> value.equalsIgnoreCase(v.name()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import fr.insee.survey.datacollectionmanagement.config.ApplicationConfig;
import fr.insee.survey.datacollectionmanagement.config.auth.user.AuthUser;
import fr.insee.survey.datacollectionmanagement.constants.AuthConstants;
import fr.insee.survey.datacollectionmanagement.constants.CheckHabilitationsRoles;
import fr.insee.survey.datacollectionmanagement.constants.UserRoles;
import fr.insee.survey.datacollectionmanagement.exception.NotFoundException;
import fr.insee.survey.datacollectionmanagement.query.service.CheckHabilitationService;
import fr.insee.survey.datacollectionmanagement.user.domain.User;
Expand Down Expand Up @@ -40,7 +40,7 @@ public boolean checkHabilitation(String role, String idSu, String campaignId, Au
}

//respondents
if (role == null || role.isBlank() || role.equals(CheckHabilitationsRoles.INTERVIEWER)) {
if (role == null || role.isBlank() || role.equals(UserRoles.INTERVIEWER)) {
if (isUserInRole(authUser.getRoles(), applicationConfig.getRoleRespondent())) {
boolean habilitated = viewService.countViewByIdentifierIdSuCampaignId(userId.toUpperCase(), idSu, campaignId) != 0;
log.info("Check habilitation of interviewer {} for accessing survey-unit {} of campaign {} resulted in {}", userId, idSu, campaignId, habilitated);
Expand All @@ -52,7 +52,7 @@ public boolean checkHabilitation(String role, String idSu, String campaignId, Au


// internal users
if (!role.equals(CheckHabilitationsRoles.REVIEWER)) {
if (!role.equals(UserRoles.REVIEWER)) {
log.warn("User {} - internal user habilitation not found in token - Check habilitation:false", userId);
return false;
}
Expand All @@ -76,7 +76,7 @@ public boolean checkHabilitation(String role, String idSu, String campaignId, Au


}
log.warn("Only '{}' and '{}' are accepted as a role in query argument", CheckHabilitationsRoles.REVIEWER, CheckHabilitationsRoles.INTERVIEWER);
log.warn("Only '{}' and '{}' are accepted as a role in query argument", UserRoles.REVIEWER, UserRoles.INTERVIEWER);
return false;

}
Expand Down
Loading
Loading