Java Code Examples for org.springframework.beans.MutablePropertyValues#removePropertyValue()

The following examples show how to use org.springframework.beans.MutablePropertyValues#removePropertyValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GrailsDataBinder.java    From AlgoTrader with GNU General Public License v2.0 6 votes vote down vote up
/**
 * This overrides the method from WebDataBinder to allow for nested checkbox handling, so property paths such as
 * a._b will result in the boolean b on object a getting set to false.
 */
@Override
protected void checkFieldMarkers(MutablePropertyValues mpvs) {
	if (getFieldMarkerPrefix() == null) {
		return;
	}

	String fieldMarkerPrefix = getFieldMarkerPrefix();
	PropertyValue[] pvArray = mpvs.getPropertyValues();
	for (PropertyValue pv : pvArray) {
		// start of variation from superclass method
		if (propertyStartsWithFieldMarkerPrefix(pv, fieldMarkerPrefix)) {
			String field = stripFieldMarkerPrefix(pv.getName(), fieldMarkerPrefix);
			// end of variation from superclass method
			if (getPropertyAccessor().isWritableProperty(field) && !mpvs.contains(field)) {
				Class<?> fieldType = getPropertyAccessor().getPropertyType(field);
				mpvs.add(field, getEmptyValue(field, fieldType));
			}
			mpvs.removePropertyValue(pv);
		}
	}
}
 
Example 2
Source File: SpringBeanJobFactory.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Create the job instance, populating it with property values taken
 * from the scheduler context, job data map and trigger data map.
 */
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
	Object job = super.createJobInstance(bundle);
	BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(job);
	if (isEligibleForPropertyPopulation(bw.getWrappedInstance())) {
		MutablePropertyValues pvs = new MutablePropertyValues();
		if (this.schedulerContext != null) {
			pvs.addPropertyValues(this.schedulerContext);
		}
		pvs.addPropertyValues(bundle.getJobDetail().getJobDataMap());
		pvs.addPropertyValues(bundle.getTrigger().getJobDataMap());
		if (this.ignoredUnknownProperties != null) {
			for (String propName : this.ignoredUnknownProperties) {
				if (pvs.contains(propName) && !bw.isWritableProperty(propName)) {
					pvs.removePropertyValue(propName);
				}
			}
			bw.setPropertyValues(pvs);
		}
		else {
			bw.setPropertyValues(pvs, true);
		}
	}
	return job;
}
 
Example 3
Source File: WebDataBinder.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Check the given property values for field markers,
 * i.e. for fields that start with the field marker prefix.
 * <p>The existence of a field marker indicates that the specified
 * field existed in the form. If the property values do not contain
 * a corresponding field value, the field will be considered as empty
 * and will be reset appropriately.
 * @param mpvs the property values to be bound (can be modified)
 * @see #getFieldMarkerPrefix
 * @see #getEmptyValue(String, Class)
 */
protected void checkFieldMarkers(MutablePropertyValues mpvs) {
	if (getFieldMarkerPrefix() != null) {
		String fieldMarkerPrefix = getFieldMarkerPrefix();
		PropertyValue[] pvArray = mpvs.getPropertyValues();
		for (PropertyValue pv : pvArray) {
			if (pv.getName().startsWith(fieldMarkerPrefix)) {
				String field = pv.getName().substring(fieldMarkerPrefix.length());
				if (getPropertyAccessor().isWritableProperty(field) && !mpvs.contains(field)) {
					Class<?> fieldType = getPropertyAccessor().getPropertyType(field);
					mpvs.add(field, getEmptyValue(field, fieldType));
				}
				mpvs.removePropertyValue(pv);
			}
		}
	}
}
 
Example 4
Source File: WebDataBinder.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Check the given property values for field defaults,
 * i.e. for fields that start with the field default prefix.
 * <p>The existence of a field defaults indicates that the specified
 * value should be used if the field is otherwise not present.
 * @param mpvs the property values to be bound (can be modified)
 * @see #getFieldDefaultPrefix
 */
protected void checkFieldDefaults(MutablePropertyValues mpvs) {
	if (getFieldDefaultPrefix() != null) {
		String fieldDefaultPrefix = getFieldDefaultPrefix();
		PropertyValue[] pvArray = mpvs.getPropertyValues();
		for (PropertyValue pv : pvArray) {
			if (pv.getName().startsWith(fieldDefaultPrefix)) {
				String field = pv.getName().substring(fieldDefaultPrefix.length());
				if (getPropertyAccessor().isWritableProperty(field) && !mpvs.contains(field)) {
					mpvs.add(field, pv.getValue());
				}
				mpvs.removePropertyValue(pv);
			}
		}
	}
}
 
