public ConceptName(String name, Locale locale) { setName(name); setLocale(locale); }
conceptName.setName(rs.getString("name"));
private Concept createConceptWithName(String name) { Concept concept = new Concept(new Random().nextInt()); ConceptName conceptName = new ConceptName(); conceptName.setName(name); conceptName.setLocale(Context.getLocale()); conceptName.setLocalePreferred(true); concept.addName(conceptName); return concept; }
@Test public void validate_shouldPassIfTheDuplicateConceptNameIsNeitherPreferredNorFullySpecified() { Context.setLocale(new Locale("en", "GB")); Concept concept = cd4Count; //use a synonym as the duplicate name ConceptName duplicateName = concept.getSynonyms(Context.getLocale()).iterator().next(); Assert.assertEquals(true, duplicateName.isSynonym()); Concept anotherConcept = weight; anotherConcept.getFullySpecifiedName(Context.getLocale()).setName(duplicateName.getName()); Errors errors = new BindException(anotherConcept, "concept"); validator.validate(anotherConcept, errors); Assert.assertEquals(false, errors.hasErrors()); }
/** * @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())); } } }
/** * @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); }
/** * @see ConceptService#saveConcept(Concept) */ @Test public void saveConcept_shouldVoidTheConceptNameIfTheTextOfTheNameHasChanged() { Concept concept = conceptService.getConceptByName("cd4 count"); Assert.assertEquals(false, conceptService.getConceptName(1847).getVoided()); for (ConceptName cn : concept.getNames()) { if (cn.getConceptNameId().equals(1847)) { cn.setName("new name"); } } //ensure that the conceptName has actually been found and replaced Assert.assertEquals(true, concept.hasName("new name", new Locale("en", "GB"))); conceptService.saveConcept(concept); Assert.assertEquals(true, conceptService.getConceptName(1847).getVoided()); }
@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); }
/** * 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; }
@Test public void validate_shouldPassIfTheConceptWithADuplicateNameIsRetired() { Context.setLocale(new Locale("en", "GB")); Concept concept = cd4Count; concept.setRetired(true); conceptService.saveConcept(concept); String duplicateName = concept.getFullySpecifiedName(Context.getLocale()).getName(); Concept anotherConcept = weight; anotherConcept.getFullySpecifiedName(Context.getLocale()).setName(duplicateName); Errors errors = new BindException(anotherConcept, "concept"); validator.validate(anotherConcept, errors); Assert.assertFalse(errors.hasErrors()); }
@Test public void validate_shouldPassIfTheConceptBeingValidatedIsRetiredAndHasADuplicateName() { Context.setLocale(new Locale("en", "GB")); Concept concept = cd4Count; conceptService.saveConcept(concept); String duplicateName = concept.getFullySpecifiedName(Context.getLocale()).getName(); Concept anotherConcept = weight; anotherConcept.setRetired(true); anotherConcept.getFullySpecifiedName(Context.getLocale()).setName(duplicateName); Errors errors = new BindException(anotherConcept, "concept"); validator.validate(anotherConcept, errors); Assert.assertFalse(errors.hasErrors()); }
@Test public void validate_shouldFailIfThereIsADuplicateUnretiredFullySpecifiedNameInTheSameLocale() { Context.setLocale(new Locale("en", "GB")); Assert.assertEquals(true, cd4Count.getFullySpecifiedName(Context.getLocale()).isFullySpecifiedName()); String duplicateName = cd4Count.getFullySpecifiedName(Context.getLocale()).getName(); Concept anotherConcept = weight; anotherConcept.getFullySpecifiedName(Context.getLocale()).setName(duplicateName); Errors errors = new BindException(anotherConcept, "concept"); expectedException.expect(DuplicateConceptNameException.class); expectedException .expectMessage("'" + duplicateName + "' is a duplicate name in locale '" + Context.getLocale() + "'"); validator.validate(anotherConcept, errors); }
@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); }
@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()); }
public List<ConceptName> getMockNamesList() { ConceptName oldName1 = new ConceptName(); oldName1.setLocale(Locale.ENGLISH); oldName1.setConceptNameType(ConceptNameType.FULLY_SPECIFIED); oldName1.setName("oldName1"); oldName1.setUuid("uuid1"); ConceptName oldName2 = new ConceptName(); oldName2.setLocale(Locale.ENGLISH); oldName2.setConceptNameType(ConceptNameType.SHORT); oldName2.setName("oldName2"); oldName2.setUuid("uuid2"); List<ConceptName> oldNames = new ArrayList<ConceptName>(); oldNames.add(oldName1); oldNames.add(oldName2); return oldNames; }
@Override public MRSConcept saveConcept(MRSConcept mrsConcept) { Concept concept = new Concept(); List<Concept> conceptList = conceptService.getConceptsByName(mrsConcept.getName().getName()); if (CollectionUtils.isNotEmpty(conceptList)) { concept = conceptList.get(0); } else{ ConceptName conceptName = new ConceptName(); conceptName.setName(mrsConcept.getName().getName()); concept.addName(conceptName); } conceptService.saveConcept(concept); eventRelay.sendEventMessage(new MotechEvent(EventKeys.CREATED_NEW_CONCEPT_SUBJECT, EventHelper.conceptParameters(mrsConcept))); return getMrsConcept(concept); }
private List<Concept> generateConceptList() { List<Concept> conceptList = new ArrayList<Concept>(); Concept concept1 = new Concept(); concept1.setConceptId(1); ConceptName concept1Name = new ConceptName(); concept1Name.setName("concept1"); concept1.addName(concept1Name); conceptList.add(concept1); Concept concept2 = new Concept(); concept2.setConceptId(2); ConceptName concept2Name = new ConceptName(); concept1Name.setName("concept2"); concept1.addName(concept2Name); conceptList.add(concept2); Concept concept3 = new Concept(); concept3.setConceptId(3); ConceptName concept3Name = new ConceptName(); concept1Name.setName("concept3"); concept1.addName(concept3Name); conceptList.add(concept3); return conceptList; }
private List<Concept> generateConceptList() { List<Concept> conceptList = new ArrayList<Concept>(); Concept concept1 = new Concept(); concept1.setConceptId(1); ConceptName concept1Name = new ConceptName(); concept1Name.setName("concept1"); concept1.addName(concept1Name); conceptList.add(concept1); Concept concept2 = new Concept(); concept2.setConceptId(2); ConceptName concept2Name = new ConceptName(); concept1Name.setName("concept2"); concept1.addName(concept2Name); conceptList.add(concept2); Concept concept3 = new Concept(); concept3.setConceptId(3); ConceptName concept3Name = new ConceptName(); concept1Name.setName("concept3"); concept1.addName(concept3Name); conceptList.add(concept3); return conceptList; }
field1.setName("Field 1"); ConceptName conceptName1 = new ConceptName(); conceptName1.setName("Should Not Be Overridden by field name"); Concept concept1 = mock(Concept.class); when(concept1.getName()).thenReturn(conceptName1); conceptName2.setName("Field 2"); Concept concept2 = mock(Concept.class); when(concept2.getName()).thenReturn(conceptName2);
@Test public void testSetNames() throws Exception { Concept instance = new Concept(); List<ConceptName> otherNames = new ArrayList<ConceptName>(); ConceptName otherName = new ConceptName(); otherName.setLocale(Locale.ENGLISH); otherName.setConceptNameType(ConceptNameType.FULLY_SPECIFIED); otherName.setName("newName"); otherName.setUuid("newUuid"); otherNames.add(otherName); ConceptResource1_8.setNames(instance, otherNames); assertEquals(instance.getNames().size(), 1); assertTrue(instance.getNames().contains(otherName)); ConceptResource1_8.setNames(instance, getMockNamesList()); assertEquals(instance.getNames().size(), 2); assertFalse(instance.getNames().contains(otherName)); otherNames.addAll(getMockNamesList()); ConceptResource1_8.setNames(instance, otherNames); assertEquals(instance.getNames().size(), 3); assertTrue(instance.getNames().contains(otherName)); ConceptResource1_8.setNames(instance, getMockNamesList()); assertEquals(instance.getNames().size(), 2); assertFalse(instance.getNames().contains(otherName)); }