001: /*******************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-api/api/src/java/org/sakaiproject/metaobj/shared/model/ElementListBeanWrapper.java $
003: * $Id: ElementListBeanWrapper.java 21196 2007-02-09 18:57:43Z john.ellis@rsmart.com $
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.shared.model;
021:
022: import org.sakaiproject.metaobj.utils.mvc.intf.FieldValueWrapper;
023: import org.sakaiproject.metaobj.utils.TypedMap;
024: import org.springframework.beans.BeanWrapper;
025: import org.springframework.beans.BeanWrapperImpl;
026:
027: import java.util.*;
028: import java.beans.PropertyDescriptor;
029: import java.beans.IntrospectionException;
030:
031: /**
032: * Created by IntelliJ IDEA.
033: * User: johnellis
034: * Date: Jan 15, 2007
035: * Time: 10:46:04 AM
036: * To change this template use File | Settings | File Templates.
037: */
038: public class ElementListBeanWrapper extends HashMap implements TypedMap {
039:
040: private ElementListBean list;
041: private FieldValueWrapper wrapper;
042: private List wrapperList;
043: private HashMap<Object, PropertyDescriptor> descriptors = new HashMap<Object, PropertyDescriptor>();
044:
045: public ElementListBeanWrapper(ElementListBean list,
046: FieldValueWrapper wrapper) {
047: this .list = list;
048: this .wrapper = wrapper;
049: this .wrapperList = new ArrayList();
050: }
051:
052: public Object get(Object key) {
053: return super .get(key);
054: }
055:
056: public Object put(Object key, Object newValue) {
057: List values = new ArrayList();
058:
059: if (newValue instanceof String[]) {
060: values = Arrays.asList((String[]) newValue);
061: } else if (newValue != null) {
062: values = new ArrayList();
063: values.add(newValue);
064: }
065:
066: processValues(key, values);
067:
068: return super .put(key, newValue);
069: }
070:
071: protected void processValues(Object key, List values) {
072: boolean createWrappers = false;
073: if (wrapperList.size() != values.size()) {
074: wrapperList.clear();
075: createWrappers = true;
076: }
077:
078: for (int i = 0; i < values.size(); i++) {
079: BeanWrapper wrapper;
080: if (createWrappers) {
081: wrapper = new BeanWrapperImpl(getWrapper().clone());
082: wrapperList.add(wrapper);
083: } else {
084: wrapper = (BeanWrapper) wrapperList.get(i);
085: }
086:
087: setValue(wrapper, key, values.get(i));
088: }
089:
090: }
091:
092: protected void setValue(BeanWrapper wrapper, Object key,
093: Object value) {
094: wrapper.setPropertyValue((String) key, value);
095: }
096:
097: public void validate(List errors) {
098: getList().clear();
099: for (Iterator<BeanWrapper> i = wrapperList.iterator(); i
100: .hasNext();) {
101: FieldValueWrapper fieldWrapper = (FieldValueWrapper) i
102: .next().getWrappedInstance();
103:
104: if (fieldWrapper.getValue() != null) {
105: ElementBean bean = getList().createBlank();
106: fieldWrapper.validate(
107: bean.getCurrentSchema().getName(), errors, bean
108: .getCurrentSchema().getLabel());
109: bean.getBaseElement().addContent(
110: bean.getCurrentSchema()
111: .getSchemaNormalizedValue(
112: fieldWrapper.getValue()));
113: getList().add(bean);
114: }
115: }
116: }
117:
118: protected PropertyDescriptor getPropertyDescriptor(Object key) {
119: PropertyDescriptor descriptor = descriptors.get(key);
120: if (descriptor == null) {
121: try {
122: descriptor = new PropertyDescriptor((String) key,
123: getWrapper().getClass());
124: descriptors.put(key, descriptor);
125: } catch (IntrospectionException e) {
126: throw new RuntimeException(e);
127: }
128: }
129: return descriptor;
130: }
131:
132: public ElementListBean getList() {
133: return list;
134: }
135:
136: public void setList(ElementListBean list) {
137: this .list = list;
138: }
139:
140: public FieldValueWrapper getWrapper() {
141: return wrapper;
142: }
143:
144: public void setWrapper(FieldValueWrapper wrapper) {
145: this .wrapper = wrapper;
146: }
147:
148: public Class getType(String key) {
149: return getPropertyDescriptor(key).getPropertyType();
150: }
151:
152: }
|