/
SampleController.java
605 lines (533 loc) · 27 KB
/
SampleController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/*******************************************************************************
* SORMAS® - Surveillance Outbreak Response Management & Analysis System
* Copyright © 2016-2018 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.ui.samples;
import static de.symeda.sormas.ui.utils.CssStyles.VSPACE_NONE;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import com.vaadin.navigator.Navigator;
import com.vaadin.server.Page;
import com.vaadin.server.Sizeable.Unit;
import com.vaadin.shared.ui.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Notification.Type;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.ValoTheme;
import com.vaadin.v7.data.Buffered.SourceException;
import com.vaadin.v7.data.Validator.InvalidValueException;
import com.vaadin.v7.ui.CheckBox;
import com.vaadin.v7.ui.ComboBox;
import com.vaadin.v7.ui.Field;
import de.symeda.sormas.api.CountryHelper;
import de.symeda.sormas.api.Disease;
import de.symeda.sormas.api.FacadeProvider;
import de.symeda.sormas.api.caze.CaseDataDto;
import de.symeda.sormas.api.caze.CaseReferenceDto;
import de.symeda.sormas.api.contact.ContactDto;
import de.symeda.sormas.api.contact.ContactReferenceDto;
import de.symeda.sormas.api.event.EventDto;
import de.symeda.sormas.api.event.EventParticipantDto;
import de.symeda.sormas.api.event.EventParticipantReferenceDto;
import de.symeda.sormas.api.event.EventReferenceDto;
import de.symeda.sormas.api.i18n.Captions;
import de.symeda.sormas.api.i18n.I18nProperties;
import de.symeda.sormas.api.i18n.Strings;
import de.symeda.sormas.api.i18n.Validations;
import de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto;
import de.symeda.sormas.api.sample.PathogenTestDto;
import de.symeda.sormas.api.sample.PathogenTestResultType;
import de.symeda.sormas.api.sample.SampleCriteria;
import de.symeda.sormas.api.sample.SampleDto;
import de.symeda.sormas.api.sample.SampleIndexDto;
import de.symeda.sormas.api.sample.SamplePurpose;
import de.symeda.sormas.api.sample.SampleReferenceDto;
import de.symeda.sormas.api.sample.SpecimenCondition;
import de.symeda.sormas.api.task.TaskContext;
import de.symeda.sormas.api.user.UserRight;
import de.symeda.sormas.api.utils.DataHelper;
import de.symeda.sormas.ui.ControllerProvider;
import de.symeda.sormas.ui.SormasUI;
import de.symeda.sormas.ui.UserProvider;
import de.symeda.sormas.ui.labmessage.LabMessagesView;
import de.symeda.sormas.ui.utils.ButtonHelper;
import de.symeda.sormas.ui.utils.CommitDiscardWrapperComponent;
import de.symeda.sormas.ui.utils.ConfirmationComponent;
import de.symeda.sormas.ui.utils.DateComparisonValidator;
import de.symeda.sormas.ui.utils.DateFormatHelper;
import de.symeda.sormas.ui.utils.DateTimeField;
import de.symeda.sormas.ui.utils.NullableOptionGroup;
import de.symeda.sormas.ui.utils.VaadinUiUtil;
import de.symeda.sormas.ui.utils.components.page.title.TitleLayout;
public class SampleController {
public SampleController() {
}
public void registerViews(Navigator navigator) {
navigator.addView(SamplesView.VIEW_NAME, SamplesView.class);
navigator.addView(SampleDataView.VIEW_NAME, SampleDataView.class);
if (UserProvider.getCurrent().hasUserRight(UserRight.LAB_MESSAGES)) {
navigator.addView(LabMessagesView.VIEW_NAME, LabMessagesView.class);
}
}
public void navigateToData(String sampleUuid) {
String navigationState = SampleDataView.VIEW_NAME + "/" + sampleUuid;
SormasUI.get().getNavigator().navigateTo(navigationState);
}
public void create(CaseReferenceDto caseRef, Disease disease, Runnable callback) {
createSample(SampleDto.build(UserProvider.getCurrent().getUserReference(), caseRef), disease, callback);
}
public void create(ContactReferenceDto contactRef, Disease disease, Runnable callback) {
createSample(SampleDto.build(UserProvider.getCurrent().getUserReference(), contactRef), disease, callback);
}
public void create(EventParticipantReferenceDto eventParticipantRef, Disease disease, Runnable callback) {
createSample(SampleDto.build(UserProvider.getCurrent().getUserReference(), eventParticipantRef), disease, callback);
}
private void createSample(SampleDto sampleDto, Disease disease, Runnable callback) {
final CommitDiscardWrapperComponent<SampleCreateForm> editView = getSampleCreateComponent(sampleDto, disease, callback);
// add option to create additional pathogen tests
addPathogenTestButton(editView, false);
VaadinUiUtil.showModalPopupWindow(editView, I18nProperties.getString(Strings.headingCreateNewSample));
}
/**
*
* Works just like the addPathogenTestComponent(..., int caseSampleCount, ...), but additionally determines the caseSampleCount.
* For performance reasons, this method shall just be called when the caseSampleCount is not known already and just one pathogen test
* create component shall be added.
*
* @param sampleComponent
* to add the pathogen test create component to.
* @return the pathogen test create component added.
*/
public PathogenTestForm addPathogenTestComponent(CommitDiscardWrapperComponent<? extends AbstractSampleForm> sampleComponent) {
int caseSampleCount = caseSampleCountOf(sampleComponent.getWrappedComponent().getValue());
return addPathogenTestComponent(sampleComponent, null, caseSampleCount, SormasUI::refreshView);
}
public PathogenTestForm addPathogenTestComponent(
CommitDiscardWrapperComponent<? extends AbstractSampleForm> sampleComponent,
PathogenTestDto pathogenTest,
int caseSampleCount) {
return addPathogenTestComponent(sampleComponent, pathogenTest, caseSampleCount, null);
}
/**
*
* @param sampleComponent
* to add the pathogen test create component to.
* @param pathogenTest
* the preset values to insert. May be null.
* @param caseSampleCount
* describes how many samples already exist for a case related to the pathogen test's sample (if a case exists, otherwise 0
* is valid).
* @param callback
* use it to define additional actions that need to be taken after the pathogen test is saved (e.g. refresh the UI)
* @return the pathogen test create component added.
*/
public PathogenTestForm addPathogenTestComponent(
CommitDiscardWrapperComponent<? extends AbstractSampleForm> sampleComponent,
PathogenTestDto pathogenTest,
int caseSampleCount,
Runnable callback) {
// add horizontal rule to clearly distinguish the component
Label horizontalRule = new Label("<br><hr /><br>", ContentMode.HTML);
horizontalRule.setWidth(100f, Unit.PERCENTAGE);
sampleComponent.addComponent(horizontalRule, sampleComponent.getComponentCount() - 1);
PathogenTestForm pathogenTestForm = new PathogenTestForm(sampleComponent.getWrappedComponent().getValue(), true, caseSampleCount, false);
// prefill fields
if (pathogenTest != null) {
pathogenTestForm.setValue(pathogenTest);
// show typingId field when it has a preset value
if (pathogenTest.getTypingId() != null && !"".equals(pathogenTest.getTypingId())) {
pathogenTestForm.getField(PathogenTestDto.TYPING_ID).setVisible(true);
}
} else {
pathogenTestForm.setValue(PathogenTestDto.build(sampleComponent.getWrappedComponent().getValue(), UserProvider.getCurrent().getUser()));
// remove value invalid for newly created pathogen tests
ComboBox pathogenTestResultField = pathogenTestForm.getField(PathogenTestDto.TEST_RESULT);
pathogenTestResultField.removeItem(PathogenTestResultType.NOT_DONE);
pathogenTestResultField.setValue(PathogenTestResultType.PENDING);
ComboBox testDiseaseField = pathogenTestForm.getField(PathogenTestDto.TESTED_DISEASE);
testDiseaseField.setValue(FacadeProvider.getDiseaseConfigurationFacade().getDefaultDisease());
}
// setup field updates
Field testLabField = pathogenTestForm.getField(PathogenTestDto.LAB);
NullableOptionGroup samplePurposeField = sampleComponent.getWrappedComponent().getField(SampleDto.SAMPLE_PURPOSE);
Runnable updateTestLabFieldRequired = () -> testLabField.setRequired(!SamplePurpose.INTERNAL.equals(samplePurposeField.getValue()));
updateTestLabFieldRequired.run();
samplePurposeField.addValueChangeListener(e -> updateTestLabFieldRequired.run());
// validate pathogen test create component before saving the sample
sampleComponent.addFieldGroups(pathogenTestForm.getFieldGroup());
CommitDiscardWrapperComponent.CommitListener savePathogenTest = () -> {
ControllerProvider.getPathogenTestController().savePathogenTest(pathogenTestForm.getValue(), null, true, true);
if (callback != null) {
callback.run();
}
};
sampleComponent.addCommitListener(savePathogenTest);
// Discard button configuration
Button discardButton = ButtonHelper.createButton(I18nProperties.getCaption(Captions.pathogenTestRemove));
VerticalLayout buttonLayout = new VerticalLayout(discardButton);
buttonLayout.setComponentAlignment(discardButton, Alignment.TOP_LEFT);
// add the discard button above the overall discard and commit buttons
sampleComponent.addComponent(buttonLayout, sampleComponent.getComponentCount() - 1);
discardButton.addClickListener(o -> {
sampleComponent.removeComponent(horizontalRule);
sampleComponent.removeComponent(buttonLayout);
sampleComponent.removeComponent(pathogenTestForm);
sampleComponent.removeFieldGroups(pathogenTestForm.getFieldGroup());
sampleComponent.removeCommitListener(savePathogenTest);
pathogenTestForm.discard();
});
// Country specific configuration
boolean germanInstance = FacadeProvider.getConfigFacade().isConfiguredCountry(CountryHelper.COUNTRY_CODE_GERMANY);
pathogenTestForm.getField(PathogenTestDto.REPORT_DATE).setVisible(germanInstance);
pathogenTestForm.getField(PathogenTestDto.EXTERNAL_ID).setVisible(germanInstance);
pathogenTestForm.getField(PathogenTestDto.EXTERNAL_ORDER_ID).setVisible(germanInstance);
pathogenTestForm.getField(PathogenTestDto.VIA_LIMS).setVisible(germanInstance);
// Sample creation specific configuration
final DateTimeField sampleDateField = sampleComponent.getWrappedComponent().getField(SampleDto.SAMPLE_DATE_TIME);
final DateTimeField testDateField = pathogenTestForm.getField(PathogenTestDto.TEST_DATE_TIME);
testDateField.addValidator(
new DateComparisonValidator(
testDateField,
sampleDateField,
false,
false,
I18nProperties.getValidationError(Validations.afterDate, testDateField.getCaption(), sampleDateField.getCaption())));
// add the pathogenTestForm above the overall discard and commit buttons
sampleComponent.addComponent(pathogenTestForm, sampleComponent.getComponentCount() - 1);
return pathogenTestForm;
}
public CommitDiscardWrapperComponent<SampleCreateForm> getSampleCreateComponent(SampleDto sampleDto, Disease disease, Runnable callback) {
final SampleCreateForm createForm = new SampleCreateForm(disease);
createForm.setValue(sampleDto);
final CommitDiscardWrapperComponent<SampleCreateForm> editView = new CommitDiscardWrapperComponent<>(
createForm,
UserProvider.getCurrent().hasUserRight(UserRight.SAMPLE_CREATE),
createForm.getFieldGroup());
editView.addCommitListener(() -> {
if (!createForm.getFieldGroup().isModified()) {
FacadeProvider.getSampleFacade().saveSample(sampleDto);
callback.run();
}
});
return editView;
}
public void addPathogenTestButton(CommitDiscardWrapperComponent<? extends AbstractSampleForm> editView, boolean viaLims) {
Button addPathogenTestButton = new Button(I18nProperties.getCaption(Captions.pathogenTestAdd));
addPathogenTestButton.addClickListener((e) -> {
PathogenTestForm pathogenTestForm = addPathogenTestComponent(editView);
if (viaLims) {
setViaLimsFieldChecked(pathogenTestForm);
}
});
editView.getButtonsPanel().addComponent(addPathogenTestButton, 0);
}
public void setViaLimsFieldChecked(PathogenTestForm pathogenTestForm) {
CheckBox viaLimsCheckbox = pathogenTestForm.getField(PathogenTestDto.VIA_LIMS);
viaLimsCheckbox.setValue(Boolean.TRUE);
}
public void createReferral(SampleDto existingSample, Disease disease) {
CommitDiscardWrapperComponent<SampleCreateForm> createView = getSampleReferralCreateComponent(existingSample, disease);
createView.addDoneListener(() -> navigateToData(existingSample.getUuid()));
createView.getWrappedComponent().getValue().setPathogenTestResult(PathogenTestResultType.PENDING);
VaadinUiUtil.showModalPopupWindow(createView, I18nProperties.getString(Strings.headingReferSample));
}
public CommitDiscardWrapperComponent<SampleCreateForm> getSampleReferralCreateComponent(SampleDto existingSample, Disease disease) {
final SampleDto referralSample = SampleDto.buildReferralDto(UserProvider.getCurrent().getUserReference(), existingSample);
final CommitDiscardWrapperComponent<SampleCreateForm> createView = getSampleCreateComponent(referralSample, disease, () -> {
});
createView.addCommitListener(() -> {
if (!createView.getWrappedComponent().getFieldGroup().isModified()) {
SampleDto updatedSample = FacadeProvider.getSampleFacade().getSampleByUuid(existingSample.getUuid());
updatedSample.setReferredTo(referralSample.toReference());
FacadeProvider.getSampleFacade().saveSample(updatedSample);
}
});
return createView;
}
public CommitDiscardWrapperComponent<SampleEditForm> getSampleEditComponent(
final String sampleUuid,
boolean isPseudonymized,
Disease disease,
boolean showDeleteButton) {
SampleEditForm form = new SampleEditForm(isPseudonymized, disease);
form.setWidth(form.getWidth() * 10 / 12, Unit.PIXELS);
SampleDto dto = FacadeProvider.getSampleFacade().getSampleByUuid(sampleUuid);
form.setValue(dto);
final CommitDiscardWrapperComponent<SampleEditForm> editView = new CommitDiscardWrapperComponent<SampleEditForm>(
form,
UserProvider.getCurrent().hasUserRight(UserRight.SAMPLE_EDIT),
form.getFieldGroup());
editView.addCommitListener(() -> {
if (!form.getFieldGroup().isModified()) {
SampleDto changedDto = form.getValue();
SampleDto originalDto = FacadeProvider.getSampleFacade().getSampleByUuid(changedDto.getUuid());
FacadeProvider.getSampleFacade().saveSample(changedDto);
SormasUI.refreshView();
final CaseReferenceDto associatedCase = changedDto.getAssociatedCase();
final CaseDataDto caseDataByUuid = FacadeProvider.getCaseFacade().getCaseDataByUuid(associatedCase.getUuid());
FacadeProvider.getCaseFacade().save(caseDataByUuid);
if (changedDto.getSpecimenCondition() != originalDto.getSpecimenCondition()
&& changedDto.getSpecimenCondition() == SpecimenCondition.NOT_ADEQUATE
&& UserProvider.getCurrent().hasUserRight(UserRight.TASK_CREATE)) {
requestSampleCollectionTaskCreation(changedDto, form);
} else {
Notification.show(I18nProperties.getString(Strings.messageSampleSaved), Type.TRAY_NOTIFICATION);
}
}
});
if (showDeleteButton && UserProvider.getCurrent().hasUserRight(UserRight.SAMPLE_DELETE)) {
editView.addDeleteListener(() -> {
FacadeProvider.getSampleFacade().deleteSample(dto.toReference());
UI.getCurrent().getNavigator().navigateTo(SamplesView.VIEW_NAME);
}, I18nProperties.getString(Strings.entitySample));
}
if (dto.getReferredTo() != null || dto.getSamplePurpose() == SamplePurpose.EXTERNAL) {
editView.getWrappedComponent().getField(SampleDto.SAMPLE_PURPOSE).setEnabled(false);
}
return editView;
}
/**
* Initialize 'Refer to another laboratory' button or link to referred sample
*
* @param editForm
* the edit form to attach the 'Refer to another laboratory' button to.
* @param disease
* required for field visibility checks in the sample create form opened when a sample reference shall be created.
* @param createReferral
* instructions for what shall happen when the user chooses to create a referral
* @param openReferredSample
* instructions for what shall happen when the user chooses to open the referred sample
*/
public void addReferOrLinkToOtherLabButton(
CommitDiscardWrapperComponent<SampleEditForm> editForm,
Disease disease,
Consumer<Disease> createReferral,
Consumer<SampleDto> openReferredSample) {
Button referOrLinkToOtherLabButton = null;
SampleDto sample = editForm.getWrappedComponent().getValue();
if (sample.getReferredTo() == null) {
if (sample.getSamplePurpose() == SamplePurpose.EXTERNAL && UserProvider.getCurrent().hasUserRight(UserRight.SAMPLE_TRANSFER)) {
referOrLinkToOtherLabButton =
ButtonHelper.createButton("referOrLinkToOtherLab", I18nProperties.getCaption(Captions.sampleRefer), new ClickListener() {
private static final long serialVersionUID = 1L;
@Override
public void buttonClick(ClickEvent event) {
try {
createReferral.accept(disease);
} catch (SourceException | InvalidValueException e) {
Notification.show(I18nProperties.getString(Strings.messageSampleErrors), Type.ERROR_MESSAGE);
}
}
}, ValoTheme.BUTTON_LINK);
}
} else {
SampleDto referredDto = FacadeProvider.getSampleFacade().getSampleByUuid(sample.getReferredTo().getUuid());
FacilityReferenceDto referredDtoLab = referredDto.getLab();
String referOrLinkToOtherLabButtonCaption = referredDtoLab == null
? I18nProperties.getCaption(Captions.sampleReferredToInternal) + " ("
+ DateFormatHelper.formatLocalDateTime(referredDto.getSampleDateTime()) + ")"
: I18nProperties.getCaption(Captions.sampleReferredTo) + " " + referredDtoLab.toString();
referOrLinkToOtherLabButton = ButtonHelper.createButton("referOrLinkToOtherLab", referOrLinkToOtherLabButtonCaption, new ClickListener() {
private static final long serialVersionUID = 1L;
@Override
public void buttonClick(ClickEvent event) {
openReferredSample.accept(referredDto);
}
});
}
if (referOrLinkToOtherLabButton != null) {
editForm.getButtonsPanel().addComponentAsFirst(referOrLinkToOtherLabButton);
editForm.getButtonsPanel().setComponentAlignment(referOrLinkToOtherLabButton, Alignment.BOTTOM_LEFT);
}
}
/**
*
* @param editForm
* the edit form to attach the 'Referred from' button to.
* @param navigation
* instructions for what shall happen when the user chooses to open the related sample
*/
public void addReferredFromButton(CommitDiscardWrapperComponent<SampleEditForm> editForm, Consumer<SampleDto> navigation) {
SampleReferenceDto referredFromRef = FacadeProvider.getSampleFacade().getReferredFrom(editForm.getWrappedComponent().getValue().getUuid());
if (referredFromRef != null) {
SampleDto referredFrom = FacadeProvider.getSampleFacade().getSampleByUuid(referredFromRef.getUuid());
FacilityReferenceDto referredFromLab = referredFrom.getLab();
String referredButtonCaption = referredFromLab == null
? I18nProperties.getCaption(Captions.sampleReferredFromInternal) + " ("
+ DateFormatHelper.formatLocalDateTime(referredFrom.getSampleDateTime()) + ")"
: I18nProperties.getCaption(Captions.sampleReferredFrom) + " " + referredFromLab.toString();
Button referredButton = ButtonHelper
.createButton("referredFrom", referredButtonCaption, event -> navigation.accept(referredFrom), ValoTheme.BUTTON_LINK, VSPACE_NONE);
editForm.getWrappedComponent().addReferredFromButton(referredButton);
}
}
private void requestSampleCollectionTaskCreation(SampleDto dto, SampleEditForm form) {
VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
ConfirmationComponent requestTaskComponent = VaadinUiUtil.buildYesNoConfirmationComponent();
Label description = new Label(I18nProperties.getString(Strings.messageCreateCollectionTask), ContentMode.HTML);
description.setWidth(100, Unit.PERCENTAGE);
layout.addComponent(description);
layout.addComponent(requestTaskComponent);
layout.setComponentAlignment(requestTaskComponent, Alignment.BOTTOM_RIGHT);
layout.setSizeUndefined();
layout.setSpacing(true);
Window popupWindow = VaadinUiUtil.showPopupWindow(layout);
popupWindow.setSizeUndefined();
popupWindow.setCaption(I18nProperties.getString(Strings.headingCreateNewTaskQuestion));
requestTaskComponent.getConfirmButton().addClickListener(new ClickListener() {
private static final long serialVersionUID = 1L;
@Override
public void buttonClick(ClickEvent event) {
popupWindow.close();
final CaseReferenceDto associatedCase = dto.getAssociatedCase();
final ContactReferenceDto associatedContact = dto.getAssociatedContact();
final EventParticipantReferenceDto associatedEventParticipant = dto.getAssociatedEventParticipant();
if (associatedCase != null) {
final CaseDataDto caseDto = FacadeProvider.getCaseFacade().getCaseDataByUuid(associatedCase.getUuid());
ControllerProvider.getTaskController().createSampleCollectionTask(TaskContext.CASE, associatedCase, dto, caseDto.getDisease());
} else if (associatedContact != null) {
final ContactDto contactDto = FacadeProvider.getContactFacade().getByUuid(associatedContact.getUuid());
ControllerProvider.getTaskController()
.createSampleCollectionTask(TaskContext.CONTACT, associatedContact, dto, contactDto.getDisease());
} else if (associatedEventParticipant != null) {
final EventParticipantDto eventParticipantDto =
FacadeProvider.getEventParticipantFacade().getEventParticipantByUuid(associatedEventParticipant.getUuid());
final EventDto eventDto = FacadeProvider.getEventFacade().getEventByUuid(eventParticipantDto.getEvent().getUuid(), false);
ControllerProvider.getTaskController()
.createSampleCollectionTask(TaskContext.EVENT, eventParticipantDto.getEvent(), dto, eventDto.getDisease());
}
}
});
requestTaskComponent.getCancelButton().addClickListener(event -> popupWindow.close());
}
public void showChangePathogenTestResultWindow(
CommitDiscardWrapperComponent<? extends AbstractSampleForm> editComponent,
String sampleUuid,
PathogenTestResultType newResult,
Consumer<Boolean> callback) {
VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
ConfirmationComponent confirmationComponent = VaadinUiUtil.buildYesNoConfirmationComponent();
Label description = new Label(String.format(I18nProperties.getString(Strings.messageChangePathogenTestResult), newResult.toString()));
description.setWidth(100, Unit.PERCENTAGE);
layout.addComponent(description);
layout.addComponent(confirmationComponent);
layout.setComponentAlignment(confirmationComponent, Alignment.BOTTOM_RIGHT);
layout.setSizeUndefined();
layout.setSpacing(true);
Window popupWindow = VaadinUiUtil.showPopupWindow(layout);
popupWindow.setSizeUndefined();
popupWindow.setCaption(I18nProperties.getString(Strings.headingChangePathogenTestResult));
confirmationComponent.getConfirmButton().addClickListener(new ClickListener() {
private static final long serialVersionUID = 1L;
@Override
public void buttonClick(ClickEvent event) {
if (editComponent != null && !SampleCreateForm.class.equals(editComponent.getWrappedComponent().getClass())) {
editComponent.commit();
}
SampleDto sample = FacadeProvider.getSampleFacade().getSampleByUuid(sampleUuid);
sample.setPathogenTestResult(newResult);
FacadeProvider.getSampleFacade().saveSample(sample);
popupWindow.close();
SormasUI.refreshView();
if (callback != null) {
callback.accept(true);
}
}
});
confirmationComponent.getCancelButton().addClickListener(new ClickListener() {
private static final long serialVersionUID = 1L;
@Override
public void buttonClick(ClickEvent event) {
popupWindow.close();
if (callback != null) {
callback.accept(false);
}
}
});
}
public void deleteAllSelectedItems(Collection<SampleIndexDto> selectedRows, Runnable callback) {
if (selectedRows.size() == 0) {
new Notification(
I18nProperties.getString(Strings.headingNoSamplesSelected),
I18nProperties.getString(Strings.messageNoSamplesSelected),
Type.WARNING_MESSAGE,
false).show(Page.getCurrent());
} else {
VaadinUiUtil
.showDeleteConfirmationWindow(String.format(I18nProperties.getString(Strings.confirmationDeleteSamples), selectedRows.size()), () -> {
List<String> sampleIndexDtoList = selectedRows.stream().map(SampleIndexDto::getUuid).collect(Collectors.toList());
FacadeProvider.getSampleFacade().deleteAllSamples(sampleIndexDtoList);
callback.run();
new Notification(
I18nProperties.getString(Strings.headingSamplesDeleted),
I18nProperties.getString(Strings.messageSamplesDeleted),
Type.HUMANIZED_MESSAGE,
false).show(Page.getCurrent());
});
}
}
public TitleLayout getSampleViewTitleLayout(SampleDto sample) {
TitleLayout titleLayout = new TitleLayout();
titleLayout.addRow(DataHelper.getShortUuid(sample.getUuid()));
titleLayout.addRow(DateFormatHelper.formatDate(sample.getSampleDateTime()));
String mainRowText = SampleReferenceDto.buildCaption(
sample.getSampleMaterial(),
sample.getAssociatedCase() != null ? sample.getAssociatedCase().getUuid() : null,
sample.getAssociatedContact() != null ? sample.getAssociatedContact().getUuid() : null,
sample.getAssociatedEventParticipant() != null ? sample.getAssociatedEventParticipant().getUuid() : null);
titleLayout.addMainRow(mainRowText);
return titleLayout;
}
public int caseSampleCountOf(SampleDto sample) {
CaseReferenceDto cazeRef = sample.getAssociatedCase();
if (cazeRef == null) {
return 0;
} else {
SampleCriteria sampleCriteria = new SampleCriteria();
sampleCriteria.caze(cazeRef);
return (int) FacadeProvider.getSampleFacade().count(sampleCriteria);
}
}
public Disease getDiseaseOf(SampleDto sample) {
CaseReferenceDto cazeRef = sample.getAssociatedCase();
if (cazeRef != null) {
return FacadeProvider.getCaseFacade().getByUuid(cazeRef.getUuid()).getDisease();
}
ContactReferenceDto contactRef = sample.getAssociatedContact();
if (contactRef != null) {
return FacadeProvider.getContactFacade().getByUuid(contactRef.getUuid()).getDisease();
}
EventParticipantReferenceDto eventPartRef = sample.getAssociatedEventParticipant();
if (eventPartRef != null) {
EventReferenceDto eventRef = FacadeProvider.getEventParticipantFacade().getByUuid(eventPartRef.getUuid()).getEvent();
if (eventRef != null) {
return FacadeProvider.getEventFacade().getEventByUuid(eventRef.getUuid(), false).getDisease();
}
}
return null;
}
}