Example 5
Source File: DictionaryBeanFactoryPostProcessor.java    From rice with Educational Community License v2.0 6 votes vote down vote up
/**
 * Iterates through the properties defined for the bean definition and invokes helper methods to process
 * the property value
 *
 * @param beanDefinition bean definition whose properties will be processed
 * @param nestedBeanStack stack of beans which contain the given bean
 */
protected void processBeanProperties(BeanDefinition beanDefinition, Stack<BeanDefinitionHolder> nestedBeanStack) {
    // iterate through properties and check for any configured message keys within the value
    MutablePropertyValues pvs = beanDefinition.getPropertyValues();
    PropertyValue[] pvArray = pvs.getPropertyValues();
    for (PropertyValue pv : pvArray) {
        Object newPropertyValue = null;
        if (isStringValue(pv.getValue())) {
            newPropertyValue = processStringPropertyValue(pv.getName(), getString(pv.getValue()), nestedBeanStack);
        } else {
            newPropertyValue = visitPropertyValue(pv.getName(), pv.getValue(), nestedBeanStack);
        }

        pvs.removePropertyValue(pv.getName());
        pvs.addPropertyValue(pv.getName(), newPropertyValue);
    }
}
 
Example 6
Source File: WebDataBinder.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Check the given property values for field markers,
 * i.e. for fields that start with the field marker prefix.
 * <p>The existence of a field marker indicates that the specified
 * field existed in the form. If the property values do not contain
 * a corresponding field value, the field will be considered as empty
 * and will be reset appropriately.
 * @param mpvs the property values to be bound (can be modified)
 * @see #getFieldMarkerPrefix
 * @see #getEmptyValue(String, Class)
 */
protected void checkFieldMarkers(MutablePropertyValues mpvs) {
	String fieldMarkerPrefix = getFieldMarkerPrefix();
	if (fieldMarkerPrefix != null) {
		PropertyValue[] pvArray = mpvs.getPropertyValues();
		for (PropertyValue pv : pvArray) {
			if (pv.getName().startsWith(fieldMarkerPrefix)) {
				String field = pv.getName().substring(fieldMarkerPrefix.length());
				if (getPropertyAccessor().isWritableProperty(field) && !mpvs.contains(field)) {
					Class<?> fieldType = getPropertyAccessor().getPropertyType(field);
					mpvs.add(field, getEmptyValue(field, fieldType));
				}
				mpvs.removePropertyValue(pv);
			}
		}
	}
}
 
Example 7
Source File: WebDataBinder.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Check the given property values for field defaults,
 * i.e. for fields that start with the field default prefix.
 * <p>The existence of a field defaults indicates that the specified
 * value should be used if the field is otherwise not present.
 * @param mpvs the property values to be bound (can be modified)
 * @see #getFieldDefaultPrefix
 */
protected void checkFieldDefaults(MutablePropertyValues mpvs) {
	String fieldDefaultPrefix = getFieldDefaultPrefix();
	if (fieldDefaultPrefix != null) {
		PropertyValue[] pvArray = mpvs.getPropertyValues();
		for (PropertyValue pv : pvArray) {
			if (pv.getName().startsWith(fieldDefaultPrefix)) {
				String field = pv.getName().substring(fieldDefaultPrefix.length());
				if (getPropertyAccessor().isWritableProperty(field) && !mpvs.contains(field)) {
					mpvs.add(field, pv.getValue());
				}
				mpvs.removePropertyValue(pv);
			}
		}
	}
}
 
Example 8
Source File: SpringBeanJobFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create the job instance, populating it with property values taken
 * from the scheduler context, job data map and trigger data map.
 */
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
	Object job = super.createJobInstance(bundle);
	if (isEligibleForPropertyPopulation(job)) {
		BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(job);
		MutablePropertyValues pvs = new MutablePropertyValues();
		if (this.schedulerContext != null) {
			pvs.addPropertyValues(this.schedulerContext);
		}
		pvs.addPropertyValues(bundle.getJobDetail().getJobDataMap());
		pvs.addPropertyValues(bundle.getTrigger().getJobDataMap());
		if (this.ignoredUnknownProperties != null) {
			for (String propName : this.ignoredUnknownProperties) {
				if (pvs.contains(propName) && !bw.isWritableProperty(propName)) {
					pvs.removePropertyValue(propName);
				}
			}
			bw.setPropertyValues(pvs);
		}
		else {
			bw.setPropertyValues(pvs, true);
		}
	}
	return job;
}
 
