Example usage for org.apache.commons.beanutils PropertyUtils setProperty

List of usage examples for org.apache.commons.beanutils PropertyUtils setProperty

Introduction

In this page you can find the example usage for org.apache.commons.beanutils PropertyUtils setProperty.

Prototype

public static void setProperty(Object bean, String name, Object value)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Set the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.

For more details see PropertyUtilsBean.

Usage

From source file:org.openmrs.module.kenyarx.mapping.ObjectObsMarshaller.java

/**
 * Un-marshals the given obs into a new object instance
 * @param obs the obs/* w ww .j  a v a  2  s.co m*/
 * @param clazz the class of the object
 * @param <T> the class of the object
 * @return the new object instance
 */
public <T> T unmarshal(Obs obs, Class<T> clazz) throws IllegalAccessException, InstantiationException,
        InvocationTargetException, NoSuchMethodException {
    // Handle simple data types first
    if (clazz.equals(String.class)) {
        return (T) obs.getValueText();
    } else if (clazz.equals(Double.class)) {
        return (T) obs.getValueNumeric();
    } else if (clazz.equals(Date.class)) {
        return (T) obs.getValueDatetime();
    } else if (clazz.equals(Concept.class)) {
        return (T) obs.getValueCoded();
    }

    T object = clazz.newInstance();

    ClassConceptMapping mapping = getMapping(clazz);

    for (Map.Entry<String, String> memberMapping : mapping.getMemberMappings().entrySet()) {
        String propertyName = memberMapping.getKey();
        Concept memberConcept = getConcept(memberMapping.getValue());

        Obs propertyObs = findMember(obs, memberConcept);

        if (propertyObs != null) {
            Class propertyType = PropertyUtils.getPropertyType(object, propertyName);

            Object propertyValue = unmarshal(propertyObs, propertyType);

            PropertyUtils.setProperty(object, propertyName, propertyValue);
        }
    }

    return object;
}

From source file:org.openmrs.module.muzima.web.resource.wrapper.FakeCohort.java

public static FakeCohort copyCohort(final Cohort cohort) {
    FakeCohort fakeCohort = new FakeCohort();
    for (String property : properties) {
        try {//  www.  j ava 2  s . c  o m
            Object o = PropertyUtils.getProperty(cohort, property);
            PropertyUtils.setProperty(fakeCohort, property, o);
        } catch (Exception e) {
            log.error(
                    "Copying property failed for property: '" + property + "' with message: " + e.getMessage(),
                    e);
        }
    }
    fakeCohort.setVoided(cohort.getVoided());
    return fakeCohort;
}

From source file:org.openmrs.module.muzima.web.resource.wrapper.FakeConcept.java

public static FakeConcept copyConcept(final Concept concept) {
    if (concept == null)
        return null;
    FakeConcept fakeConcept = new FakeConcept();
    for (String property : properties) {
        try {//from   w ww .  ja  va 2  s .c  o  m
            Object o = PropertyUtils.getProperty(concept, property);
            PropertyUtils.setProperty(fakeConcept, property, o);
        } catch (Exception e) {
            log.error(
                    "Copying property failed for property: '" + property + "' with message: " + e.getMessage(),
                    e);
        }
    }

    if (concept.isNumeric()) {
        ConceptNumeric numeric = Context.getConceptService().getConceptNumeric(concept.getConceptId());
        fakeConcept.setUnits(numeric.getUnits());
        fakeConcept.setPrecise(numeric.getPrecise());
        fakeConcept.setNumeric(concept.isNumeric());
    }

    fakeConcept.setRetired(concept.isRetired());
    fakeConcept.setDisplayConcept(concept.getDisplayString());
    return fakeConcept;
}

From source file:org.openmrs.module.muzima.web.resource.wrapper.FakeEncounter.java

public static FakeEncounter copyEncounter(final Encounter encounter) {
    FakeEncounter fakeEncounter = new FakeEncounter();
    for (String property : properties) {
        try {/*w  w w  . j  av a  2  s . c o  m*/
            Object o = PropertyUtils.getProperty(encounter, property);
            PropertyUtils.setProperty(fakeEncounter, property, o);
        } catch (Exception e) {
            log.error(
                    "Copying property failed for property: '" + property + "' with message: " + e.getMessage(),
                    e);
        }
    }
    fakeEncounter.setProvider(encounter.getProvider());
    fakeEncounter.setVoided(encounter.getVoided());
    return fakeEncounter;
}

From source file:org.openmrs.module.muzima.web.resource.wrapper.FakeObs.java

public static FakeObs copyObs(final Obs obs) {
    FakeObs fakeObs = new FakeObs();
    for (String property : properties) {
        try {/*from   w w w  .ja  v  a  2 s. c  om*/
            Object o = PropertyUtils.getProperty(obs, property);
            PropertyUtils.setProperty(fakeObs, property, o);
        } catch (Exception e) {
            log.error(
                    "Copying property failed for property: '" + property + "' with message: " + e.getMessage(),
                    e);
        }
    }
    fakeObs.setVoided(obs.getVoided());
    return fakeObs;
}

From source file:org.openmrs.module.muzima.web.resource.wrapper.FakePatient.java

