001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-util/tool-lib/src/java/org/sakaiproject/metaobj/utils/mvc/impl/MapWrapper.java $
003: * $Id: MapWrapper.java 14230 2006-09-05 18:02:51Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.metaobj.utils.mvc.impl;
021:
022: import java.beans.PropertyEditor;
023: import java.util.Map;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.sakaiproject.metaobj.utils.TypedMap;
028: import org.springframework.beans.BeanWrapperImpl;
029: import org.springframework.beans.BeansException;
030: import org.springframework.beans.FatalBeanException;
031:
032: public class MapWrapper extends BeanWrapperBase {
033: protected final Log logger = LogFactory.getLog(getClass());
034:
035: public MapWrapper() {
036: }
037:
038: /**
039: * Create new BeanWrapperImpl for the given object.
040: *
041: * @param object object wrapped by this BeanWrapper.
042: * @throws BeansException if the object cannot be wrapped by a BeanWrapper
043: */
044: public MapWrapper(Object object) throws BeansException {
045: super .setWrappedInstance(object);
046: }
047:
048: /**
049: * Create new BeanWrapperImpl for the given object,
050: * registering a nested path that the object is in.
051: *
052: * @param object object wrapped by this BeanWrapper.
053: * @param nestedPath the nested path of the object
054: * @param rootObject the root object at the top of the path
055: * @throws org.springframework.beans.BeansException
056: * if the object cannot be wrapped by a BeanWrapper
057: */
058: public MapWrapper(Map object, String nestedPath, Object rootObject)
059: throws BeansException {
060: super (object, nestedPath, rootObject);
061: }
062:
063: public Object getPropertyValue(String propertyName)
064: throws BeansException {
065: if (!(getWrappedInstance() instanceof Map)) {
066: throw new FatalBeanException(
067: getWrappedInstance().getClass()
068: + ": bean is not a map, BeanWrapperImpl might be a better choice");
069: }
070:
071: if (isNestedProperty(propertyName)) {
072: return super .getPropertyValue(propertyName);
073: } else {
074: return ((Map) getWrappedInstance()).get(propertyName);
075: }
076: }
077:
078: protected BeanWrapperImpl createNestedWrapper(String parentPath,
079: String nestedProperty) {
080: Map map = (Map) getWrappedInstance();
081:
082: if (map instanceof TypedMap
083: && java.util.Map.class
084: .isAssignableFrom(((TypedMap) map)
085: .getType(nestedProperty))) {
086: return new MapWrapper((Map) map.get(nestedProperty),
087: parentPath + NESTED_PROPERTY_SEPARATOR
088: + nestedProperty, getWrappedInstance());
089: } else {
090: return super
091: .createNestedWrapper(parentPath, nestedProperty);
092: }
093: }
094:
095: protected BeanWrapperBase constructWrapper(Object propertyValue,
096: String nestedProperty) {
097: return new MixedBeanWrapper(propertyValue, nestedProperty,
098: getWrappedInstance());
099: }
100:
101: public void setPropertyValue(String propertyName, Object value)
102: throws BeansException {
103:
104: if (!(getWrappedInstance() instanceof Map)) {
105: throw new FatalBeanException(
106: getWrappedInstance().getClass()
107: + ": bean is not a map, BeanWrapperImpl might be a better choice");
108: }
109:
110: Map map = (Map) getWrappedInstance();
111:
112: if (isNestedProperty(propertyName)) {
113: super .setPropertyValue(propertyName, value);
114: return;
115: }
116:
117: if (!(map instanceof TypedMap)
118: || ((TypedMap) map).getType(propertyName) == null) {
119: return;
120: }
121:
122: map.put(propertyName, value);
123: }
124:
125: public PropertyEditor findCustomEditor(Class requiredType,
126: String propertyPath) {
127: return null;
128: }
129:
130: public Class getPropertyType(String propertyName)
131: throws BeansException {
132: if (!(getWrappedInstance() instanceof TypedMap)) {
133: return String.class;
134: }
135: return ((TypedMap) getWrappedInstance()).getType(propertyName);
136: }
137:
138: }
|