packageDocumentation module import { AccessToken , GuidString , IModelHubStatus , Logger } from "@itwin/core-bentley" ; import { CancelRequest , FileHandler } from "../itwin-client/FileHandler" ; import { ProgressCallback } from "../itwin-client/Request" ; import { IModelClient } from "../IModelClient" ; import { IModelHubClientLoggerCategory } from "../IModelHubClientLoggerCategories" ; import { ECJsonTypeMap , WsgInstance } from "../wsg/ECJsonTypeMap" ; import { WsgQuery } from "../wsg/WsgQuery" ; import { IModelBaseHandler } from "./BaseHandler" ; import { ArgumentCheck , IModelHubClientError , IModelHubError } from "./Errors" ; import { addSelectApplicationData , addSelectFileAccessKey } from "./HubQuery" ; import { LockQuery } from "./Locks" ; const loggerCategory = IModelHubClientLoggerCategory . IModelHub ; internal export enum BriefcaseAccessMode { Shared = 0 , Exclusive = 1 } internal @ ECJsonTypeMap . classToJson ( "wsg" , "iModelScope.Briefcase" , { schemaPropertyName : "schemaName" , classPropertyName : "className" } ) @ ECJsonTypeMap . classToJson ( "ecdb" , "ServiceStore.Briefcase" , { classKeyPropertyName : "className" } ) export class Briefcase extends WsgInstance { /** File name of the master file. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.FileName" ) public fileName ? : string ; /** Description of the master file. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.FileDescription" ) public fileDescription ? : string ; /** Size of the latest copy of the master file in iModelHub. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.FileSize" ) public fileSize ? : string ; /** FileId of the master file. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.FileId" ) public fileId ? : GuidString ; /** Id of the briefcase. See [BriefcaseId]($backend) */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.BriefcaseId" ) @ ECJsonTypeMap . propertyToJson ( "ecdb" , "briefcaseId" ) public briefcaseId ? : number ; /** Id of the user that acquired this briefcase. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.UserOwned" ) @ ECJsonTypeMap . propertyToJson ( "ecdb" , "userId" ) public userId ? : string ; /** Id of the last [[ChangeSet]] that was merged into the latest copy of master file on iModelHub. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.MergedChangeSetId" ) public mergedChangeSetId ? : string ; /** Date when this briefcase was acquired. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.AcquiredDate" ) public acquiredDate ? : string ; /** Date until this briefcase is valid. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.ExpirationDate" ) public expirationDate ? : string ; /** Name of the device on which this briefcase is downloaded. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.DeviceName" ) public deviceName ? : string ; /** Id of the latest [[ChangeSet]] on device. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.ChangeSetIdOnDevice" ) public changeSetIdOnDevice ? : string ; /** Shows whether the user who acquired this briefcase can perform write operations. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "properties.IsReadOnly" ) @ ECJsonTypeMap . propertyToJson ( "ecdb" , "isReadOnly" ) public isReadOnly ? : boolean ; /** URL that can be used to download the latest copy of master file from iModelHub. See [[BriefcaseQuery.selectDownloadUrl]]. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "relationshipInstances[FileAccessKey].relatedInstance[AccessKey].properties.DownloadUrl" ) public downloadUrl ? : string ; /** Id of the application that created this Briefcase. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "relationshipInstances[CreatedByApplication].relatedInstance[Application].properties.Id" ) public applicationId ? : string ; /** Name of the application that created this Briefcase. */ @ ECJsonTypeMap . propertyToJson ( "wsg" , "relationshipInstances[CreatedByApplication].relatedInstance[Application].properties.Name" ) public applicationName ? : string ; @ ECJsonTypeMap . propertyToJson ( "ecdb" , "accessMode" ) public accessMode ? : BriefcaseAccessMode ; @ ECJsonTypeMap . propertyToJson ( "ecdb" , "localPathname" ) public localPathname ? : string ; @ ECJsonTypeMap . propertyToJson ( "ecdb" , "lastAccessedAt" ) public lastAccessedAt ? : Date ; @ ECJsonTypeMap . propertyToJson ( "ecdb" , "iModelId" ) public iModelId ? : GuidString ; } internal export class BriefcaseQuery extends WsgQuery { private _byId ? : number ; param id returns throws public byId ( id ) { ArgumentCheck . validBriefcaseId ( "id" , id ) ; this . _byId = id ; return this ; } returns internal public getId ( ) { return this . _byId ; } returns public selectDownloadUrl ( ) { addSelectFileAccessKey ( this . _query ) ; return this ; } returns public selectApplicationData ( ) { addSelectApplicationData ( this . _query ) ; return this ; } returns public ownedByMe ( ) { this . addFilter ( "UserOwned+eq+'@me'" ) ; return this ; } } internal export class BriefcaseHandler { private _handler : IModelBaseHandler ; private _imodelClient : IModelClient ; private _fileHandler ? : FileHandler ; param handler param fileHandler internal constructor ( handler , imodelClient , fileHandler ? ) { this . _handler = handler ; this . _imodelClient = imodelClient ; this . _fileHandler = fileHandler ; } param iModelId param briefcaseId private getRelativeUrl ( iModelId , briefcaseId ? ) { return `/Repositories/iModel--${ iModelId }/iModelScope/Briefcase/${ briefcaseId || "" }` ; } param iModelId param briefcase returns throws throws public async create ( accessToken , iModelId , briefcase = new Briefcase ( ) ) { Logger . logInfo ( loggerCategory , "Acquiring briefcase for iModel" , ( ) => ( { iModelId } ) ) ; ArgumentCheck . validGuid ( "iModelId" , iModelId ) ; briefcase = await this . _handler . postInstance < Briefcase > ( accessToken , Briefcase , this . getRelativeUrl ( iModelId ) , briefcase ) ; Logger . logTrace ( loggerCategory , "Acquired briefcase for iModel" , ( ) => briefcase ) ; return briefcase ; } param iModelId param briefcase returns throws public async update ( accessToken , iModelId , briefcase ) { Logger . logInfo ( loggerCategory , "Updating briefcase for iModel" , ( ) => ( { iModelId , briefcase } ) ) ; ArgumentCheck . validGuid ( "iModelId" , iModelId ) ; const briefcaseId = parseInt ( briefcase . wsgId , 10 ) ; ArgumentCheck . validBriefcaseId ( "briefcase.wsgId" , briefcaseId ) ; const updatedBriefcase = await this . _handler . postInstance < Briefcase > ( accessToken , Briefcase , this . getRelativeUrl ( iModelId , briefcaseId ) , briefcase ) ; Logger . logTrace ( loggerCategory , "Updated briefcase for iModel" , ( ) => ( { iModelId , briefcase } ) ) ; return updatedBriefcase ; } param iModelId param briefcaseId throws public async delete ( accessToken , iModelId , briefcaseId ) { Logger . logInfo ( loggerCategory , "Deleting briefcase from iModel" , ( ) => ( { iModelId , briefcaseId } ) ) ; ArgumentCheck . validGuid ( "iModelId" , iModelId ) ; ArgumentCheck . validBriefcaseId ( "briefcaseId" , briefcaseId ) ; const existingLocksOwnedByBriefcase = await this . _imodelClient . locks . get ( accessToken , iModelId , new LockQuery ( ) . byBriefcaseId ( briefcaseId ) . top ( 1 ) ) ; if ( existingLocksOwnedByBriefcase . length > 0 ) { try { await this . _imodelClient . locks . deleteAll ( accessToken , iModelId , briefcaseId ) ; } catch ( error ) { if ( ! ( error instanceof IModelHubError ) || error . errorNumber !== IModelHubStatus . UserDoesNotHavePermission ) { throw error ; } } } await this . _handler . delete ( accessToken , this . getRelativeUrl ( iModelId , briefcaseId ) ) ; Logger . logTrace ( loggerCategory , "Deleted briefcase from iModel" , ( ) => ( { iModelId , briefcaseId } ) ) ; } param iModelId param query returns throws throws public async get ( accessToken , iModelId , query = new BriefcaseQuery ( ) ) { Logger . logInfo ( loggerCategory , "Querying briefcases for iModel" , ( ) => ( { iModelId } ) ) ; ArgumentCheck . validGuid ( "iModelId" , iModelId ) ; const id = query . getId ( ) ; const briefcases = await this . _handler . getInstances < Briefcase > ( accessToken , Briefcase , this . getRelativeUrl ( iModelId , id ) , query . getQueryOptions ( ) ) ; for ( const briefcase of briefcases ) { briefcase . iModelId = iModelId ; } Logger . logTrace ( loggerCategory , "Queried briefcases for iModel" , ( ) => ( { iModelId , count : briefcases . length } ) ) ; return briefcases ; } param briefcase param path param progressCallback param cancelRequest throws throws throws throws public async download ( accessToken , briefcase , path , progressCallback ? , cancelRequest ? ) { Logger . logTrace ( loggerCategory , "Started downloading briefcase" , ( ) => ( { ... briefcase , path } ) ) ; ArgumentCheck . defined ( "briefcase" , briefcase ) ; ArgumentCheck . defined ( "path" , path ) ; if ( typeof window !== "undefined" ) throw IModelHubClientError . browser ( ) ; if ( ! this . _fileHandler ) throw IModelHubClientError . fileHandler ( ) ; if ( ! briefcase . downloadUrl ) throw IModelHubClientError . missingDownloadUrl ( "briefcase" ) ; await this . _fileHandler . downloadFile ( accessToken , briefcase . downloadUrl , path , parseInt ( briefcase . fileSize ! , 10 ) , progressCallback , cancelRequest ) ; Logger . logTrace ( loggerCategory , "Finished downloading briefcase" , ( ) => ( { ... briefcase , path } ) ) ; } }