Example 9
Source File: WebDataBinder.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Check the given property values for field markers,
 * i.e. for fields that start with the field marker prefix.
 * <p>The existence of a field marker indicates that the specified
 * field existed in the form. If the property values do not contain
 * a corresponding field value, the field will be considered as empty
 * and will be reset appropriately.
 * @param mpvs the property values to be bound (can be modified)
 * @see #getFieldMarkerPrefix
 * @see #getEmptyValue(String, Class)
 */
protected void checkFieldMarkers(MutablePropertyValues mpvs) {
	String fieldMarkerPrefix = getFieldMarkerPrefix();
	if (fieldMarkerPrefix != null) {
		PropertyValue[] pvArray = mpvs.getPropertyValues();
		for (PropertyValue pv : pvArray) {
			if (pv.getName().startsWith(fieldMarkerPrefix)) {
				String field = pv.getName().substring(fieldMarkerPrefix.length());
				if (getPropertyAccessor().isWritableProperty(field) && !mpvs.contains(field)) {
					Class<?> fieldType = getPropertyAccessor().getPropertyType(field);
					mpvs.add(field, getEmptyValue(field, fieldType));
				}
				mpvs.removePropertyValue(pv);
			}
		}
	}
}
 
Example 10
Source File: WebDataBinder.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Check the given property values for field defaults,
 * i.e. for fields that start with the field default prefix.
 * <p>The existence of a field defaults indicates that the specified
 * value should be used if the field is otherwise not present.
 * @param mpvs the property values to be bound (can be modified)
 * @see #getFieldDefaultPrefix
 */
protected void checkFieldDefaults(MutablePropertyValues mpvs) {
	String fieldDefaultPrefix = getFieldDefaultPrefix();
	if (fieldDefaultPrefix != null) {
		PropertyValue[] pvArray = mpvs.getPropertyValues();
		for (PropertyValue pv : pvArray) {
			if (pv.getName().startsWith(fieldDefaultPrefix)) {
				String field = pv.getName().substring(fieldDefaultPrefix.length());
				if (getPropertyAccessor().isWritableProperty(field) && !mpvs.contains(field)) {
					mpvs.add(field, pv.getValue());
				}
				mpvs.removePropertyValue(pv);
			}
		}
	}
}
 
Example 11
Source File: WebDataBinder.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Check the given property values for field markers,
 * i.e. for fields that start with the field marker prefix.
 * <p>The existence of a field marker indicates that the specified
 * field existed in the form. If the property values do not contain
 * a corresponding field value, the field will be considered as empty
 * and will be reset appropriately.
 * @param mpvs the property values to be bound (can be modified)
 * @see #getFieldMarkerPrefix
 * @see #getEmptyValue(String, Class)
 */
protected void checkFieldMarkers(MutablePropertyValues mpvs) {
	String fieldMarkerPrefix = getFieldMarkerPrefix();
	if (fieldMarkerPrefix != null) {
		PropertyValue[] pvArray = mpvs.getPropertyValues();
		for (PropertyValue pv : pvArray) {
			if (pv.getName().startsWith(fieldMarkerPrefix)) {
				String field = pv.getName().substring(fieldMarkerPrefix.length());
				if (getPropertyAccessor().isWritableProperty(field) && !mpvs.contains(field)) {
					Class<?> fieldType = getPropertyAccessor().getPropertyType(field);
					mpvs.add(field, getEmptyValue(field, fieldType));
				}
				mpvs.removePropertyValue(pv);
			}
		}
	}
}
 
Example 12
Source File: SpringBeanJobFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Create the job instance, populating it with property values taken
 * from the scheduler context, job data map and trigger data map.
 */
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
	Object job = (this.applicationContext != null ?
			this.applicationContext.getAutowireCapableBeanFactory().createBean(
					bundle.getJobDetail().getJobClass(), AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false) :
			super.createJobInstance(bundle));

	if (isEligibleForPropertyPopulation(job)) {
		BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(job);
		MutablePropertyValues pvs = new MutablePropertyValues();
		if (this.schedulerContext != null) {
			pvs.addPropertyValues(this.schedulerContext);
		}
		pvs.addPropertyValues(bundle.getJobDetail().getJobDataMap());
		pvs.addPropertyValues(bundle.getTrigger().getJobDataMap());
		if (this.ignoredUnknownProperties != null) {
			for (String propName : this.ignoredUnknownProperties) {
				if (pvs.contains(propName) && !bw.isWritableProperty(propName)) {
					pvs.removePropertyValue(propName);
				}
			}
			bw.setPropertyValues(pvs);
		}
		else {
			bw.setPropertyValues(pvs, true);
		}
	}

	return job;
}
 
