Example usage for org.springframework.beans BeanWrapper setPropertyValue

List of usage examples for org.springframework.beans BeanWrapper setPropertyValue

Introduction

In this page you can find the example usage for org.springframework.beans BeanWrapper setPropertyValue.

Prototype

void setPropertyValue(String propertyName, @Nullable Object value) throws BeansException;

Source Link

Document

Set the specified value as current property value.

Usage

From source file:org.apache.syncope.client.console.policies.PolicyRuleWizardBuilder.java

@Override
protected Serializable onApplyInternal(final PolicyRuleDirectoryPanel.PolicyRuleWrapper modelObject) {
    BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(modelObject.getConf());
    wrapper.setPropertyValue("name", modelObject.getName());

    AbstractPolicyTO policyTO = restClient.getPolicy(policy);

    final ComposablePolicy<RuleConf> composable;
    if (policyTO instanceof ComposablePolicy) {
        composable = (ComposablePolicy<RuleConf>) policyTO;
    } else {/* ww  w  .  ja v  a2 s  . c o m*/
        throw new IllegalStateException("Non composable policy");
    }

    if (modelObject.isNew()) {
        composable.getRuleConfs().add(modelObject.getConf());
    } else {
        CollectionUtils.filter(composable.getRuleConfs(), new Predicate<RuleConf>() {

            @Override
            public boolean evaluate(final RuleConf object) {
                return !object.getName().equals(modelObject.getOldName());
            }
        });
        composable.getRuleConfs().add(modelObject.getConf());
    }

    restClient.updatePolicy(policyTO);
    return modelObject;
}

From source file:org.apache.syncope.client.console.reports.ReportletWizardBuilder.java

@Override
protected Serializable onApplyInternal(final ReportletDirectoryPanel.ReportletWrapper modelObject) {
    modelObject.getConf().setName(modelObject.getName());

    final ReportTO reportTO = restClient.read(report);

    if (modelObject.isNew()) {
        reportTO.getReportletConfs().add(modelObject.getConf());
    } else {// w  w  w .j  a va 2 s .  co  m
        CollectionUtils.filter(reportTO.getReportletConfs(), new Predicate<AbstractReportletConf>() {

            @Override
            public boolean evaluate(final AbstractReportletConf object) {
                return !object.getName().equals(modelObject.getOldName());
            }
        });
        reportTO.getReportletConfs().add(modelObject.getConf());
    }

    BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(modelObject.getConf());
    for (Map.Entry<String, Pair<AbstractFiqlSearchConditionBuilder, List<SearchClause>>> entry : modelObject
            .getSCondWrapper().entrySet()) {
        wrapper.setPropertyValue(entry.getKey(),
                SearchUtils.buildFIQL(entry.getValue().getRight(), entry.getValue().getLeft()));
    }

    restClient.update(reportTO);
    return modelObject;
}

From source file:org.codehaus.groovy.grails.cli.jndi.JndiBindingSupport.java

