Skip to content

Commit 2944d6c

Browse files
committed
Replace event status option with new option to read value from fylr object via custom function definition
1 parent 48ac98b commit 2944d6c

3 files changed

Lines changed: 25 additions & 87 deletions

File tree

l10n/custom-data-type-nfis-geometry.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ server.config.parameter.system.nfisGeoservices.wfs_configuration.geometry_fields
4646
server.config.parameter.system.nfisGeoservices.wfs_configuration.geometry_fields.edit_wfs_feature_type.label,WFS-Feature-Type für Datenübertragung,WFS feature type for data transfer
4747
server.config.parameter.system.nfisGeoservices.wfs_configuration.geometry_fields.style_uuid.label,UUID des Style-Objekts,Style object UUID
4848
server.config.parameter.system.nfisGeoservices.wfs_configuration.geometry_fields.fields.label,Zu übertragende Felddaten,Field data to be transferred
49-
server.config.parameter.system.nfisGeoservices.wfs_configuration.geometry_fields.fields.fylr_field_name.label,Name des Feldes im Fylr-Objekt (inkl. Pfad),Name of field in Fylr object (including path)
5049
server.config.parameter.system.nfisGeoservices.wfs_configuration.geometry_fields.fields.wfs_field_name.label,Name des WFS-Zielfeldes,Name of WFS target field
51-
server.config.parameter.system.nfisGeoservices.wfs_configuration.geometry_fields.wfs_event_status_field.label,WFS-Zielfeld für den Status des neuesten Ausweisungsereignisses,WFS target field for the status of the newest designation event
50+
server.config.parameter.system.nfisGeoservices.wfs_configuration.geometry_fields.fields.fylr_field_name.label,Name des Feldes im fylr-Objekt (inkl. Pfad),Name of field in fylr object (including path)
51+
server.config.parameter.system.nfisGeoservices.wfs_configuration.geometry_fields.fields.fylr_function.label,JavaScript-Funktion zum Auslesen des Wertes aus dem fylr-Objekt (Alternative zur Angabe des Feldnamens),JavaScript function for reading the value from the fylr object (Alternative to specifying the field name)
5252
server.config.parameter.system.nfisGeoservices.wfs_configuration.geometry_fields.wfs_pool_field.label,WFS-Zielfeld für die Poolbezeichnung,WFS target field for pool name
5353
server.config.parameter.system.nfisGeoservices.wfs_configuration.geometry_fields.allowed_pool_names.label,Erlaubte Poolbezeichnungen für Datenübertragung,Allowed pool names for data transfer
5454
base.error.realm.multipleGeometryLinking,Mehrfachverknüpfung von Geometrien nicht erlaubt,Multiple linking of geometries not permitted

manifest.master.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,26 +97,26 @@ base_config:
9797
- name: fields
9898
type: table
9999
fields:
100-
- name: fylr_field_name
100+
- name: wfs_field_name
101101
type: text
102102
position: 0
103-
- name: wfs_field_name
103+
- name: fylr_field_name
104104
type: text
105105
position: 1
106+
- name: fylr_function
107+
type: text
108+
position: 2
106109
position: 11
107-
- name: wfs_event_status_field
108-
type: text
109-
position: 12
110110
- name: wfs_pool_field
111111
type: text
112-
position: 13
112+
position: 12
113113
- name: allowed_pool_names
114114
type: table
115115
fields:
116116
- name: allowed_pool_name
117117
type: text
118118
position: 0
119-
position: 14
119+
position: 13
120120
position: 1
121121
position: 4
122122

src/server/sendDataToGeoserver.js

