if (StringUtils.isBlank(nameInLocale.getName())) { updateWarnings.add("ConceptName with id " + nameInLocale.getConceptNameId() + " (" + nameInLocale.getName() + ") is null, white space character or empty string"); updateWarnings.add("ConceptName with id: " + nameInLocale.getConceptNameId() + " (" + nameInLocale.getName() + ") has a locale (" + conceptNameLocale + ") that isn't listed among the allowed ones by the system admin"); if (nameInLocale.getLocalePreferred() != null) { if (nameInLocale.getLocalePreferred() && !preferredNameForLocaleFound) { if (nameInLocale.isIndexTerm()) { nameInLocale.setLocalePreferred(false); reportUpdatedName(nameInLocale, "Preferred name '" + nameInLocale.getName() + "' in locale '" + conceptNameLocale.getDisplayName() + "' has been dropped as the preferred name because it is a search term"); } else if (nameInLocale.isShort()) { nameInLocale.setLocalePreferred(false); reportUpdatedName(nameInLocale, "Preferred name '" + nameInLocale.getName() + "' in locale '" + conceptNameLocale.getDisplayName() + "' has been dropped as the preferred name because it is a short name"); else if (nameInLocale.getLocalePreferred() && preferredNameForLocaleFound) { nameInLocale.setLocalePreferred(false); reportUpdatedName( nameInLocale, "Preferred name '" + nameInLocale.getName() + "' in locale '"
localeConceptNamesMap = new HashMap<>(); ConceptName conceptName = new ConceptName(); conceptName.setConceptNameId(rs.getInt("concept_name_id")); conceptName.setName(rs.getString("name")); conceptNameType = ConceptNameType.INDEX_TERM; conceptName.setConceptNameType(conceptNameType); conceptName.setLocale(!StringUtils.isBlank(localeString) ? LocaleUtility.fromSpecification(localeString) : null); conceptName.setLocalePreferred(rs.getBoolean("locale_preferred")); conceptName.setVoided(false); if (!localeConceptNamesMap.containsKey(conceptName.getLocale())) { localeConceptNamesMap.put(conceptName.getLocale(), new LinkedList<>()); localeConceptNamesMap.get(conceptName.getLocale()).add(conceptName);
if (name.getVoided()) { return false; if (name.getConcept() != null) { if (name.getConcept().getRetired()) { return false; if (!name.equals(name.getConcept().getName(name.getLocale()))) { return false; criteria.add(Restrictions.or(Restrictions.eq("locale", name.getLocale()), Restrictions.eq("locale", new Locale(name .getLocale().getLanguage())))); if (Context.getAdministrationService().isDatabaseStringComparisonCaseSensitive()) { criteria.add(Restrictions.eq("name", name.getName()).ignoreCase()); } else { criteria.add(Restrictions.eq("name", name.getName())); if (candidateName.getConcept().getRetired()) { continue; if (candidateName.getConcept().equals(name.getConcept())) { continue; if (candidateName.getConcept().getName(candidateName.getLocale()).equals(candidateName)) { return true;
private void makeLocaleNotPreferred(ConceptName conceptName) { if (conceptName.getLocalePreferred()) { conceptName.setLocalePreferred(false); } }
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); } }
@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()); }
/** * Utility method which loads the previous version of a conceptName to check if the name * property of the given conceptName has changed. * * @param conceptName to be modified * @return boolean indicating change in the name property */ private boolean hasNameChanged(ConceptName conceptName) { String newName = conceptName.getName(); String oldName = dao.getSavedConceptName(conceptName).getName(); return !oldName.equalsIgnoreCase(newName); }
/** * @see Concept#getShortestName(Locale,Boolean) */ @Test public void getShortestName_shouldReturnTheShortestNameInAGivenLocaleForAConceptIfExactIsTrue() { Concept concept = new Concept(); concept.addName(new ConceptName("shortName123", Context.getLocale())); concept.addName(new ConceptName("shortName12", Context.getLocale())); concept.addName(new ConceptName("shortName1", Locale.US)); concept.addName(new ConceptName("shortName", Locale.FRANCE)); Assert.assertEquals("shortName12", concept.getShortestName(Context.getLocale(), true).getName()); }
@Test public void validate_shouldPassIfTheDuplicateNameInTheLocaleForTheConceptBeingValidatedIsVoided() { ConceptName otherName = conceptService.getConceptName(1439); //sanity check since names should only be unique amongst preferred and fully specified names Assert.assertTrue(otherName.isFullySpecifiedName() || otherName.isPreferred()); Assert.assertFalse(otherName.getVoided()); Assert.assertFalse(otherName.getConcept().getRetired()); //change to a duplicate name in the same locale ConceptName duplicateName = conceptService.getConceptName(2477); duplicateName.setName(otherName.getName()); Concept concept = duplicateName.getConcept(); concept.setPreferredName(duplicateName); //ensure that the name has been marked as preferred in its locale Assert.assertEquals(duplicateName, concept.getPreferredName(duplicateName.getLocale())); Assert.assertTrue(duplicateName.isPreferred()); duplicateName.setVoided(true); Errors errors = new BindException(concept, "concept"); validator.validate(concept, errors); Assert.assertFalse(errors.hasErrors()); }
@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()); }
/** * @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); }
@Test public void validate_shouldFailIfThereIsADuplicateUnretiredConceptNameInTheSameLocaleDifferentThanTheSystemLocale() { Context.setLocale(new Locale("pl")); Locale en = new Locale("en", "GB"); Concept concept = cd4Count; Assert.assertEquals(true, concept.getFullySpecifiedName(en).isFullySpecifiedName()); String duplicateName = concept.getFullySpecifiedName(en).getName(); Concept anotherConcept = weight; anotherConcept.getFullySpecifiedName(en).setName(duplicateName); Errors errors = new BindException(anotherConcept, "concept"); expectedException.expect(DuplicateConceptNameException.class); expectedException .expectMessage("'" + duplicateName + "' is a duplicate name in locale '" + en + "'"); validator.validate(concept, errors); validator.validate(anotherConcept, errors); }
conceptName.setConcept(this); if (names == null) { names = new HashSet<>(); && !ConceptNameType.FULLY_SPECIFIED.equals(conceptName.getConceptNameType())) { conceptName.setConceptNameType(ConceptNameType.FULLY_SPECIFIED); } else { if (conceptName.isPreferred() && !conceptName.isIndexTerm() && conceptName.getLocale() != null) { ConceptName prefName = getPreferredName(conceptName.getLocale()); if (prefName != null) { prefName.setLocalePreferred(false); if (conceptName.isFullySpecifiedName() && conceptName.getLocale() != null) { ConceptName fullySpecName = getFullySpecifiedName(conceptName.getLocale()); if (fullySpecName != null) { fullySpecName.setConceptNameType(null); } else if (conceptName.isShort() && conceptName.getLocale() != null) { ConceptName shortName = getShortNameInLocale(conceptName.getLocale()); if (shortName != null) { shortName.setConceptNameType(null);
/** * 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; }
/** * @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()); }
/** * @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()); }
/** * 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); } }
@Test public void validate_shouldFailIfThereIsADuplicateUnretiredPreferredNameInTheSameLocale() { Context.setLocale(new Locale("en", "GB")); Concept concept = cd4Count; ConceptName preferredName = new ConceptName("preferred name", Context.getLocale()); concept.setPreferredName(preferredName); conceptService.saveConcept(concept); Assert.assertEquals("preferred name", concept.getPreferredName(Context.getLocale()).getName()); Concept anotherConcept = weight; anotherConcept.getFullySpecifiedName(Context.getLocale()).setName("preferred name"); Errors errors = new BindException(anotherConcept, "concept"); expectedException.expect(DuplicateConceptNameException.class); expectedException .expectMessage("'" + preferredName + "' is a duplicate name in locale '" + Context.getLocale() + "'"); validator.validate(anotherConcept, errors); }
/** * @see ConceptService#saveConcept(Concept) */ @Test public void saveConcept_shouldCreateANewConceptNameWhenTheOldNameIsChanged() { Concept concept = conceptService.getConceptByName("cd4 count"); Assert.assertEquals(3, concept.getNames(true).size()); ConceptName oldName = null; for (ConceptName cn : concept.getNames()) { if (cn.getConceptNameId().equals(1847)) { oldName = cn; cn.setName("new name"); } } conceptService.saveConcept(concept); //force Hibernate interceptor to set dateCreated Context.flushSession(); Assert.assertEquals(4, concept.getNames(true).size()); for (ConceptName cn : concept.getNames()) { if (cn.getName().equals("new name")) { Assert.assertTrue(oldName.getDateCreated().before(cn.getDateCreated())); } } }