@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 11.7 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.33/esri/copyright.txt for details.
*/
import{_ as e}from"../chunks/tslib.es6.js";import t from"../request.js";import r from"../core/Error.js";import s from"../core/JSONSupport.js";import i from"../core/Loadable.js";import{removeTrailingSlash as n,Url as o}from"../core/urlUtils.js";import{property as a}from"../core/accessorSupport/decorators/property.js";import"../core/has.js";import"../core/Logger.js";import"../core/RandomLCG.js";import{reader as u}from"../core/accessorSupport/decorators/reader.js";import{subclass as l}from"../core/accessorSupport/decorators/subclass.js";import{writer as c}from"../core/accessorSupport/decorators/writer.js";import{onApplyEditsEvent as d,emitUpdateMomentEvent as m}from"../layers/mixins/EditBusLayer.js";import{parseUrl as f}from"../rest/utils.js";import g from"./support/EditMomentStack.js";import{versionCollection as p,versionCollectionCount as h,defaultVersionNameLookup as V,currentSessionId as v}from"./support/versionManagementUtils.js";import{createVersionAdapters as y}from"./versionAdapters/utils.js";let w=class extends(s.JSONSupportMixin(i)){constructor(e){super(e),this.url=null,this.sourceJSON=null,this.name=null,this.defaultVersionIdentifier=null,this.capabilities=null,this._isDefault=e=>!e||e===this.defaultVersionIdentifier.name,this._dateEquals=(e,t)=>!e&&!t||!(!e||!t)&&e.toISOString()===t.toISOString(),this._applyEditsHandler=e=>{const{serviceUrl:t,gdbVersion:r,result:s}=e;t===this._featureServiceUrl&&s.then((e=>{const t=e.historicMoment;t&&this._addMomentToVersionItem(r,t)}))}}initialize(){this.url=n(this.url),this._featureServiceUrl=this.url.replace(/\/VersionManagementServer/i,"/FeatureServer"),p.has(this.url)||p.set(this.url,new Map);const e=(h.get(this.url)??0)+1;h.set(this.url,e),this.when().then((()=>this.addHandles(d(this._applyEditsHandler))),(()=>{}))}destroy(){const e=(h.get(this.url)??1)-1;h.set(this.url,e),0===e&&p.delete(this.url),V.delete(this._featureServiceUrl)}read(e,t){this.sourceJSON=e,super.read(e,t)}readDefaultVersionIdentifier(e,t){return{name:t.defaultVersionName,guid:t.defaultVersionGuid}}writeDefaultVersionIdentifier(e,t){t.defaultVersionName=e.name,t.defaultVersionGuid=e.guid}load(e){return this.addResolvingPromise(this._fetchService(this.url,e)),Promise.resolve(this)}async createVersion(e){const[{createVersion:t},{default:r}]=await Promise.all([import("../rest/versionManagement/createVersion.js"),import("../rest/versionManagement/support/CreateVersionParameters.js")]),s=r.from(e);return t(this.url,s)}async deleteVersion(e){const[{deleteVersion:t},{default:r}]=await Promise.all([import("../rest/versionManagement/deleteVersion.js"),import("../rest/versionManagement/support/DeleteVersionParameters.js")]);let s;e.guid&&p.get(this.url)?.has(e.guid)&&(s=v??void 0);const i=new r({versionName:e.name,sessionId:s});return t(this.url,i)}async getVersionInfoExtended(e){const{getVersion:t}=await import("../rest/versionManagement/getVersion.js");return t(this.url,e.guid)}async getVersionInfos(e={}){const[{getVersionInfos:t},{default:r}]=await Promise.all([import("../rest/versionManagement/getVersionInfos.js"),import("../rest/versionManagement/support/GetVersionInfosParameters.js")]),s=r.from(e);return t(this.url,s)}async reconcile(e,t={}){const[{reconcile:r},{default:s}]=await Promise.all([import("../rest/versionManagement/gdbVersion/reconcile.js"),import("../rest/versionManagement/gdbVersion/support/ReconcileParameters.js")]),i=s.from(t);return i.sessionId=v,r(this.url,e.guid,i)}async post(e){const[{post:t},{default:r}]=await Promise.all([import("../rest/versionManagement/gdbVersion/post.js"),import("../rest/versionManagement/gdbVersion/support/PostParameters.js")]),s=r.from({sessionId:v});return t(this.url,e.guid,s)}async alterVersion(e,t){const[{alterVersion:r},{default:s}]=await Promise.all([import("../rest/versionManagement/gdbVersion/alterVersion.js"),import("../rest/versionManagement/gdbVersion/support/AlterVersionParameters.js")]),i=s.from(t);return r(this.url,e.guid,i)}async startReading(e){return(await this.startReadingWithResult(e)).success}async startReadingWithResult(e){const{startReading:t}=await import("../rest/versionManagement/gdbVersion/startReading.js"),r=await t(this.url,e.guid,v);if(r.success){const t=await this.getVersionInfoExtended(e),r=new Date(t.modifiedDate),s={name:t.versionIdentifier.name,moment:r,lockType:"read"};this._addOrUpdateItem(e.guid,s),m(this._featureServiceUrl,null,e.name,r)}return r}async stopReading(e){return(await this.stopReadingWithResult(e)).success}async stopReadingWithResult(e){const{stopReading:t}=await import("../rest/versionManagement/gdbVersion/stopReading.js"),r=await t(this.url,e.guid,v);return r.success&&(p.get(this.url)?.delete(e.guid),m(this._featureServiceUrl,null,e.name,null)),r}async startEditing(e){return(await this.startEditingWithResult(e)).success}async startEditingWithResult(e){const{startEditing:t}=await import("../rest/versionManagement/gdbVersion/startEditing.js"),r=await t(this.url,e.guid,v);if(r.success){const t=await this.getVersionInfoExtended(e),r=new Date(t.modifiedDate),s=new g(r),i={name:e.name,moment:r,lockType:"edit",stack:s};this._addOrUpdateItem(e.guid,i),m(this._featureServiceUrl,null,e.name,r)}return r}async stopEditing(e,t){return(await this.stopEditingWithResult(e,t)).success}async stopEditingWithResult(e,t){if(t){const t=p.get(this.url)?.get(e.guid);if(t?.stack?.canRedo()){const[{deleteForwardEdits:r},{default:s}]=await Promise.all([import("../rest/versionManagement/gdbVersion/deleteForwardEdits.js"),import("../rest/versionManagement/gdbVersion/support/DeleteForwardEditsParameters.js")]),i=await r(this.url,e.guid,new s({sessionId:v,moment:t.moment}));if(!i.success)return i}}const{stopEditing:r}=await import("../rest/versionManagement/gdbVersion/stopEditing.js"),s=await r(this.url,e.guid,v,t);if(s.success){const t=await this.getVersionInfoExtended(e),r=new Date(t.modifiedDate);this._addOrUpdateItem(e.guid,{name:e.name,moment:r,lockType:"read",editMomentStack:void 0}),m(this._featureServiceUrl,null,e.name,r)}return s}getLockType(e){const t=p.get(this.url)?.get(e.guid)?.lockType;return t??"none"}async changeVersion(e,t,s){if(s&&"name"in s&&!await this.getVersionInfoExtended(s))throw new r("version-management-service:invalid-version","version does not exist");if("networkServiceUrl"in e){if(this._featureServiceUrl.toLowerCase()===e.featureServiceUrl.toLowerCase())return this._changeVersionInternal(e,t,s)}else{let r;"layers"in e?(r=e.allTables.concat(e.allLayers),e.utilityNetworks&&e.utilityNetworks.forEach((e=>{this._featureServiceUrl.toLowerCase()===e.featureServiceUrl.toLowerCase()&&this._changeVersionInternal(e,t,s)}))):r=e;for(const e of r)if("feature"===e.type||"subtype-group"===e.type){const r=/^(.*\/FeatureServer)\/\d+$/,i=e.url.replace(r,"$1");if(this._featureServiceUrl.toLowerCase()===i.toLowerCase()&&!this._changeVersionInternal(e,t,s))return!1}else if("group"===e.type){const r=e.allTables.concat(e.allLayers);for(const e of r)if("feature"===e.type||"subtype-group"===e.type){const r=/^(.*\/FeatureServer)\/\d+$/,i=e.url.replace(r,"$1");if(this._featureServiceUrl.toLowerCase()===i.toLowerCase()&&!this._changeVersionInternal(e,t,s))return!1}}}return!0}async changeVersionWithResult(e,t,r){let s;if("layers"in e){const t=y(e.allTables.concat(e.allLayers).filter((e=>"group"!==e.type)).toArray());e.utilityNetworks&&e.utilityNetworks.forEach((e=>{const r=y([e]);t.push(...r)})),s=t}else s=e;if(r&&"name"in r){if(!await this.getVersionInfoExtended(r)){const e=new Map;for(const t of s)e.set(t,{success:!1});return e}}if(t&&"name"in t){if("none"!==this.getLockType(t)){const e=new Map;for(const t of s)e.set(t,{success:!1});return e}}const i=new Map;for(const n of s)if(n)if(n.featureServiceUrl.toLowerCase()===this._featureServiceUrl.toLowerCase()){const e=this._changeVersionInternalWithResult(n,t,r);i.set(n,e)}else i.set(n,{success:!1});return i}async getVersionIdentifierFromName(e){try{const t=await this.getVersionInfos({includeHidden:!0});return t.find((t=>t.versionIdentifier.name.toUpperCase()===e.toUpperCase()))?.versionIdentifier||null}catch{return null}}async getVersionIdentifierFromGuid(e){const{getVersion:t}=await import("../rest/versionManagement/getVersion.js");try{return(await t(this.url,e)).versionIdentifier}catch{return null}}canUndo(e){const t=p.get(this.url)?.get(e.guid);return!!t?.stack?.canUndo()}canRedo(e){const t=p.get(this.url)?.get(e.guid);return!!t?.stack?.canRedo()}undo(e){const t=p.get(this.url)?.get(e.guid),r=t?.stack?.undo()||void 0;if(t&&r){const r=t.stack.peek();m(this._featureServiceUrl,null,e.name,r),p.get(this.url)?.set(e.guid,{...t,moment:r})}}redo(e){const t=p.get(this.url)?.get(e.guid),r=t?.stack?.redo()||void 0;t&&r&&(m(this._featureServiceUrl,null,e.name,r),p.get(this.url)?.set(e.guid,{...t,moment:r}))}_setUpData(e,t){let s=null,i=null,n=null,o=null;const a=e=>{const t=p?.get(this.url)?.get(e.guid);n=e.name,o=t?.moment??null};if(e&&"name"in e){if("none"!==this.getLockType(e))throw new r("version-management-service:version-locked","version is locked");s=e.name,i=null,t&&"name"in t?a(t):(n=this.defaultVersionIdentifier.name,o=t)}else s=this.defaultVersionIdentifier.name,i=e,t&&"name"in t?a(t):(n=this.defaultVersionIdentifier.name,o=t);return{fromVersionName:s,fromMoment:i,toVersionName:n,toMoment:o}}_changeVersionInternal(e,t,r){try{const{fromVersionName:s,fromMoment:i,toVersionName:n,toMoment:o}=this._setUpData(t,r);(this._isDefault(s)&&this._isDefault(e.gdbVersion)&&this._dateEquals(e.historicMoment,i)||s===e.gdbVersion&&this._dateEquals(e.historicMoment,i))&&(e.gdbVersion=n,e.historicMoment=o)}catch(s){return!1}return!0}_changeVersionInternalWithResult(e,t,r){try{const{fromVersionName:s,fromMoment:i,toVersionName:n,toMoment:o}=this._setUpData(t,r);return this._isDefault(s)&&this._isDefault(e.gdbVersion)&&this._dateEquals(e.historicMoment,i)||s===e.gdbVersion&&this._dateEquals(e.historicMoment,i)?(e.gdbVersion=n,e.historicMoment=o,{success:!0}):{success:!1}}catch(s){return{success:!1}}}_addMomentToVersionItem(e,t){const r=p.get(this.url);if(r)for(const[s,i]of r){i.name===e&&this._addToStack(s,t)}}_addToStack(e,t){const r=p.get(this.url),s=r?.get(e);return!!s?.stack&&(S(s.stack.peek(),t)&&s.stack.push(t),r.set(e,{...s,moment:t}),!0)}_addOrUpdateItem(e,t){const r=p.get(this.url),s=r?.get(e);return s?(r.set(e,{...s,...t}),!0):!(!t.name||!t.lockType)&&(r?.set(e,{...t,lockType:t.lockType}),!0)}async _fetchService(e,r){if(this.sourceJSON){this.read(this.sourceJSON,{origin:"service",url:f(e)});const t=new o(this.url).host;return void V.set(t,this.defaultVersionIdentifier.name)}const s=await t(e,{responseType:"json",query:{f:"json"},...r});this.read(s.data)}};function S(e,t){if(e){return e.getTime()<t.getTime()}return!0}e([a()],w.prototype,"url",void 0),e([a()],w.prototype,"sourceJSON",void 0),e([a({type:String,json:{write:!0}})],w.prototype,"name",void 0),e([a({json:{write:{target:{defaultVersionName:{type:String},defaultVersionGuid:{type:String}}},read:{source:["defaultVersionName","defaultVersionGuid"]}}})],w.prototype,"defaultVersionIdentifier",void 0),e([u("defaultVersionIdentifier",["defaultVersionName","defaultVersionGuid"])],w.prototype,"readDefaultVersionIdentifier",null),e([c("defaultVersionIdentifier",{defaultVersionName:{type:String},defaultVersionGuid:{type:String}})],w.prototype,"writeDefaultVersionIdentifier",null),e([a({json:{write:!0}})],w.prototype,"capabilities",void 0),w=e([l("esri.versionManagement.VersionManagementService")],w);const I=w;export{I as default};