2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.slc
.geotools
;
19 import java
.beans
.PropertyDescriptor
;
20 import java
.util
.ArrayList
;
21 import java
.util
.List
;
23 import org
.argeo
.slc
.SlcException
;
24 import org
.geotools
.feature
.simple
.SimpleFeatureBuilder
;
25 import org
.geotools
.feature
.simple
.SimpleFeatureTypeBuilder
;
26 import org
.geotools
.referencing
.crs
.DefaultGeographicCRS
;
27 import org
.opengis
.feature
.simple
.SimpleFeature
;
28 import org
.opengis
.feature
.simple
.SimpleFeatureType
;
29 import org
.springframework
.beans
.BeanWrapper
;
30 import org
.springframework
.beans
.BeanWrapperImpl
;
31 import org
.springframework
.beans
.factory
.FactoryBean
;
33 public class BeanFeatureTypeBuilder
<T
> implements FactoryBean
{
34 private final BeanWrapper classBeanWrapper
;
36 private SimpleFeatureType cachedFeatureType
;
37 private List
<String
> cachedAttributeList
;
39 public BeanFeatureTypeBuilder(Class
<?
extends T
> clss
) {
40 this.classBeanWrapper
= new BeanWrapperImpl(clss
);
41 cachedFeatureType
= doBuildFeatureType();
44 protected SimpleFeatureType
doBuildFeatureType() {
45 SimpleFeatureTypeBuilder builder
= new SimpleFeatureTypeBuilder();
48 .setName(getClassBeanWrapper().getWrappedClass()
51 cachedAttributeList
= new ArrayList
<String
>();
52 for (PropertyDescriptor pd
: getClassBeanWrapper()
53 .getPropertyDescriptors()) {
54 builder
.add(pd
.getName(), pd
.getPropertyType());
55 cachedAttributeList
.add(pd
.getName());
58 // TODO: make it configurable
59 builder
.setNamespaceURI("http://localhost/");
60 builder
.setCRS(DefaultGeographicCRS
.WGS84
);
62 return builder
.buildFeatureType();
65 public SimpleFeatureType
getFeatureType() {
66 if (cachedFeatureType
== null) {
67 cachedFeatureType
= doBuildFeatureType();
69 return cachedFeatureType
;
72 @SuppressWarnings("unchecked")
73 public Class
<?
extends T
> getWrappedClass() {
74 return (Class
<?
extends T
>) classBeanWrapper
.getWrappedClass();
77 protected void resetFeatureType() {
78 cachedFeatureType
= null;
79 if (cachedAttributeList
!= null) {
80 cachedAttributeList
.clear();
81 cachedAttributeList
= null;
85 protected List
<String
> getCachedAttributeList() {
86 if (cachedAttributeList
== null)
87 throw new SlcException(
88 "Cached attribute list not set: initialize the object properly before calling this method");
89 return cachedAttributeList
;
92 public SimpleFeature
buildFeature(T object
) {
93 return buildFeature(object
, null);
96 public SimpleFeature
buildFeature(Object object
, String id
) {
97 if (!((Class
<?
>) classBeanWrapper
.getWrappedClass())
98 .isAssignableFrom(object
.getClass())) {
99 throw new SlcException("Object type " + object
.getClass()
100 + " not compatible with wrapped class "
101 + classBeanWrapper
.getWrappedClass());
104 BeanWrapper instanceWrapper
= new BeanWrapperImpl(object
);
105 SimpleFeatureType type
= getFeatureType();
106 SimpleFeatureBuilder featureBuilder
= new SimpleFeatureBuilder(type
);
107 for (String attr
: getCachedAttributeList()) {
108 featureBuilder
.add(instanceWrapper
.getPropertyValue(attr
));
111 return featureBuilder
.buildFeature(id
);
114 protected BeanWrapper
getClassBeanWrapper() {
115 return classBeanWrapper
;
118 public Object
getObject() throws Exception
{
119 return getFeatureType();
122 public Class
<?
> getObjectType() {
123 return classBeanWrapper
.getWrappedClass();
126 public boolean isSingleton() {