Skip to content

Commit

Permalink
#183 added validation for date of report and occurrence in signal inf…
Browse files Browse the repository at this point in the history
…ormation form
  • Loading branch information
Anthony4m committed Nov 28, 2024
1 parent 84fddc2 commit 14c0e2b
Showing 1 changed file with 226 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package de.symeda.sormas.ui.ebs;

import static de.symeda.sormas.api.utils.DataHelper.isNullOrEmpty;
import static de.symeda.sormas.ui.utils.CssStyles.H3;
import static de.symeda.sormas.ui.utils.LayoutUtil.fluidRowLocs;
import static de.symeda.sormas.ui.utils.LayoutUtil.loc;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

import com.vaadin.ui.Label;
import com.vaadin.v7.ui.DateField;
Expand All @@ -24,6 +27,9 @@
import de.symeda.sormas.api.utils.fieldaccess.UiFieldAccessCheckers;
import de.symeda.sormas.api.utils.fieldvisibility.FieldVisibilityCheckers;
import de.symeda.sormas.ui.utils.AbstractEditForm;
import de.symeda.sormas.ui.utils.EbsAnimalDeathValidator;
import de.symeda.sormas.ui.utils.EbsDateValidator;
import de.symeda.sormas.ui.utils.EbsPersonDeathValidator;
import de.symeda.sormas.ui.utils.FieldHelper;
import de.symeda.sormas.ui.utils.NullableOptionGroup;
import de.symeda.sormas.ui.utils.NumberNumericValueValidator;
Expand All @@ -37,9 +43,24 @@ public class SignalVerificationDataForm extends AbstractEditForm<SignalVerificat
private static final String SIGNAL_VERIFICATION_LOC = "signalVerificationLoc";

private static final String SIGNAL_VERIFICATION_ENTITY = "SignalVerification";
public static final String PERSON = "Person";
public static final String ANIMAL = "Animal";
public static final String NUMBER_OF_DEATH_MORE_CASES = "The number of death cannot be more than cases";
public final String THE_DATE_OF_OCCURRENCE_CANNOT_BE_EARLIER_THAN_THE_DATE_OF_OCCURRENCE =
"The Date cannot be earlier than the Date of Report or Date of Verification Completed Date.";

private final EbsDto ebs;
private final Class<? extends EntityDto> parentClass;
TextField numberOfPersonAnimal;
TextField numberOfDeath;
TextField numberOfPersonCases;
TextField numberOfDeathPerson;
TextArea description;
DateField verificationCompleteDate;
DateField DateOfOccurrence;
EbsAnimalDeathValidator animalDeathValidator = new EbsAnimalDeathValidator(NUMBER_OF_DEATH_MORE_CASES, false);
EbsPersonDeathValidator personDeathValidator = new EbsPersonDeathValidator(NUMBER_OF_DEATH_MORE_CASES, false);
EbsDateValidator dateValidator = new EbsDateValidator(THE_DATE_OF_OCCURRENCE_CANNOT_BE_EARLIER_THAN_THE_DATE_OF_OCCURRENCE, false);