Example 13
Source File: DataBinder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check the given property values against the allowed fields,
 * removing values for fields that are not allowed.
 * @param mpvs the property values to be bound (can be modified)
 * @see #getAllowedFields
 * @see #isAllowed(String)
 */
protected void checkAllowedFields(MutablePropertyValues mpvs) {
	PropertyValue[] pvs = mpvs.getPropertyValues();
	for (PropertyValue pv : pvs) {
		String field = PropertyAccessorUtils.canonicalPropertyName(pv.getName());
		if (!isAllowed(field)) {
			mpvs.removePropertyValue(pv);
			getBindingResult().recordSuppressedField(field);
			if (logger.isDebugEnabled()) {
				logger.debug("Field [" + field + "] has been removed from PropertyValues " +
						"and will not be bound, because it has not been found in the list of allowed fields");
			}
		}
	}
}
 
Example 14
Source File: DataBinder.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Check the given property values against the allowed fields,
 * removing values for fields that are not allowed.
 * @param mpvs the property values to be bound (can be modified)
 * @see #getAllowedFields
 * @see #isAllowed(String)
 */
protected void checkAllowedFields(MutablePropertyValues mpvs) {
	PropertyValue[] pvs = mpvs.getPropertyValues();
	for (PropertyValue pv : pvs) {
		String field = PropertyAccessorUtils.canonicalPropertyName(pv.getName());
		if (!isAllowed(field)) {
			mpvs.removePropertyValue(pv);
			getBindingResult().recordSuppressedField(field);
			if (logger.isDebugEnabled()) {
				logger.debug("Field [" + field + "] has been removed from PropertyValues " +
						"and will not be bound, because it has not been found in the list of allowed fields");
			}
		}
	}
}
 
Example 15
Source File: SpringBeanJobFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Create the job instance, populating it with property values taken
 * from the scheduler context, job data map and trigger data map.
 */
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
	Object job = (this.applicationContext != null ?
			this.applicationContext.getAutowireCapableBeanFactory().createBean(
					bundle.getJobDetail().getJobClass(), AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false) :
			super.createJobInstance(bundle));

	if (isEligibleForPropertyPopulation(job)) {
		BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(job);
		MutablePropertyValues pvs = new MutablePropertyValues();
		if (this.schedulerContext != null) {
			pvs.addPropertyValues(this.schedulerContext);
		}
		pvs.addPropertyValues(bundle.getJobDetail().getJobDataMap());
		pvs.addPropertyValues(bundle.getTrigger().getJobDataMap());
		if (this.ignoredUnknownProperties != null) {
			for (String propName : this.ignoredUnknownProperties) {
				if (pvs.contains(propName) && !bw.isWritableProperty(propName)) {
					pvs.removePropertyValue(propName);
				}
			}
			bw.setPropertyValues(pvs);
		}
		else {
			bw.setPropertyValues(pvs, true);
		}
	}

	return job;
}
 
Example 16
Source File: DataBinder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Check the given property values against the allowed fields,
 * removing values for fields that are not allowed.
 * @param mpvs the property values to be bound (can be modified)
 * @see #getAllowedFields
 * @see #isAllowed(String)
 */
protected void checkAllowedFields(MutablePropertyValues mpvs) {
	PropertyValue[] pvs = mpvs.getPropertyValues();
	for (PropertyValue pv : pvs) {
		String field = PropertyAccessorUtils.canonicalPropertyName(pv.getName());
		if (!isAllowed(field)) {
			mpvs.removePropertyValue(pv);
			getBindingResult().recordSuppressedField(field);
			if (logger.isDebugEnabled()) {
				logger.debug("Field [" + field + "] has been removed from PropertyValues " +
						"and will not be bound, because it has not been found in the list of allowed fields");
			}
		}
	}
}
 
Example 17
Source File: GrailsDataBinder.java    From AlgoTrader with GNU General Public License v2.0 5 votes vote down vote up
private void filterNestedParameterMaps(MutablePropertyValues mpvs) {
	for (PropertyValue pv : mpvs.getPropertyValues()) {
		final Object value = pv.getValue();
		if (isNotCandidateForBinding(value)) {
			mpvs.removePropertyValue(pv);
		}
	}
}
 
