Skip to content
This repository has been archived by the owner on Apr 26, 2021. It is now read-only.

Commit

Permalink
#31 : Create datatype class, Create tag, Update -> datatype class, Cr…
Browse files Browse the repository at this point in the history
…eate/Update -> tag, Create -> datatype class, Create -> tag, Create StatusCode if it does not exist
  • Loading branch information
faizanvahevaria committed Apr 20, 2020
1 parent a3f7059 commit 52afe70
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 4 deletions.
1 change: 1 addition & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function init() {
app.commands.register('tool.transport:import', transport.importModel);
app.commands.register('tool.transport:export', transport.exportModel);
app.commands.register('tool.transport:exportNew', transportNew.exportNewModel);
app.commands.register('tool.transport:importNew', transportNew.importNewModel);
app.commands.register('tool.git:initclone', git.initClone);
app.commands.register('tool.git:sync', git.sync);

Expand Down
4 changes: 4 additions & 0 deletions menus/menus.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"label": "model interchange",
"id": "tool.transport",
"submenu": [{
"label": "import",
"id": "tool.transport.importNew",
"command": "tool.transport:importNew"
},{
"label": "export",
"id": "tool.transport.exportNew",
"command": "tool.transport:exportNew"
Expand Down
3 changes: 2 additions & 1 deletion src/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ const constant = {
}],
msg_file_saveas: 'Save File as...',
msg_export_file:'Select path where you want to export',
datatype_pkg_name:'DataTypes'
datatype_pkg_name:'DataTypes',
status_code_enum_name:'StatusCode'
}
/**
* @function configMessage
Expand Down
3 changes: 2 additions & 1 deletion src/transport/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ const fields = {
'extra': 'extra',
'minCardinality': 'minCardinality',
'maxCardinality': 'maxCardinality',
'resource': 'resource'
'resource': 'resource',
'reference': 'reference'

};
module.exports = fields;
241 changes: 239 additions & 2 deletions src/transport/transportNew.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ function addRelationshipTargettingEndNone(entity, selPackage) {
let umlRelationship = app.repository.select("@UMLAssociation");
let classRelationship = umlRelationship.filter(rel => {
return entity._id == rel.end2.reference._id;
})
});
if (classRelationship.length > 0) {
classRelationship.forEach(rel => {
let relationshipObj = {};
Expand Down Expand Up @@ -382,6 +382,7 @@ function addRelationshipTargettingEnd2(entity, selPackage) {
if (rel.documentation != "") {
relationshipObj[fields.description] = rel.documentation;
}

/* Adding relationship 'status' */
addPropertyStatus(rel, relationshipObj);

Expand Down Expand Up @@ -517,4 +518,240 @@ function addPropertyCardinality(property, propertyObj) {
propertyObj[fields.maxCardinality] = maxCardinality;
}
}
module.exports.exportNewModel = exportNewModel;

function isDatatypePkgAvail() {
let project = app.project.getProject();
let rootPackages = app.repository.select(project.name + "::@UMLPackage");
let dataTypePkgResult = rootPackages.filter(pkg => {
return pkg.name == constant.datatype_pkg_name;
});
if (dataTypePkgResult.length == 1) {
return true;
} else {
return false;
}
}

function importNewModel() {
var mFiles = app.dialogs.showOpenDialog('Import file As JSON (.json)', null, JSON_FILE_FILTERS)
if (mFiles == null) {
return;
}
finalPath = mFiles[0];
let filePath = finalPath;
var contentStr = fs.readFileSync(filePath, 'utf8');
var content = JSON.parse(contentStr);
console.log("File Data : ", content);
let dataTypesContent = content.dataTypes;

if (!isStatusCodeAvail()) {
addStatusCodeEnum();
}

if (!isDatatypePkgAvail()) {
addDataTypePackage(dataTypesContent);
} else {
updateDataTypePackage(dataTypesContent);
}


app.modelExplorer.rebuild();
}

function addStatusCodeEnum() {
let project = app.project.getProject();
let createStatusCodeEnume = {};
let mainOwnedElements = [];

createStatusCodeEnume[fields._type] = 'UMLEnumeration';
createStatusCodeEnume[fields.name] = constant.status_code_enum_name;
createStatusCodeEnume[fields.ownedElements] = mainOwnedElements;
createStatusCodeEnume[fields._parent] = {
'$ref': project._id
}
let enumStatusCode = app.repository.readObject(createStatusCodeEnume);
app.engine.addItem(project, 'ownedElements', enumStatusCode);

let literals = ['active', 'deleted', 'deprecated', 'proposed'];
literals.forEach(literal => {
let createUMLLiteral = {};
createUMLLiteral[fields._type] = 'UMLEnumerationLiteral';
createUMLLiteral[fields.name] = literal;
createUMLLiteral[fields._parent] = {
'$ref': enumStatusCode._id
}

let enumLiteral = app.repository.readObject(createUMLLiteral);
app.engine.addItem(enumStatusCode, 'literals', enumLiteral);
});


app.modelExplorer.rebuild();
}