private static final String HTML_LAYOUT = loc(SIGNAL_VERIFICATION_LOC)
+ fluidRowLocs(SignalVerificationDto.VERIFICATION_SENT)
Expand Down Expand Up @@ -97,14 +118,14 @@ protected void addFields() {
getContent().addComponent(headingEventDetails, EVENT_DETAILS_LOC);
NullableOptionGroup sentVerification = addField(SignalVerificationDto.VERIFICATION_SENT, NullableOptionGroup.class);
NullableOptionGroup verified = addField(SignalVerificationDto.VERIFIED, NullableOptionGroup.class);
addField(SignalVerificationDto.VERIFICATION_COMPLETE_DATE, DateField.class);
addField(SignalVerificationDto.DATE_OF_OCCURRENCE, DateField.class);
TextField numberOfPersonAnimal = addField(SignalVerificationDto.NUMBER_OF_PERSON_ANIMAL, TextField.class);
TextField numberOfDeath = addField(SignalVerificationDto.NUMBER_OF_DEATH, TextField.class);
addField(SignalVerificationDto.DESCRIPTION, TextArea.class);
verificationCompleteDate = addField(SignalVerificationDto.VERIFICATION_COMPLETE_DATE, DateField.class);
DateOfOccurrence = addField(SignalVerificationDto.DATE_OF_OCCURRENCE, DateField.class);
numberOfPersonAnimal = addField(SignalVerificationDto.NUMBER_OF_PERSON_ANIMAL, TextField.class);
numberOfDeath = addField(SignalVerificationDto.NUMBER_OF_DEATH, TextField.class);
description = addField(SignalVerificationDto.DESCRIPTION, TextArea.class);
addField(SignalVerificationDto.WHY_NOT_VERIFY, TextArea.class);
TextField numberOfPersonCases = addField(SignalVerificationDto.NUMBER_OF_PERSON_CASES, TextField.class);
TextField numberOfDeathPerson = addField(SignalVerificationDto.NUMBER_OF_DEATH_PERSON, TextField.class);
numberOfPersonCases = addField(SignalVerificationDto.NUMBER_OF_PERSON_CASES, TextField.class);
numberOfDeathPerson = addField(SignalVerificationDto.NUMBER_OF_DEATH_PERSON, TextField.class);

numberOfPersonCases.addValidator(
new NumberNumericValueValidator(I18nProperties.getValidationError(Validations.onlyNumbersAllowed, numberOfPersonCases.getCaption())));
Expand Down Expand Up @@ -157,14 +178,18 @@ protected void addFields() {
SignalVerificationDto.VERIFICATION_COMPLETE_DATE,
SignalVerificationDto.DATE_OF_OCCURRENCE,
SignalVerificationDto.DESCRIPTION);
setRequired(true, SignalVerificationDto.WHY_NOT_VERIFY);
if (sentVerification.getValue() != null && sentVerification.getValue().toString().equals("NO")) {
setRequired(false, SignalVerificationDto.WHY_NOT_VERIFY);
} else {
setRequired(true, SignalVerificationDto.WHY_NOT_VERIFY);
}
getContent().getComponent(EVENT_DETAILS_LOC).setVisible(false);

}
});
setRequired(true, SignalVerificationDto.VERIFICATION_SENT);

sentVerification.addValueChangeListener(e -> {
String selectedValue = e.getProperty().getValue().toString();
if (e.getProperty().getValue().toString().equals("NO")) {
setRequired(false, SignalVerificationDto.WHY_NOT_VERIFY);
}
Expand All @@ -174,6 +199,198 @@ protected void addFields() {
TriagingDataForm.reviewSignal(Strings.verifyNotifs);
}
});

if (selectedEbs.getSignalVerification().getVerificationSent() != null
&& selectedEbs.getSignalVerification().getVerificationSent() == YesNo.NO) {
setRequired(false, SignalVerificationDto.WHY_NOT_VERIFY);
}
numberOfDeathPerson.addValueChangeListener(event -> {
checkDeathCount(PERSON, false);
});
numberOfPersonCases.addValueChangeListener(event -> {
checkDeathCount(PERSON, false);
});
numberOfPersonAnimal.addValueChangeListener(event -> {
checkDeathCount(ANIMAL, false);
});
numberOfDeath.addValueChangeListener(event -> {
checkDeathCount(ANIMAL, false);
});
description.addFocusListener(event -> {
checkDeathCount(ANIMAL, true);
checkDeathCount(PERSON, true);
});
DateOfOccurrence.addValueChangeListener(event -> {
validateSignalVerificationDateOfOccurrence(selectedEbs);
});
verificationCompleteDate.addValueChangeListener(event -> {
validateSignalVerificationVerificationCompleteDate(selectedEbs);
});
validateSignalVerificationDateOfOccurrence(selectedEbs);
validateSignalVerificationVerificationCompleteDate(selectedEbs);
}

private void checkDeathCount(String entity, boolean anotherField) {
String numberOfCases = "";
String value = "";

switch (entity) {
case PERSON:
numberOfCases = numberOfPersonCases.getValue();
value = numberOfDeathPerson.getValue();
break;
case ANIMAL:
numberOfCases = numberOfPersonAnimal.getValue();
value = numberOfDeath.getValue();
break;
default:
return;
}

if (anotherField) {
CheckCaseNull(entity, value, numberOfCases);
}

if (!isNullOrEmpty(value) && !isNullOrEmpty(numberOfCases)) {
try {
int deathCount = Integer.parseInt(value);
int caseCount = Integer.parseInt(numberOfCases);

if (deathCount > caseCount) {
addDeathCountExceedsCaseCountValidator(entity);
} else {
addNumberFormatValidator(entity);
removeDeathCountExceedsCaseCountValidator(entity);
}
} catch (NumberFormatException ex) {
addNumberFormatValidator(entity);
}
}
}

private void addNumberFormatValidator(String entity) {
NumberNumericValueValidator validator = new NumberNumericValueValidator(
I18nProperties.getValidationError(
Validations.numberOfDeathMoreThanCases,
entity.equals(PERSON) ? numberOfDeathPerson.getCaption() : numberOfDeath.getCaption()));

if (entity.equals(PERSON)) {
numberOfDeathPerson.addValidator(validator);
} else {
numberOfDeath.addValidator(validator);
}
}

