Skip to content

Commit

Permalink
Merge pull request #11771 from hzi-braunschweig/change-8213-enter_hom…
Browse files Browse the repository at this point in the history
…e_address_existing_persons

#8213 - Enter home address for existing persons
  • Loading branch information
sergiupacurariu committed Apr 4, 2023
2 parents 47bb159 + b07d637 commit 70a685d
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -398,29 +398,13 @@ public LocationReferenceDto toReference() {
additionalInformation);
}

public boolean checkIsEmptyLocation() {
return details == null
&& city == null
&& areaType == null
&& region == null
&& district == null
&& community == null
&& street == null
&& houseNumber == null
&& additionalInformation == null;
}

public static LocationDto build() {

LocationDto location = new LocationDto();
location.setUuid(DataHelper.createUuid());
return location;
}

public static String buildStreetAndHouseNumberCaption(String street, String houseNumber) {
return DataHelper.toStringNullable(street) + " " + DataHelper.toStringNullable(houseNumber);
}

public String buildAddressCaption() {
String streetAndNumber = DataHelper.toStringNullable(street) + " " + DataHelper.toStringNullable(houseNumber);
String postalAndCity = DataHelper.toStringNullable(postalCode) + " " + DataHelper.toStringNullable(city);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@

package de.symeda.sormas.api.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

Expand Down Expand Up @@ -44,6 +49,7 @@ public static String buildGpsCoordinatesCaption(Double latitude, Double longitud
}

public static String buildLocationString(LocationDto location) {

List<String> locationFields = new ArrayList<>();

String region = DataHelper.toStringNullable(location.getRegion());
Expand All @@ -63,4 +69,30 @@ public static String buildLocationString(LocationDto location) {

return StringUtils.join(locationFields, ", ");
}

public static boolean checkIsEmptyLocation(LocationDto location) {

try {
List<Method> methods = Arrays.stream(location.getClass().getDeclaredMethods())
.filter(
m -> !Modifier.isStatic(m.getModifiers())
&& !Modifier.isPrivate(m.getModifiers())
&& (m.getName().startsWith("get") || m.getName().startsWith("is")))
.collect(Collectors.toList());

for (Method m : methods) {
if (m.getReturnType() == String.class) {
if (StringUtils.isNotBlank((String) m.invoke(location))) {
return false;
}
} else if (m.invoke(location) != null) {
return false;
}
}
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e.getMessage());
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* SORMAS® - Surveillance Outbreak Response Management & Analysis System
* Copyright © 2016-2023 Helmholtz-Zentrum für Infektionsforschung GmbH (HZI)
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package de.symeda.sormas.api.utils;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import de.symeda.sormas.api.infrastructure.continent.ContinentReferenceDto;
import de.symeda.sormas.api.location.LocationDto;

public class LocationHelperTest {

@Test
public void testCheckIsEmptyLocation() {

LocationDto emptyLocation = LocationDto.build();
assertTrue(LocationHelper.checkIsEmptyLocation(emptyLocation));

LocationDto nonEmptyLocation = LocationDto.build();
nonEmptyLocation.setHouseNumber("12");
assertFalse(LocationHelper.checkIsEmptyLocation(nonEmptyLocation));

nonEmptyLocation.setHouseNumber("");
assertTrue(LocationHelper.checkIsEmptyLocation(nonEmptyLocation));

nonEmptyLocation.setHouseNumber(null);
nonEmptyLocation.setLatitude(14.2342D);
assertFalse(LocationHelper.checkIsEmptyLocation(nonEmptyLocation));

nonEmptyLocation.setLatitude(null);
nonEmptyLocation.setContinent(new ContinentReferenceDto());
assertFalse(LocationHelper.checkIsEmptyLocation(nonEmptyLocation));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,7 @@ public CommitDiscardWrapperComponent<CaseCreateForm> getCaseCreateComponent(
} else {
PersonDto searchedPerson = createForm.getSearchedPerson();
if (searchedPerson != null) {
updateHomeAddress(createForm, searchedPerson);
dto.setPerson(searchedPerson.toReference());
selectOrCreateCase(createForm, dto, searchedPerson.toReference());
} else {
Expand Down Expand Up @@ -873,6 +874,11 @@ private void transferDataToPerson(CaseCreateForm createForm, PersonDto person) {
createForm.getPersonCreateForm().transferDataToPerson(person);
}

private void updateHomeAddress(CaseCreateForm createForm, PersonDto person) {
createForm.getPersonCreateForm().updateHomeAddress(person);
FacadeProvider.getPersonFacade().save(person);
}

public void selectOrCreateCase(CaseDataDto caseDto, PersonDto person, Consumer<String> selectedCaseUuidConsumer) {
CaseSimilarityCriteria criteria = CaseSimilarityCriteria.forCase(caseDto, person.getUuid());

Expand Down Expand Up @@ -921,10 +927,8 @@ public CommitDiscardWrapperComponent<CaseDataForm> getCaseDataEditComponent(fina
caze.isInJurisdiction());
caseEditForm.setValue(caze);

CommitDiscardWrapperComponent<CaseDataForm> editView = new CommitDiscardWrapperComponent<CaseDataForm>(
caseEditForm,
true,
caseEditForm.getFieldGroup());
CommitDiscardWrapperComponent<CaseDataForm> editView =
new CommitDiscardWrapperComponent<CaseDataForm>(caseEditForm, true, caseEditForm.getFieldGroup());

editView.getButtonsPanel()
.addComponentAsFirst(new DeletionLabel(automaticDeletionInfoDto, manuallyDeletionInfoDto, caze.isDeleted(), CaseDataDto.I18N_PREFIX));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import de.symeda.sormas.api.person.SimilarPersonDto;
import de.symeda.sormas.api.symptoms.SymptomsDto;
import de.symeda.sormas.api.utils.DateHelper;
import de.symeda.sormas.api.utils.LocationHelper;
import de.symeda.sormas.api.utils.fieldaccess.UiFieldAccessCheckers;
import de.symeda.sormas.api.utils.fieldvisibility.FieldVisibilityCheckers;
import de.symeda.sormas.ui.ControllerProvider;
Expand All @@ -79,7 +80,7 @@ public class PersonCreateForm extends AbstractEditForm<PersonDto> {
private LocationEditForm homeAddressForm;
private Button searchPersonButton;

private PersonDto searchedPerson;
private PersonDto person;

private final boolean showHomeAddressForm;
private final boolean showPresentCondition;
Expand All @@ -104,6 +105,7 @@ public PersonCreateForm(
boolean showPresentCondition,
boolean showSymptomsOnsetDate,
boolean showPersonSearchButton) {

super(
PersonDto.class,
PersonDto.I18N_PREFIX,
Expand All @@ -124,6 +126,7 @@ protected String createHtmlLayout() {

@Override
protected void addFields() {

addField(PersonDto.FIRST_NAME, TextField.class);
addField(PersonDto.LAST_NAME, TextField.class);

Expand All @@ -144,7 +147,8 @@ protected void addFields() {
birthDateMonth.setPageLength(12);
birthDateMonth.setInputPrompt(I18nProperties.getString(Strings.month));
birthDateMonth.setCaption("");
DateHelper.getMonthsInYear().forEach(month -> birthDateMonth.setItemCaption(month, de.symeda.sormas.api.Month.values()[month - 1].toString()));
DateHelper.getMonthsInYear()
.forEach(month -> birthDateMonth.setItemCaption(month, de.symeda.sormas.api.Month.values()[month - 1].toString()));
setItemCaptionsForMonths(birthDateMonth);
ComboBox birthDateYear = addField(PersonDto.BIRTH_DATE_YYYY, ComboBox.class);
birthDateYear.setCaption(I18nProperties.getPrefixCaption(PersonDto.I18N_PREFIX, PersonDto.BIRTH_DATE));
Expand Down Expand Up @@ -241,6 +245,7 @@ private void updateListOfDays(Integer selectedYear, Integer selectedMonth) {
}

private void addHomeAddressForm() {

enterHomeAddressNow = new CheckBox(I18nProperties.getCaption(Captions.caseDataEnterHomeAddressNow));
enterHomeAddressNow.addStyleName(VSPACE_3);
getContent().addComponent(enterHomeAddressNow, ENTER_HOME_ADDRESS_NOW);
Expand All @@ -266,13 +271,14 @@ private void addHomeAddressForm() {
addressHeader.setVisible(isChecked);
homeAddressForm.setVisible(isChecked);
homeAddressForm.setFacilityFieldsVisible(isChecked, true);
if (!isChecked && searchedPerson == null) {
if (!isChecked && person == null) {
homeAddressForm.clear();
}
});
}

protected Button createPersonSearchButton(String personSearchLoc) {

return ButtonHelper.createIconButtonWithCaption(personSearchLoc, StringUtils.EMPTY, VaadinIcons.SEARCH, clickEvent -> {
VaadinIcons icon = (VaadinIcons) clickEvent.getButton().getIcon();
if (icon == VaadinIcons.SEARCH) {
Expand All @@ -286,9 +292,9 @@ protected Button createPersonSearchButton(String personSearchLoc) {
SimilarPersonDto pickedPerson = personSearchField.getValue();
if (pickedPerson != null) {
// add consumer
searchedPerson = FacadeProvider.getPersonFacade().getByUuid(pickedPerson.getUuid());
setPerson(searchedPerson);
enablePersonFields(false);
person = FacadeProvider.getPersonFacade().getByUuid(pickedPerson.getUuid());
setPerson(person);
enablePersonFields(false, true);
clickEvent.getButton().setIcon(VaadinIcons.CLOSE);
}
});
Expand All @@ -299,8 +305,8 @@ protected Button createPersonSearchButton(String personSearchLoc) {

VaadinUiUtil.showModalPopupWindow(component, I18nProperties.getString(Strings.headingSelectPerson));
} else {
searchedPerson = null;
setPerson(searchedPerson);
person = null;
setPerson(person);
enablePersonFields(true);
clickEvent.getButton().setIcon(VaadinIcons.SEARCH);
}
Expand All @@ -309,9 +315,11 @@ protected Button createPersonSearchButton(String personSearchLoc) {

public void setPerson(PersonDto person) {

this.person = person;

if (showHomeAddressForm) {
enterHomeAddressNow.setEnabled(searchedPerson == null);
if (searchedPerson == null) {
enterHomeAddressNow.setEnabled(person == null || LocationHelper.checkIsEmptyLocation(person.getAddress()));
if (this.person == null) {
homeAddressForm.clear();
homeAddressForm.setFacilityFieldsVisible(false, true);
homeAddressForm.setVisible(false);
Expand Down Expand Up @@ -339,6 +347,7 @@ public void setPerson(PersonDto person) {
}

public void transferDataToPerson(PersonDto person) {

commit();
PersonDto personCreated = getValue();

Expand All @@ -363,22 +372,35 @@ public void transferDataToPerson(PersonDto person) {
}
}

public void enablePersonFields(Boolean enable) {
getField(PersonDto.FIRST_NAME).setEnabled(enable);
getField(PersonDto.LAST_NAME).setEnabled(enable);
getField(PersonDto.BIRTH_DATE_DD).setEnabled(enable);
getField(PersonDto.BIRTH_DATE_MM).setEnabled(enable);
getField(PersonDto.BIRTH_DATE_YYYY).setEnabled(enable);
getField(PersonDto.SEX).setEnabled(enable);
getField(PersonDto.PRESENT_CONDITION).setEnabled(enable);
getField(PersonDto.PHONE).setEnabled(enable);
getField(PersonDto.EMAIL_ADDRESS).setEnabled(enable);
getField(PersonDto.PASSPORT_NUMBER).setEnabled(enable);
getField(PersonDto.NATIONAL_HEALTH_ID).setEnabled(enable);
public void updateHomeAddress(PersonDto person) {

commit();
if (getHomeAddressForm() != null && getHomeAddressForm().getValue() != null) {
person.setAddress(getHomeAddressForm().getValue());
}
}

public void enablePersonFields(Boolean enabled) {
enablePersonFields(enabled, false);
}

public void enablePersonFields(Boolean enabled, boolean alwaysEnableAddressFields) {

getField(PersonDto.FIRST_NAME).setEnabled(enabled);
getField(PersonDto.LAST_NAME).setEnabled(enabled);
getField(PersonDto.BIRTH_DATE_DD).setEnabled(enabled);
getField(PersonDto.BIRTH_DATE_MM).setEnabled(enabled);
getField(PersonDto.BIRTH_DATE_YYYY).setEnabled(enabled);
getField(PersonDto.SEX).setEnabled(enabled);
getField(PersonDto.PRESENT_CONDITION).setEnabled(enabled);
getField(PersonDto.PHONE).setEnabled(enabled);
getField(PersonDto.EMAIL_ADDRESS).setEnabled(enabled);
getField(PersonDto.PASSPORT_NUMBER).setEnabled(enabled);
getField(PersonDto.NATIONAL_HEALTH_ID).setEnabled(enabled);
if (homeAddressForm != null) {
homeAddressForm.setEnabled(enable);
homeAddressForm.setEnabled(enabled || alwaysEnableAddressFields);
}
setRequired(enable, PersonDto.FIRST_NAME, PersonDto.LAST_NAME, PersonDto.SEX);
setRequired(enabled, PersonDto.FIRST_NAME, PersonDto.LAST_NAME, PersonDto.SEX);
}

public void setPersonalDetailsReadOnlyIfNotEmpty(boolean readOnly) {
Expand Down Expand Up @@ -435,6 +457,7 @@ public void setSymptoms(SymptomsDto symptoms) {
}

public void updatePresentConditionEnum(Disease disease) {

ComboBox presentConditionField = getField(PersonDto.PRESENT_CONDITION);
PresentCondition currentValue = (PresentCondition) presentConditionField.getValue();
List<PresentCondition> validValues;
Expand Down Expand Up @@ -465,10 +488,10 @@ public Date getOnsetDate() {
}

public PersonDto getSearchedPerson() {
return searchedPerson;
return person;
}

public void setSearchedPerson(PersonDto searchedPerson) {
this.searchedPerson = searchedPerson;
this.person = searchedPerson;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import de.symeda.sormas.api.sample.SampleMaterial;
import de.symeda.sormas.api.user.DefaultUserRole;
import de.symeda.sormas.api.user.UserDto;
import de.symeda.sormas.api.utils.LocationHelper;
import de.symeda.sormas.api.utils.YesNoUnknown;
import de.symeda.sormas.api.vaccination.VaccinationDto;
import de.symeda.sormas.ui.AbstractBeanTest;
Expand Down Expand Up @@ -357,7 +358,7 @@ public void testImportAddressTypes()
assertTrue(CollectionUtils.isEmpty(casePerson2.getAddresses()));
assertEquals("132", casePerson2.getAddress().getHouseNumber());

assertTrue(casePerson3.getAddress().checkIsEmptyLocation());
assertTrue(LocationHelper.checkIsEmptyLocation(casePerson3.getAddress()));
assertEquals(1, casePerson3.getAddresses().size());
assertEquals("133", casePerson3.getAddresses().get(0).getHouseNumber());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import de.symeda.sormas.api.person.SimilarPersonDto;
import de.symeda.sormas.api.user.DefaultUserRole;
import de.symeda.sormas.api.user.UserDto;
import de.symeda.sormas.api.utils.LocationHelper;
import de.symeda.sormas.api.utils.YesNoUnknown;
import de.symeda.sormas.api.vaccination.VaccinationDto;
import de.symeda.sormas.backend.contact.ContactFacadeEjb;
Expand Down Expand Up @@ -281,7 +282,7 @@ public void testImportCaseContactsDifferentAddressTypes()
}
if ("Oona".equals(person.getFirstName())) {
foundOona = true;
assertTrue(person.getAddress().checkIsEmptyLocation());
assertTrue(LocationHelper.checkIsEmptyLocation(person.getAddress()));
assertEquals(1, person.getAddresses().size());
assertEquals("133", person.getAddresses().get(0).getHouseNumber());
}
Expand Down
Loading

0 comments on commit 70a685d

Please sign in to comment.