Class c = OpenmrsClassLoader.getInstance().loadClass(getAttributeType().getFormat()); try { Object o = c.newInstance();
@PropertyGetter("concept") public Object getConcept(PersonAttributeType delegate) { if (OpenmrsUtil.nullSafeEquals(delegate.getFormat(), Concept.class.getCanonicalName())) { Concept concept = Context.getConceptService().getConcept(delegate.getForeignKey()); return ConversionUtil.convertToRepresentation(concept, Representation.FULL); } return null; } }
/** * Gets the display string for a person attribute. * * @param pa the person attribute. * @return attribute type + value (for concise display purposes) */ @PropertyGetter("display") public String getDisplayString(PersonAttribute pa) { if (pa.getAttributeType() == null) return ""; if (Concept.class.getName().equals(pa.getAttributeType().getFormat()) && pa.getValue() != null) { Concept concept = Context.getConceptService().getConcept(pa.getValue()); return concept == null ? null : concept.getDisplayString(); } return pa.getAttributeType().getName() + " = " + pa.getValue(); }
@PropertySetter("hydratedObject") public void setHydratedObject(PersonAttribute personAttribute, String attributableUuid) { try { Class<?> attributableClass = OpenmrsClassLoader.getInstance().loadClass( personAttribute.getAttributeType().getFormat()); Attributable value = (Attributable) ConversionUtil.convert(attributableUuid, attributableClass); personAttribute.setValue(value.serialize()); } catch (ClassNotFoundException e) { throw new APIException("Could not convert value to Attributable", e); } }
String format = personAttribute.getAttributeType().getFormat(); if (format == null) { personAttribute.setValue(value); } else { Class<?> clazz = Context.loadClass(personAttribute.getAttributeType().getFormat()); if (Attributable.class.isAssignableFrom(clazz)) { Attributable instance = (Attributable) ConversionUtil.convert(value, clazz);
@Test public void setValue_shouldSetThePassedValueForNonAttributableClasses() throws ClassNotFoundException { PersonAttributeType type = personService.getPersonAttributeTypeByName("Race"); PersonAttribute attribute = new PersonAttribute(type, null); Assert.assertNull(attribute.getValue()); Assert.assertEquals("java.lang.String", type.getFormat()); Assert.assertFalse(Attributable.class.isAssignableFrom(Context.loadClass(type.getFormat()))); resource.setValue(attribute, "arab"); Assert.assertEquals("arab", attribute.getValue()); } }
@Test public void setValue_shouldSetPassedValueIfCouldNotBeConvertedToAttributable() throws ClassNotFoundException { PersonAttributeType type = new PersonAttributeType(); type.setFormat("org.openmrs.Location"); type.setName("Second Home"); type.setDescription("Testing Attributable domain objects"); type.setSortWeight(5.5); type.setSearchable(false); type = personService.savePersonAttributeType(type); String nonExistenceLocationUuid = "this-uuid-does-not-exist-of-course"; PersonAttribute attribute = new PersonAttribute(type, null); Assert.assertNull(attribute.getValue()); Assert.assertTrue(Attributable.class.isAssignableFrom(Context.loadClass(type.getFormat()))); resource.setValue(attribute, nonExistenceLocationUuid); Assert.assertEquals(nonExistenceLocationUuid, attribute.getValue()); }
@Override public void validateDefaultRepresentation() throws Exception { super.validateDefaultRepresentation(); assertPropEquals("name", getObject().getName()); assertPropEquals("description", getObject().getDescription()); assertPropEquals("format", getObject().getFormat()); assertPropEquals("foreignKey", getObject().getForeignKey()); assertPropEquals("sortWeight", getObject().getSortWeight()); assertPropEquals("searchable", getObject().getSearchable()); assertPropEquals("editPrivilege", getObject().getEditPrivilege()); assertPropEquals("retired", getObject().getRetired()); }
@Override public void validateFullRepresentation() throws Exception { super.validateFullRepresentation(); assertPropEquals("name", getObject().getName()); assertPropEquals("description", getObject().getDescription()); assertPropEquals("format", getObject().getFormat()); assertPropEquals("foreignKey", getObject().getForeignKey()); assertPropEquals("sortWeight", getObject().getSortWeight()); assertPropEquals("searchable", getObject().getSearchable()); assertPropEquals("editPrivilege", getObject().getEditPrivilege()); assertPropEquals("retired", getObject().getRetired()); assertPropPresent("auditInfo"); }