@SuppressWarnings("rawtypes")
private static void bindProperties(Object obj, Map entryProperties) {
    BeanWrapper dsBean = new BeanWrapperImpl(obj);
    for (Object o : entryProperties.entrySet()) {
        Map.Entry entry2 = (Map.Entry) o;
        final String propertyName = entry2.getKey().toString();
        if (dsBean.isWritableProperty(propertyName)) {
            dsBean.setPropertyValue(propertyName, entry2.getValue());
        }/*from   w ww  .  j a  v  a 2 s  . c o m*/
    }
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil.java

public static void autoAssociateBidirectionalOneToOnes(DefaultGrailsDomainClass domainClass, Object target) {
    List<GrailsDomainClassProperty> associations = domainClass.getAssociations();
    for (GrailsDomainClassProperty association : associations) {
        if (association.isOneToOne() && association.isBidirectional() && association.isOwningSide()) {
            if (isInitialized(target, association.getName())) {
                GrailsDomainClassProperty otherSide = association.getOtherSide();
                if (otherSide != null) {
                    BeanWrapper bean = new BeanWrapperImpl(target);
                    Object inverseObject = bean.getPropertyValue(association.getName());
                    if (inverseObject != null) {
                        if (isInitialized(inverseObject, otherSide.getName())) {
                            BeanWrapper inverseBean = new BeanWrapperImpl(inverseObject);
                            Object propertyValue = inverseBean.getPropertyValue(otherSide.getName());
                            if (propertyValue == null) {
                                inverseBean.setPropertyValue(otherSide.getName(), target);
                            }/*  www. j  a  va 2s  .  c om*/
                        }
                    }
                }
            }
        }
    }
}

From source file:org.codehaus.groovy.grails.orm.hibernate.metaclass.AbstractSavePersistentMethod.java

/**
 * Performs automatic association retrieval
 * @param domainClass The domain class to retrieve associations for
 * @param target The target object/*from   w  ww.j  a v  a2  s.com*/
 */
@SuppressWarnings("unchecked")
private void autoRetrieveAssocations(GrailsDomainClass domainClass, Object target) {
    BeanWrapper bean = new BeanWrapperImpl(target);
    HibernateTemplate t = getHibernateTemplate();
    GrailsDomainClassProperty[] props = domainClass.getPersistentProperties();
    for (int i = 0; i < props.length; i++) {
        GrailsDomainClassProperty prop = props[i];
        if (!prop.isManyToOne() && !prop.isOneToOne()) {
            continue;
        }

        Object propValue = bean.getPropertyValue(prop.getName());
        if (propValue == null || t.contains(propValue)) {
            continue;
        }

        GrailsDomainClass otherSide = (GrailsDomainClass) application
                .getArtefact(DomainClassArtefactHandler.TYPE, prop.getType().getName());
        if (otherSide == null) {
            continue;
        }

        BeanWrapper propBean = new BeanWrapperImpl(propValue);
        try {
            Serializable id = (Serializable) propBean.getPropertyValue(otherSide.getIdentifier().getName());
            if (id != null) {
                final Object propVal = t.get(prop.getType(), id);
                if (propVal != null) {
                    bean.setPropertyValue(prop.getName(), propVal);
                }
            }
        } catch (InvalidPropertyException ipe) {
            // property is not accessable
        }
    }
}

From source file:org.codehaus.groovy.grails.web.binding.GrailsDataBinder.java

@SuppressWarnings("unchecked")
private Object autoCreatePropertyIfPossible(BeanWrapper wrapper, String propertyName, Object propertyValue) {

    propertyName = PropertyAccessorUtils.canonicalPropertyName(propertyName);
    int currentKeyStart = propertyName.indexOf(PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR);
    int currentKeyEnd = propertyName.indexOf(PropertyAccessor.PROPERTY_KEY_SUFFIX_CHAR);
    String propertyNameWithIndex = propertyName;
    if (currentKeyStart > -1) {
        propertyName = propertyName.substring(0, currentKeyStart);
    }//from   ww  w.  ja  v a2  s . co m

    Class<?> type = wrapper.getPropertyType(propertyName);
    Object val = wrapper.isReadableProperty(propertyName) ? wrapper.getPropertyValue(propertyName) : null;

    LOG.debug(
            "Checking if auto-create is possible for property [" + propertyName + "] and type [" + type + "]");
    if (type != null && val == null && (isDomainClass(type) || isEmbedded(wrapper, propertyName))) {
        if (!shouldPropertyValueSkipAutoCreate(propertyValue)
                && isNullAndWritableProperty(wrapper, propertyName)) {
            if (isDomainClass(type)) {
                Object created = autoInstantiateDomainInstance(type);
                if (created != null) {
                    val = created;
                    wrapper.setPropertyValue(propertyName, created);
                }
            } else if (isEmbedded(wrapper, propertyName)) {
                Object created = autoInstantiateEmbeddedInstance(type);
                if (created != null) {
                    val = created;
                    wrapper.setPropertyValue(propertyName, created);
                }
            }
        }
    } else {
        final Object beanInstance = wrapper.getWrappedInstance();
        if (type != null && Collection.class.isAssignableFrom(type)) {
            Collection<?> c = null;
            final Class<?> referencedType = getReferencedTypeForCollection(propertyName, beanInstance);

            if (isNullAndWritableProperty(wrapper, propertyName)) {
                c = decorateCollectionForDomainAssociation(GrailsClassUtils.createConcreteCollection(type),
                        referencedType);
            } else {
                if (wrapper.isReadableProperty(propertyName)) {
                    c = decorateCollectionForDomainAssociation(
                            (Collection<?>) wrapper.getPropertyValue(propertyName), referencedType);
                }
            }

            if (wrapper.isWritableProperty(propertyName) && c != null) {
                wrapper.setPropertyValue(propertyName, c);
            }

            val = c;

            if (c != null && currentKeyStart > -1 && currentKeyEnd > -1) {
                String indexString = propertyNameWithIndex.substring(currentKeyStart + 1, currentKeyEnd);
                int index = Integer.parseInt(indexString);

                // See if we have an instance in the collection. If so, that specific instance
                // is the value to return for this indexed property.
                Object instance = findIndexedValue(c, index);
                if (instance != null) {
                    val = instance;
                }
                // If no value in the collection, this might be a domain class
                else if (isDomainClass(referencedType)) {
                    instance = autoInstantiateDomainInstance(referencedType);
                    if (instance != null) {
                        val = instance;
                        if (index == c.size()) {
                            addAssociationToTarget(propertyName, beanInstance, instance);
                        } else if (index > c.size()) {
                            while (index > c.size()) {
                                addAssociationToTarget(propertyName, beanInstance,
                                        autoInstantiateDomainInstance(referencedType));
                            }

                            addAssociationToTarget(propertyName, beanInstance, instance);
                        }
                    }
                }
            }
        } else if (type != null && Map.class.isAssignableFrom(type)) {
            Map<String, Object> map;
            if (isNullAndWritableProperty(wrapper, propertyName)) {
                map = new HashMap<String, Object>();
                wrapper.setPropertyValue(propertyName, map);
            } else {
                map = (Map) wrapper.getPropertyValue(propertyName);
            }
            val = map;
            wrapper.setPropertyValue(propertyName, val);

            if (currentKeyStart > -1 && currentKeyEnd > -1) {
                String indexString = propertyNameWithIndex.substring(currentKeyStart + 1, currentKeyEnd);
                Class<?> referencedType = getReferencedTypeForCollection(propertyName, beanInstance);
                if (isDomainClass(referencedType)) {
                    final Object domainInstance = autoInstantiateDomainInstance(referencedType);
                    val = domainInstance;
                    map.put(indexString, domainInstance);
                }
            }
        }
    }

    return val;
}

From source file:org.grails.orm.hibernate.cfg.GrailsHibernateUtil.java

public static void autoAssociateBidirectionalOneToOnes(DefaultGrailsDomainClass domainClass, Object target) {
    List<GrailsDomainClassProperty> associations = domainClass.getAssociations();
    for (GrailsDomainClassProperty association : associations) {
        if (!association.isOneToOne() || !association.isBidirectional() || !association.isOwningSide()) {
            continue;
        }/*from   www. ja  v a  2s .  co  m*/

        if (!isInitialized(target, association.getName())) {
            continue;
        }

        GrailsDomainClassProperty otherSide = association.getOtherSide();
        if (otherSide == null) {
            continue;
        }

        BeanWrapper bean = new BeanWrapperImpl(target);
        Object inverseObject = bean.getPropertyValue(association.getName());
        if (inverseObject == null) {
            continue;
        }

        if (!isInitialized(inverseObject, otherSide.getName())) {
            continue;
        }

        BeanWrapper inverseBean = new BeanWrapperImpl(inverseObject);
        Object propertyValue = inverseBean.getPropertyValue(otherSide.getName());
        if (propertyValue == null) {
            inverseBean.setPropertyValue(otherSide.getName(), target);
        }
    }
}

From source file:org.hoteia.qalingo.core.web.util.BeanUtilsBean.java

public static void copyNotNullProperties(final Object source, final Object target,
        final Iterable<String> properties) {
    final BeanWrapper src = new BeanWrapperImpl(source);
    final BeanWrapper trg = new BeanWrapperImpl(target);
    for (final String propertyName : properties) {
        trg.setPropertyValue(propertyName, src.getPropertyValue(propertyName));
    }/*from www .  ja va 2s .co m*/
}

From source file:org.nextframework.persistence.GenericDAO.java

public void loadDescriptionProperty(BEAN object) {
    BeanDescriptor beanDescriptor = BeanDescriptorFactory.forBean(object);
    String descriptionPropertyName = beanDescriptor.getDescriptionPropertyName();
    BEAN newValue = query().select(getSelectClauseForIdAndDescription()).from(beanClass).entity(object)
            .unique();//from w  w w  .  ja  v  a2s  . c o m

    try {
        Object value = PropertyAccessorFactory.forBeanPropertyAccess(newValue)
                .getPropertyValue(descriptionPropertyName);
        BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(object);
        bw.setPropertyValue(descriptionPropertyName, value);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.opennms.netmgt.alarmd.northbounder.snmptrap.SnmpTrapNorthbounderConfigDaoTest.java

/**
 * Test bean wrapper./*from  w ww  .  j  av a2 s  .co  m*/
 *
 * @throws Exception the exception
 */
@Test
public void testBeanWrapper() throws Exception {
    SnmpTrapSink sink = configDao.getConfig().getSnmpTrapSink("localTest2");
    final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(sink);
    Map<String, String> params = new HashMap<String, String>();
    params.put("ipAddress", "192.168.0.1");
    Assert.assertEquals("127.0.0.2", sink.getIpAddress());
    boolean modified = false;
    for (final String key : params.keySet()) {
        if (wrapper.isWritableProperty(key)) {
            final String stringValue = params.get(key);
            final Object value = wrapper.convertIfNecessary(stringValue,
                    (Class<?>) wrapper.getPropertyType(key));
            wrapper.setPropertyValue(key, value);
            modified = true;
        }
    }
    Assert.assertTrue(modified);
    Assert.assertEquals("192.168.0.1", sink.getIpAddress());
    configDao.save();
    configDao.reload();
    Assert.assertEquals("192.168.0.1", configDao.getConfig().getSnmpTrapSink("localTest2").getIpAddress());
}