1 package org
.argeo
.slc
.geotools
;
3 import java
.beans
.PropertyDescriptor
;
4 import java
.util
.ArrayList
;
7 import org
.argeo
.slc
.SlcException
;
8 import org
.geotools
.feature
.simple
.SimpleFeatureBuilder
;
9 import org
.geotools
.feature
.simple
.SimpleFeatureTypeBuilder
;
10 import org
.geotools
.referencing
.crs
.DefaultGeographicCRS
;
11 import org
.opengis
.feature
.simple
.SimpleFeature
;
12 import org
.opengis
.feature
.simple
.SimpleFeatureType
;
13 import org
.springframework
.beans
.BeanWrapper
;
14 import org
.springframework
.beans
.BeanWrapperImpl
;
15 import org
.springframework
.beans
.factory
.FactoryBean
;
17 public class BeanFeatureTypeBuilder
<T
> implements FactoryBean
{
18 private final BeanWrapper classBeanWrapper
;
20 private SimpleFeatureType cachedFeatureType
;
21 private List
<String
> cachedAttributeList
;
23 public BeanFeatureTypeBuilder(Class
<?
extends T
> clss
) {
24 this.classBeanWrapper
= new BeanWrapperImpl(clss
);
25 cachedFeatureType
= doBuildFeatureType();
28 protected SimpleFeatureType
doBuildFeatureType() {
29 SimpleFeatureTypeBuilder builder
= new SimpleFeatureTypeBuilder();
32 .setName(getClassBeanWrapper().getWrappedClass()
35 cachedAttributeList
= new ArrayList
<String
>();
36 for (PropertyDescriptor pd
: getClassBeanWrapper()
37 .getPropertyDescriptors()) {
38 builder
.add(pd
.getName(), pd
.getPropertyType());
39 cachedAttributeList
.add(pd
.getName());
42 // TODO: make it configurable
43 builder
.setNamespaceURI("http://localhost/");
44 builder
.setCRS(DefaultGeographicCRS
.WGS84
);
46 return builder
.buildFeatureType();
49 public SimpleFeatureType
getFeatureType() {
50 if (cachedFeatureType
== null) {
51 cachedFeatureType
= doBuildFeatureType();
53 return cachedFeatureType
;
56 @SuppressWarnings("unchecked")
57 public Class
<?
extends T
> getWrappedClass() {
58 return (Class
<?
extends T
>) classBeanWrapper
.getWrappedClass();
61 protected void resetFeatureType() {
62 cachedFeatureType
= null;
63 if (cachedAttributeList
!= null) {
64 cachedAttributeList
.clear();
65 cachedAttributeList
= null;
69 protected List
<String
> getCachedAttributeList() {
70 if (cachedAttributeList
== null)
71 throw new SlcException(
72 "Cached attribute list not set: initialize the object properly before calling this method");
73 return cachedAttributeList
;
76 public SimpleFeature
buildFeature(T object
) {
77 return buildFeature(object
, null);
80 public SimpleFeature
buildFeature(Object object
, String id
) {
81 if (!((Class
<?
>) classBeanWrapper
.getWrappedClass())
82 .isAssignableFrom(object
.getClass())) {
83 throw new SlcException("Object type " + object
.getClass()
84 + " not compatible with wrapped class "
85 + classBeanWrapper
.getWrappedClass());
88 BeanWrapper instanceWrapper
= new BeanWrapperImpl(object
);
89 SimpleFeatureType type
= getFeatureType();
90 SimpleFeatureBuilder featureBuilder
= new SimpleFeatureBuilder(type
);
91 for (String attr
: getCachedAttributeList()) {
92 featureBuilder
.add(instanceWrapper
.getPropertyValue(attr
));
95 return featureBuilder
.buildFeature(id
);
98 protected BeanWrapper
getClassBeanWrapper() {
99 return classBeanWrapper
;
102 public Object
getObject() throws Exception
{
103 return getFeatureType();
106 public Class
<?
> getObjectType() {
107 return classBeanWrapper
.getWrappedClass();
110 public boolean isSingleton() {