11import { ArcGISPluginConfig } from "./ArcGISPluginConfig" ;
22import { ArcObjects } from './ArcObjects' ;
33import { ArcObservation , ArcAttachment } from './ArcObservation' ;
4- import { HttpClient } from './HttpClient' ;
54import { LayerInfo } from "./LayerInfo" ;
65import { EditResult } from './EditResult' ;
76import { AttachmentInfosResult , AttachmentInfo } from './AttachmentInfosResult' ;
87import environment from '@ngageoint/mage.service/lib/environment/env'
98import fs from 'fs'
109import path from 'path'
1110import FormData from 'form-data' ;
12- import { ArcGISIdentityManager , request } from "@esri/arcgis-rest-request"
11+ import { ArcGISIdentityManager , IFeature , request } from "@esri/arcgis-rest-request"
12+ import { addFeatures , updateFeatures , deleteFeatures , getAttachments , updateAttachment , addAttachment , deleteAttachments } from "@esri/arcgis-rest-feature-service" ;
1313
1414/**
1515 * Class that transforms observations into a json string that can then be sent to an arcgis server.
@@ -36,11 +36,6 @@ export class ObservationsSender {
3636 */
3737 private _console : Console ;
3838
39- /**
40- * Used to send the observations to an arc server.
41- */
42- private _httpClient : HttpClient ;
43-
4439 /**
4540 * The attachment base directory
4641 */
@@ -64,7 +59,6 @@ export class ObservationsSender {
6459 this . _urlAdd = this . _url + '/addFeatures' ;
6560 this . _urlUpdate = this . _url + '/updateFeatures' ;
6661 this . _console = console ;
67- this . _httpClient = new HttpClient ( console , layerInfo . token ) ;
6862 this . _attachmentDirectory = environment . attachmentBaseDirectory ;
6963 this . _config = config ;
7064 this . _identityManager = identityManager ;
@@ -76,13 +70,14 @@ export class ObservationsSender {
7670 * @param observations The observations to convert.
7771 */
7872 sendAdds ( observations : ArcObjects ) {
79- const contentString = 'gdbVersion=&rollbackOnFailure=true&timeReferenceUnknownClient=false&features=' + JSON . stringify ( observations . objects ) ;
80-
8173 this . _console . info ( 'ArcGIS addFeatures url ' + this . _urlAdd ) ;
82- this . _console . info ( 'ArcGIS addFeatures content ' + contentString ) ;
8374
84- let responseHandler = this . addResponseHandler ( observations )
85- this . _httpClient . sendPostHandleResponse ( this . _urlAdd , contentString , responseHandler ) ;
75+ let responseHandler = this . addResponseHandler ( observations ) ;
76+ addFeatures ( {
77+ url : this . _url ,
78+ authentication : this . _identityManager ,
79+ features : observations . objects as IFeature [ ]
80+ } ) . then ( responseHandler ) . catch ( ( error ) => this . _console . error ( error ) ) ;
8681 }
8782
8883 /**
@@ -92,30 +87,29 @@ export class ObservationsSender {
9287 * @returns The json string of the observations.
9388 */
9489 sendUpdates ( observations : ArcObjects ) {
95- const contentString = 'gdbVersion=&rollbackOnFailure=true&timeReferenceUnknownClient=false&features=' + JSON . stringify ( observations . objects ) ;
96-
9790 this . _console . info ( 'ArcGIS updateFeatures url ' + this . _urlUpdate ) ;
98- this . _console . info ( 'ArcGIS updateFeatures content ' + contentString ) ;
9991
100- let responseHandler = this . updateResponseHandler ( observations )
101- this . _httpClient . sendPostHandleResponse ( this . _urlUpdate , contentString , responseHandler ) ;
92+ let responseHandler = this . updateResponseHandler ( observations ) ;
93+ updateFeatures ( {
94+ url : this . _url ,
95+ authentication : this . _identityManager ,
96+ features : observations . objects as IFeature [ ]
97+ } ) . then ( responseHandler ) . catch ( ( error ) => this . _console . error ( error ) ) ;
10298 }
10399
104100 /**
105101 * Delete an observation.
106102 * @param id The observation id.
107103 */
108- sendDelete ( id : string ) {
109-
104+ sendDelete ( id : number ) {
110105 const url = this . _url + '/deleteFeatures'
111-
112106 this . _console . info ( 'ArcGIS deleteFeatures url ' + url + ', ' + this . _config . observationIdField + ': ' + id )
113107
114- const form = new FormData ( )
115- form . append ( 'where' , this . _config . observationIdField + ' LIKE\'' + id + "%\'" )
116-
117- this . _httpClient . sendPostForm ( url , form )
118-
108+ deleteFeatures ( {
109+ url ,
110+ authentication : this . _identityManager ,
111+ objectIds : [ id ]
112+ } ) . catch ( ( error ) => this . _console . error ( error ) ) ;
119113 }
120114
121115 /**
@@ -136,8 +130,7 @@ export class ObservationsSender {
136130 form . append ( 'where' , this . _config . eventIdField + '=' + id )
137131 }
138132
139- this . _httpClient . sendPostForm ( url , form )
140-
133+ this . sendDelete ( id ) ;
141134 }
142135
143136 /**
@@ -167,8 +160,8 @@ export class ObservationsSender {
167160 private responseHandler ( observations : ArcObjects , update : boolean ) : ( chunk : any ) => void {
168161 const console = this . _console
169162 return ( chunk : any ) => {
170- console . log ( 'ArcGIS ' + ( update ? 'Update' : 'Add' ) + ' Response: ' + chunk )
171- const response = JSON . parse ( chunk )
163+ console . log ( 'ArcGIS ' + ( update ? 'Update' : 'Add' ) + ' Response: ' + JSON . stringify ( chunk ) )
164+ const response = chunk
172165 const results = response [ update ? 'updateResults' : 'addResults' ] as EditResult [ ]
173166 if ( results != null ) {
174167 const obs = observations . observations
@@ -213,15 +206,17 @@ export class ObservationsSender {
213206 * @param objectId The arc object id of the observation.
214207 */
215208 private queryAndUpdateAttachments ( observation : ArcObservation , objectId : number ) {
216-
217209 // Query for existing attachments
218210 const queryUrl = this . _url + '/' + objectId + '/attachments'
219- this . _httpClient . sendGetHandleResponse ( queryUrl , ( chunk ) => {
220- this . _console . info ( 'ArcGIS response for ' + queryUrl + ' ' + chunk )
221- const result = JSON . parse ( chunk ) as AttachmentInfosResult
211+ getAttachments ( {
212+ url : this . _url ,
213+ authentication : this . _identityManager ,
214+ featureId : objectId
215+ } ) . then ( ( response ) => {
216+ this . _console . info ( 'ArcGIS response for ' + queryUrl + ' ' + response )
217+ const result = response as AttachmentInfosResult
222218 this . updateAttachments ( observation , objectId , result . attachmentInfos )
223- } )
224-
219+ } ) . catch ( ( error ) => this . _console . error ( error ) ) ;
225220 }
226221
227222 /**
@@ -274,55 +269,45 @@ export class ObservationsSender {
274269 * @param attachment The observation attachment.
275270 * @param objectId The arc object id of the observation.
276271 */
277- private sendAttachment ( attachment : ArcAttachment , objectId : number ) {
278- this . sendAttachmentPost ( attachment , objectId , 'addAttachment' , new FormData ( ) )
279- }
272+ private async sendAttachment ( attachment : ArcAttachment , objectId : number ) {
273+ const file = path . join ( this . _attachmentDirectory , attachment . contentLocator ! )
280274
281- /**
282- * Update an observation attachment.
283- * @param attachment The observation attachment.
284- * @param objectId The arc object id of the observation.
285- * @param attachmentId The observation arc attachment id.
286- */
287- private updateAttachment ( attachment : ArcAttachment , objectId : number , attachmentId : number ) {
288-
289- const form = new FormData ( )
290- form . append ( 'attachmentId' , attachmentId )
275+ const fileName = this . attachmentFileName ( attachment )
276+ this . _console . info ( 'ArcGIS ' + request + ' file ' + fileName + ' from ' + file )
291277
292- this . sendAttachmentPost ( attachment , objectId , 'updateAttachment' , form )
278+ const readStream = await fs . openAsBlob ( file )
279+ const test = new File ( [ readStream ] , fileName )
293280
281+ addAttachment ( {
282+ url : this . _url ,
283+ authentication : this . _identityManager ,
284+ featureId : objectId ,
285+ attachment : test
286+ } ) . catch ( ( error ) => this . _console . error ( error ) ) ;
294287 }
295288
296289 /**
297- * Send an observation attachment post request .
290+ * Update an observation attachment.
298291 * @param attachment The observation attachment.
299292 * @param objectId The arc object id of the observation.
300- * @param request The attachment request type.
301- * @param form The request form data
293+ * @param attachmentId The observation arc attachment id.
302294 */
303- private sendAttachmentPost ( attachment : ArcAttachment , objectId : number , request : string , form : FormData ) {
304-
305- if ( attachment . contentLocator != null ) {
306-
307- const url = this . _url + '/' + objectId + '/' + request
308-
309- const file = path . join ( this . _attachmentDirectory , attachment . contentLocator ! )
310-
311- const fileName = this . attachmentFileName ( attachment )
312-
313- this . _console . info ( 'ArcGIS ' + request + ' url ' + url )
314- this . _console . info ( 'ArcGIS ' + request + ' file ' + fileName + ' from ' + file )
315-
316- const readStream = fs . createReadStream ( file )
317-
318- form . append ( 'attachment' , readStream , {
319- filename : fileName
320- } )
321-
322- this . _httpClient . sendPostForm ( url , form )
323-
324- }
325-
295+ private async updateAttachment ( attachment : ArcAttachment , objectId : number , attachmentId : number ) {
296+ const file = path . join ( this . _attachmentDirectory , attachment . contentLocator ! )
297+
298+ const fileName = this . attachmentFileName ( attachment )
299+ this . _console . info ( 'ArcGIS ' + request + ' file ' + fileName + ' from ' + file )
300+
301+ const readStream = await fs . openAsBlob ( file )
302+ const test = new File ( [ readStream ] , fileName )
303+
304+ updateAttachment ( {
305+ url : this . _url ,
306+ authentication : this . _identityManager ,
307+ featureId : objectId ,
308+ attachmentId,
309+ attachment : test
310+ } ) . catch ( ( error ) => this . _console . error ( error ) ) ;
326311 }
327312
328313 /**
@@ -338,7 +323,7 @@ export class ObservationsSender {
338323 attachmentIds . push ( attachmentInfo . id )
339324 }
340325
341- this . deleteAttachmentIds ( objectId , attachmentIds )
326+ this . deleteAttachmentIds ( objectId , attachmentIds ) ;
342327 }
343328
344329 /**
@@ -347,24 +332,14 @@ export class ObservationsSender {
347332 * @param attachmentIds The arc attachment ids.
348333 */
349334 private deleteAttachmentIds ( objectId : number , attachmentIds : number [ ] ) {
350-
351- const url = this . _url + '/' + objectId + '/deleteAttachments'
352-
353- let ids = ''
354- for ( const id of attachmentIds ) {
355- if ( ids . length > 0 ) {
356- ids += ', '
357- }
358- ids += id
359- }
360-
361- this . _console . info ( 'ArcGIS deleteAttachments url ' + url + ', ids: ' + ids )
362-
363- const form = new FormData ( )
364- form . append ( 'attachmentIds' , ids )
365-
366- this . _httpClient . sendPostForm ( url , form )
367-
335+ this . _console . info ( 'ArcGIS deleteAttachments ' + attachmentIds )
336+
337+ deleteAttachments ( {
338+ url : this . _url ,
339+ authentication : this . _identityManager ,
340+ featureId : objectId ,
341+ attachmentIds
342+ } ) . catch ( ( error ) => this . _console . error ( error ) ) ;
368343 }
369344
370345 /**
0 commit comments