use of org.openmrs.ConceptDescription in project openmrs-core by openmrs.
the class ConceptServiceTest method saveConcept_shouldCreateNewConceptInDatabase.
@Test
public void saveConcept_shouldCreateNewConceptInDatabase() {
executeDataSet(INITIAL_CONCEPTS_XML);
Concept conceptToAdd = new Concept();
ConceptName cn = new ConceptName("new name", Context.getLocale());
conceptToAdd.addName(cn);
conceptToAdd.addDescription(new ConceptDescription("some description", null));
conceptToAdd.setDatatype(new ConceptDatatype(1));
conceptToAdd.setConceptClass(new ConceptClass(1));
assertFalse(conceptService.getAllConcepts().contains(conceptToAdd));
conceptService.saveConcept(conceptToAdd);
assertTrue(conceptService.getAllConcepts().contains(conceptToAdd));
}
use of org.openmrs.ConceptDescription in project openmrs-core by openmrs.
the class ConceptServiceTest method saveConcept_shouldSetAuditInfoIfAnItemIsAddedToAnyOfItsChildCollections.
/**
* @see ConceptService#saveConcept(Concept)
*/
@Test
public void saveConcept_shouldSetAuditInfoIfAnItemIsAddedToAnyOfItsChildCollections() {
Concept concept = conceptService.getConcept(3);
Assert.assertNull(concept.getDateChanged());
Assert.assertNull(concept.getChangedBy());
ConceptDescription description = new ConceptDescription("new description", Context.getLocale());
concept.addDescription(description);
conceptService.saveConcept(concept);
Assert.assertNotNull(description.getConceptDescriptionId());
Assert.assertNotNull(concept.getDateChanged());
Assert.assertNotNull(concept.getChangedBy());
}
use of org.openmrs.ConceptDescription in project openmrs-module-pihcore by PIH.
the class ConceptDictionaryExportTest method exportData.
/**
* Intentional issues currently with this, in order to isolate away known issues. These should be removed as appropriate:
* - Normalizes concept source name, due to known issue needed in OCL to facilitate source matching
* - Excludes voided concept names (which are really retired names that may have references), as these are not provided by ocl
* - Converts answer sort weight from null -> 1.0 if only one answer exists for the concept
*/
@Test
public void exportData() throws Exception {
// Only run this test if it is being run alone. This ensures this test will not run in normal build.
if (props == null) {
return;
}
if (!Context.isSessionOpen()) {
Context.openSession();
}
authenticate();
List<Map<String, Object>> concepts = new ArrayList<>();
System.out.println("Pulling concepts");
int i = 0;
List<Concept> conceptList = Context.getConceptService().getAllConcepts();
conceptList.sort(Comparator.comparing(t -> t.getUuid().toUpperCase()));
List<Concept> conceptsToIgnore = getConceptsToIgnore();
conceptList.removeAll(conceptsToIgnore);
for (Concept concept : conceptList) {
System.out.println(i++ + ": Pulling concept: " + concept.getUuid());
Map<String, Object> c = new LinkedHashMap<>();
concepts.add(c);
c.put("uuid", concept.getUuid());
c.put("concept_class", concept.getConceptClass().getName());
c.put("concept_datatype", concept.getDatatype().getName());
c.put("is_set", BooleanUtils.isTrue(concept.getSet()) ? "true" : "false");
c.put("version", concept.getVersion() == null ? "" : concept.getVersion());
c.put("retired", BooleanUtils.isTrue(concept.getRetired()) ? "true" : "false");
c.put("retireReason", concept.getRetireReason() == null ? "" : concept.getRetireReason());
if (concept instanceof ConceptNumeric) {
ConceptNumeric cn = (ConceptNumeric) concept;
c.put("hi_absolute", cn.getHiAbsolute() == null ? "" : cn.getHiAbsolute().toString());
c.put("hi_critical", cn.getLowAbsolute() == null ? "" : cn.getLowAbsolute().toString());
c.put("hi_normal", cn.getHiNormal() == null ? "" : cn.getHiNormal().toString());
c.put("low_absolute", cn.getLowAbsolute() == null ? "" : cn.getLowAbsolute().toString());
c.put("low_critical", cn.getLowCritical() == null ? "" : cn.getLowCritical().toString());
c.put("low_normal", cn.getLowNormal() == null ? "" : cn.getLowNormal().toString());
c.put("units", cn.getUnits() == null ? "" : cn.getUnits());
c.put("allow_decimal", BooleanUtils.isTrue(cn.getAllowDecimal()) ? "true" : "false");
c.put("display_precision", cn.getHiAbsolute() == null ? "" : cn.getHiAbsolute().toString());
}
if (concept instanceof ConceptComplex) {
ConceptComplex cc = (ConceptComplex) concept;
c.put("handler", cc.getHandler());
}
List<ConceptName> names = new ArrayList<>(concept.getNames(true));
names.sort(Comparator.comparing(ConceptName::getName).thenComparing(cn -> cn.getConceptNameType() == null ? "" : cn.getConceptNameType().name()).thenComparing(cn -> cn.getLocale().toString()).thenComparing(ConceptName::getLocalePreferred));
List<Map<String, Object>> nameList = new ArrayList<>();
for (ConceptName name : names) {
if (BooleanUtils.isNotTrue(name.getVoided())) {
Map<String, Object> n = new LinkedHashMap<>();
n.put("name", name.getName());
n.put("locale", name.getLocale().toString());
n.put("locale_preferred", BooleanUtils.isTrue(name.getLocalePreferred()) ? "true" : "false");
n.put("type", name.getConceptNameType() == null ? "" : name.getConceptNameType().name());
n.put("voided", BooleanUtils.isTrue(name.getVoided()) ? "true" : "false");
nameList.add(n);
}
}
c.put("names", nameList);
List<ConceptDescription> descriptions = new ArrayList<>(concept.getDescriptions());
descriptions.sort(Comparator.comparing(ConceptDescription::getDescription));
List<Map<String, Object>> descriptionList = new ArrayList<>();
for (ConceptDescription description : descriptions) {
Map<String, Object> n = new LinkedHashMap<>();
n.put("description", description.getDescription());
n.put("locale", description.getLocale().toString());
descriptionList.add(n);
}
c.put("descriptions", descriptionList);
List<ConceptAnswer> answers = new ArrayList<>(concept.getAnswers(true));
answers.sort(Comparator.comparingDouble(ConceptAnswer::getSortWeight).thenComparing(ca -> ca.getAnswerConcept().getUuid()));
List<Map<String, Object>> answerList = new ArrayList<>();
for (ConceptAnswer ca : answers) {
if (!conceptsToIgnore.contains(ca.getAnswerConcept())) {
Map<String, Object> n = new LinkedHashMap<>();
n.put("answerConcept", ca.getAnswerConcept().getUuid());
n.put("sortWeight", ca.getSortWeight() == null ? (answers.size() == 1 ? "1.0" : "") : ca.getSortWeight().toString());
answerList.add(n);
}
}
c.put("answers", answerList);
List<ConceptSet> setMembers = new ArrayList<>(concept.getConceptSets());
setMembers.sort(Comparator.comparingDouble(ConceptSet::getSortWeight).thenComparing(cs -> cs.getConcept().getUuid()));
List<Map<String, Object>> setMemberList = new ArrayList<>();
for (ConceptSet cs : setMembers) {
if (!conceptsToIgnore.contains(cs.getConcept())) {
Map<String, Object> n = new LinkedHashMap<>();
n.put("concept", cs.getConcept().getUuid());
n.put("sortWeight", cs.getSortWeight() == null ? "" : cs.getSortWeight().toString());
setMemberList.add(n);
}
}
c.put("setMembers", setMemberList);
List<ConceptMap> mappings = new ArrayList<>(concept.getConceptMappings());
mappings.sort(Comparator.comparing((ConceptMap m) -> m.getConceptReferenceTerm().getConceptSource().getName()).thenComparing(m -> m.getConceptMapType().getName()).thenComparing(m -> m.getConceptReferenceTerm().getCode()));
List<Map<String, Object>> mappingList = new ArrayList<>();
for (ConceptMap cm : mappings) {
Map<String, Object> n = new LinkedHashMap<>();
n.put("source", normalizeSourceName(cm.getConceptReferenceTerm().getConceptSource().getName()));
n.put("type", cm.getConceptMapType().getName());
n.put("code", cm.getConceptReferenceTerm().getCode());
mappingList.add(n);
}
c.put("mappings", mappingList);
}
File exportFile = new File(EXPORT_DIR, EXPORT_FILENAME);
System.out.println("Writing concepts to: " + exportFile);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writerWithDefaultPrettyPrinter().writeValue(exportFile, concepts);
System.out.println("Concepts exported successfully");
}
use of org.openmrs.ConceptDescription in project openmrs-core by openmrs.
the class ConceptServiceTest method saveConcept_shouldNotFailWhenADuplicateNameIsEditedToAUniqueValue.
/**
* @see ConceptService#saveConcept(Concept)
*/
@Test
public void saveConcept_shouldNotFailWhenADuplicateNameIsEditedToAUniqueValue() {
// Insert a row to simulate an existing duplicate fully specified/preferred name that needs to be edited
executeDataSet("org/openmrs/api/include/ConceptServiceTest-conceptWithDuplicateName.xml");
Concept conceptToEdit = conceptService.getConcept(10000);
Locale locale = new Locale("en", "GB");
conceptToEdit.addDescription(new ConceptDescription("some description", locale));
ConceptName duplicateNameToEdit = conceptToEdit.getFullySpecifiedName(locale);
// Ensure the name is a duplicate in it's locale
Concept otherConcept = conceptService.getConcept(5497);
Assert.assertTrue(duplicateNameToEdit.getName().equalsIgnoreCase(otherConcept.getFullySpecifiedName(locale).getName()));
duplicateNameToEdit.setName("new unique name");
conceptService.saveConcept(conceptToEdit);
}
use of org.openmrs.ConceptDescription in project openmrs-core by openmrs.
the class ConceptServiceTest method saveConcept_shouldSaveAConceptNumericAsAConcept.
/**
* @see ConceptService#saveConcept(Concept)
*/
@Test
public void saveConcept_shouldSaveAConceptNumericAsAConcept() {
executeDataSet(INITIAL_CONCEPTS_XML);
// This will automatically add the given locale to the list of allowed locales
Context.setLocale(Locale.US);
// this tests saving a previously conceptnumeric as just a concept
Concept c2 = new Concept(2);
ConceptName cn = new ConceptName("not a numeric anymore", Locale.US);
c2.addName(cn);
c2.addDescription(new ConceptDescription("some description", null));
c2.setDatatype(new ConceptDatatype(3));
c2.setConceptClass(new ConceptClass(1));
conceptService.saveConcept(c2);
Concept secondConcept = conceptService.getConcept(2);
// this will probably still be a ConceptNumeric object. what to do about that?
// revisit this problem when discriminators are in place
// assertFalse(secondConcept instanceof ConceptNumeric);
// this shouldn't think its a conceptnumeric object though
assertFalse(secondConcept.isNumeric());
assertEquals("not a numeric anymore", secondConcept.getName(Locale.US).getName());
}
Aggregations