UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

3 lines (2 loc) • 14.3 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{__decorate as e}from"tslib";import r from"../core/Accessor.js";import o from"../core/Collection.js";import t from"../core/Error.js";import i from"../core/Logger.js";import{watch as s,on as n}from"../core/reactiveUtils.js";import{removeTrailingSlash as a}from"../core/urlUtils.js";import{property as d,subclass as l}from"../core/accessorSupport/decorators.js";import{UpdatingHandles as u}from"../core/support/UpdatingHandles.js";import{getOwningPortalUrl as h}from"../layers/support/layerUtils.js";import p from"../portal/Portal.js";import{hasPrivilege as v}from"../portal/support/utils.js";import m from"../rest/featureService/FeatureService.js";import{createFeatureServices as c}from"../rest/featureService/utils.js";import{getVersioningStates as g}from"./utils.js";import w from"./VersioningState.js";import f from"./VersionManagementService.js";import{createVersionAdapters as _}from"./versionAdapters/utils.js";let E=class extends r{constructor(e){super(e),this._portalLookup=new Map,this._updatingHandlesLoad=new u,this._updatingHandlesExecute=new u,this.executionError=void 0,this.featureServiceLookup=new Map,this.loadError=void 0,this.serverVersionLookup=new Map,this.serviceNameLookup=new Map,this.userLookup=new Map,this.versionAdministratorLookup=new Map,this.versioningStateLookup=new Map,this.view=null,this.versioningStates=null}initialize(){this._viewChangeHandle(),this.addHandles([s(()=>this.view,()=>{this._resetAllLookupProperties(),this._viewChangeHandle()}),s(()=>this.versioningStates,()=>{this._resetVersioningLookupProperties(),this._setVersioningStatesLookups()})])}destroy(){this._updatingHandlesLoad.destroy(),this._updatingHandlesExecute.destroy()}get state(){return this._updatingHandlesLoad.updating?"loading":this.loadError?"disabled":this._updatingHandlesExecute.updating?"executing":this.executionError?"failed":"ready"}async alterVersion(e){return this._updatingHandlesExecute.addPromise(this._alterVersionInternal(e))}async changeVersion(e,r,o){return this._updatingHandlesExecute.addPromise(this._changeVersionInternal(e,r,o))}async createVersion(e){return this._updatingHandlesExecute.addPromise(this._createVersionInternal(e))}async deleteVersion(e,r,o){return this._updatingHandlesExecute.addPromise(this._deleteVersionInternal(e,r,o))}async getVersionInfos(e){return this._updatingHandlesExecute.addPromise(this._getVersionInfosInternal(e))}async _alterVersionInternal(e){if("disabled"===this.state)throw this._logError("version-management-view-model:load-error",this.loadError),new t("version-management-view-model:load-error",this.loadError??"");this._setExecutionError();const r=this.versioningStateLookup.get(e.featureServerUrl);if(!r)throw this._logError("version-management-view-model:execution-error","no-versioning-state-found"),new t("version-management-view-model:execution-error",this.executionError??"");if(!this.serverVersionLookup.has(e.featureServerUrl)||this.serverVersionLookup.get(e.featureServerUrl)<=11.1)throw this._logError("version-management-view-model:execution-error","no-valid-enterprise-version"),new t("version-management-view-model:execution-error",this.executionError??"");const{featureServerUrl:o,versionIdentifier:i,...s}=e,n=await r.alterVersion(i,s).catch(e=>{throw this._logExecutionError(e),e});return await this.getVersionInfos(o),n}async _changeVersionInternal(e,r,o){if("disabled"===this.state)throw this._logError("version-management-view-model:load-error",this.loadError),new t("version-management-view-model:load-error",this.loadError??"");this._setExecutionError();const i=this.versioningStateLookup.get(e);if(!i)throw this._logError("version-management-view-model:execution-error","no-versioning-state-found"),new t("version-management-view-model:execution-error",this.executionError??"");const s={name:r,guid:o};return await i.changeVersion(s).catch(e=>{throw this._logExecutionError(e),e})}_updateVersionableItems(e){this._updatingHandlesLoad.addPromise((async()=>{_(e.added).forEach(e=>{const r=e.featureServiceUrl,o=this.versioningStateLookup.get(r);o&&(o.versionableItems.find(r=>r.versionableItem===e.versionableItem)||o.versionableItems.add(e))});_(e.removed).forEach(e=>{const{allLayers:r,allTables:o}=this.view.map,t=e.featureServiceUrl,i=this.versioningStateLookup.get(t);if(!i)return void this._logError("version-management-view-model:execution-error","no-version-management-service-found");const s=e=>("feature"===e.type||"subtype-group"===e.type)&&(e.url?.includes(t)??!1);[...r.filter(s),...o.filter(s)].length||(this.featureServiceLookup.delete(t),this.serviceNameLookup.delete(t),this.versioningStateLookup.delete(t),this.versioningStates.remove(i));const n=i.versionableItems.find(r=>r.versionableItem===e.versionableItem);n&&i.versionableItems.remove(n)})})())}async _createVersionInternal(e){if("disabled"===this.state)throw this._logError("version-management-view-model:load-error",this.loadError),new t("version-management-view-model:load-error",this.loadError??"");this._setExecutionError();const r=e.featureServerUrl,o=this.versioningStateLookup.get(r);if(!o)throw this._logError("version-management-view-model:execution-error","no-versioning-state-found"),new t("version-management-view-model:execution-error",this.executionError??"");const i=this.userLookup.get(r).toUpperCase(),s=this._isEmptyString(e.ownerName)?i:e.ownerName?.trim().toUpperCase();if(s!==i&&this.serverVersionLookup.get(r)<=11.1)throw this._logError("version-management-view-model:execution-error","versioning-api-error"),new t("version-management-view-model:execution-error",this.executionError??"");let n=await(this.versioningStateLookup.get(r)?.getVersionInfos());if("SDE"===s?.toUpperCase()&&"DEFAULT"===e.versionName.toUpperCase()||n?.find(r=>r.versionIdentifier.name.toUpperCase()===(s+"."+e.versionName).toUpperCase()||r.versionIdentifier.name.toUpperCase()===(i+"."+e.versionName).toUpperCase()))throw this._logError("version-management-view-model:execution-error","no-valid-version-name"),new t("version-management-view-model:execution-error",this.executionError??"");const a=await o.versionManagementService.createVersion({versionName:e.versionName,access:s!==i?"public":e.access,description:e.description}).catch(e=>{throw this._logExecutionError(e),e});if(s!==i){const{guid:o,name:t}=a.versionIdentifier,n={featureServerUrl:r,versionIdentifier:{guid:o,name:t},access:e.access,ownerName:s};await this.alterVersion(n)||this.deleteVersion(r,i+"."+e.versionName,a.versionIdentifier.guid)}return await this.getVersionInfos(r),n=await(this.versioningStateLookup.get(r)?.getVersionInfos()),n?.find(e=>e.versionIdentifier.guid===a.versionIdentifier.guid)}async _deleteVersionInternal(e,r,o){if("disabled"===this.state)throw this._logError("version-management-view-model:load-error",this.loadError),new t("version-management-view-model:load-error",this.loadError??"");this._setExecutionError();const i=this.versioningStateLookup.get(e);if(!i)throw this._logError("version-management-view-model:execution-error","no-versioning-state-found"),new t("version-management-view-model:execution-error",this.executionError??"");if(this.serverVersionLookup.get(e)<=11.1)throw this._logError("version-management-view-model:execution-error","versioning-api-error"),new t("version-management-view-model:execution-error",this.executionError??"");const s={name:r,guid:o};return await i.deleteVersion(s).catch(e=>{throw this._logExecutionError(e),new t("version-management-view-model:execution-error",this.executionError??"")})}async _getVersionInfosInternal(e){if("disabled"===this.state)throw this._logError("version-management-view-model:load-error",this.loadError),new t("version-management-view-model:load-error",this.loadError??"");this._setExecutionError();const r=this.featureServiceLookup.get(e)?.featureService;if(!r)throw this._logError("version-management-view-model:execution-error","no-feature-service-found"),new t("version-management-view-model:execution-error",this.executionError??"");r.loaded||await r.load().catch(e=>{throw this._logExecutionError(e),e});const o=r.url;this.serverVersionLookup.set(o,r.sourceJSON?.currentVersion??0);const i=this.userLookup.get(e);this._portalLookup.get(e)&&i?this.versionAdministratorLookup.set(o,await v(this._portalLookup.get(e),i,"features:user:manageVersions")):this.versionAdministratorLookup.set(o,!1);const s=this.versioningStateLookup.get(e);if(!s)throw this._logError("version-management-view-model:execution-error","no-versioning-state-found"),new t("version-management-view-model:execution-error",this.executionError??"");s.loaded||await s.load().catch(e=>{throw this._logExecutionError(e),e});return await s.getVersionInfos(!0).catch(e=>{throw this._logExecutionError(e),e})}_isEmptyString(e){return!e||0===e.trim().length}_logError(e,r){switch(i.getLogger(this).error(new t(e,r??"")),e){case"version-management-view-model:load-error":this._setLoadError(r);break;case"version-management-view-model:execution-error":this._setExecutionError(r)}}_logExecutionError(e){this._logError("version-management-view-model:execution-error",e.message)}async _viewChangeHandle(){this._updatingHandlesLoad.addPromise((async()=>{if(this._setLoadError(),!this.view)return void this._setLoadError("no-view-property");if(this.view&&"2d"!==this.view.type)return void this._logError("version-management-view-model:load-error","sceneView-not-supported");this.removeHandles("map-change-handle"),this.addHandles([s(()=>this.view?.map,()=>{this._resetAllLookupProperties(),this._mapChangeHandle(this.versioningStates)})],"map-change-handle");const e=await this._getVersioningStates();await this._mapChangeHandle(e)})())}async _mapChangeHandle(e){this._updatingHandlesLoad.addPromise((async()=>{this._setLoadError(),this.removeHandles("map-layers-tables-change-handle"),this.addHandles([n(()=>this.view?.map.allLayers,"change",e=>{(e.added.some(({type:e})=>"feature"===e)||e.removed.some(({type:e})=>"feature"===e)||e.added.some(({type:e})=>"subtype-group"===e)||e.removed.some(({type:e})=>"subtype-group"===e))&&(this._resetServiceRelatedLookupProperties(),this._propertiesChangeInternal(this.versioningStates),this._updateVersionableItems(e))}),n(()=>this.view?.map.allTables,"change",e=>{(e.added.some(({type:e})=>"feature"===e)||e.removed.some(({type:e})=>"feature"===e)||e.added.some(({type:e})=>"subtype-group"===e)||e.removed.some(({type:e})=>"subtype-group"===e))&&(this._resetServiceRelatedLookupProperties(),this._propertiesChangeInternal(this.versioningStates),this._updateVersionableItems(e))})],"map-layers-tables-change-handle"),await this._propertiesChangeInternal(e)})())}async _propertiesChangeInternal(e){this._updatingHandlesLoad.addPromise((async()=>{const r=e=>"feature"===e.type||"subtype-group"===e.type,{allLayers:o,allTables:t}=this.view.map;if(this._setLoadError(),this.featureServiceLookup=c([...o.filter(r),...t.filter(r)]),this.featureServiceLookup.size){this._updateFeatureServiceLookup(e),this.serviceNameLookup=new Map;for(const e of this.featureServiceLookup.values()){const{featureService:r,featureService:{url:o}}=e;if(!this.serviceNameLookup.has(o)){if(r.loaded||await r.load().catch(e=>{i.getLogger(this).error(e)}),!r.versionManagementServiceUrl){this.featureServiceLookup.delete(o);continue}const e=await h(o),t=new p({authMode:"immediate",url:e});await t.load().catch(e=>{i.getLogger(this).error(e)});const s=t.user?.username;if(!r.loaded||!t.loaded||!s)continue;this.serviceNameLookup.set(a(o),o.split("/").at(-2)),this.userLookup.set(o,s),this._portalLookup.set(o,t)}}e.forEach(e=>this._handleVersioningStateLookupUpdate(e)),await this._updateVersioningStates(e),this.versioningStates=e}else this._logError("version-management-view-model:load-error","no-feature-services")})())}_updateFeatureServiceLookup(e){for(const r of e){const e=r.featureServiceUrl;if(!this.featureServiceLookup.has(e)){const o=new m({url:e}),t=r.versionableItems.toArray().flatMap(e=>"network"===e.type?null:e.versionableItem).filter(e=>!!e);this.featureServiceLookup.set(e,{featureService:o,layers:t})}}}async _updateVersioningStates(e){for(const r of this.featureServiceLookup.values()){const t=r.featureService;if(!t.versionManagementServiceUrl)continue;if(!e.find(e=>e.featureServiceUrl===t.url)&&t.versionManagementServiceUrl){const i=new o(_(r.layers)),s=new w({versionManagementService:new f({url:t.versionManagementServiceUrl}),versionableItems:i});e.add(s),this._handleVersioningStateLookupUpdate(s)}await this.getVersionInfos(t.url)}}async _getVersioningStates(){return this.versioningStates&&this.versioningStates.length?this.versioningStates:this.view?await g(this.view,!1):(this._logError("version-management-view-model:load-error","no-view-property"),new o)}_handleVersioningStateLookupUpdate(e){this.versioningStateLookup.set(e.featureServiceUrl,e)}async _setVersioningStatesLookups(){this._updatingHandlesLoad.addPromise((async()=>{this.versioningStates&&(this._updateFeatureServiceLookup(this.versioningStates),this.versioningStates.forEach(e=>this._handleVersioningStateLookupUpdate(e)),await this._updateVersioningStates(this.versioningStates))})())}_resetVersioningLookupProperties(){this.versioningStateLookup=new Map}_resetAllLookupProperties(){this._portalLookup=new Map,this.serverVersionLookup=new Map,this.userLookup=new Map,this.versioningStates=new o,this._resetVersioningLookupProperties(),this._resetServiceRelatedLookupProperties()}_resetServiceRelatedLookupProperties(){this.featureServiceLookup=new Map,this.serviceNameLookup=new Map}_setExecutionError(e){this._set("executionError",e)}_setLoadError(e){this._set("loadError",e)}};e([d()],E.prototype,"_portalLookup",void 0),e([d({readOnly:!0})],E.prototype,"executionError",void 0),e([d()],E.prototype,"featureServiceLookup",void 0),e([d({readOnly:!0})],E.prototype,"loadError",void 0),e([d()],E.prototype,"serverVersionLookup",void 0),e([d()],E.prototype,"serviceNameLookup",void 0),e([d({readOnly:!0})],E.prototype,"state",null),e([d()],E.prototype,"userLookup",void 0),e([d()],E.prototype,"versionAdministratorLookup",void 0),e([d()],E.prototype,"versioningStateLookup",void 0),e([d()],E.prototype,"view",void 0),e([d()],E.prototype,"versioningStates",void 0),E=e([l("esri.versionManagement.VersionManager")],E);const L=E;export{L as default};