Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#12794 - The same event participant should not be allowed to be selected #12843

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import de.symeda.sormas.app.backend.common.AbstractAdoDao;
import de.symeda.sormas.app.backend.common.AbstractDomainObject;
import de.symeda.sormas.app.backend.config.ConfigProvider;
import de.symeda.sormas.app.backend.person.Person;

public class EventParticipantDao extends AbstractAdoDao<EventParticipant> {

Expand Down Expand Up @@ -66,6 +67,27 @@ public List<EventParticipant> getByEvent(Event event) {
}
}

public boolean eventParticipantAlreadyExists(Event event, Person person) {

if (event.isSnapshot()) {
throw new IllegalArgumentException("Does not support snapshot entities");
}

try {
List<EventParticipant> eventParticipants = queryBuilder().where()
.eq(EventParticipant.EVENT + "_id", event)
.and()
.eq(AbstractDomainObject.SNAPSHOT, false)
.and()
.eq(EventParticipant.PERSON + "_id", person)
.query();
return eventParticipants.size() > 0;
} catch (SQLException e) {
Log.e(getTableName(), "Could not perform getByEvent on EventParticipant");
throw new RuntimeException(e);
}
}

public Long countByEvent(Event event) {
if (event.isSnapshot()) {
throw new IllegalArgumentException("Does not support snapshot entities");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,10 @@ private void linkEventToCase() {
eventParticipantToSave.setResultingCaseUuid(caze.getUuid());
EventParticipantSaver eventParticipantSaver = new EventParticipantSaver(this);

if (!isEventLinkedToCase(caze, event)) {
boolean eventParticipantAlreadyExists =
carina29 marked this conversation as resolved.
Show resolved Hide resolved
DatabaseHelper.getEventParticipantDao().eventParticipantAlreadyExists(event, caze.getPerson());

if (!isEventLinkedToCase(caze, event) && !eventParticipantAlreadyExists) {
eventParticipantSaver.saveEventParticipantLinkedToCase(eventParticipantToSave);
} else {
NotificationHelper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public void saveData() {

// Person selection can be skipped if the case was created from a contact or event participant
if (contactUuid == null && eventParticipantUuid == null) {
SelectOrCreatePersonDialog.selectOrCreatePerson(caze.getPerson(), person -> {
SelectOrCreatePersonDialog.selectOrCreatePerson(caze.getPerson(), caze, person -> {
if (person != null) {
caze.setPerson(person);
pickOrCreateCaseAndSave(caze, fragment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void saveData() {
return;
}

SelectOrCreatePersonDialog.selectOrCreatePerson(contactToSave.getPerson(), new Consumer<Person>() {
SelectOrCreatePersonDialog.selectOrCreatePerson(contactToSave.getPerson(), contactToSave, new Consumer<Person>() {

@Override
public void accept(Person person) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ public void saveData() {
return;
}

SelectOrCreatePersonDialog.selectOrCreatePerson(eventParticipantToSave.getPerson(), new Consumer<Person>() {
final Event event = DatabaseHelper.getEventDao().queryUuid(eventUuid);

SelectOrCreatePersonDialog.selectOrCreatePerson(eventParticipantToSave.getPerson(), event, new Consumer<Person>() {

@Override
public void accept(Person person) {
Expand All @@ -151,7 +153,6 @@ protected void onPreExecute() {
@Override
protected void doInBackground(TaskResultHolder resultHolder) throws Exception {
DatabaseHelper.getPersonDao().saveAndSnapshot(eventParticipantToSave.getPerson());
final Event event = DatabaseHelper.getEventDao().queryUuid(eventUuid);
eventParticipantToSave.setEvent(event);
DatabaseHelper.getEventParticipantDao().saveAndSnapshot(eventParticipantToSave);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void saveData() {
if (caseUuid != null || contactUuid != null || eventParticipantUuid != null) {
pickOrCreateImmunizationAndSave(immunization, fragment);
} else {
SelectOrCreatePersonDialog.selectOrCreatePerson(immunization.getPerson(), person -> {
SelectOrCreatePersonDialog.selectOrCreatePerson(immunization.getPerson(), immunization, person -> {
if (person != null) {
immunization.setPerson(person);
pickOrCreateImmunizationAndSave(immunization, fragment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@

import android.content.Context;
import android.view.View;

import androidx.databinding.Observable;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ViewDataBinding;
import androidx.databinding.library.baseAdapters.BR;
import androidx.fragment.app.FragmentActivity;

import de.symeda.sormas.api.i18n.I18nProperties;
import de.symeda.sormas.api.i18n.Strings;
import de.symeda.sormas.api.person.PersonHelper;
import de.symeda.sormas.api.person.PersonNameDto;
import de.symeda.sormas.api.person.PersonSimilarityCriteria;
import de.symeda.sormas.app.BaseActivity;
import de.symeda.sormas.app.R;
import de.symeda.sormas.app.backend.common.DatabaseHelper;
import de.symeda.sormas.app.backend.common.PseudonymizableAdo;
import de.symeda.sormas.app.backend.event.Event;
import de.symeda.sormas.app.backend.person.Person;
import de.symeda.sormas.app.component.controls.ControlButton;
import de.symeda.sormas.app.component.dialog.AbstractDialog;
Expand All @@ -61,9 +63,11 @@ public class SelectOrCreatePersonDialog extends AbstractDialog {
private Callback createCallback;
private final ObservableField<Person> selectedPerson = new ObservableField<>();

private View selectedPersonItemView;

// Static methods

public static void selectOrCreatePerson(final Person person, final Consumer<Person> resultConsumer) {
public static void selectOrCreatePerson(final Person person, PseudonymizableAdo ado, final Consumer<Person> resultConsumer) {
final SelectOrCreatePersonDialog personDialog = new SelectOrCreatePersonDialog(BaseActivity.getActiveActivity(), person);

if (!personDialog.hasSimilarPersons()) {
Expand All @@ -73,7 +77,26 @@ public static void selectOrCreatePerson(final Person person, final Consumer<Pers

personDialog.setPositiveCallback(() -> {
if (personDialog.getSelectedPerson() != null) {
resultConsumer.accept(personDialog.getSelectedPerson());
if (ado instanceof Event) {
carina29 marked this conversation as resolved.
Show resolved Hide resolved
boolean eventParticipantAlreadyExists =
DatabaseHelper.getEventParticipantDao().eventParticipantAlreadyExists((Event) ado, personDialog.getSelectedPerson());

if (eventParticipantAlreadyExists) {
personDialog.suppressNextDismiss();

personDialog.setSelectedPerson(null);
personDialog.setSelectedPersonItemView(null);

NotificationHelper.showDialogNotification(
personDialog,
NotificationType.WARNING,
I18nProperties.getString(Strings.messageAlreadyEventParticipant));
} else {
resultConsumer.accept(personDialog.getSelectedPerson());
}
} else {
resultConsumer.accept(personDialog.getSelectedPerson());
}
} else {
personDialog.suppressNextDismiss();
NotificationHelper.showDialogNotification(personDialog, NotificationType.ERROR, R.string.info_select_create_person);
Expand Down Expand Up @@ -150,8 +173,10 @@ private void setUpControlListeners() {

if (itemViewId == vId && v.isSelected()) {
itemView.setSelected(false);
setSelectedPerson(personItem);
} else if (itemViewId == vId && !v.isSelected()) {
itemView.setSelected(true);
setSelectedPersonItemView(itemView);
setSelectedPerson(personItem);
} else {
itemView.setSelected(false);
Expand Down Expand Up @@ -193,6 +218,11 @@ public void onPropertyChanged(Observable observable, int i) {
if (getSelectedPerson() == null) {
btnCreate.setVisibility(View.VISIBLE);
btnSelect.setVisibility(View.GONE);

//TODO: View based on tag and the item will be set to false
carina29 marked this conversation as resolved.
Show resolved Hide resolved
if (selectedPersonItemView != null) {
selectedPersonItemView.setSelected(false);
}
} else {
btnCreate.setVisibility(View.GONE);
btnSelect.setVisibility(View.VISIBLE);
Expand Down Expand Up @@ -229,4 +259,8 @@ private Person getSelectedPerson() {
private void setSelectedPerson(Person selectedPerson) {
this.selectedPerson.set(selectedPerson);
}

public void setSelectedPersonItemView(View selectedPersonItemView) {
this.selectedPersonItemView = selectedPersonItemView;
}
}
Loading