diff --git a/api-remocra/api/src/main/java/fr/sdis83/remocra/repository/HydrantVisitesRepository.java b/api-remocra/api/src/main/java/fr/sdis83/remocra/repository/HydrantVisitesRepository.java index f7ade3c3d..f047a52b7 100644 --- a/api-remocra/api/src/main/java/fr/sdis83/remocra/repository/HydrantVisitesRepository.java +++ b/api-remocra/api/src/main/java/fr/sdis83/remocra/repository/HydrantVisitesRepository.java @@ -17,7 +17,9 @@ import java.time.Instant; import java.time.ZonedDateTime; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.Optional; import javax.inject.Inject; import javax.inject.Provider; import org.jooq.Condition; @@ -252,26 +254,8 @@ public HydrantVisite addVisite(HydrantVisite visite, Long organismeId) { TableField field = null; - TypeVisite typeVisite; try { - typeVisite = TypeVisite.valueOf(codeTypeVisite); - switch (typeVisite) { - case CREATION: - field = HYDRANT.DATE_CREA; - break; - - case RECEPTION: - field = HYDRANT.DATE_RECEP; - break; - - case RECONNAISSANCE: - field = HYDRANT.DATE_RECO; - break; - - case CONTROLE: - field = HYDRANT.DATE_CONTR; - break; - } + field = findFieldDate(codeTypeVisite); } catch (IllegalArgumentException iae) { // rien à faire } @@ -399,27 +383,9 @@ public void deleteVisite(HydrantVisite visite, String codeVisite) { } // On met à jour la date dans la table hydrant - TypeVisite typeVisite; TableField field = null; try { - typeVisite = TypeVisite.valueOf(codeVisite); - switch (typeVisite) { - case CREATION: - field = HYDRANT.DATE_CREA; - break; - - case RECEPTION: - field = HYDRANT.DATE_RECEP; - break; - - case RECONNAISSANCE: - field = HYDRANT.DATE_RECO; - break; - - case CONTROLE: - field = HYDRANT.DATE_CONTR; - break; - } + field = findFieldDate(codeVisite); } catch (IllegalArgumentException iae) { // rien à faire } @@ -509,4 +475,34 @@ public HydrantVisite getLatestVisite(Long idHydrant) { .limit(1) .fetchOneInto(HydrantVisite.class); } + + private TableField findFieldDate(String codeTypeVisite) { + TableField field = null; + Optional optionalTypeVisite = + Arrays.stream(TypeVisite.values()) + .filter(it -> it.getCode().equals(codeTypeVisite)) + .findFirst(); + if (optionalTypeVisite.isEmpty()) { + return null; + } + TypeVisite typeVisite = optionalTypeVisite.get(); + switch (typeVisite) { + case CREATION: + field = HYDRANT.DATE_CREA; + break; + + case RECEPTION: + field = HYDRANT.DATE_RECEP; + break; + + case RECONNAISSANCE: + field = HYDRANT.DATE_RECO; + break; + + case CONTROLE: + field = HYDRANT.DATE_CONTR; + break; + } + return field; + } }