use of org.openmrs.Drug in project openmrs-core by openmrs.
the class ConceptAnswersEditor method setAsText.
/**
* loops over the textbox assigned to this property. The textbox is assumed to be a string of
* conceptIds^drugIds separated by spaces.
*
* @param text list of conceptIds (not conceptAnswerIds)
* @should set the sort weights with the least possible changes
*/
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (StringUtils.hasText(text)) {
ConceptService cs = Context.getConceptService();
String[] conceptIds = text.split(" ");
List<String> requestConceptIds = new ArrayList<>();
// set up parameter answer Set for easier add/delete functions and removal of duplicates
for (String id : conceptIds) {
id = id.trim();
if (!("".equals(id)) && !requestConceptIds.contains(id)) {
// remove whitespace, blank lines, and duplicates
requestConceptIds.add(id);
}
}
Collection<ConceptAnswer> deletedConceptAnswers = new HashSet<>();
// loop over original concept answers to find any deleted answers
for (ConceptAnswer origConceptAnswer : originalConceptAnswers) {
boolean answerDeleted = true;
for (String conceptId : requestConceptIds) {
Integer id = getConceptId(conceptId);
Integer drugId = getDrugId(conceptId);
Drug answerDrug = origConceptAnswer.getAnswerDrug();
if (id.equals(origConceptAnswer.getAnswerConcept().getConceptId())) {
if (drugId == null && answerDrug == null) {
answerDeleted = false;
} else if ((drugId != null && answerDrug != null) && drugId.equals(origConceptAnswer.getAnswerDrug().getDrugId())) {
answerDeleted = false;
}
}
}
if (answerDeleted) {
deletedConceptAnswers.add(origConceptAnswer);
}
}
// loop over those deleted answers to delete them
for (ConceptAnswer conceptAnswer : deletedConceptAnswers) {
originalConceptAnswers.remove(conceptAnswer);
}
// loop over concept ids in the request to add any that are new
for (String conceptId : requestConceptIds) {
Integer id = getConceptId(conceptId);
Integer drugId = getDrugId(conceptId);
boolean newAnswerConcept = true;
for (ConceptAnswer origConceptAnswer : originalConceptAnswers) {
Drug answerDrug = origConceptAnswer.getAnswerDrug();
if (id.equals(origConceptAnswer.getAnswerConcept().getConceptId())) {
if (drugId == null && answerDrug == null) {
newAnswerConcept = false;
} else if ((drugId != null && answerDrug != null) && drugId.equals(answerDrug.getDrugId())) {
newAnswerConcept = false;
}
}
}
// if the current request answer is new, add it to the originals
if (newAnswerConcept) {
Concept answer = cs.getConcept(id);
Drug drug = null;
if (drugId != null) {
drug = cs.getDrug(drugId);
}
ConceptAnswer ac = new ConceptAnswer(answer, drug);
originalConceptAnswers.add(ac);
}
}
// loop over to set the order
// as the list comes into 'requestConceptIds' in the order the user wants
// there are 2 conditions that will require the sort_weights to be reassigned
// 1) any ConceptAnswer.sortWeight == NULL (meaning it is just added)
// 2) the list is not in ASCENDING order (example sort order of the list is 1, 2, 10, 9)
// -startIdx (start index) is where in this list we will start to reassign the sort_weights
Double lastWeightSeen = null;
// the idx to start at, if we have a NULL sort weight (new concept answer) or sort weights are not ascending
int startIdx = -1;
for (int i = 0; i < requestConceptIds.size() - 1; i++) {
Integer id1 = getConceptId(requestConceptIds.get(i));
ConceptAnswer ca1 = getConceptAnswerFromOriginal(id1);
if (ca1.getSortWeight() == null) {
if (lastWeightSeen == null) {
// start at 1, we're at the beginning
lastWeightSeen = 1d;
} else {
// we start at +1
lastWeightSeen += 1;
}
startIdx = i;
break;
}
Integer id2 = getConceptId(requestConceptIds.get(i + 1));
ConceptAnswer ca2 = getConceptAnswerFromOriginal(id2);
int c = ca1.compareTo(ca2);
if (c > 0) {
startIdx = i;
lastWeightSeen = ca1.getSortWeight();
break;
}
lastWeightSeen = ca1.getSortWeight();
}
if (startIdx != -1) {
// then we need to re-weight
for (int i = startIdx; i < requestConceptIds.size(); i++) {
Integer id = getConceptId(requestConceptIds.get(i));
ConceptAnswer ca = getConceptAnswerFromOriginal(id);
ca.setSortWeight(lastWeightSeen++);
}
}
log.debug("originalConceptAnswers.getConceptId(): ");
for (ConceptAnswer a : originalConceptAnswers) {
log.debug("id: " + a.getAnswerConcept().getConceptId());
}
log.debug("requestConceptIds: ");
for (String i : requestConceptIds) {
log.debug("id: " + i);
}
} else {
originalConceptAnswers.clear();
}
setValue(originalConceptAnswers);
}
use of org.openmrs.Drug in project openmrs-core by openmrs.
the class DrugsByNameComparatorTest method compareDrugNamesIgnoringNumericals_shouldReturnZeroIfNameForDrug1ComesBeforeThatOfDrug2.
/**
* @see DrugsByNameComparator#compareDrugNamesIgnoringNumericals(Drug,Drug)
*/
@Test
public void compareDrugNamesIgnoringNumericals_shouldReturnZeroIfNameForDrug1ComesBeforeThatOfDrug2() {
Drug drug1 = new Drug();
drug1.setName("ABCD");
Drug drug2 = new Drug();
drug2.setName("abcd");
DrugsByNameComparator dComparator = new DrugsByNameComparator();
int actualValue = dComparator.compare(drug1, drug2);
Assert.assertEquals(actualValue, 0);
}
use of org.openmrs.Drug in project openmrs-core by openmrs.
the class DrugsByNameComparatorTest method compareDrugNamesIgnoringNumericals_shouldReturnPositiveIfNameForDrug1ComesBeforeThatOfDrug2IgnoringNumerics.
/**
* @see DrugsByNameComparator#compareDrugNamesIgnoringNumericals(Drug,Drug)
*/
@Test
public void compareDrugNamesIgnoringNumericals_shouldReturnPositiveIfNameForDrug1ComesBeforeThatOfDrug2IgnoringNumerics() {
Drug drug1 = new Drug();
drug1.setName("AB1AB");
Drug drug2 = new Drug();
drug2.setName("ABAA");
DrugsByNameComparator dComparator = new DrugsByNameComparator();
int actualValue = dComparator.compare(drug1, drug2);
Assert.assertEquals(actualValue, 1);
}
use of org.openmrs.Drug in project openmrs-core by openmrs.
the class OrderServiceTest method saveOrder_shouldSetAutoExpireDateOfDrugOrderIfAutoExpireDateIsNotSet.
@Test
public void saveOrder_shouldSetAutoExpireDateOfDrugOrderIfAutoExpireDateIsNotSet() throws ParseException {
executeDataSet("org/openmrs/api/include/OrderServiceTest-drugOrderAutoExpireDate.xml");
Drug drug = conceptService.getDrug(3000);
DrugOrder drugOrder = new DrugOrder();
Encounter encounter = encounterService.getEncounter(3);
drugOrder.setEncounter(encounter);
drugOrder.setPatient(patientService.getPatient(7));
drugOrder.setCareSetting(orderService.getCareSetting(1));
drugOrder.setOrderer(Context.getProviderService().getProvider(1));
drugOrder.setDrug(drug);
drugOrder.setDosingType(SimpleDosingInstructions.class);
drugOrder.setDose(300.0);
drugOrder.setDoseUnits(conceptService.getConcept(50));
drugOrder.setQuantity(20.0);
drugOrder.setQuantityUnits(conceptService.getConcept(51));
drugOrder.setFrequency(orderService.getOrderFrequency(3));
drugOrder.setRoute(conceptService.getConcept(22));
drugOrder.setNumRefills(0);
drugOrder.setOrderType(null);
drugOrder.setDateActivated(TestUtil.createDateTime("2014-08-03"));
// 20 days
drugOrder.setDuration(20);
drugOrder.setDurationUnits(conceptService.getConcept(1001));
Order savedOrder = orderService.saveOrder(drugOrder, null);
Order loadedOrder = orderService.getOrder(savedOrder.getId());
Assert.assertEquals(TestUtil.createDateTime("2014-08-22 23:59:59"), loadedOrder.getAutoExpireDate());
}
use of org.openmrs.Drug in project openmrs-core by openmrs.
the class OrderServiceTest method saveOrder_shouldFailIfTheExistingDrugOrderMatchesTheConceptAndNotDrugOfTheDCOrder.
/**
* @see OrderService#saveOrder(org.openmrs.Order, OrderContext)
*/
@Test
public void saveOrder_shouldFailIfTheExistingDrugOrderMatchesTheConceptAndNotDrugOfTheDCOrder() {
final DrugOrder orderToDiscontinue = (DrugOrder) orderService.getOrder(5);
assertTrue(OrderUtilTest.isActiveOrder(orderToDiscontinue, null));
// create a different test drug
Drug discontinuationOrderDrug = new Drug();
discontinuationOrderDrug.setConcept(orderToDiscontinue.getConcept());
discontinuationOrderDrug = conceptService.saveDrug(discontinuationOrderDrug);
assertNotEquals(discontinuationOrderDrug, orderToDiscontinue.getDrug());
assertNotNull(orderToDiscontinue.getDrug());
DrugOrder order = orderToDiscontinue.cloneForRevision();
order.setDateActivated(new Date());
order.setOrderer(providerService.getProvider(1));
order.setEncounter(encounterService.getEncounter(6));
order.setDrug(discontinuationOrderDrug);
order.setOrderReasonNonCoded("Discontinue this");
expectedException.expect(EditedOrderDoesNotMatchPreviousException.class);
expectedException.expectMessage("The orderable of the previous order and the new one order don't match");
orderService.saveOrder(order, null);
}
Aggregations