Example 18
Source File: LegacyConfigPostProcessor.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Given a bean name (assumed to implement {@link org.springframework.core.io.support.PropertiesLoaderSupport})
 * checks whether it already references the <code>global-properties</code> bean. If not, 'upgrades' the bean by
 * appending all additional resources it mentions in its <code>locations</code> property to
 * <code>globalPropertyLocations</code>, except for those resources mentioned in <code>newLocations</code>. A
 * reference to <code>global-properties</code> will then be added and the resource list in
 * <code>newLocations<code> will then become the new <code>locations</code> list for the bean.
 * 
 * @param beanFactory
 *            the bean factory
 * @param globalPropertyLocations
 *            the list of global property locations to be appended to
 * @param beanName
 *            the bean name
 * @param newLocations
 *            the new locations to be set on the bean
 * @return the mutable property values
 */
@SuppressWarnings("unchecked")
private MutablePropertyValues processLocations(ConfigurableListableBeanFactory beanFactory,
        Collection<Object> globalPropertyLocations, String beanName, String[] newLocations)
{
    // Get the bean an check its existing properties value
    MutablePropertyValues beanProperties = beanFactory.getBeanDefinition(beanName).getPropertyValues();
    PropertyValue pv = beanProperties.getPropertyValue(LegacyConfigPostProcessor.PROPERTY_PROPERTIES);
    Object value;

    // If the properties value already references the global-properties bean, we have nothing else to do. Otherwise,
    // we have to 'upgrade' the bean definition.
    if (pv == null || (value = pv.getValue()) == null || !(value instanceof BeanReference)
            || ((BeanReference) value).getBeanName().equals(LegacyConfigPostProcessor.BEAN_NAME_GLOBAL_PROPERTIES))
    {
        // Convert the array of new locations to a managed list of type string values, so that it is
        // compatible with a bean definition
        Collection<Object> newLocationList = new ManagedList(newLocations.length);
        if (newLocations != null && newLocations.length > 0)
        {
            for (String preserveLocation : newLocations)
            {
                newLocationList.add(new TypedStringValue(preserveLocation));
            }
        }

        // If there is currently a locations list, process it
        pv = beanProperties.getPropertyValue(LegacyConfigPostProcessor.PROPERTY_LOCATIONS);
        if (pv != null && (value = pv.getValue()) != null && value instanceof Collection)
        {
            Collection<Object> locations = (Collection<Object>) value;

            // Compute the set of locations that need to be added to globalPropertyLocations (preserving order) and
            // warn about each
            Set<Object> addedLocations = new LinkedHashSet<Object>(locations);
            addedLocations.removeAll(globalPropertyLocations);
            addedLocations.removeAll(newLocationList);

            for (Object location : addedLocations)
            {
                LegacyConfigPostProcessor.logger.warn("Legacy configuration detected: adding "
                        + (location instanceof TypedStringValue ? ((TypedStringValue) location).getValue()
                                : location.toString()) + " to global-properties definition");
                globalPropertyLocations.add(location);
            }

        }
        // Ensure the bean now references global-properties
        beanProperties.addPropertyValue(LegacyConfigPostProcessor.PROPERTY_PROPERTIES, new RuntimeBeanReference(
                LegacyConfigPostProcessor.BEAN_NAME_GLOBAL_PROPERTIES));

        // Ensure the new location list is now set on the bean
        if (newLocationList.size() > 0)
        {
            beanProperties.addPropertyValue(LegacyConfigPostProcessor.PROPERTY_LOCATIONS, newLocationList);
        }
        else
        {
            beanProperties.removePropertyValue(LegacyConfigPostProcessor.PROPERTY_LOCATIONS);
        }
    }
    return beanProperties;
}
 
Example 19
Source File: UifBeanFactoryPostProcessor.java    From rice with Educational Community License v2.0 4 votes vote down vote up
/**
 * If the bean class is type UifDictionaryBean, iterate through configured property values
 * and check for expressions.
 *
 * @param beanName name of the bean in the factory (only set for top level beans, not nested)
 * @param beanDefinition bean definition to process for expressions
 * @param nestedPropertyName
 * @param expressionGraph
 * @param beanFactory bean factory being processed
 * @param processedBeanNames
 */
