-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathFieldService.groovy
312 lines (268 loc) · 11.2 KB
/
FieldService.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**************************************************************************
* Copyright (C) 2010 Atlas of Living Australia
* All Rights Reserved.
* <p>
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
* <p>
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
***************************************************************************/
package au.org.ala.spatial
import au.org.ala.spatial.dto.IntersectionFile
import groovy.sql.Sql
import org.codehaus.jackson.map.DeserializationConfig
import org.codehaus.jackson.map.ObjectMapper
import org.postgresql.core.Field
import java.sql.ResultSet
class FieldService {
LayerService layerService
SpatialObjectsService spatialObjectsService
def dataSource
SpatialConfig spatialConfig
Fields getFieldById(String id, boolean enabledFieldsOnly = true) {
log.debug("Getting enabled field info for id = " + id)
String sql = "select *, number_of_objects from fields, (select count(*) as number_of_objects from objects where fid = :id ) o where id = :id "
if (enabledFieldsOnly) {
sql += " and enabled=true"
}
Fields field = null
Sql.newInstance(dataSource).query(sql, [id: id], { ResultSet rs ->
if (rs.next()) {
field = new Fields()
rs.fields.each { Field f ->
if (field.properties.containsKey(f.columnLabel)) {
field.properties.putAt(f.columnLabel, rs.getObject(f.columnLabel))
}
}
if ("a".equalsIgnoreCase(field.type) || "b".equalsIgnoreCase(field.type)) {
// fetch object count for this 'grid as contextual'
IntersectionFile f = layerService.getIntersectionFile(id)
if (f != null && f.getClasses() != null) {
field.number_of_objects = f.getClasses().size()
}
}
}
})
field
}
List<Fields> getFieldsByDB() {
log.debug("Getting a list of all enabled fields with indb")
Fields.findAllByEnabledAndIndb(true, true)
}
/**
* Return the count of fields of a layer, no matter they are enabled or not
* @return
*/
int countBySpid(spid) {
Fields.countBySpid(spid)
}
/**
* Return the largest sequence number + 1.
* Avoid getting the incorrect seq if some of the records in the middle are deleted
* @param spid
* @return
*/
def calculateNextSequenceId(spid) {
def requestIds = Fields.findAllBySpid(spid).collect { it.id }
if (requestIds.size() == 0) {
return ''
} else {
def maxSequenceNumber = requestIds
.findAll { it.endsWith("${spid}") }
.collect { it.replaceFirst(/^.{2}/, '')
.replaceAll("${spid}", "") }
.collect {it == '' ? 0 : it.toInteger()}
.max()
maxSequenceNumber + 1
}
}
// synchronized void addField(Field field) {
// log.debug("Add new field for " + field.getName())
//
// Map<String, Object> parameters = field.toMap()
// parameters.remove("id")
// parameters.remove("layer")
//
// //calc new fieldId
// String idPrefix = "Contextual".equalsIgnoreCase(layerDao.getLayerById(Integer.parseInt(field.getSpid()), false).getType())
// ? "cl" : "el"
//
// //test for requested id
// String newId = field.getId()
//
// if (newId == null || getFieldById(newId) != null) {
// newId = getFieldById(idPrefix + field.getSpid()) == null ? idPrefix + field.getSpid() : null
// if (newId == null) {
// //calculate next field Id using general form: prefix (n x 1000 + layerId)
// String idEnd = field.getSpid()
// while (idEnd.length() < 3) {
// idEnd = "0" + idEnd
// }
// int maxNFound = 0
// for (Field f : getFields(false)) {
// if (f.getId().startsWith(idPrefix) && f.getId().endsWith(idEnd)) {
// if (f.getId().length() - idEnd.length() > 2) {
// int n = Integer.parseInt(f.getId().substring(2, f.getId().length() - idEnd.length()))
// if (n > maxNFound) {
// maxNFound = n
// }
// }
// }
// }
//
// newId = idPrefix + (maxNFound + 1) + idEnd
// }
// }
//
// parameters.put("id", newId)
// //fix for field 'desc' and 'intersect'
// if (parameters.containsKey("desc")) {
// parameters.put("\"desc\"", parameters.get("desc"))
// parameters.remove("desc")
// }
// if (parameters.containsKey("intersect")) {
// parameters.put("\"intersect\"", parameters.get("intersect"))
// parameters.remove("intersect")
// }
//
// insertField.execute(parameters)
//
// field.setId(newId)
// }
//
//
void delete(String fieldId) {
Fields f = getFieldById(fieldId, false)
if (f != null) {
Sql.newInstance(dataSource).execute("delete from objects where fid=?", [f.getId()] as List<Object>)
Sql.newInstance(dataSource).execute("delete from fields where id=?", [f.getId()] as List<Object>)
}
}
void updateField(Fields field) {
log.debug("Updating field metadata for " + field.getName())
String sql = "update fields set name=:name, " +
"\"desc\"=:desc, type=:type, " +
"spid=:spid, sname=:sname, " +
"sdesc=:sdesc, indb=:indb, enabled=:enabled, " +
"namesearch=:namesearch, defaultlayer=:defaultlayer, " +
"\"intersect\"=:intersect, layerbranch=:layerbranch, analysis=:analysis," +
" addtomap=:addtomap where id=:id"
Map map = field.properties
map.put('id', field.id)
Sql.newInstance(dataSource).executeUpdate(sql, map)
}
List<Layers> getLayersByCriteria(String keywords) {
mapsToLayers(getByKeywords(keywords))
}
List<Fields> getByKeywords(String keywords) {
log.debug("Getting a list of all enabled fields by criteria: " + keywords)
String sql = ""
sql += "select f.*, l.* from fields f inner join layers l on f.spid = l.id || '' where "
sql += "l.enabled=true AND f.enabled=true AND ( "
sql += "l.keywords ilike :keywords "
sql += "or l.displayname ilike :keywords "
sql += "or l.name ilike :keywords "
sql += "or l.domain ilike :keywords "
sql += "or f.name ilike :keywords "
sql += ") order by f.name "
keywords = keywords == null ? "%" : ("%" + keywords.toLowerCase() + "%")
List<Fields> fields = new ArrayList()
Sql.newInstance(dataSource).query(sql, [keywords: keywords], {
while (it.next()) {
Fields field = new Fields()
Layers layer = new Layers()
int fieldTableOid = 0
it.fields.eachWithIndex { Field fname, Integer idx ->
// field first, then layer
if (idx > 0 && fieldTableOid != fname.tableOid) {
if (layer.properties.containsKey(fname.columnLabel)) {
layer.setProperty(fname.columnLabel, it.getObject(idx + 1))
}
} else {
if (field.properties.containsKey(fname.columnLabel)) {
field.setProperty(fname.columnLabel, it.getObject(idx + 1))
}
fieldTableOid = fname.tableOid
}
}
updateDisplayPath(layer)
layer.displaypath = layer.displaypath.replace("&styles=", "") + "&style=" + field.id
// field name will be unique but layer display name may not be
layer.displayname = field.name
field.layer = layer
fields.add(field)
}
})
fields
}
List<Fields> getFieldsByCriteria(String keywords) {
getByKeywords(keywords)
}
private static List<Layers> mapsToLayers(List<Fields> fields) {
fields.collect { it.layer }.asList()
}
private List<Fields> mapsToFields(List<Map<String, Object>> maps) {
List<Fields> list = new ArrayList<Fields>()
ObjectMapper om = new ObjectMapper()
om.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false)
for (Map<String, Object> map : maps) {
try {
Map field = new HashMap()
Map layer = new HashMap()
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getKey().startsWith("layer_"))
layer.put(entry.getKey().substring("layer_".length()), entry.getValue())
else field.put(entry.getKey(), entry.getValue())
}
Fields f = om.readValue(om.writeValueAsString(field), Fields.class)
Layers l = om.readValue(om.writeValueAsString(layer), Layers.class)
updateDisplayPath(l)
f.setLayer(l)
l.setDisplaypath(l.getDisplaypath().replace("&styles=", "") + "&style=" + f.getId())
list.add(f)
} catch (Exception e) {
log.error("failed to read field/layer " + map.get("id"), e)
}
}
return list
}
List<Fields> getFields(boolean includeAdmin = false) {
if (includeAdmin) {
Fields.findAll()
} else {
Fields.findAllByEnabled(true)
}
}
void updateDisplayPaths(List<Layers> layers) {
if (layers == null) {
return
}
for (Layers layer : layers) {
updateDisplayPath(layer)
}
}
void updateDisplayPath(Layers layer) {
if (layer && layer.getDisplaypath() != null) {
if (!layer.getDisplaypath().startsWith("/")) {
layer.setDisplaypath(layer.getDisplaypath().replace(DistributionsService.GEOSERVER_URL_PLACEHOLDER, spatialConfig.geoserver.url))
} else {
layer.setDisplaypath(spatialConfig.geoserver.url + layer.getDisplaypath())
}
}
}
Fields get(String id, String q, int start, int pageSize) {
//test field id value
Fields field = getFieldById(id, false)
if (field) {
//include field objects
log.debug('field id: ' + id)
field.objects = spatialObjectsService.getObjectsById(id, start, pageSize, q)
}
field
}
}