This is an automated email from the git hooks/post-receive script. New commit to branch feature/2264 in repository wao. See http://git.codelutin.com/wao.git commit 166ecae2b55bb45e0173748e135a913e37528a82 Author: Brendan Le Ny <bleny@codelutin.com> Date: Fri Dec 26 16:13:07 2014 +0100 Remaniement de la validation des contacts (séparation en méthodes du service) --- .../wao/services/service/ContactsService.java | 278 +++++++++++---------- 1 file changed, 144 insertions(+), 134 deletions(-) diff --git a/wao-services/src/main/java/fr/ifremer/wao/services/service/ContactsService.java b/wao-services/src/main/java/fr/ifremer/wao/services/service/ContactsService.java index 798e593..cefdd93 100644 --- a/wao-services/src/main/java/fr/ifremer/wao/services/service/ContactsService.java +++ b/wao-services/src/main/java/fr/ifremer/wao/services/service/ContactsService.java @@ -594,7 +594,9 @@ public class ContactsService extends WaoServiceSupport { MissingContactDataInputDateException, ContactRestitutionDateBeforeDataInputDateException, MissingContactDataReliabilityException, - MissingContactCommentAdminException, MissingContactTerrestrialLocationException, IllegalAcceptationException { + MissingContactCommentAdminException, + MissingContactTerrestrialLocationException, + IllegalAcceptationException { // Check if contact can be updated boolean canUpdateContact = authenticatedWaoUser.isAuthorizedToEditContact(updateContactCommand.getContact()) @@ -607,35 +609,10 @@ public class ContactsService extends WaoServiceSupport { // Pass validation controls Contact contact = updateContactCommand.getContact(); - SampleRow sampleRow = contact.getSampleRow(); - - WaoUser mainObserver = contact.getMainObserver(); - - if (mainObserver == null) { - throw new MissingContactMainObserverException(contact); - } - - Set<WaoUser> possibleObservers = getPossibleObservers(sampleRow); - if ( ! possibleObservers.contains(mainObserver)) { - throw new MismatchContactMainObserverCompanyException(contact, mainObserver, sampleRow.getOrganisation()); - } - - if (contact.isSecondaryObserversNotEmpty()) { - - //--- - // secondaryObservers - //--- - - if (contact.getSecondaryObservers().contains(mainObserver)) { - throw new DuplicatedContactMainObserverInSecondaryObserversException(contact); - } - - for (WaoUser secondaryObserver : contact.getSecondaryObservers()) { - if ( ! possibleObservers.contains(secondaryObserver)) { - throw new MismatchContactSecondaryObserverCompanyException(contact, secondaryObserver, sampleRow.getOrganisation()); - } - } - } + validateObservers(contact); + validateChronology(contact); + validateContactStateMotif(contact); + validateComments(contact); Date observationBeginDate = contact.getObservationBeginDate(); Date observationEndDate = contact.getObservationEndDate(); @@ -643,11 +620,7 @@ public class ContactsService extends WaoServiceSupport { if (observationBeginDate != null) { - //--- - // observationBeginDate - //--- - - SampleMonth month = sampleRow.getSampleMonth(observationBeginDate); + SampleMonth month = contact.getSampleRow().getSampleMonth(observationBeginDate); boolean operationBeginDateIsValid = month != null; if (!operationBeginDateIsValid) { @@ -655,154 +628,191 @@ public class ContactsService extends WaoServiceSupport { } } - Date currentDate = serviceContext.getNow(); - - if (observationEndDate != null) { - - //--- - // observationEndDate - //--- + ContactState contactState = contact.getContactState(); - if (observationBeginDate != null - && observationEndDate.before(observationBeginDate)) { - throw new ContactObservationEndDateBeforeBeginDateException(contact); + if (ContactState.OBSERVATION_DONE.equals(contactState)) { // Observation réalisée + if (observationBeginDate == null) { + throw new MissingContactObservationBeginDateException(contact); } - - if (observationEndDate.after(currentDate)) { - throw new ContactObservationEndDateAfterTodayException(contact, currentDate); + if (observationEndDate == null) { + throw new MissingContactObservationEndDateException(contact); + } + if (contact.getObsProgram().isObsVente() && contact.getTerrestrialLocation() == null) { + throw new MissingContactTerrestrialLocationException(contact); } - } - if (dataInputDate != null) { + if (BooleanUtils.isTrue(contact.getValidationCompany())) { //--- - // dataInputDate + // validationCompany //--- - if (observationEndDate != null) { - - // Here, we check that the date input date is after the end of the - // observation. Since the first is a day (at 00:00) and the second - // is date-time, there may be a bug if the data input day is the same - // as the end of observation - boolean dataInputDateAfterObservationEndDate = - observationEndDate.after(DateUtil.setMaxTimeOfDay(dataInputDate)); - - if (dataInputDateAfterObservationEndDate) { - throw new ContactDataInputDateBeforeObservationEndDateException(contact); + if (ContactState.OBSERVATION_DONE.equals(contactState)) { + if (contact.getObsProgram().isObsMer() && contact.getObservedDataControl() == null) { + throw new MissingContactObservedDataControlException(contact); + } else if (ObservedDataControl.CORRECTION_ASKED.equals(contact.getObservedDataControl())) { + throw new ContactWithObservedDataControlToCorrectionAskedException(contact); } } - if (dataInputDate.after(currentDate)) { - throw new ContactDataInputDateAfterTodayException(contact, currentDate); + // Pour valider un contact société, il faut que le contact aie une date de transmission + Date restitution = contact.getRestitution(); + if (ObservedDataControl.ACCEPTED.equals(contact.getObservedDataControl()) && restitution == null) { + throw new MissingContactRestitutionException(contact); + } + if (dataInputDate == null && restitution != null) { + throw new MissingContactDataInputDateException(contact); + } + if (dataInputDate != null && restitution != null && restitution.before(dataInputDate)) { + throw new ContactRestitutionDateBeforeDataInputDateException(contact); } } - //--- - // contactState - //--- - - ContactState contactState = contact.getContactState(); + if (contact.getValidationProgram() != null && contact.getValidationCompany() == null) { + throw new IllegalAcceptationException(contact); + } - boolean contactMustHaveAMotif = false; - switch (contactState) { - case OBSERVATION_CANCELLED: + if (BooleanUtils.isTrue(contact.getValidationProgram())) { - // Non abouti + //--- + // validationProgram + //--- + if (contact.getObsProgram().isObsMer()) { - if (StringUtils.isBlank(contact.getComment())) { - throw new MissingContactCommentException(contact); + // it's an admin validation, data-reliability field must be filled + if (contact.getDataReliability() == null || DataReliability.UNKNOWN == contact.getDataReliability()) { + throw new MissingContactDataReliabilityException(contact); } - break; - case OBSERVATION_DONE: + } + } + } - // Observation réalisée + /** + * On vérifie que les différents commentaires complémentaires à une indication sont bien renseignés. + */ + protected void validateComments(Contact contact) throws + MissingContactCommentException, + MissingContactMammalsInfoException, + MissingContactCommentAdminException { - if (observationBeginDate == null) { - throw new MissingContactObservationBeginDateException(contact); - } - if (observationEndDate == null) { - throw new MissingContactObservationEndDateException(contact); - } - if (contact.getObsProgram().isObsVente() && contact.getTerrestrialLocation() == null) { - throw new MissingContactTerrestrialLocationException(contact); - } - break; + if (ContactState.OBSERVATION_CANCELLED.equals(contact.getContactState())) { + if (StringUtils.isBlank(contact.getComment())) { + throw new MissingContactCommentException(contact); + } + } - case CONTACT_REFUSED: - contactMustHaveAMotif = true; - break; - case CONTACT_DEFINITELY_REFUSED: - contactMustHaveAMotif = true; - break; + if (contact.isMammalsCapture() && StringUtils.isBlank(contact.getMammalsInfo())) { + throw new MissingContactMammalsInfoException(contact); + } + DataReliability dataReliability = contact.getDataReliability(); + if (dataReliability != null && dataReliability.isCommentRequired() && StringUtils.isBlank(contact.getCommentAdmin())) { + throw new MissingContactCommentAdminException(contact); } + } + + /** + * On s'assure que le champs {@link fr.ifremer.wao.entity.Contact#getContactStateMotif()} n'a + * été rempli que si nécessaire. + */ + protected void validateContactStateMotif(Contact contact) throws + MissingContactStateMotifException, + UnwantedContactContactStateMotifException { - //--- - // contactStateMotif - //--- + boolean contactMustHaveAMotif = ImmutableSet.of( + ContactState.CONTACT_REFUSED, + ContactState.CONTACT_DEFINITELY_REFUSED).contains(contact.getContactState()); if (contactMustHaveAMotif && contact.getContactStateMotif() == null) { throw new MissingContactStateMotifException(contact); } - if (!contactMustHaveAMotif && contact.getContactStateMotif() != null) { + if ( ! contactMustHaveAMotif && contact.getContactStateMotif() != null) { throw new UnwantedContactContactStateMotifException(contact); } - if (contact.isMammalsCapture() && StringUtils.isBlank(contact.getMammalsInfo())) { - throw new MissingContactMammalsInfoException(contact); - } + } - if (BooleanUtils.isTrue(contact.getValidationCompany())) { + /** + * Partie de la validation dans laquelle on s'intéresse à la chronologie des événement. + * + * On ne vérifie pas si les champs ont été renseignés mais que s'ils l'ont été, c'est + * de façon cohérente (champs entre eux et par rapport à la date courante. + */ + protected void validateChronology(Contact contact) throws + ContactObservationEndDateBeforeBeginDateException, + ContactObservationEndDateAfterTodayException, + ContactDataInputDateBeforeObservationEndDateException, + ContactDataInputDateAfterTodayException { - //--- - // validationCompany - //--- + Date observationBeginDate = contact.getObservationBeginDate(); + Date observationEndDate = contact.getObservationEndDate(); + Date dataInputDate = contact.getDataInputDate(); - if (ContactState.OBSERVATION_DONE == contactState) { - if (contact.getObsProgram().isObsMer() && contact.getObservedDataControl() == null) { - throw new MissingContactObservedDataControlException(contact); - } else if (ObservedDataControl.CORRECTION_ASKED.equals(contact.getObservedDataControl())) { - throw new ContactWithObservedDataControlToCorrectionAskedException(contact); - } - } + Date now = serviceContext.getNow(); - // Pour valider un contact société, il faut que le contact aie une date de transmission - Date restitution = contact.getRestitution(); - if (ObservedDataControl.ACCEPTED.equals(contact.getObservedDataControl()) && restitution == null) { - throw new MissingContactRestitutionException(contact); - } - if (dataInputDate == null && restitution != null) { - throw new MissingContactDataInputDateException(contact); + if (observationEndDate != null) { + + if (observationBeginDate != null && observationEndDate.before(observationBeginDate)) { + throw new ContactObservationEndDateBeforeBeginDateException(contact); } - if (dataInputDate != null && restitution != null && restitution.before(dataInputDate)) { - throw new ContactRestitutionDateBeforeDataInputDateException(contact); + + if (observationEndDate.after(now)) { + throw new ContactObservationEndDateAfterTodayException(contact, now); } + } - if (contact.getValidationProgram() != null && contact.getValidationCompany() == null) { + if (dataInputDate != null) { - throw new IllegalAcceptationException(contact); + if (observationEndDate != null) { + + // Here, we check that the date input date is after the end of the + // observation. Since the first is a day (at 00:00) and the second + // is date-time, there may be a bug if the data input day is the same + // as the end of observation + boolean dataInputDateAfterObservationEndDate = + observationEndDate.after(DateUtil.setMaxTimeOfDay(dataInputDate)); + + if (dataInputDateAfterObservationEndDate) { + throw new ContactDataInputDateBeforeObservationEndDateException(contact); + } + } + + if (dataInputDate.after(now)) { + throw new ContactDataInputDateAfterTodayException(contact, now); + } } - DataReliability dataReliability = contact.getDataReliability(); + } - if (dataReliability != null && dataReliability.isCommentRequired() && StringUtils.isBlank(contact.getCommentAdmin())) { - throw new MissingContactCommentAdminException(contact); + /** + * Partie de la validation d'un contact chargée de vérifier la cohérence des observateurs renseignés. + */ + protected void validateObservers(Contact contact) throws + MissingContactMainObserverException, MismatchContactMainObserverCompanyException, + DuplicatedContactMainObserverInSecondaryObserversException, + MismatchContactSecondaryObserverCompanyException { + WaoUser mainObserver = contact.getMainObserver(); + + if (mainObserver == null) { + throw new MissingContactMainObserverException(contact); } - if (BooleanUtils.isTrue(contact.getValidationProgram())) { + Set<WaoUser> possibleObservers = getPossibleObservers(contact.getSampleRow()); + if ( ! possibleObservers.contains(mainObserver)) { + throw new MismatchContactMainObserverCompanyException(contact, mainObserver, contact.getSampleRow().getOrganisation()); + } - //--- - // validationProgram - //--- - if (contact.getObsProgram().isObsMer()) { + if (contact.isSecondaryObserversNotEmpty()) { + if (contact.getSecondaryObservers().contains(mainObserver)) { + throw new DuplicatedContactMainObserverInSecondaryObserversException(contact); + } - // it's an admin validation, data-reliability field must be filled - if (dataReliability == null || DataReliability.UNKNOWN == dataReliability) { - throw new MissingContactDataReliabilityException(contact); + for (WaoUser secondaryObserver : contact.getSecondaryObservers()) { + if ( ! possibleObservers.contains(secondaryObserver)) { + throw new MismatchContactSecondaryObserverCompanyException(contact, secondaryObserver, contact.getSampleRow().getOrganisation()); } } } -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.