/* * Copyright (C) 2010 Mathieu Baudier * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.argeo.slc.geotools; import java.beans.PropertyDescriptor; import java.util.ArrayList; import java.util.List; import org.argeo.slc.SlcException; import org.geotools.feature.simple.SimpleFeatureBuilder; import org.geotools.feature.simple.SimpleFeatureTypeBuilder; import org.geotools.referencing.crs.DefaultGeographicCRS; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; import org.springframework.beans.BeanWrapper; import org.springframework.beans.BeanWrapperImpl; import org.springframework.beans.factory.FactoryBean; public class BeanFeatureTypeBuilder implements FactoryBean { private final BeanWrapper classBeanWrapper; private SimpleFeatureType cachedFeatureType; private List cachedAttributeList; public BeanFeatureTypeBuilder(Class clss) { this.classBeanWrapper = new BeanWrapperImpl(clss); cachedFeatureType = doBuildFeatureType(); } protected SimpleFeatureType doBuildFeatureType() { SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); builder .setName(getClassBeanWrapper().getWrappedClass() .getSimpleName()); cachedAttributeList = new ArrayList(); for (PropertyDescriptor pd : getClassBeanWrapper() .getPropertyDescriptors()) { builder.add(pd.getName(), pd.getPropertyType()); cachedAttributeList.add(pd.getName()); } // TODO: make it configurable builder.setNamespaceURI("http://localhost/"); builder.setCRS(DefaultGeographicCRS.WGS84); return builder.buildFeatureType(); } public SimpleFeatureType getFeatureType() { if (cachedFeatureType == null) { cachedFeatureType = doBuildFeatureType(); } return cachedFeatureType; } @SuppressWarnings("unchecked") public Class getWrappedClass() { return (Class) classBeanWrapper.getWrappedClass(); } protected void resetFeatureType() { cachedFeatureType = null; if (cachedAttributeList != null) { cachedAttributeList.clear(); cachedAttributeList = null; } } protected List getCachedAttributeList() { if (cachedAttributeList == null) throw new SlcException( "Cached attribute list not set: initialize the object properly before calling this method"); return cachedAttributeList; } public SimpleFeature buildFeature(T object) { return buildFeature(object, null); } public SimpleFeature buildFeature(Object object, String id) { if (!((Class) classBeanWrapper.getWrappedClass()) .isAssignableFrom(object.getClass())) { throw new SlcException("Object type " + object.getClass() + " not compatible with wrapped class " + classBeanWrapper.getWrappedClass()); } BeanWrapper instanceWrapper = new BeanWrapperImpl(object); SimpleFeatureType type = getFeatureType(); SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(type); for (String attr : getCachedAttributeList()) { featureBuilder.add(instanceWrapper.getPropertyValue(attr)); } return featureBuilder.buildFeature(id); } protected BeanWrapper getClassBeanWrapper() { return classBeanWrapper; } public Object getObject() throws Exception { return getFeatureType(); } public Class getObjectType() { return classBeanWrapper.getWrappedClass(); } public boolean isSingleton() { return true; } }