Skip to content

Commit

Permalink
fix: sonar bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
BettyB979 committed Nov 24, 2023
1 parent ca97075 commit f5a06f4
Show file tree
Hide file tree
Showing 16 changed files with 356 additions and 127 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.11</version>
<configuration>
<excludes>
<exclude>**/dataloader/**/*.class</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>prepare-agent</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
String ao = applicationConfig.getAllowedOrigin().isPresent() ? applicationConfig.getAllowedOrigin().get() : applicationConfig.getAllowedOrigin().orElse("*");
String ao = applicationConfig.getAllowedOrigin().orElse("*");
registry.addMapping("/**")
.allowedOrigins(ao)
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.insee.survey.datacollectionmanagement.util;
package fr.insee.survey.datacollectionmanagement.dataloader;

import com.github.javafaker.Faker;
import com.github.javafaker.Name;
Expand Down Expand Up @@ -77,7 +77,7 @@ public void init() {
Faker faker = new Faker();
EasyRandom generator = new EasyRandom();

initOrder();
initOrder();
// initContact(faker);
// initMetadata(faker, generator);
// initQuestionning(faker, generator);
Expand Down Expand Up @@ -180,7 +180,7 @@ private void initOrder() {
Long nbExistingOrders = orderRepository.count();
log.info("{} orders in database", nbExistingOrders);

if (nbExistingOrders !=8 ) {
if (nbExistingOrders != 8) {
// Creating table order
log.info("loading eventorder data");
orderRepository.deleteAll();
Expand Down Expand Up @@ -317,8 +317,8 @@ private void initMetadata(Faker faker, EasyRandom generator2) {
while (sourceRepository.count() < 10) {

Source source = new Source();
String nameSource = "LOAD"+String.valueOf((char)(sourceRepository.count() + 'A'));

String nameSource = "LOAD" + String.valueOf((char) (sourceRepository.count() + 'A'));

if (!StringUtils.contains(nameSource, " ") && sourceRepository.findById(nameSource).isEmpty()) {

Expand Down Expand Up @@ -361,7 +361,7 @@ private void initMetadata(Faker faker, EasyRandom generator2) {
for (int k = 0; k < 12; k++) {
Campaign campaign = new Campaign();
int month = k + 1;
String period = month<10? "M0" + month:"M"+month;
String period = month < 10 ? "M0" + month : "M" + month;
campaign.setYear(year - j);
campaign.setPeriod(PeriodEnum.valueOf(period));
campaign.setId(nameSource + (year - j) + period);
Expand Down Expand Up @@ -400,13 +400,13 @@ private void initMetadata(Faker faker, EasyRandom generator2) {
sourceRepository.save(source);
ownerInsee.setSources(setSourcesInsee);
ownerAgri.setSources(setSourcesSsp);
ownerRepository.saveAll(Arrays.asList(new Owner[] {
ownerRepository.saveAll(Arrays.asList(new Owner[]{
ownerInsee, ownerAgri
}));

supportInseeHdf.setSources(setSourcesSupportInsee);
supportSsne.setSources(setSourcesSupportSsne);
supportRepository.saveAll(Arrays.asList(new Support[] {
supportRepository.saveAll(Arrays.asList(new Support[]{
supportInseeHdf, supportSsne
}));
}
Expand Down Expand Up @@ -549,62 +549,63 @@ private void initQuestioningEvents(Faker faker, EasyRandom generator) {
// questioning events
// everybody in INITLA
Optional<Partitioning> part = partitioningRepository.findById(qu.getIdPartitioning());
Date eventDate = faker.date().between(part.get().getOpeningDate(), part.get().getClosingDate());
qe.setType(TypeQuestioningEvent.INITLA);
qe.setDate(eventDate);
qe.setQuestioning(qu);
qeList.add(qe);

int qeProfile = qeRan.nextInt(10);

switch (qeProfile) {
case 0:
qeList.add(new QuestioningEvent(
faker.date().between(part.get().getOpeningDate(), part.get().getClosingDate()),
TypeQuestioningEvent.REFUSAL, qu));
break;
case 1:
qeList.add(new QuestioningEvent(
faker.date().between(part.get().getOpeningDate(), part.get().getClosingDate()),
TypeQuestioningEvent.PND, qu));
break;
case 2:
qeList.add(new QuestioningEvent(
faker.date().between(part.get().getOpeningDate(), part.get().getClosingDate()),
TypeQuestioningEvent.FOLLOWUP, qu));
qeList.add(new QuestioningEvent(
faker.date().between(part.get().getOpeningDate(), part.get().getClosingDate()),
TypeQuestioningEvent.FOLLOWUP, qu));
qeList.add(new QuestioningEvent(
faker.date().between(part.get().getOpeningDate(), part.get().getClosingDate()),
TypeQuestioningEvent.PARTIELINT, qu));
break;
case 3:
case 4:
qeList.add(new QuestioningEvent(
faker.date().between(part.get().getOpeningDate(), part.get().getClosingDate()),
TypeQuestioningEvent.FOLLOWUP, qu));
qeList.add(new QuestioningEvent(
faker.date().between(part.get().getOpeningDate(), part.get().getClosingDate()),
TypeQuestioningEvent.FOLLOWUP, qu));
qeList.add(new QuestioningEvent(
faker.date().between(part.get().getOpeningDate(), part.get().getClosingDate()),
TypeQuestioningEvent.VALINT, qu));
break;
case 5:
case 6:
case 7:
qeList.add(new QuestioningEvent(
faker.date().between(part.get().getOpeningDate(), part.get().getClosingDate()),
TypeQuestioningEvent.PARTIELINT, qu));
qeList.add(new QuestioningEvent(
faker.date().between(part.get().getOpeningDate(), part.get().getClosingDate()),
TypeQuestioningEvent.VALINT, qu));
break;
}

qeList.stream().forEach(questEvent -> questioningEventRepository.save(questEvent));
if (part.isPresent()) {
Date eventDate = faker.date().between(part.get().getOpeningDate(), part.get().getClosingDate());
qe.setType(TypeQuestioningEvent.INITLA);
qe.setDate(eventDate);
qe.setQuestioning(qu);
qeList.add(qe);

int qeProfile = qeRan.nextInt(10);

switch (qeProfile) {
case 0:
qeList.add(new QuestioningEvent(
faker.date().between(part.get().getOpeningDate(), part.get().getClosingDate()),
TypeQuestioningEvent.REFUSAL, qu));
break;
case 1:
qeList.add(new QuestioningEvent(
faker.date().between(part.get().getOpeningDate(), part.get().getClosingDate()),
TypeQuestioningEvent.PND, qu));
break;
case 2:
qeList.add(new QuestioningEvent(
faker.date().between(part.get().getOpeningDate(), part.get().getClosingDate()),
TypeQuestioningEvent.FOLLOWUP, qu));
qeList.add(new QuestioningEvent(
faker.date().between(part.get().getOpeningDate(), part.get().getClosingDate()),
TypeQuestioningEvent.FOLLOWUP, qu));
qeList.add(new QuestioningEvent(
faker.date().between(part.get().getOpeningDate(), part.get().getClosingDate()),
TypeQuestioningEvent.PARTIELINT, qu));
break;
case 3:
case 4:
qeList.add(new QuestioningEvent(
faker.date().between(part.get().getOpeningDate(), part.get().getClosingDate()),
TypeQuestioningEvent.FOLLOWUP, qu));
qeList.add(new QuestioningEvent(
faker.date().between(part.get().getOpeningDate(), part.get().getClosingDate()),
TypeQuestioningEvent.FOLLOWUP, qu));
qeList.add(new QuestioningEvent(
faker.date().between(part.get().getOpeningDate(), part.get().getClosingDate()),
TypeQuestioningEvent.VALINT, qu));
break;
case 5:
case 6:
case 7:
qeList.add(new QuestioningEvent(
faker.date().between(part.get().getOpeningDate(), part.get().getClosingDate()),
TypeQuestioningEvent.PARTIELINT, qu));
qeList.add(new QuestioningEvent(
faker.date().between(part.get().getOpeningDate(), part.get().getClosingDate()),
TypeQuestioningEvent.VALINT, qu));
break;
}

qeList.stream().forEach(questEvent -> questioningEventRepository.save(questEvent));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.insee.survey.datacollectionmanagement.util;
package fr.insee.survey.datacollectionmanagement.dataloader;

import com.github.javafaker.Faker;
import com.github.javafaker.Name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public ResponseEntity<?> putQuestioning(@RequestBody QuestioningWebclientDto que
// save questioning and su
questioningService.saveQuestioning(questioning);
su.getQuestionings().add(questioning);
su = surveyUnitService.saveSurveyUnitAndAddress(su);
surveyUnitService.saveSurveyUnitAndAddress(su);


questioningReturn.setIdPartitioning(idPartitioning);
Expand Down Expand Up @@ -400,8 +400,12 @@ public ResponseEntity<?> getMainContact(
List<QuestioningAccreditation> listQa = questioning.getQuestioningAccreditations().stream()
.filter(qa -> qa.isMain()).toList();
if (listQa != null && !listQa.isEmpty()) {
Contact c = contactService.findByIdentifier(listQa.get(0).getIdContact()).get();
return ResponseEntity.status(HttpStatus.OK).body(convertToDto((c)));
Optional<Contact> c = contactService.findByIdentifier(listQa.get(0).getIdContact());
if(c.isPresent())
return ResponseEntity.status(HttpStatus.OK).body(convertToDto((c.get())));
else
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No contact found");

}
}
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No contact found");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public ResultUpload save(String idCampaign, UploadDto uploadDto) throws Ressourc
if (setParts.isEmpty()) {
throw new RessourceNotValidatedException("No partitionings found for campaign ", idCampaign);
}

Set<Questioning> questionings = questioningService.findBySurveyUnitIdSu(mmDto.getIdSu());

List<String> listIdParts = campaignService.findById(idCampaign).get().getPartitionings().stream().map(Partitioning::getId).toList();
Expand All @@ -83,7 +83,7 @@ public ResultUpload save(String idCampaign, UploadDto uploadDto) throws Ressourc
qe.setPayload(objectMapper.readTree(jo.toString()));
qe.setDate(today);
liste.add(questioningEventService.saveQuestioningEvent(qe));
if(quest.isPresent()){
if (quest.isPresent()) {
quest.get().getQuestioningEvents().add(qe);
questioningService.saveQuestioning(quest.get());
}
Expand All @@ -99,7 +99,7 @@ public ResultUpload save(String idCampaign, UploadDto uploadDto) throws Ressourc
return result;
}
up.setQuestioningEvents(liste);
up = saveAndFlush(up);
saveAndFlush(up);

return result;
}
Expand All @@ -113,14 +113,17 @@ public Optional<Upload> findById(long id) {
public List<Upload> findAllByIdCampaign(String idCampaign) {

Optional<Campaign> campaign = campaignService.findById(idCampaign);
if (campaign.isPresent()) {

List<String> partitioningIds = campaign.get().getPartitionings().stream().map(Partitioning::getId).toList();
List<String> partitioningIds = campaign.get().getPartitionings().stream().map(Partitioning::getId).toList();

// Keeps the uploads which first managementMonitoringInfo belongs to the survey
return uploadRepository.findAll().stream().filter(upload -> !upload.getQuestioningEvents().isEmpty())
.filter(upload -> partitioningIds.contains(upload.getQuestioningEvents().stream().findFirst().get().getQuestioning().getIdPartitioning()
))
.toList();
// Keeps the uploads which first managementMonitoringInfo belongs to the survey
return uploadRepository.findAll().stream().filter(upload -> !upload.getQuestioningEvents().isEmpty())
.filter(upload -> partitioningIds.contains(upload.getQuestioningEvents().stream().findFirst().get().getQuestioning().getIdPartitioning()
))
.toList();
}
return Collections.emptyList();
}


Expand All @@ -137,18 +140,27 @@ public Upload saveAndFlush(Upload up) {
@Override
public boolean checkUploadDate(String idCampaign, Date date) {
Optional<Campaign> campaign = campaignService.findById(idCampaign);
Long timestamp = date.getTime();
Long start = campaign.get().getPartitionings().stream().map(Partitioning::getOpeningDate)
.toList().stream()
.min(Comparator.comparing(Date::getTime)).get().getTime();
Long end = campaign.get().getPartitionings().stream().map(Partitioning::getClosingDate)
.toList().stream()
.max(Comparator.comparing(Date::getTime)).get().getTime();
return (start < timestamp && timestamp < end);
long timestamp = date.getTime();
if (campaign.isPresent()) {
Optional<Date> openingDate = campaign.get().getPartitionings().stream().map(Partitioning::getOpeningDate)
.toList().stream()
.min(Comparator.comparing(Date::getTime));
Optional<Date> closingDate = campaign.get().getPartitionings().stream().map(Partitioning::getClosingDate)
.toList().stream()
.max(Comparator.comparing(Date::getTime));
if(openingDate.isPresent() && closingDate.isPresent()){
long start = openingDate.get().getTime();
long end = closingDate.get().getTime();
return (start < timestamp && timestamp < end);

}

}
return false;
}

@Override
public void removeEmptyUploads() {
uploadRepository.findByQuestioningEventsIsEmpty().forEach(u -> uploadRepository.delete(u));
uploadRepository.findByQuestioningEventsIsEmpty().forEach(uploadRepository::delete);
}
}
Loading

0 comments on commit f5a06f4

Please sign in to comment.