001: // Copyright 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal.services;
016:
017: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newList;
018: import static org.apache.tapestry.ioc.internal.util.Defense.notNull;
019:
020: import java.util.List;
021:
022: import org.apache.tapestry.ComponentResources;
023: import org.apache.tapestry.beaneditor.BeanModel;
024: import org.apache.tapestry.beaneditor.NonVisual;
025: import org.apache.tapestry.internal.TapestryInternalUtils;
026: import org.apache.tapestry.internal.beaneditor.BeanModelImpl;
027: import org.apache.tapestry.ioc.Messages;
028: import org.apache.tapestry.ioc.services.ClassFactory;
029: import org.apache.tapestry.ioc.services.ClassPropertyAdapter;
030: import org.apache.tapestry.ioc.services.PropertyAccess;
031: import org.apache.tapestry.ioc.services.PropertyAdapter;
032: import org.apache.tapestry.ioc.services.TypeCoercer;
033: import org.apache.tapestry.services.BeanModelSource;
034: import org.apache.tapestry.services.DataTypeAnalyzer;
035: import org.apache.tapestry.services.PropertyConduitSource;
036:
037: public class BeanModelSourceImpl implements BeanModelSource {
038: private final TypeCoercer _typeCoercer;
039:
040: private final PropertyAccess _propertyAccess;
041:
042: private final PropertyConduitSource _propertyConduitSource;
043:
044: private final ClassFactory _classFactory;
045:
046: private final DataTypeAnalyzer _dataTypeAnalyzer;
047:
048: public BeanModelSourceImpl(TypeCoercer typeCoercer,
049: PropertyAccess propertyAccess,
050: PropertyConduitSource propertyConduitSource,
051: ClassFactory classFactory, DataTypeAnalyzer dataTypeAnalyzer) {
052: _typeCoercer = typeCoercer;
053: _propertyAccess = propertyAccess;
054: _propertyConduitSource = propertyConduitSource;
055: _classFactory = classFactory;
056: _dataTypeAnalyzer = dataTypeAnalyzer;
057: }
058:
059: public BeanModel create(Class beanClass,
060: boolean filterReadOnlyProperties,
061: ComponentResources resources) {
062: notNull(beanClass, "beanClass");
063: notNull(resources, "resources");
064:
065: Messages messages = resources.getMessages();
066:
067: ClassPropertyAdapter adapter = _propertyAccess
068: .getAdapter(beanClass);
069:
070: BeanModel model = new BeanModelImpl(beanClass,
071: _propertyConduitSource, _typeCoercer, messages);
072:
073: List<String> propertyNames = newList();
074:
075: for (String propertyName : adapter.getPropertyNames()) {
076: PropertyAdapter pa = adapter
077: .getPropertyAdapter(propertyName);
078:
079: if (!pa.isRead())
080: continue;
081:
082: if (pa.getAnnotation(NonVisual.class) != null)
083: continue;
084:
085: if (filterReadOnlyProperties && !pa.isUpdate())
086: continue;
087:
088: String dataType = _dataTypeAnalyzer.identifyDataType(pa);
089:
090: // If an unregistered type, then ignore the property.
091:
092: if (dataType == null)
093: continue;
094:
095: model.add(propertyName).dataType(dataType);
096:
097: propertyNames.add(propertyName);
098: }
099:
100: // Set default property order for properties that are not explicit.
101:
102: List<String> orderedNames = TapestryInternalUtils
103: .orderProperties(adapter, _classFactory, propertyNames);
104:
105: for (int i = 0; i < orderedNames.size(); i++) {
106: String propertyName = orderedNames.get(i);
107:
108: model.get(propertyName).order(i);
109: }
110:
111: return model;
112: }
113: }
|