]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.gis/src/main/java/org/argeo/slc/geotools/BeanFeatureTypeBuilder.java
Introduce minimal GIS runtime
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.gis / src / main / java / org / argeo / slc / geotools / BeanFeatureTypeBuilder.java
1 package org.argeo.slc.geotools;
2
3 import java.beans.PropertyDescriptor;
4 import java.util.ArrayList;
5 import java.util.List;
6
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;
16
17 public class BeanFeatureTypeBuilder<T> implements FactoryBean {
18 private final BeanWrapper classBeanWrapper;
19
20 private SimpleFeatureType cachedFeatureType;
21 private List<String> cachedAttributeList;
22
23 public BeanFeatureTypeBuilder(Class<? extends T> clss) {
24 this.classBeanWrapper = new BeanWrapperImpl(clss);
25 cachedFeatureType = doBuildFeatureType();
26 }
27
28 protected SimpleFeatureType doBuildFeatureType() {
29 SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
30
31 builder
32 .setName(getClassBeanWrapper().getWrappedClass()
33 .getSimpleName());
34
35 cachedAttributeList = new ArrayList<String>();
36 for (PropertyDescriptor pd : getClassBeanWrapper()
37 .getPropertyDescriptors()) {
38 builder.add(pd.getName(), pd.getPropertyType());
39 cachedAttributeList.add(pd.getName());
40 }
41
42 // TODO: make it configurable
43 builder.setNamespaceURI("http://localhost/");
44 builder.setCRS(DefaultGeographicCRS.WGS84);
45
46 return builder.buildFeatureType();
47 }
48
49 public SimpleFeatureType getFeatureType() {
50 if (cachedFeatureType == null) {
51 cachedFeatureType = doBuildFeatureType();
52 }
53 return cachedFeatureType;
54 }
55
56 @SuppressWarnings("unchecked")
57 public Class<? extends T> getWrappedClass() {
58 return (Class<? extends T>) classBeanWrapper.getWrappedClass();
59 }
60
61 protected void resetFeatureType() {
62 cachedFeatureType = null;
63 if (cachedAttributeList != null) {
64 cachedAttributeList.clear();
65 cachedAttributeList = null;
66 }
67 }
68
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;
74 }
75
76 public SimpleFeature buildFeature(T object) {
77 return buildFeature(object, null);
78 }
79
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());
86 }
87
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));
93 }
94
95 return featureBuilder.buildFeature(id);
96 }
97
98 protected BeanWrapper getClassBeanWrapper() {
99 return classBeanWrapper;
100 }
101
102 public Object getObject() throws Exception {
103 return getFeatureType();
104 }
105
106 public Class<?> getObjectType() {
107 return classBeanWrapper.getWrappedClass();
108 }
109
110 public boolean isSingleton() {
111 return true;
112 }
113
114 }