001: /*
002: * (c) Copyright 2007 by Volker Bergmann. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, is permitted under the terms of the
006: * GNU General Public License.
007: *
008: * For redistributing this software or a derivative work under a license other
009: * than the GPL-compatible Free Software License as defined by the Free
010: * Software Foundation or approved by OSI, you must first obtain a commercial
011: * license to this software product from Volker Bergmann.
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
014: * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
015: * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
016: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
017: * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
018: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
019: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
020: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
021: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
022: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
023: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGE.
025: */
026:
027: package org.databene.platform.bean;
028:
029: import org.databene.commons.BeanUtil;
030: import org.databene.commons.ConfigurationError;
031: import org.databene.model.data.EntityDescriptor;
032: import org.databene.model.data.AttributeDescriptor;
033: import org.databene.model.data.DescriptorProvider;
034: import org.databene.model.data.TypeMapper;
035:
036: import java.beans.PropertyDescriptor;
037: import java.math.BigDecimal;
038: import java.math.BigInteger;
039:
040: /**
041: * Provides EntityDescriptors for JavaBeanClasses
042: * Created: 27.06.2007 23:04:19
043: */
044: public class BeanDescriptorProvider implements DescriptorProvider {
045:
046: private TypeMapper<Class<? extends Object>> mapper;
047:
048: public BeanDescriptorProvider() {
049: mapper = new TypeMapper<Class<? extends Object>>("byte",
050: byte.class, "byte", Byte.class,
051:
052: "short", short.class, "short", Short.class,
053:
054: "int", int.class, "int", Integer.class,
055:
056: "long", long.class, "long", Long.class,
057:
058: "big_integer", BigInteger.class,
059:
060: "float", float.class, "float", Float.class,
061:
062: "double", double.class, "double", Double.class,
063:
064: "big_decimal", BigDecimal.class,
065:
066: "boolean", boolean.class, "boolean", Boolean.class,
067:
068: "char", char.class, "char", Character.class,
069:
070: "date", java.util.Date.class, "timestamp",
071: java.sql.Timestamp.class,
072:
073: "string", String.class, "object", Object.class,
074: "binary", byte[].class);
075: }
076:
077: public EntityDescriptor[] getTypeDescriptors() {
078: return new EntityDescriptor[0]; // There are way too many candidates
079: }
080:
081: public EntityDescriptor getTypeDescriptor(String typeName) {
082: return createTypeDescriptor(typeName);
083: }
084:
085: /*
086: public String getType(Object bean) {
087: return bean.getClass().getName();
088: }
089:
090: public String[] getFeatureNames(Object bean) {
091: PropertyDescriptor[] descriptors = BeanUtil.getPropertyDescriptors(bean.getClass());
092: String[] propertyNames = new String[descriptors.length - 1];
093: int i = 0;
094: for (PropertyDescriptor descriptor : descriptors)
095: if (!"class".equals(descriptor.getName()))
096: propertyNames[i++] = descriptor.getName();
097: return propertyNames;
098: }
099: */
100: // private helpers -------------------------------------------------------------------------------------------------
101: /**
102: * @param concreteType
103: * @return
104: * @see org.databene.model.data.TypeMapper#abstractType(java.lang.Object)
105: */
106: public String abstractType(Class<? extends Object> concreteType) {
107: return mapper.abstractType(concreteType);
108: }
109:
110: /**
111: * @param abstractType
112: * @return
113: * @see org.databene.model.data.TypeMapper#concreteType(java.lang.String)
114: */
115: public Class<? extends Object> concreteType(String abstractType) {
116: try {
117: Class<? extends Object> result = mapper
118: .concreteType(abstractType);
119: if (result == null)
120: result = Class.forName(abstractType);
121: return result;
122: } catch (ClassNotFoundException e) {
123: throw new ConfigurationError("No class mapping found for '"
124: + abstractType + "'", e);
125: }
126: }
127:
128: private EntityDescriptor createTypeDescriptor(String typeName) {
129: Class<? extends Object> beanClass = BeanUtil.forName(typeName);
130: EntityDescriptor td = new EntityDescriptor(typeName, true);
131: for (PropertyDescriptor propertyDescriptor : BeanUtil
132: .getPropertyDescriptors(beanClass)) {
133: if ("class".equals(propertyDescriptor.getName()))
134: continue;
135: AttributeDescriptor ad = new AttributeDescriptor(
136: propertyDescriptor.getName());
137: Class<?> propertyType = propertyDescriptor
138: .getPropertyType();
139: String abstractType = mapper.abstractType(propertyType);
140: if (abstractType == null)
141: abstractType = propertyType.getName();
142: ad.setType(abstractType);
143: td.setComponentDescriptor(ad);
144: }
145: return td;
146: }
147:
148: // private helpers -------------------------------------------------------------------------------------------------
149:
150: public Class<? extends Object> javaTypeForAbstractType(
151: String abstractType) {
152: Class<? extends Object> type = mapper
153: .concreteType(abstractType);
154: if (type == null)
155: throw new UnsupportedOperationException(
156: "Not mapped to a Java type: " + abstractType);
157: return type;
158: }
159:
160: }
|