private void addDeathCountExceedsCaseCountValidator(String entity) {
if (entity.equals(PERSON)) {
personDeathValidator.setValidDeath(false);
numberOfDeathPerson.removeAllValidators();
numberOfDeathPerson.addValidator(personDeathValidator);
addNumberFormatValidator(entity);
} else {
animalDeathValidator.setValidDeath(false);
numberOfDeath.removeAllValidators();
numberOfDeath.addValidator(animalDeathValidator);
addNumberFormatValidator(entity);
}

}

private void removeDeathCountExceedsCaseCountValidator(String entity) {
if (entity.equals(PERSON)) {
personDeathValidator.setValidDeath(true);
numberOfDeathPerson.removeValidator(personDeathValidator);
} else {
animalDeathValidator.setValidDeath(true);
numberOfDeath.removeValidator(animalDeathValidator);
}
}

private void CheckCaseNull(String entity, String value, String numberOfCases) {
if (!isNullOrEmpty(value) && isNullOrEmpty(numberOfCases) && entity.equals(PERSON)) {
int valueToInt = Integer.parseInt(value);
numberOfPersonCases.setValue(String.valueOf(valueToInt));
}
if (!isNullOrEmpty(value) && isNullOrEmpty(numberOfCases) && entity.equals(ANIMAL)) {
int valueToInt = Integer.parseInt(value);
numberOfPersonAnimal.setValue(String.valueOf(valueToInt));
}
}

public void validateSignalVerificationVerificationCompleteDate(EbsDto selectedEbs) {
Date dateOfReport = selectedEbs.getReportDateTime();
Date dateOfDecision = selectedEbs.getTriaging().getDecisionDate();
DateField verificationCompleteDateField = verificationCompleteDate;
Date DateverificationCompleteDate = verificationCompleteDateField.getValue();
if (dateOfReport == null) {
dateOfReport = new Date(0);
}

Date dateOfReportDate = clearTime(dateOfReport);
Date dateOfDecisionDate = clearTime(dateOfDecision);
if (DateverificationCompleteDate != null) {
DateverificationCompleteDate = clearTime(DateverificationCompleteDate);
if (DateverificationCompleteDate.before(dateOfReportDate) || DateverificationCompleteDate.before(dateOfDecisionDate)) {
if (!dateOfReportDate.toString().equals(DateverificationCompleteDate.toString())
|| !dateOfDecision.toString().equals(DateverificationCompleteDate.toString())) {
verificationCompleteDateField.addValidator(dateValidator);
addVerificationDateValidator();
} else {
verificationCompleteDate.removeAllValidators();
}
} else {
verificationCompleteDate.removeAllValidators();
}
}
}

public void validateSignalVerificationDateOfOccurrence(EbsDto selectedEbs) {
Date dateOfReport = selectedEbs.getReportDateTime();
Date dateOfDecision = selectedEbs.getTriaging().getDecisionDate();
DateField DateOfOccurrenceField = DateOfOccurrence;
Date DateverificationCompleteDate = DateOfOccurrenceField.getValue();
if (dateOfReport == null) {
dateOfReport = new Date(0);
}

Date dateOfReportDate = clearTime(dateOfReport);
Date dateOfDecisionDate = clearTime(dateOfDecision);
if (DateverificationCompleteDate != null) {
DateverificationCompleteDate = clearTime(DateverificationCompleteDate);
if (DateverificationCompleteDate.before(dateOfReportDate) || DateverificationCompleteDate.before(dateOfDecisionDate)) {
if (!dateOfReportDate.toString().equals(DateverificationCompleteDate.toString())
|| !dateOfDecision.toString().equals(DateverificationCompleteDate.toString())) {
DateOfOccurrenceField.addValidator(dateValidator);
addOccurrenceDateValidator();
} else {
DateOfOccurrence.removeAllValidators();
}
} else {
DateOfOccurrence.removeAllValidators();
}
}
}

private Date clearTime(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}

private void addVerificationDateValidator() {
dateValidator.setValidDate(false);
verificationCompleteDate.removeValidator(dateValidator);
verificationCompleteDate.addValidator(dateValidator);
}

private void addOccurrenceDateValidator() {
dateValidator.setValidDate(false);
verificationCompleteDate.removeValidator(dateValidator);
verificationCompleteDate.addValidator(dateValidator);
}

}

0 comments on commit 14c0e2b

Please sign in to comment.