public static FakePatient copyPatient(final Patient patient) {
    FakePatient fakePatient = new FakePatient();
    for (String property : properties) {
        try {//w w  w .  ja  v a  2 s.c  o  m
            Object o = PropertyUtils.getProperty(patient, property);
            PropertyUtils.setProperty(fakePatient, property, o);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
    fakePatient.setVoided(patient.getVoided());
    return fakePatient;
}

From source file:org.openmrs.module.OpenmrsLite.fragment.controller.DiagnosesFragmentController.java

/**
 * This is public so that it can be used by a fragment that needs to prepopulate a diagnoses widget that is normally
 * populated with AJAX results from the #search method.
 * @param result//from w w w  .  j ava  2  s .co m
 * @param ui
 * @param locale
 * @return
 * @throws Exception
 */
public SimpleObject simplify(ConceptSearchResult result, UiUtils ui, Locale locale) throws Exception {
    SimpleObject simple = SimpleObject.fromObject(result, ui, "word", "conceptName.id",
            "conceptName.conceptNameType", "conceptName.name", "concept.id",
            "concept.conceptMappings.conceptMapType", "concept.conceptMappings.conceptReferenceTerm.code",
            "concept.conceptMappings.conceptReferenceTerm.name",
            "concept.conceptMappings.conceptReferenceTerm.conceptSource.name");

    Concept concept = result.getConcept();
    ConceptName conceptName = result.getConceptName();
    ConceptName preferredName = getPreferredName(locale, concept);
    PropertyUtils.setProperty(simple, "concept.preferredName", preferredName.getName());

    return simple;
}

From source file:org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingConverter.java

protected void setPropertyWhichMayBeAHibernateCollection(Object instance, String propertyName, Object value)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    if (value instanceof Collection) {
        //We need to handle collections in a way that Hibernate can track.
        Collection<?> newCollection = (Collection<?>) value;
        Object oldValue = PropertyUtils.getProperty(instance, propertyName);
        if (oldValue instanceof Collection) {
            Collection collection = (Collection) oldValue;
            collection.clear();//from  ww w. j  a v a2s. co  m
            collection.addAll(newCollection);
        } else {
            PropertyUtils.setProperty(instance, propertyName, value);
        }
    } else {
        PropertyUtils.setProperty(instance, propertyName, value);
    }
}

From source file:org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource.java

/**
 * @see org.openmrs.module.webservices.rest.web.resource.api.Converter#setProperty(java.lang.Object,
 *      java.lang.String, java.lang.Object)
 *///from  w w w.  j  av  a 2  s. c om
@Override
public void setProperty(Object instance, String propertyName, Object value) throws ConversionException {
    if (propertiesIgnoredWhenUpdating.contains(propertyName)) {
        return;
    }
    try {
        DelegatingResourceHandler<? extends T> handler;

        try {
            handler = getResourceHandler((T) instance);
        } catch (Exception e) {
            // this try/catch isn't really needed because of java erasure behaviour at run time.
            // but I'm putting in here just in case
            handler = this;
        }

        // try to find a @PropertySetter-annotated method
        Method annotatedSetter = findSetterMethod(handler, propertyName);
        if (annotatedSetter != null) {
            Type expectedType = annotatedSetter.getGenericParameterTypes()[1];
            value = ConversionUtil.convert(value, expectedType);
            annotatedSetter.invoke(handler, instance, value);
            return;
        }

        // we need the generic type of this property, not just the class
        Method setter = PropertyUtils.getPropertyDescriptor(instance, propertyName).getWriteMethod();
        value = ConversionUtil.convert(value, setter.getGenericParameterTypes()[0]);

        if (value instanceof Collection) {
            //We need to handle collections in a way that Hibernate can track.
            Collection<?> newCollection = (Collection<?>) value;
            Object oldValue = PropertyUtils.getProperty(instance, propertyName);
            if (oldValue instanceof Collection) {
                Collection collection = (Collection) oldValue;
                collection.clear();
                collection.addAll(newCollection);
            } else {
                PropertyUtils.setProperty(instance, propertyName, value);
            }
        } else {
            PropertyUtils.setProperty(instance, propertyName, value);
        }
    } catch (Exception ex) {
        throw new ConversionException(propertyName + " on " + instance.getClass(), ex);
    }
}

From source file:org.openmrs.ui.framework.resource.ResourceFactory.java

/**
  * Registers a Resource Provider.//from   ww w. j av  a2  s.  co  m
 * 
 * If a system property exists called "uiFramework.development.${ key }", and the resource provider has
 * a "developmentFolder" property, the value of "${systemProperty}/omod/src/main/webapp/resources" will be set
 * for that property 
 * 
 * @param key
 * @param provider
  */
public void addResourceProvider(String key, ResourceProvider provider) {
    if (resourceProviders == null)
        resourceProviders = new LinkedHashMap<String, ResourceProvider>();

    String devRootFolder = System.getProperty("uiFramework.development." + key);
    if (devRootFolder != null) {
        File devFolder = new File(devRootFolder + File.separator + "omod" + File.separator + "src"
                + File.separator + "main" + File.separator + "webapp" + File.separator + "resources");
        if (devFolder.exists() && devFolder.isDirectory()) {
            try {
                PropertyUtils.setProperty(provider, "developmentFolder", devFolder);
                resourceProvidersInDevelopmentMode.add(key);
            } catch (Exception ex) {
                // pass
            }
        } else {
            log.warn("Failed to set development mode for ResourceProvider " + key + " because "
                    + devFolder.getAbsolutePath() + " does not exist or is not a directory");
        }
    }

    resourceProviders.put(key, provider);
}