@@ -16,22 +16,26 @@ process.stdin.on('data', d => {
1616
1717process . stdin . on ( 'end' , async ( ) => {
1818 const data = JSON . parse ( input ) ;
19+ const changedObjects = [ ] ;
1920
2021 for ( let object of data . objects ) {
2122 await updateObject (
2223 getObjectData ( object ) ,
2324 object . _current ? getObjectData ( object . _current ) : undefined
2425 ) ;
26+ if ( await handleNewlyDrawnGeometries ( object ) ) changedObjects . push ( object ) ;
2527 }
2628
27- console . log ( JSON . stringify ( { objects : [ ] } ) ) ;
28- console . error ( 'No changes' ) ;
29- process . exit ( 0 ) ;
30- return ;
29+ console . log ( JSON . stringify ( { objects : changedObjects } ) ) ;
30+
31+ if ( ! changedObjects . length ) {
32+ console . error ( 'No changes' ) ;
33+ process . exit ( 0 ) ;
34+ }
3135} ) ;
3236
3337function getObjectData ( object ) {
34- const objectData = object [ object . _objecttype ] ;
38+ const objectData = JSON . parse ( JSON . stringify ( object [ object . _objecttype ] ) ) ;
3539 objectData . _uuid = object . _uuid ;
3640 objectData . _objecttype = object . _objecttype ;
3741 return objectData ;
@@ -46,16 +50,8 @@ function getWFSConfiguration(configuration, objectType) {
4650 return wfsConfiguration ?. find ( configuration => configuration . object_type === objectType ) ;
4751}
4852
49- function getAuthorizationString ( configuration ) {
50- const username = configuration . geoserver_write_username ;
51- const password = configuration . geoserver_write_password ;
52-
53- return btoa ( username + ':' + password ) ;
54- }
55-
5653async function updateObject ( object , currentObject ) {
5754 const configuration = getPluginConfiguration ( ) ;
58- const authorizationString = getAuthorizationString ( configuration ) ;
5955
6056 if ( currentObject ) addDataFromCurrentObject ( object , currentObject ) ;
6157 addToObjectCache ( object ) ;
@@ -72,8 +68,8 @@ async function updateObject(object, currentObject) {
7268 return throwErrorToFrontend ( 'Eine oder mehrere Geometrien sind bereits mit anderen Objekten verknüpft.' , undefined , 'multipleGeometryLinking' ) ;
7369 }
7470
75- await editGeometries ( object , fieldConfiguration , geometryIds , authorizationString ) ;
76- if ( currentObject ) await deleteGeometries ( fieldConfiguration , geometryIds , currentObject , authorizationString ) ;
71+ await editGeometries ( object , fieldConfiguration , geometryIds ) ;
72+ if ( currentObject ) await deleteGeometries ( fieldConfiguration , geometryIds , currentObject ) ;
7773 }
7874}
7975
@@ -145,19 +141,20 @@ function getGeometryFieldPaths(configuration) {
145141 return fieldPaths ;
146142}
147143
148- async function editGeometries ( object , fieldConfiguration , geometryIds , authorizationString ) {
144+ async function editGeometries ( object , fieldConfiguration , geometryIds ) {
149145 if ( isSendingDataToGeoserverActivated ( fieldConfiguration , geometryIds ) ) {
150146 const changeMap = await getChangeMap ( object , fieldConfiguration ) ;
151147 if ( Object . keys ( changeMap ) . length ) {
152- await performEditTransaction ( geometryIds , changeMap , fieldConfiguration , authorizationString ) ;
148+ const requestXml = getEditRequestXml ( geometryIds , changeMap , fieldConfiguration . edit_wfs_feature_type ) ;
149+ await performEditTransaction ( geometryIds , requestXml , fieldConfiguration ) ;
153150 }
154151 }
155152}
156153
157- async function deleteGeometries ( fieldConfiguration , geometryIds , currentObject , authorizationString ) {
154+ async function deleteGeometries ( fieldConfiguration , geometryIds , currentObject ) {
158155 if ( ! currentObject ) return ;
159156 const deletedGeometryIds = await getDeletedGeometryIds ( geometryIds , currentObject , fieldConfiguration ) ;
160- if ( deletedGeometryIds . length ) await performDeleteTransaction ( deletedGeometryIds , fieldConfiguration , authorizationString ) ;
157+ if ( deletedGeometryIds . length ) await performDeleteTransaction ( deletedGeometryIds , fieldConfiguration ) ;
161158}
162159
163160async function getDeletedGeometryIds ( geometryIds , currentObject , fieldConfiguration ) {
@@ -332,39 +329,34 @@ function isDanteConcept(fieldValue) {
332329 && fieldValue . conceptURI !== undefined && typeof fieldValue . conceptURI === 'string' ;
333330}
334331
335- async function performEditTransaction ( geometryIds , changeMap , fieldConfiguration , authorizationString ) {
336- const result = await performTransaction (
337- getEditRequestXml ( geometryIds , changeMap , fieldConfiguration . edit_wfs_feature_type ) ,
338- fieldConfiguration . edit_wfs_url ,
339- authorizationString
340- ) ;
332+ async function performEditTransaction ( geometryIds , requestXml , fieldConfiguration ) {
333+ const result = await performTransaction ( requestXml , fieldConfiguration . edit_wfs_url ) ;
341334
342335 if ( ! new RegExp ( '<wfs:totalUpdated>' + geometryIds . length + '<\/wfs:totalUpdated>' ) . test ( result ) ) {
343336 throwErrorToFrontend ( 'Bei der Aktualisierung von Geometrie-Datensätzen ist ein Fehler aufgetreten:' , result ) ;
344337 }
345338}
346339
347- async function performDeleteTransaction ( geometryIds , fieldConfiguration , authorizationString ) {
340+ async function performDeleteTransaction ( geometryIds , fieldConfiguration ) {
348341 const result = await performTransaction (
349342 getDeleteRequestXml ( geometryIds , fieldConfiguration . edit_wfs_feature_type ) ,
350- fieldConfiguration . edit_wfs_url ,
351- authorizationString
343+ fieldConfiguration . edit_wfs_url
352344 ) ;
353345
354346 if ( ! new RegExp ( '<wfs:totalDeleted>' + geometryIds . length + '<\/wfs:totalDeleted>' ) . test ( result ) ) {
355347 throwErrorToFrontend ( 'Beim Löschen von Geometrie-Datensätzen ist ein Fehler aufgetreten:' , result ) ;
356348 }
357349}
358350
359- async function performTransaction ( requestXml , wfsUrl , authorizationString ) {
360- const transactionUrl = wfsUrl + '?service=WFS&version=1.1.0&request=Transaction' ;
351+ async function performTransaction ( requestXml , wfsUrl ) {
352+ const transactionUrl = wfsUrl + '?service=WFS&version=1.1.0&request=Transaction' ; ;
361353
362354 try {
363355 const response = await fetch ( transactionUrl , {
364356 method : 'POST' ,
365357 headers : {
366358 'Content-Type' : 'application/xml' ,
367- 'Authorization' : 'Basic ' + authorizationString
359+ 'Authorization' : 'Basic ' + getAuthorizationString ( getPluginConfiguration ( ) )
368360 } ,
369361 body : requestXml
370362 } ) ;
@@ -383,6 +375,18 @@ function getEditRequestXml(geometryIds, changeMap, featureType) {
383375 ) ;
384376}
385377
378+ function getMarkAsTemporaryRequestXml ( geometryIds , propertyName , featureType ) {
379+ return getTransactionXml (
380+ '<wfs:Update typeName="' + featureType + '">'
381+ + '<wfs:Property>'
382+ + '<wfs:Name>' + propertyName + '</wfs:Name>'
383+ + '<wfs:Value>true</wfs:Value>'
384+ + '</wfs:Property>'
385+ + getFilterXml ( geometryIds )
386+ + '</wfs:Update>'
387+ ) ;
388+ }
389+
386390function getDeleteRequestXml ( geometryIds , featureType ) {
387391 return getTransactionXml (
388392 '<wfs:Delete typeName="' + featureType + '">'
@@ -431,6 +435,44 @@ function getGeometryFilterXml(geometryId) {
431435 + '</ogc:PropertyIsEqualTo>' ;
432436}
433437
438+ async function handleNewlyDrawnGeometries ( object ) {
439+ const configuration = getPluginConfiguration ( ) ;
440+ const wfsConfiguration = getWFSConfiguration ( configuration , object . _objecttype ) ;
441+ if ( ! wfsConfiguration ) return false ;
442+
443+ const wfsTemporaryGeometryFieldName = configuration . wfs_temporary_geometry_field_name ;
444+ const temporaryGeometryTagId = configuration . temporary_geometry_tag_id ;
445+
446+ let changed = false ;
447+ for ( let fieldConfiguration of wfsConfiguration . geometry_fields ) {
448+ for ( let fieldValue of await getFieldValues ( object [ object . _objecttype ] , fieldConfiguration . field_path . split ( '.' ) ) ) {
449+ const newlyDrawnGeometryIds = fieldValue . newly_drawn_geometry_ids ;
450+ delete fieldValue . newly_drawn_geometry_ids ;
451+ changed = true ;
452+ if ( ! newlyDrawnGeometryIds ?. length ) continue ;
453+
454+ if ( wfsTemporaryGeometryFieldName ) {
455+ await markGeometriesAsTemporary ( newlyDrawnGeometryIds , fieldConfiguration , wfsTemporaryGeometryFieldName ) ;
456+ }
457+ if ( temporaryGeometryTagId ) object . _tags = [ { _id : temporaryGeometryTagId } ] ;
458+ }
459+ }
460+
461+ return changed ;
462+ }
463+
464+ async function markGeometriesAsTemporary ( geometryIds , fieldConfiguration , wfsTemporaryGeometryFieldName ) {
465+ const requestXml = getMarkAsTemporaryRequestXml ( geometryIds , wfsTemporaryGeometryFieldName , fieldConfiguration . edit_wfs_feature_type ) ;
466+ await performEditTransaction ( geometryIds , requestXml , fieldConfiguration ) ;
467+ }
468+
469+ function getAuthorizationString ( configuration ) {
470+ const username = configuration . geoserver_write_username ;
471+ const password = configuration . geoserver_write_password ;
472+
473+ return btoa ( username + ':' + password ) ;
474+ }
475+
434476function throwErrorToFrontend ( error , description , realm ) {
435477 console . log ( JSON . stringify ( {
436478 error : {
0 commit comments