protected void processNestedBeanDefinition(String beanName, BeanDefinition beanDefinition,
        String nestedPropertyName, Map<String, String> expressionGraph,
        ConfigurableListableBeanFactory beanFactory, Set<String> processedBeanNames) {
    Class<?> beanClass = getBeanClass(beanDefinition, beanFactory);
    if ((beanClass == null) || !UifDictionaryBean.class.isAssignableFrom(beanClass) || processedBeanNames.contains(
            beanName)) {
        return;
    }

    LOG.debug("Processing bean name '" + beanName + "'");

    Map<String, String> parentExpressionGraph = getExpressionGraphFromParent(beanDefinition.getParentName(),
            beanFactory, processedBeanNames);

    // process expressions on property values
    MutablePropertyValues pvs = beanDefinition.getPropertyValues();
    PropertyValue[] pvArray = pvs.getPropertyValues();
    for (PropertyValue pv : pvArray) {
        if (pv.getName().equals(UifPropertyPaths.EXPRESSION_GRAPH)) {
            continue;
        }

        String propertyPath = pv.getName();
        if (StringUtils.isNotBlank(nestedPropertyName)) {
            propertyPath = nestedPropertyName + "." + propertyPath;
        }

        // for reloading, need to remove the property from the previously loaded bean definition
        if (expressionGraph.containsKey(propertyPath)) {
            expressionGraph.remove(propertyPath);
        }

        if (hasExpression(pv.getValue())) {
            // process expression
            String strValue = getStringValue(pv.getValue());
            expressionGraph.put(propertyPath, strValue);

            // remove property value so expression will not cause binding exception
            pvs.removePropertyValue(pv.getName());
        } else {
            // process nested objects
            Object newValue = processPropertyValue(propertyPath, pv.getName(), pv.getValue(), beanDefinition,
                    parentExpressionGraph, expressionGraph, beanFactory, processedBeanNames);

            pvs.removePropertyValue(pv.getName());
            pvs.addPropertyValue(pv.getName(), newValue);
        }

        // removed expression (if exists) from parent map since the property was set on child
        if (parentExpressionGraph.containsKey(pv.getName())) {
            parentExpressionGraph.remove(pv.getName());
        }
    }

    // if nested bean set expression graph to null so it is not inherited from parent definition
    if (StringUtils.isNotBlank(nestedPropertyName)) {
        pvs.addPropertyValue(UifPropertyPaths.EXPRESSION_GRAPH, null);
    }

    // add remaining expressions from parent to expression graph
    for (Map.Entry<String, String> parentExpression : parentExpressionGraph.entrySet()) {
        String expressionPath = parentExpression.getKey();
        if (StringUtils.isNotBlank(nestedPropertyName)) {
            expressionPath = nestedPropertyName + "." + expressionPath;
        }

        if (!expressionGraph.containsKey(expressionPath)) {
            expressionGraph.put(expressionPath, parentExpression.getValue());
        }
    }

    if (StringUtils.isNotBlank(beanName)) {
        processedBeanNames.add(beanName);
    }
}
 
Example 20
Source File: GrailsDataBinder.java    From AlgoTrader with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void bindCollectionAssociation(MutablePropertyValues mpvs, PropertyValue pv) {
	Object v = pv.getValue();

	Collection collection = (Collection) this.bean.getPropertyValue(pv.getName());
	collection.clear();
	final Class associatedType = getReferencedTypeForCollection(pv.getName(), getTarget());
	final boolean isArray = v != null && v.getClass().isArray();
	final PropertyEditor propertyEditor = findCustomEditor(collection.getClass(), pv.getName());
	if (propertyEditor == null) {
		if (isDomainAssociation(associatedType)) {
			if (isArray) {
				Object[] identifiers = (Object[]) v;
				for (Object id : identifiers) {
					if (id != null) {
						associateObjectForId(pv, id, associatedType);
					}
				}
				mpvs.removePropertyValue(pv);
			} else if (v != null && (v instanceof String)) {
				associateObjectForId(pv, v, associatedType);
				mpvs.removePropertyValue(pv);
			}
		} else if (GrailsDomainConfigurationUtil.isBasicType(associatedType)) {
			if (isArray) {
				Object[] values = (Object[]) v;
				List list = collection instanceof List ? (List) collection : null;
				for (int i = 0; i < values.length; i++) {
					Object value = values[i];
					try {
						Object newValue = getTypeConverter().convertIfNecessary(value, associatedType);
						if (list != null) {
							if (i > list.size() - 1) {
								list.add(i, newValue);
							} else {
								list.set(i, newValue);
							}
						} else {
							collection.add(newValue);
						}
					} catch (TypeMismatchException e) {
						// ignore
					}
				}
			}
		}
	}
}