1
2
3
4
5
6 package org.tailormap.api.geotools.featuresources;
7
8 import static org.geotools.jdbc.JDBCDataStore.JDBC_PRIMARY_KEY_COLUMN;
9 import static org.geotools.jdbc.JDBCDataStore.JDBC_READ_ONLY;
10 import static org.tailormap.api.persistence.helper.GeoToolsHelper.crsToString;
11
12 import java.io.IOException;
13 import java.lang.invoke.MethodHandles;
14 import java.util.Arrays;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18 import org.apache.commons.lang3.StringUtils;
19 import org.geotools.api.data.DataStore;
20 import org.geotools.api.data.DataStoreFinder;
21 import org.geotools.api.data.ResourceInfo;
22 import org.geotools.api.data.SimpleFeatureSource;
23 import org.geotools.api.feature.simple.SimpleFeatureType;
24 import org.geotools.api.feature.type.AttributeDescriptor;
25 import org.geotools.api.feature.type.AttributeType;
26 import org.geotools.jdbc.JDBCFeatureStore;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.tailormap.api.persistence.TMFeatureSource;
30 import org.tailormap.api.persistence.TMFeatureType;
31 import org.tailormap.api.persistence.helper.GeoToolsHelper;
32 import org.tailormap.api.persistence.json.TMAttributeDescriptor;
33 import org.tailormap.api.persistence.json.TMAttributeType;
34 import org.tailormap.api.persistence.json.TMFeatureTypeInfo;
35 import org.tailormap.api.persistence.json.TMServiceCaps;
36 import org.tailormap.api.persistence.json.TMServiceInfo;
37
38 public abstract class FeatureSourceHelper {
39 private static final Logger logger =
40 LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
41
42 public DataStore createDataStore(TMFeatureSource tmfs) throws IOException {
43 return createDataStore(tmfs, null);
44 }
45
46
47
48
49
50
51
52
53
54
55 public abstract DataStore createDataStore(TMFeatureSource tmfs, Integer timeout) throws IOException;
56
57 public SimpleFeatureSource openGeoToolsFeatureSource(TMFeatureType tmft, Integer timeout) throws IOException {
58 DataStore ds = createDataStore(tmft.getFeatureSource(), timeout);
59 return ds.getFeatureSource(tmft.getName());
60 }
61
62 public void loadCapabilities(TMFeatureSource tmfs) throws IOException {
63 loadCapabilities(tmfs, null);
64 }
65
66 public DataStore openDatastore(Map<String, Object> params, String passwordKey) throws IOException {
67 Map<String, Object> logParams = new HashMap<>(params);
68 String passwd = (String) params.get(passwordKey);
69 if (passwd != null) {
70 logParams.put(passwordKey, String.valueOf(new char[passwd.length()]).replace("\0", "*"));
71 }
72 logger.debug("Opening datastore using parameters: {}", logParams);
73 DataStore ds;
74 try {
75 ds = DataStoreFinder.getDataStore(params);
76 } catch (Exception e) {
77 throw new IOException("Cannot open datastore using parameters: " + logParams, e);
78 }
79 if (ds == null) {
80 throw new IOException("No datastore found using parameters " + logParams);
81 }
82 return ds;
83 }
84
85 public void loadCapabilities(TMFeatureSource tmfs, Integer timeout) throws IOException {
86 DataStore ds = createDataStore(tmfs, timeout);
87 try {
88 if (StringUtils.isBlank(tmfs.getTitle())) {
89 tmfs.setTitle(ds.getInfo().getTitle());
90 }
91
92 org.geotools.api.data.ServiceInfo si = ds.getInfo();
93 tmfs.setServiceCapabilities(new TMServiceCaps()
94 .serviceInfo(new TMServiceInfo()
95 .title(si.getTitle())
96 .keywords(si.getKeywords())
97 .description(si.getDescription())
98 .publisher(si.getPublisher())
99 .schema(si.getSchema())
100 .source(si.getSource())));
101
102 List<String> typeNames = Arrays.asList(ds.getTypeNames());
103 logger.info(
104 "Type names for {} {}: {}",
105 tmfs.getProtocol().getValue(),
106 tmfs.getProtocol() == TMFeatureSource.Protocol.WFS ? tmfs.getUrl() : tmfs.getJdbcConnection(),
107 typeNames);
108
109 tmfs.getFeatureTypes().removeIf(tmft -> {
110 if (!typeNames.contains(tmft.getName())) {
111 logger.info("Feature type removed: {}", tmft.getName());
112 return true;
113 } else {
114 return false;
115 }
116 });
117
118 for (String typeName : typeNames) {
119 TMFeatureType pft = tmfs.getFeatureTypes().stream()
120 .filter(ft -> ft.getName().equals(typeName))
121 .findFirst()
122 .orElseGet(() -> new TMFeatureType().setName(typeName).setFeatureSource(tmfs));
123 if (!tmfs.getFeatureTypes().contains(pft)) {
124 tmfs.getFeatureTypes().add(pft);
125 }
126 try {
127 logger.debug("Get feature source from GeoTools datastore for type \"{}\"", typeName);
128 SimpleFeatureSource gtFs = ds.getFeatureSource(typeName);
129 ResourceInfo info = gtFs.getInfo();
130 if (info != null) {
131 pft.setTitle(info.getTitle());
132 pft.setInfo(getFeatureTypeInfo(pft, info, gtFs));
133 pft.getAttributes().clear();
134
135 SimpleFeatureType gtFt = gtFs.getSchema();
136 pft.setWriteable(gtFs instanceof JDBCFeatureStore
137 && !Boolean.TRUE.equals(gtFt.getUserData().get(JDBC_READ_ONLY)));
138 String primaryKeyName = null;
139 for (AttributeDescriptor gtAttr : gtFt.getAttributeDescriptors()) {
140 AttributeType type = gtAttr.getType();
141 if (Boolean.TRUE.equals(gtAttr.getUserData().get(JDBC_PRIMARY_KEY_COLUMN))) {
142 if (primaryKeyName == null) {
143 logger.debug(
144 "Found primary key attribute \"{}\" for type \"{}\"",
145 gtAttr.getLocalName(),
146 typeName);
147 primaryKeyName = gtAttr.getLocalName();
148 } else {
149 logger.warn(
150 "Multiple primary key attributes found for type \"{}\": \"{}\" and \"{}\". Composite primary keys are not supported for writing at the moment, setting as read-only.",
151 typeName,
152 primaryKeyName,
153 gtAttr.getLocalName());
154 pft.setWriteable(false);
155 }
156 }
157 TMAttributeDescriptor tmAttr = new TMAttributeDescriptor()
158 .name(gtAttr.getLocalName())
159 .type(GeoToolsHelper.toAttributeType(type))
160 .nullable(gtAttr.isNillable())
161 .defaultValue(
162 gtAttr.getDefaultValue() == null
163 ? null
164 : gtAttr.getDefaultValue().toString())
165 .description(
166 type.getDescription() == null
167 ? null
168 : type.getDescription().toString());
169 if (tmAttr.getType() == TMAttributeType.OBJECT) {
170 tmAttr.setUnknownTypeClassName(type.getBinding().getName());
171 }
172 pft.getAttributes().add(tmAttr);
173 }
174 pft.setPrimaryKeyAttribute(primaryKeyName);
175 pft.setDefaultGeometryAttribute(pft.findDefaultGeometryAttribute());
176 }
177 } catch (Exception e) {
178 logger.error("Exception reading feature type \"{}\"", typeName, e);
179 }
180 }
181 } finally {
182 ds.dispose();
183 }
184 }
185
186 protected TMFeatureTypeInfo getFeatureTypeInfo(TMFeatureType pft, ResourceInfo info, SimpleFeatureSource gtFs) {
187 return new TMFeatureTypeInfo()
188 .keywords(info.getKeywords())
189 .description(info.getDescription())
190 .bounds(GeoToolsHelper.fromEnvelope(info.getBounds()))
191 .crs(crsToString(info.getCRS()));
192 }
193 }