function isStatusCodeAvail() {
let result = app.repository.select(constant.status_code_enum_name);
result = result.filter(mEnum => {
return mEnum instanceof type.UMLEnumeration;
});
if (result.length == 1) {
return true;
} else {
return false;
}
}

function updateDataTypePackage(dataTypesContent) {
// 'active', 'deleted', 'deprecated', 'proposed'
let statusCodeEnum = app.repository.select(constant.status_code_enum_name)
statusCodeEnum = statusCodeEnum.filter(cEnum => {
return cEnum instanceof type.UMLEnumeration;
});
if (statusCodeEnum.length == 1) {
statusCodeEnum = statusCodeEnum[0];
}
let enumStatusLiterals = statusCodeEnum.literals;

let project = app.project.getProject();
let rootPackages = app.repository.select(project.name + "::@UMLPackage");
let dataTypePkgResult = rootPackages.filter(pkg => {
return pkg.name == constant.datatype_pkg_name;
});


if (dataTypePkgResult.length == 1) {
let dataTypePackage = dataTypePkgResult[0];
let dataTypeClasses = app.repository.select(dataTypePackage.name + "::@UMLClass");

let needToCreateType = [];
dataTypesContent.forEach(dtPr => {
let resFlter = dataTypeClasses.filter(res => {
return dtPr.name == res.name;
});
if(resFlter.length == 0){
needToCreateType.push(dtPr);
}
});

/* Update -> datatype class, Creat/Update -> tag */
if (dataTypeClasses.length > 0) {
dataTypeClasses.forEach(dataTypeClass => {

console.log("Updated datatype : ",dataTypeClass.name);
let result = dataTypesContent.filter(contentClass => {
return contentClass.name == dataTypeClass.name;
});

if (result.length != 0) {
let contentClass = result[0];

let contentClassKeys = Object.keys(contentClass);

contentClassKeys.forEach(key => {
if (dataTypeClass.hasOwnProperty(key)) {
app.engine.setProperty(dataTypeClass, key, dataTypeClass[key]);
}

let tags = dataTypeClass.tags;
if (key == 'status' && tags.length > 0) {

tags.filter(tag => {
if (tag.name == key) {

let statusName = contentClass[key];
console.log("Updated status: ", statusName);
let resStatus = enumStatusLiterals.filter(statusLiteral => {
return statusLiteral.name == statusName;
});
if (resStatus.length == 1) {
app.engine.setProperty(tag, 'reference', resStatus[0]);
}
}
});
} else if (key == 'status' && tags.length == 0) {
console.log("Created status : ", contentClass[key]);
createStatusTag(contentClass,dataTypeClass);
}
});

}
});
}

/* Create -> datatype class, Create -> tag */
createDataType(needToCreateType,dataTypePackage);
}
}

function addDataTypePackage(dataTypesContent) {
let project = app.project.getProject();
let createDataTypePackage = {};
let mainOwnedElements = [];

createDataTypePackage[fields._type] = 'UMLPackage';
createDataTypePackage[fields.name] = constant.datatype_pkg_name;
createDataTypePackage[fields.ownedElements] = mainOwnedElements;
createDataTypePackage[fields._parent] = {
'$ref': project._id
}
let pkg = app.repository.readObject(createDataTypePackage);
app.engine.addItem(project, 'ownedElements', pkg);

createDataType(dataTypesContent,pkg);

app.modelExplorer.rebuild();
}
function createDataType(dataTypesContent,parent){
dataTypesContent.forEach(contentClass => {
console.log("Created datatype : ",contentClass.name);
let createClass = [];
createClass[fields.name] = contentClass.name;
createClass[fields._type] = 'UMLClass';
let dtClass = app.repository.readObject(createClass);
app.engine.addItem(parent, 'ownedElements', dtClass);

if (contentClass.hasOwnProperty('status')) {
createStatusTag(contentClass,dtClass);
}
});
}
function createStatusTag(contentClass,dtClass) {
let statusName = contentClass.status;
let arrTag = [];

let createTag = {};
createTag[fields._type] = 'Tag';
createTag[fields.name] = 'status';
createTag[fields.kind] = 'reference';
createTag[fields._parent] = {
'$ref': dtClass._id
}
let resultReference = app.repository.select(statusName);
resultReference = resultReference.filter(refe => {
return refe instanceof type.UMLEnumerationLiteral;
});

if (resultReference.length > 0) {
createTag[fields.reference] = {
'$ref': resultReference[0]._id
}
}
let cTag = app.repository.readObject(createTag);
arrTag.push(cTag);
app.engine.setProperty(dtClass, 'tags', arrTag);

/*
"_type": "Tag",
"_id": "AAAAAAFxXx3BFEbB/xo=",
"_parent": {
"$ref": "AAAAAAFxXxLWFT56jMo="
},
"name": "status",
"kind": "reference",
"reference": {
"$ref": "AAAAAAFxXxijZUC75JM="
}
*/
}
module.exports.exportNewModel = exportNewModel;
module.exports.importNewModel = importNewModel;

0 comments on commit 52afe70

Please sign in to comment.