private void makeVoidedNameSynonym(ConceptName conceptName) { // Helps to avoid having multiple fully // specified or preferred names in a locale // in case the name is unvoided if (!conceptName.isSynonym()) { conceptName.setConceptNameType(null); } }
/** * Sets the specified name as the short name for the locale and the current shortName(if any) * ceases to be the short name for the locale. * * @param shortName the new shortName to set * @should set the concept name type of the specified name to short * @should convert the previous shortName if any to a synonym * @should add the name to the list of names if it not among them before * @should void old short name if new one is blank (do not save blanks!) */ public void setShortName(ConceptName shortName) { if (shortName != null) { if (shortName.getLocale() == null) { throw new APIException(CONCEPT_NAME_LOCALE_NULL, (Object[]) null); } ConceptName oldShortName = getShortNameInLocale(shortName.getLocale()); if (oldShortName != null) { oldShortName.setConceptNameType(null); } shortName.setConceptNameType(ConceptNameType.SHORT); if (StringUtils.isNotBlank(shortName.getName()) && (shortName.getConceptNameId() == null || !getNames().contains(shortName))) { //add this name, if it is new or not among this concept's names addName(shortName); } } else { throw new APIException("Concept.error.shortName.null", (Object[]) null); } }
/** * Sets the specified name as the fully specified name for the locale and the current fully * specified (if any) ceases to be the fully specified name for the locale. * * @param fullySpecifiedName the new fully specified name to set * @should set the concept name type of the specified name to fully specified * @should convert the previous fully specified name if any to a synonym * @should add the name to the list of names if it not among them before */ public void setFullySpecifiedName(ConceptName fullySpecifiedName) { if (fullySpecifiedName == null || fullySpecifiedName.getLocale() == null) { throw new APIException(CONCEPT_NAME_LOCALE_NULL, (Object[]) null); } else if (fullySpecifiedName.getVoided()) { throw new APIException("Concept.error.fullySpecifiedName.null", (Object[]) null); } ConceptName oldFullySpecifiedName = getFullySpecifiedName(fullySpecifiedName.getLocale()); if (oldFullySpecifiedName != null) { oldFullySpecifiedName.setConceptNameType(null); } fullySpecifiedName.setConceptNameType(ConceptNameType.FULLY_SPECIFIED); //add this name, if it is new or not among this concept's names if (fullySpecifiedName.getConceptNameId() == null || !getNames().contains(fullySpecifiedName)) { addName(fullySpecifiedName); } }
@Test public void getShortNameInLocale_shouldReturnTheBestShortNameForAConcept() { Concept concept = new Concept(); concept.addName(new ConceptName("Giant cat", new Locale("en"))); concept.addName(new ConceptName("Gato gigante", new Locale("es", "MX"))); ConceptName shortName1 = new ConceptName("Cat", new Locale("en")); shortName1.setConceptNameType(ConceptNameType.SHORT); concept.addName(shortName1); ConceptName shortName2 = new ConceptName("Gato", new Locale("es")); shortName2.setConceptNameType(ConceptNameType.SHORT); concept.addName(shortName2); Assert.assertEquals("Gato", concept.getShortNameInLocale(new Locale("es", "ES")).getName()); }
@Test public void validate_shouldFailIfThereIsNoNameExplicitlyMarkedAsFullySpecified() { Concept concept = cd4Count; for (ConceptName name : concept.getNames()) { name.setConceptNameType(null); } Errors errors = new BindException(concept, "concept"); validator.validate(concept, errors); assertThat(errors, hasGlobalErrors("Concept.error.no.FullySpecifiedName")); }
@Test public void getSynonyms_shouldSortPreferredFirst() { Concept concept = new Concept(); ConceptName conceptNameNotPreferred = new ConceptName("Non Preferred", Locale.ENGLISH); ConceptName conceptNameNotPreferred2 = new ConceptName("Non Preferred2", Locale.ENGLISH); ConceptName conceptNamePreferred = new ConceptName("Preferred", Locale.ENGLISH); conceptNamePreferred.setLocalePreferred(true); concept.addName(conceptNameNotPreferred); concept.addName(conceptNameNotPreferred2); concept.addName(conceptNamePreferred); conceptNameNotPreferred.setConceptNameType(null); conceptNameNotPreferred2.setConceptNameType(null); conceptNamePreferred.setConceptNameType(null); ConceptName conceptNameExpectedPreferred = concept.getSynonyms(Locale.ENGLISH).iterator().next(); assertEquals("Preferred", conceptNameExpectedPreferred.getName()); }
@Test public void validate_shouldPassIfFullySpecifiedNameIsTheSameAsShortName() { ConceptName conceptFullySpecifiedName = new ConceptName("YES", new Locale("pl")); conceptFullySpecifiedName.setConceptNameType(ConceptNameType.FULLY_SPECIFIED); ConceptName conceptShortName = new ConceptName("yes", new Locale("pl")); conceptShortName.setConceptNameType(ConceptNameType.SHORT); concept.addName(conceptFullySpecifiedName); concept.addName(conceptShortName); concept.addDescription(new ConceptDescription("some description",null)); concept.setConceptClass(new ConceptClass()); concept.setDatatype(new ConceptDatatype()); validator.validate(concept, errors); Assert.assertFalse(errors.hasErrors()); }
/** * @see ConceptServiceImpl#saveConcept(Concept) */ @Test public void saveConcept_shouldNotSetDefaultPreferredNameToShortOrIndexTerms() { Locale loc = new Locale("fr", "CA"); ConceptName shortName = new ConceptName("short name", loc); shortName.setConceptNameType(ConceptNameType.SHORT); //be explicit for test case ConceptName indexTerm = new ConceptName("indexTerm", loc); indexTerm.setConceptNameType(ConceptNameType.INDEX_TERM); //synonyms are id'd by a null type Concept c = new Concept(); HashSet<ConceptName> allNames = new HashSet<>(4); allNames.add(indexTerm); allNames.add(shortName); c.setNames(allNames); //The API will throw a validation error because preferred name is an index term //ignore it so we can test the set default preferred name functionality try { Context.getConceptService().saveConcept(c); } catch (org.openmrs.api.APIException e) { //ignore it } assertNull("there's a preferred name", c.getPreferredName(loc)); assertFalse("name was explicitly marked preferred", shortName.isPreferred()); assertFalse("name was explicitly marked preferred", indexTerm.isPreferred()); }
/** * @see Concept#getName(Locale,null) */ @Test public void getName_shouldReturnExactNameLocaleMatchGivenExactEqualsTrue() { Locale definedNameLocale = new Locale("en", "US"); Locale localeToSearch = new Locale("en", "US"); Concept concept = new Concept(); ConceptName fullySpecifiedName = new ConceptName("some name", definedNameLocale); fullySpecifiedName.setConceptNameId(1); fullySpecifiedName.setConceptNameType(ConceptNameType.FULLY_SPECIFIED); fullySpecifiedName.setLocalePreferred(false); concept.addName(fullySpecifiedName); Assert.assertNotNull(concept.getName(localeToSearch, true)); Assert.assertEquals("some name", concept.getName(localeToSearch, true).getName()); }
Locale loc = new Locale("fr", "CA"); ConceptName fullySpecifiedName = new ConceptName("fully specified", loc); fullySpecifiedName.setConceptNameType(ConceptNameType.FULLY_SPECIFIED); //be explicit for test case ConceptName shortName = new ConceptName("short name", loc); shortName.setConceptNameType(ConceptNameType.SHORT); //be explicit for test case ConceptName synonym = new ConceptName("synonym", loc); synonym.setConceptNameType(null); //synonyms are id'd by a null type ConceptName indexTerm = new ConceptName("indexTerm", loc); indexTerm.setConceptNameType(ConceptNameType.INDEX_TERM); //synonyms are id'd by a null type
fullySpecifiedName.setConceptNameType(ConceptNameType.FULLY_SPECIFIED); //be explicit for test case ConceptName shortName = new ConceptName("short name", loc); shortName.setConceptNameType(ConceptNameType.SHORT); //be explicit for test case ConceptName synonym = new ConceptName("synonym", loc); synonym.setConceptNameType(null); //synonyms are id'd by a null type ConceptName indexTerm = new ConceptName("indexTerm", loc); indexTerm.setConceptNameType(ConceptNameType.INDEX_TERM); //synonyms are id'd by a null type
@Test public void validate_shouldPassIfDifferentConceptsHaveTheSameShortNames() { Context.setLocale(new Locale("en", "GB")); List<Concept> concepts = conceptService.getConceptsByName("HSM"); Assert.assertEquals(1, concepts.size()); Assert.assertEquals(true, concepts.get(0).getShortNameInLocale(Context.getLocale()).getName() .equalsIgnoreCase("HSM")); concept.setConceptClass(new ConceptClass()); concept.setDatatype(new ConceptDatatype()); ConceptName conceptFullySpecifiedName = new ConceptName("holosystolic murmur", Context.getLocale()); conceptFullySpecifiedName.setConceptNameType(ConceptNameType.FULLY_SPECIFIED); ConceptName conceptShortName = new ConceptName("HSM", Context.getLocale()); conceptShortName.setConceptNameType(ConceptNameType.SHORT); concept.addName(conceptFullySpecifiedName); concept.addName(conceptShortName); concept.addDescription(new ConceptDescription("some description",null)); validator.validate(concept, errors); Assert.assertFalse(errors.hasErrors()); }
/** * @see Concept#addName(ConceptName) */ @Test public void addName_shouldReplaceTheOldShortNameWithACurrentOne() { Concept concept = new Concept(); ConceptName oldShortName = new ConceptName("some name", Context.getLocale()); oldShortName.setConceptNameType(ConceptNameType.SHORT); concept.addName(oldShortName); ConceptName newShortName = new ConceptName("new name", Context.getLocale()); newShortName.setConceptNameType(ConceptNameType.SHORT); concept.addName(newShortName); Assert.assertEquals(false, oldShortName.isShort()); Assert.assertEquals("new name", concept.getShortNameInLocale(Context.getLocale()).getName()); }
@Test //TRUNK-4967 public void isConceptNameDuplicate_shouldNotFailIfConceptDoesNotHaveADefaultNameForLocale() { //given ConceptClass diagnosis = dao.getConceptClasses("Diagnosis").get(0); ConceptDatatype na = dao.getConceptDatatypeByName("N/A"); Concept tuberculosis = new Concept(); tuberculosis.addName(new ConceptName("Tuberculosis", Locale.US)); tuberculosis.setDatatype(na); tuberculosis.setConceptClass(diagnosis); dao.saveConcept(tuberculosis); ConceptName shortName = new ConceptName("TB", Locale.FRANCE); shortName.setConceptNameType(ConceptNameType.SHORT); tuberculosis.addName(shortName); //when boolean duplicate = dao.isConceptNameDuplicate(shortName); //then //no NPE exception thrown assertThat(duplicate, is(false)); }
/** * @see Concept#setPreferredName(ConceptName) */ @Test(expected = APIException.class) public void setPreferredName_shouldFailIfThePreferredNameToSetToIsAnIndexTerm() { Concept concept = new Concept(); concept.addName(new ConceptName("some name", Context.getLocale())); ConceptName preferredName = new ConceptName("some pref name", Context.getLocale()); preferredName.setLocalePreferred(true); preferredName.setConceptNameType(ConceptNameType.INDEX_TERM); concept.setPreferredName(preferredName); }
/** * Convenient factory method to create a populated Concept name. * * @param id id for the conceptName * @param locale the locale or context locale if null * @param conceptNameType the conceptNameType of the concept * @param isLocalePreferred if this name should be marked as preferred in its locale */ private ConceptName createConceptName(int id, String name, Locale locale, ConceptNameType conceptNameType, Boolean isLocalePreferred) { ConceptName result = new ConceptName(); result.setConceptNameId(id); result.setName(name); if (locale == null) { result.setLocale(Context.getLocale()); } else { result.setLocale(locale); } result.setConceptNameType(conceptNameType); result.setLocalePreferred(isLocalePreferred); return result; }
Locale loc = new Locale("fr", "CA"); ConceptName fullySpecifiedName = new ConceptName("fully specified", loc); fullySpecifiedName.setConceptNameType(ConceptNameType.FULLY_SPECIFIED); //be explicit for test case ConceptName shortName = new ConceptName("short name", loc); shortName.setConceptNameType(ConceptNameType.SHORT); //be explicit for test case ConceptName synonym = new ConceptName("synonym", loc); synonym.setConceptNameType(null); //synonyms are id'd by a null type ConceptName indexTerm = new ConceptName("indexTerm", loc); indexTerm.setConceptNameType(ConceptNameType.INDEX_TERM); //synonyms are id'd by a null type
@Test public void validate_shouldPassIfTheConceptHasASynonymThatIsAlsoAShortName() { concept.addName(new ConceptName("CD4", Context.getLocale())); concept.addDescription(new ConceptDescription("some description",null)); concept.setConceptClass(new ConceptClass()); concept.setDatatype(new ConceptDatatype()); // Add the short name. Because the short name is not counted as a Synonym. // ConceptValidator will not record any errors. ConceptName name = new ConceptName("CD4", Context.getLocale()); name.setConceptNameType(ConceptNameType.SHORT); concept.addName(name); validator.validate(concept, errors); Assert.assertFalse(errors.hasErrors()); }
/** * @see Concept#addName(ConceptName) */ @Test public void addName_shouldReplaceTheOldFullySpecifiedNameWithACurrentOne() { Concept concept = new Concept(); ConceptName oldFullySpecName = new ConceptName("some name", Context.getLocale()); concept.addName(oldFullySpecName); Assert.assertEquals(true, oldFullySpecName.isFullySpecifiedName()); ConceptName newFullySpecName = new ConceptName("new name", Context.getLocale()); newFullySpecName.setConceptNameType(ConceptNameType.FULLY_SPECIFIED); concept.addName(newFullySpecName); Assert.assertEquals(false, oldFullySpecName.isFullySpecifiedName()); Assert.assertEquals("new name", concept.getFullySpecifiedName(Context.getLocale()).getName()); }
cn1b.setConceptNameType(ConceptNameType.FULLY_SPECIFIED); cn1b.setLocalePreferred(true); c1.addName(cn1b);