Lines changed: 16 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@ process.stdin.on('end', async () => {
1818
const data = JSON.parse(input);
1919
const configuration = await getPluginConfiguration();
2020
const authorizationString = getAuthorizationString(serverConfiguration);
21-
const yesId = await getYesId();
2221

2322
for (let object of data.objects) {
2423
await updateObject(
2524
object[object._objecttype],
2625
object._objecttype,
2726
object._uuid,
2827
configuration,
29-
authorizationString,
30-
yesId
28+
authorizationString
3129
);
3230
}
3331

@@ -72,33 +70,7 @@ function getAuthorizationString(serverConfiguration) {
7270
return btoa(username + ':' + password);
7371
}
7472

75-
async function getYesId() {
76-
const url = info.api_url + '/api/v1/search?access_token=' + info.api_user_access_token;
77-
const searchRequest = {
78-
search: [{
79-
type: 'match',
80-
bool: 'should',
81-
fields: ['ja_nein_objekttyp.name'],
82-
string: 'ja'
83-
}]
84-
};
85-
86-
try {
87-
const response = await fetch(url, {
88-
method: 'POST',
89-
headers: {
90-
'Content-Type': 'application/json'
91-
},
92-
body: JSON.stringify(searchRequest)
93-
});
94-
const result = await response.json();
95-
return result.objects?.[0]?.ja_nein_objekttyp._id;
96-
} catch (err) {
97-
throwErrorToFrontend('Search request failed', JSON.stringify(err));
98-
}
99-
}
100-
101-
async function updateObject(object, objectType, uuid, configuration, authorizationString, yesId) {
73+
async function updateObject(object, objectType, uuid, configuration, authorizationString) {
10274
const wfsConfiguration = getWFSConfiguration(configuration, objectType);
10375
if (!wfsConfiguration) return;
10476

@@ -110,7 +82,7 @@ async function updateObject(object, objectType, uuid, configuration, authorizati
11082

11183
const poolName = getPoolName(object, fieldConfiguration);
11284
if (isSendingDataToGeoserverActivated(fieldConfiguration, geometryIds, poolName)) {
113-
const changeMap = getChangeMap(object, objectType, fieldConfiguration, poolName, yesId);
85+
const changeMap = getChangeMap(object, fieldConfiguration, poolName);
11486
if (Object.keys(changeMap).length) {
11587
await performTransaction(
11688
geometryIds, changeMap, fieldConfiguration.edit_wfs_url.ValueText,
@@ -219,71 +191,37 @@ function getAllowedPoolNames(fieldConfiguration) {
219191
}) ?? [];
220192
}
221193

222-
function getChangeMap(object, objectType, fieldConfiguration, poolName, yesId) {
194+
function getChangeMap(object, fieldConfiguration, poolName) {
223195
const changeMap = {};
224196
addPoolFieldToChangeMap(fieldConfiguration, poolName, changeMap);
225-
addDesignationEventStatusFieldToChangeMap(object, objectType, fieldConfiguration, changeMap, yesId);
226197

227198
const fields = fieldConfiguration.fields?.ValueTable ?? [];
228199
return fields.reduce((result, field) => {
229200
const wfsFieldName = field.wfs_field_name.ValueText;
230201
const fylrFieldName = field.fylr_field_name.ValueText;
231-
const fieldValues = getFieldValues(object, fylrFieldName.split('.'));
232-
233-
addToChangeMap(wfsFieldName, fieldValues?.[0], result);
234-
202+
const fylrFunction = field.fylr_function.ValueText;
203+
if (fylrFieldName || fylrFunction) {
204+
const fieldValue = fylrFieldName
205+
? getFieldValues(object, fylrFieldName.split('.'))?.[0]
206+
: getValueFromCustomFunction(object, fylrFunction);
207+
addToChangeMap(wfsFieldName, fieldValue, result);
208+
}
235209
return result;
236210
}, changeMap);
237211
}
238212

213+
function getValueFromCustomFunction(object, functionDefinition) {
214+
const customFunction = new Function('object', functionDefinition);
215+
return customFunction(object);
216+
}
217+
239218
function addPoolFieldToChangeMap(fieldConfiguration, poolName, changeMap) {
240219
const targetFieldName = fieldConfiguration.wfs_pool_field.ValueText;
241220
if (!targetFieldName) return;
242221

243222
changeMap[targetFieldName] = poolName;
244223
}
245224

246-
function addDesignationEventStatusFieldToChangeMap(object, objectType, fieldConfiguration, changeMap, yesId) {
247-
const targetFieldName = fieldConfiguration.wfs_event_status_field.ValueText;
248-
if (!targetFieldName) return;
249-
250-
if (!yesId) throwErrorToFrontend('Missing value: ja', 'ja_nein_objekttyp is not set up correctly in Fylr database');
251-
252-
const latestEvent = getLatestDesignationEvent(object, objectType, yesId);
253-
if (!latestEvent) return;
254-
255-
addToChangeMap(targetFieldName, latestEvent.lk_status, changeMap);
256-
}
257-
258-
function getLatestDesignationEvent(object, objectType, yesId) {
259-
const events = getDesignationEvents(object, objectType);
260-
const latestEvent = getLatestEvent(events);
261-
262-
return latestEvent?.lk_veroeffentlichen?.ja_nein_objekttyp?._id === yesId
263-
? latestEvent
264-
: undefined;
265-
}
266-
267-
function getDesignationEvents(object, objectType) {
268-
return object['_nested:' + objectType + '__event']
269-
.filter(event => {
270-
return event.lk_eventtyp?.conceptName === 'Ausweisung'
271-
&& event.lk_status !== undefined
272-
&& event.datum_ausweisung_beginn?.value !== undefined;
273-
}
274-
);
275-
}
276-
277-
function getLatestEvent(events) {
278-
if (!events.length) return undefined;
279-
280-
events.sort((event1, event2) => {
281-
return new Date(event2.datum_ausweisung_beginn.value) - new Date(event1.datum_ausweisung_beginn.value);
282-
});
283-
284-
return events[0];
285-
}
286-
287225
function addToChangeMap(wfsFieldName, fieldValue, changeMap) {
288226
if (!fieldValue) return;
289227

0 commit comments

Comments
 (0)