GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / src / web / org / codehaus / groovy / grails / web / metaclass / SetPropertiesDynamicProperty.java
blobf8627e9cd2a0794bb24a78ffd056e390003c95df
1 /*
2 * Copyright 2004-2005 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.codehaus.groovy.grails.web.metaclass;
18 import groovy.lang.MissingPropertyException;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.codehaus.groovy.grails.commons.metaclass.AbstractDynamicProperty;
22 import org.codehaus.groovy.grails.web.binding.DataBindingUtils;
23 import org.codehaus.groovy.runtime.DefaultGroovyMethods;
24 import org.springframework.beans.SimpleTypeConverter;
25 import org.springframework.beans.TypeConverter;
26 import org.springframework.validation.BindingResult;
28 /**
29 * A dynamic property that uses a Map of OGNL expressions to sets properties on the target object
31 * @author Graeme Rocher
32 * @since Oct 24, 2005
34 public class SetPropertiesDynamicProperty extends AbstractDynamicProperty {
36 private static final Log LOG = LogFactory.getLog( SetPropertiesDynamicProperty.class );
38 private static final String PROPERTY_NAME = "properties";
39 private TypeConverter converter = new SimpleTypeConverter();
41 public SetPropertiesDynamicProperty() {
42 super(PROPERTY_NAME);
45 /**
46 * @return A org.apache.commons.beanutils.BeanMap instance
48 public Object get(Object object) {
49 return DefaultGroovyMethods.getProperties(object);
52 /**
53 * Sets the property on the specified object with the specified value. The
54 * value is expected to be a Map containing OGNL expressions for the keys
55 * and objects for the values.
57 * @param object The target object
58 * @param newValue The value to set
60 public void set(Object object, Object newValue) {
61 if(newValue == null)
62 return;
64 BindingResult result = DataBindingUtils.bindObjectToInstance(object, newValue);
65 if(result == null)
66 throw new MissingPropertyException(PROPERTY_NAME,object.getClass());