-
Notifications
You must be signed in to change notification settings - Fork 24
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
O3-2862 - Queue Module - validate that queue entry falls within visit… #52
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ca5a2af
O3-2862 - Queue Module - validate that queue entry falls within visit…
mseaton 267177f
O3-2862 - Queue Module - validate that queue entry falls within visit…
mseaton 1f31e3d
O3-2862 - Queue Module - validate that queue entry falls within visit…
mseaton 326e966
O3-2862 - Queue Module - add visit save handler that ensures that whe…
mseaton a72599b
O3-2875 - Merge changes from main
mseaton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
api/src/main/java/org/openmrs/module/queue/api/VisitWithQueueEntriesSaveHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
* | ||
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
* graphic logo is a trademark of OpenMRS Inc. | ||
*/ | ||
package org.openmrs.module.queue.api; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.openmrs.User; | ||
import org.openmrs.Visit; | ||
import org.openmrs.annotation.Handler; | ||
import org.openmrs.api.handler.SaveHandler; | ||
import org.openmrs.module.queue.api.search.QueueEntrySearchCriteria; | ||
import org.openmrs.module.queue.model.QueueEntry; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
|
||
@Handler(supports = Visit.class) | ||
public class VisitWithQueueEntriesSaveHandler implements SaveHandler<Visit> { | ||
|
||
private final Log log = LogFactory.getLog(getClass()); | ||
|
||
private final QueueEntryService queueEntryService; | ||
|
||
@Autowired | ||
public VisitWithQueueEntriesSaveHandler(@Qualifier("queue.QueueEntryService") QueueEntryService queueEntryService) { | ||
this.queueEntryService = queueEntryService; | ||
} | ||
|
||
@Override | ||
public void handle(Visit visit, User user, Date date, String s) { | ||
if (visit.getStopDatetime() != null) { | ||
QueueEntrySearchCriteria criteria = new QueueEntrySearchCriteria(); | ||
criteria.setVisit(visit); | ||
criteria.setIsEnded(false); | ||
List<QueueEntry> queueEntries = queueEntryService.getQueueEntries(criteria); | ||
if (!queueEntries.isEmpty()) { | ||
log.debug("Closing " + +queueEntries.size() + " queue entries associated with stopped visit"); | ||
} | ||
for (QueueEntry qe : queueEntries) { | ||
qe.setEndedAt(visit.getStopDatetime()); | ||
queueEntryService.saveQueueEntry(qe); | ||
log.trace("Closed queue entry " + qe + " on " + visit.getStopDatetime()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
api/src/main/java/org/openmrs/module/queue/validators/VisitWithQueueEntriesValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
* | ||
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
* graphic logo is a trademark of OpenMRS Inc. | ||
*/ | ||
package org.openmrs.module.queue.validators; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
|
||
import org.openmrs.Visit; | ||
import org.openmrs.annotation.Handler; | ||
import org.openmrs.module.queue.api.QueueEntryService; | ||
import org.openmrs.module.queue.api.search.QueueEntrySearchCriteria; | ||
import org.openmrs.module.queue.model.QueueEntry; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.validation.Errors; | ||
import org.springframework.validation.Validator; | ||
|
||
@Handler(supports = { Visit.class }, order = 60) | ||
public class VisitWithQueueEntriesValidator implements Validator { | ||
|
||
private final QueueEntryService queueEntryService; | ||
|
||
@Autowired | ||
public VisitWithQueueEntriesValidator(@Qualifier("queue.QueueEntryService") QueueEntryService queueEntryService) { | ||
this.queueEntryService = queueEntryService; | ||
} | ||
|
||
@Override | ||
public boolean supports(Class<?> clazz) { | ||
return Visit.class.isAssignableFrom(clazz); | ||
} | ||
|
||
@Override | ||
public void validate(Object target, Errors errors) { | ||
if (!(target instanceof Visit)) { | ||
throw new IllegalArgumentException("the parameter target must be of type " + Visit.class); | ||
} | ||
Visit visit = (Visit) target; | ||
|
||
// This implementation is copied to match the core validator approach to nested encounters | ||
if (visit.getId() != null) { | ||
Date startDateTime = visit.getStartDatetime(); | ||
Date stopDateTime = visit.getStopDatetime(); | ||
|
||
QueueEntrySearchCriteria criteria = new QueueEntrySearchCriteria(); | ||
criteria.setIsEnded(null); | ||
criteria.setVisit(visit); | ||
List<QueueEntry> queueEntries = queueEntryService.getQueueEntries(criteria); | ||
for (QueueEntry queueEntry : queueEntries) { | ||
if (queueEntry.getStartedAt().before(startDateTime)) { | ||
errors.rejectValue("startDatetime", "queue.entry.error.cannotStartBeforeVisitStartDate", | ||
"This visit has queue entries whose dates cannot be before the start date"); | ||
break; | ||
} | ||
if (stopDateTime != null) { | ||
if (queueEntry.getStartedAt().after(stopDateTime)) { | ||
errors.rejectValue("stopDatetime", "queue.entry.error.cannotStartAfterVisitStopDate", | ||
"This visit has queue entries which start after the stop date"); | ||
break; | ||
} | ||
// We do not fail validation if the endedAt is null. The visit saveHandler will ensure this is set | ||
if (queueEntry.getEndedAt() != null && queueEntry.getEndedAt().after(stopDateTime)) { | ||
errors.rejectValue("stopDatetime", "queue.entry.error.cannotEndAfterVisitStopDate", | ||
"This visit has queue entries which end after the stop date"); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...ests/src/test/java/org/openmrs/module/queue/api/VisitWithQueueEntriesSaveHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
* | ||
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
* graphic logo is a trademark of OpenMRS Inc. | ||
*/ | ||
package org.openmrs.module.queue.api; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import java.util.Arrays; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
import org.apache.commons.lang.time.DateUtils; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.openmrs.Visit; | ||
import org.openmrs.api.VisitService; | ||
import org.openmrs.module.queue.SpringTestConfiguration; | ||
import org.openmrs.module.queue.model.QueueEntry; | ||
import org.openmrs.test.BaseModuleContextSensitiveTest; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.test.context.ContextConfiguration; | ||
|
||
@ContextConfiguration(classes = SpringTestConfiguration.class, inheritLocations = false) | ||
public class VisitWithQueueEntriesSaveHandlerTest extends BaseModuleContextSensitiveTest { | ||
|
||
private static final List<String> INITIAL_DATASET_XML = Arrays.asList( | ||
"org/openmrs/module/queue/api/dao/QueueDaoTest_locationInitialDataset.xml", | ||
"org/openmrs/module/queue/api/dao/QueueEntryDaoTest_conceptsInitialDataset.xml", | ||
"org/openmrs/module/queue/api/dao/QueueEntryDaoTest_patientInitialDataset.xml", | ||
"org/openmrs/module/queue/api/dao/VisitQueueEntryDaoTest_visitInitialDataset.xml", | ||
"org/openmrs/module/queue/api/dao/QueueDaoTest_initialDataset.xml", | ||
"org/openmrs/module/queue/api/dao/QueueEntryDaoTest_initialDataset.xml", | ||
"org/openmrs/module/queue/validators/QueueEntryValidatorTest_globalPropertyInitialDataset.xml"); | ||
|
||
private Visit visit; | ||
|
||
private QueueEntry queueEntry; | ||
|
||
private Date stopDate; | ||
|
||
@Autowired | ||
@Qualifier("queue.QueueEntryService") | ||
private QueueEntryService queueEntryService; | ||
|
||
@Autowired | ||
private VisitService visitService; | ||
|
||
@Before | ||
public void setup() { | ||
INITIAL_DATASET_XML.forEach(this::executeDataSet); | ||
queueEntry = queueEntryService.getQueueEntryById(3).get(); | ||
visit = queueEntry.getVisit(); | ||
stopDate = DateUtils.addHours(visit.getStartDatetime(), 12); | ||
} | ||
|
||
@Test | ||
public void shouldNotEndQueueEntriesIfVisitIsNotStopped() { | ||
assertNull(visit.getStopDatetime()); | ||
assertNull(queueEntry.getEndedAt()); | ||
visit = visitService.saveVisit(visit); | ||
queueEntry = queueEntryService.getQueueEntryById(queueEntry.getId()).get(); | ||
assertNull(visit.getStopDatetime()); | ||
assertNull(queueEntry.getEndedAt()); | ||
} | ||
|
||
@Test | ||
public void shouldEndQueueEntriesIfVisitIsStopped() { | ||
assertNull(visit.getStopDatetime()); | ||
assertNull(queueEntry.getEndedAt()); | ||
visit.setStopDatetime(stopDate); | ||
visit = visitService.saveVisit(visit); | ||
queueEntry = queueEntryService.getQueueEntryById(queueEntry.getId()).get(); | ||
assertThat(visit.getStopDatetime(), equalTo(stopDate)); | ||
assertThat(queueEntry.getEndedAt(), equalTo(stopDate)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume setting this to null means returns all queues, both those that are ended and those that aren't ended.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, the default (which I don't love, but the way it is implemented) is to only return active entries by default. So to get all entries, you need to explicitly null out this property :/