ps-bridge-helpers-common
Version:
PS Connectors Common
147 lines • 8.63 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.iModelHelper = void 0;
const imodeljs_backend_1 = require("@bentley/imodeljs-backend");
const imodeljs_common_1 = require("@bentley/imodeljs-common");
const bentleyjs_core_1 = require("@bentley/bentleyjs-core");
const Logger_1 = require("./utils/Logger");
class iModelHelper {
static async SetJsonProperties(targetDb, targetFunctionalModelId) {
if (!targetDb || !targetFunctionalModelId) {
Logger_1.Logger.logInfo(this.LOG_CATEGORY, "Invalid arguments. Please provide valid imodel and/or functional model id");
throw new Error('Invalid argumrnts. Please provide valid imodel and/or functional model id');
}
try {
Logger_1.Logger.logTrace(this.LOG_CATEGORY, "Starting setting json properties");
const partitionProps = targetDb.elements.getElementProps(targetFunctionalModelId);
const partitionElement = targetDb.elements.getElement(partitionProps);
let jsonProps = { isProvisional: true, version: 1 };
partitionElement.setJsonProperty("PlantSightPartitionDefinition", jsonProps);
partitionElement.update();
Logger_1.Logger.logTrace(this.LOG_CATEGORY, "Json properties set successfully");
return true;
}
catch (error) {
console.log(error.message);
Logger_1.Logger.logError(this.LOG_CATEGORY, `Failed to set json properties. ${error}`);
return false;
}
}
static async CreateFunctionalModel(targetDb, targetSubjectId, modelName, fedrationGuid) {
if (!targetDb || !targetSubjectId || !modelName || !fedrationGuid) {
Logger_1.Logger.logInfo(this.LOG_CATEGORY, "Invalid arguments. Please provide valid imodel and/or subject id and/or model name and/or fedration guid");
throw new Error('Invalid arguments. Please provide valid imodel and/or subject id and/or model name and/or fedration guid');
}
try {
let existingId = await this.QueryFunctionalModel(targetDb, targetSubjectId, modelName);
if (!existingId) {
Logger_1.Logger.logTrace(this.LOG_CATEGORY, "Starting creating functional model with name: " + modelName);
// Create an InformationPartitionElement for the TestBridgeGroupModel to model
const partitionProps = {
classFullName: imodeljs_backend_1.FunctionalPartition.classFullName,
model: imodeljs_common_1.IModel.repositoryModelId,
parent: new imodeljs_backend_1.SubjectOwnsPartitionElements(targetSubjectId),
jsonProperties: { PlantSightPartitionDefinition: { isProvisional: true, version: 1 } },
code: this.GetFunctionalPartitionCode(targetDb, targetSubjectId, modelName),
federationGuid: fedrationGuid,
};
const partitionId = targetDb.elements.insertElement(partitionProps);
const modelProps = {
classFullName: imodeljs_backend_1.FunctionalModel.classFullName,
modeledElement: { id: partitionId },
isPrivate: true,
};
let modelId = targetDb.models.insertModel(modelProps);
Logger_1.Logger.logTrace(this.LOG_CATEGORY, "Successfully created functional model with name: " + modelName);
return modelId;
}
else {
Logger_1.Logger.logTrace(this.LOG_CATEGORY, "Functional model already exists. Setting json properties and private property");
const functionalModel = targetDb.elements.getElement(existingId);
//Setting json properties
const result1 = this.SetJsonProperties(targetDb, existingId);
//Setting private property
const result2 = this.SetPrivateProperty(targetDb, existingId);
if (result1 && result2) {
Logger_1.Logger.logTrace(this.LOG_CATEGORY, "Successfully added json properties and private property to the existing functional model");
return functionalModel.id;
}
else {
Logger_1.Logger.logTrace(this.LOG_CATEGORY, "Failed to set json properties and/or private property");
return undefined;
}
}
}
catch (error) {
console.log(error.message);
Logger_1.Logger.logError(this.LOG_CATEGORY, `Failed to create functional model. ${error}`);
return undefined;
}
}
static async SetPrivateProperty(targetDb, targetFunctionalModelId, isPrivate = true) {
if (!targetDb || !targetFunctionalModelId) {
Logger_1.Logger.logInfo(this.LOG_CATEGORY, "Invalid argument. Please provide valid imodel and/or functional model id");
throw new Error('Invalid argument. Please provide valid imodel and/or functional model id');
}
try {
Logger_1.Logger.logTrace(this.LOG_CATEGORY, "Starting setting Private property");
const modelProps = targetDb.models.getModelProps(targetFunctionalModelId);
modelProps.isPrivate = isPrivate;
targetDb.models.updateModel(modelProps);
Logger_1.Logger.logTrace(this.LOG_CATEGORY, "Private property set successfully");
return true;
}
catch (error) {
console.log(error.message);
Logger_1.Logger.logError(this.LOG_CATEGORY, `Failed to set private property. ${error}`);
return false;
}
}
static JsonPropertiesExist(dbFind, functionalModelName) {
if (!dbFind || !functionalModelName) {
Logger_1.Logger.logInfo(this.LOG_CATEGORY, "Invalid arguments. Please provide valid imodel and/or functional model name");
throw new Error('Invalid arguments. Please provide valid imodel and/or functional model name');
}
Logger_1.Logger.logTrace(this.LOG_CATEGORY, "Starting querying json properties");
this.queryResult = false;
const query = `select JsonProperties from func.functionalpartition where CodeValue='${functionalModelName}'`;
dbFind.withPreparedStatement(query, (stmt) => {
if (bentleyjs_core_1.DbResult.BE_SQLITE_ROW === stmt.step()) {
let row = stmt.getRow();
if (JSON.stringify(row).includes('jsonProperties') && JSON.stringify(row).includes('isProvisional')) {
Logger_1.Logger.logTrace(this.LOG_CATEGORY, "Json properties found");
this.queryResult = true;
}
}
});
if (!this.queryResult) {
Logger_1.Logger.logTrace(this.LOG_CATEGORY, "Json properties do not exist");
}
return this.queryResult;
}
static GetFunctionalPartitionCode(targetDb, subjectId, modelName) {
Logger_1.Logger.logTrace(this.LOG_CATEGORY, "Creating functional partition code for functional model: " + modelName);
const code = imodeljs_backend_1.FunctionalPartition.createCode(targetDb, subjectId, modelName);
if (code) {
Logger_1.Logger.logTrace(this.LOG_CATEGORY, "Successfully created functional partition code for functional model: " + modelName);
}
else {
Logger_1.Logger.logTrace(this.LOG_CATEGORY, "Failed to create functional partition code for functional model: " + modelName);
}
return code;
}
static QueryFunctionalModel(targetDb, subjectId, modelName) {
Logger_1.Logger.logTrace(this.LOG_CATEGORY, "Querying functional model with name: " + modelName + ". Need to create functional partition code for that first.");
const targetModelId = targetDb.elements.queryElementIdByCode(this.GetFunctionalPartitionCode(targetDb, subjectId, modelName));
if (targetModelId) {
Logger_1.Logger.logTrace(this.LOG_CATEGORY, "Functional model found with name: " + modelName);
}
else {
Logger_1.Logger.logError(this.LOG_CATEGORY, "No functional model was found with name:" + modelName);
}
return targetModelId;
}
}
exports.iModelHelper = iModelHelper;
iModelHelper.LOG_CATEGORY = "PSConnectorsCommon(iModel Helper):";
//# sourceMappingURL=iModelHelper.js.map