001    /*
002     * Copyright 2002-2004 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.binding.form.support;
017    
018    import java.util.Map;
019    
020    import org.springframework.beans.BeansException;
021    import org.springframework.binding.PropertyAccessStrategy;
022    import org.springframework.binding.PropertyMetadataAccessStrategy;
023    import org.springframework.binding.form.FormModel;
024    
025    /**
026     * Adapts the properties of <code>FormModel</code> so that they are accessible using the 
027     * <code>PropertyAccessStrategy</code> interface.
028     * 
029     * @author Oliver Hutchison
030     */
031    public class FormModelPropertyAccessStrategy implements PropertyAccessStrategy {
032    
033        private final FormModel formModel;
034    
035        public FormModelPropertyAccessStrategy(FormModel formModel) {
036            this.formModel = formModel;
037        }
038    
039        public Object getPropertyValue(String propertyPath) throws BeansException {
040            return formModel.getValueModel(propertyPath).getValue();
041        }
042    
043        public PropertyMetadataAccessStrategy getMetadataAccessStrategy() {
044            return new FormModelPropertyMetadataAccessStrategy();
045        }
046    
047        public Object getDomainObject() {
048            return formModel.getFormObject();
049        }
050    
051        private class FormModelPropertyMetadataAccessStrategy implements PropertyMetadataAccessStrategy {
052    
053            private FormModelPropertyMetadataAccessStrategy() {            
054            }
055    
056            /**
057             * @return Always true, current implementation doesn't allow for non-readable properties.
058             * @see org.springframework.binding.form.FieldMetadata
059             */
060            public boolean isReadable(String propertyName) {
061                return true;
062            }
063    
064            /**
065             * @return True if property isn't readOnly
066             */
067            public boolean isWriteable(String propertyName) {
068                return !formModel.getFieldMetadata(propertyName).isReadOnly();
069            }
070    
071            public Class getPropertyType(String propertyName) {
072                return formModel.getFieldMetadata(propertyName).getPropertyType();
073            }
074    
075            public Object getUserMetadata(String propertyName, String key) {
076                return formModel.getFieldMetadata(propertyName).getUserMetadata(key);
077            }
078      
079            public Map getAllUserMetadata(String propertyName) {
080                return formModel.getFieldMetadata(propertyName).getAllUserMetadata();
081            }
082        }
083    }