]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.gis/src/main/java/org/argeo/slc/geotools/BeanFeatureTypeBuilder.java
Improve GPX importer
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.gis / src / main / java / org / argeo / slc / geotools / BeanFeatureTypeBuilder.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.slc.geotools;
18
19 import java.beans.PropertyDescriptor;
20 import java.util.ArrayList;
21 import java.util.List;
22
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;
32
33 public class BeanFeatureTypeBuilder<T> implements FactoryBean {
34 private final BeanWrapper classBeanWrapper;
35
36 private SimpleFeatureType cachedFeatureType;
37 private List<String> cachedAttributeList;
38
39 public BeanFeatureTypeBuilder(Class<? extends T> clss) {
40 this.classBeanWrapper = new BeanWrapperImpl(clss);
41 cachedFeatureType = doBuildFeatureType();
42 }
43
44 protected SimpleFeatureType doBuildFeatureType() {
45 SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
46
47 builder
48 .setName(getClassBeanWrapper().getWrappedClass()
49 .getSimpleName());
50
51 cachedAttributeList = new ArrayList<String>();
52 for (PropertyDescriptor pd : getClassBeanWrapper()
53 .getPropertyDescriptors()) {
54 builder.add(pd.getName(), pd.getPropertyType());
55 cachedAttributeList.add(pd.getName());
56 }
57
58 // TODO: make it configurable
59 builder.setNamespaceURI("http://localhost/");
60 builder.setCRS(DefaultGeographicCRS.WGS84);
61
62 return builder.buildFeatureType();
63 }
64
65 public SimpleFeatureType getFeatureType() {
66 if (cachedFeatureType == null) {
67 cachedFeatureType = doBuildFeatureType();
68 }
69 return cachedFeatureType;
70 }
71
72 @SuppressWarnings("unchecked")
73 public Class<? extends T> getWrappedClass() {
74 return (Class<? extends T>) classBeanWrapper.getWrappedClass();
75 }
76
77 protected void resetFeatureType() {
78 cachedFeatureType = null;
79 if (cachedAttributeList != null) {
80 cachedAttributeList.clear();
81 cachedAttributeList = null;
82 }
83 }
84
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;
90 }
91
92 public SimpleFeature buildFeature(T object) {
93 return buildFeature(object, null);
94 }
95
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());
102 }
103
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));
109 }
110
111 return featureBuilder.buildFeature(id);
112 }
113
114 protected BeanWrapper getClassBeanWrapper() {
115 return classBeanWrapper;
116 }
117
118 public Object getObject() throws Exception {
119 return getFeatureType();
120 }
121
122 public Class<?> getObjectType() {
123 return classBeanWrapper.getWrappedClass();
124 }
125
126 public boolean isSingleton() {
127 return true;
128 }
129
130 }