UNPKG

grafo-client

Version:

Grafo Client API Library

1 lines 7.78 kB
"use strict";var EkgShareDBAdapter=require("./ekg_sharedb_adapter"),AttributeGroup=require("./models/attribute_group"),Concept=require("./models/concept"),Relationship=require("./models/relationship"),Attribute=require("./models/attribute"),Iri=require("./models/iri"),DocBuilder=require("./doc_builder"),DataTypes=require("./models/datatypes"),ColorPalette=require("./models/color_palette"),RelationshipHelper=require("./models/relationship_builder.js");function EkgApi(t){this.evenSlot=!0,this.logger=t}module.exports=EkgApi,EkgApi.prototype.init=function(t,e,i){this.shareDbAdapter=new EkgShareDBAdapter(this.logger),this.shareDbAdapter.init(t,i,this),this.userId=e},EkgApi.prototype.initServer=function(t,e){this.shareDbAdapter=new EkgShareDBAdapter(this.logger),this.userId=e,this.shareDbAdapter.initServer(t,this)},EkgApi.prototype.initAdapter=function(t,e,i){return e&&this.shareDbAdapter.setSessionId(e),this.shareDbAdapter.initConnection(t,i)},EkgApi.prototype.getEkgProxy=function(t,e){return this.document=e,this.shareDbAdapter.getEkgProxy(t)},EkgApi.prototype.initDocBuilder=function(t){this.ekgDoc=t,this.docBuilder=new DocBuilder(t,this,this.logger)},EkgApi.prototype.close=async function(){await this.shareDbAdapter.close()},EkgApi.prototype.getConversationApi=function(){return this.shareDbAdapter.getConversationApi()},EkgApi.prototype.createConcept=async function(t){this.logger.debug("Creating Concept with id = "+t.getId());var e=this.docBuilder.getNodeSlot();this.logger.debug("Slot to insert: "+e);var i={slot:"data"+e,lastUpdatedBy:this.userId};return t.update(i),this.docBuilder.updateIdToSlotMap(t.getId(),e),await this.shareDbAdapter.createConcept(t),t},EkgApi.prototype.updateConcept=async function(t,e){return this.logger.debug("Updating Concept with id = "+t.getId()),t.setLastUpdatedBy(this.userId),await this.shareDbAdapter.updateConcept(t,e),t},EkgApi.prototype.getConceptByName=function(t){var e=getObjectByName(this,t,"nodes");return new Promise((t,i)=>{e||i(new Error("Concept not found"));var r=new Iri;r.setIri(e.iri,e.hasEdited);var a=new Concept(r,e.positionX,e.positionY,e.label,e.description,e.color,e.fields,e.id);a.setSlot(e.slot),t(a)})},EkgApi.prototype.getAttribute=function(t){var e=getObjectByName(this,t,"attributeGroups");return new Promise((t,i)=>{e||i(new Error("Attribute not found")),(new Iri).setIri(e.iri.iri,e.iri.hasEdited);var r=new Attribute("",e.xsdDataType,e.label,e.description,e.fields);r.id=e.id,r.setGroupId(e.groupId),t(r)})},EkgApi.prototype.addAttribute=async function(t){if(this.logger.debug("Adding Attribute(s)"),t.getDeleted()&&t.setDeleted(!1),t.setLastUpdatedBy(this.userId),!t.getSlot()){var e=t.attributeList[0];e.getParentType()===DataTypes.ResourceType.RELATIONSHIP?t.setSlot(this.docBuilder.getIdToSlotMap(e.getToNodeId())):t.setSlot(this.docBuilder.getIdToSlotMap(t.getParentId()))}return await this.shareDbAdapter.addAttribute(t),t},EkgApi.prototype.updateAttribute=async function(t,e){if(this.logger.debug("Updating Attribute(s)"),t.setLastUpdatedBy(this.userId),!t.getSlot()&&t.attributeList.length>0){var i=t.attributeList[0];i.getParentType()===DataTypes.ResourceType.RELATIONSHIP?t.setSlot(this.docBuilder.getIdToSlotMap(i.getToNodeId())):t.setSlot(this.docBuilder.getIdToSlotMap(t.getParentId()))}return await this.shareDbAdapter.updateAttribute(t,e),t},EkgApi.prototype.updateDiagramCoordinates=function(t,e,i){this.logger.debug("******************* ekgapi updateDiagramCoordinates ********************"+e),this.logger.debug(e)},EkgApi.prototype.updateDiagramArray=function(t,e,i,r){if(!this.shareDbAdapter.getRegisteredCallbackId(i)||this.shareDbAdapter.getRegisteredCallbackId(i)&&i!==this.shareDbAdapter.getRegisteredCallbackId(i))if(this.logger.debug("******************* ekgapi updateDiagramArray ********************"+i),this.logger.debug("Array Name : "+e),this.logger.debug("Edited Id : "+i),this.logger.debug("Registered Callback id : "+this.shareDbAdapter.getRegisteredCallbackId(i)),"nodes"===e)this.logger.debug("*********** NODE CALLBACK**************"),(g=this.ekgDoc.concepts[i])&&(g.setLabel(r.label),g.setDescription(r.description),g.setFields(r.fields),g.setLastUpdatedBy(r.lastUpdatedBy),r.color&&g.setColor(ColorPalette.resolveColor(r.color)));else if("links"===e)this.logger.debug("*********** RELATIONSHIP CALLBACK**************"),r.peers&&r.peers.length>0&&updateDiagramArrayForRelationship(this,r.peers),r.self&&r.self.length>0&&updateDiagramArrayForRelationship(this,r.self),r.parents&&r.parents.length>0&&updateDiagramArrayForRelationship(this,r.parents),r.children&&r.children.length>0&&updateDiagramArrayForRelationship(this,r.children);else if("iri"===e){if(this.logger.debug("*********** IRI CALLBACK**************"),i.startsWith("node"))(g=this.ekgDoc.concepts[i])&&g.setIri(r);else if(i.startsWith("link")){(n=this.ekgDoc.relationships[i])&&n.setIri(r)}else if(i.startsWith("linkattr")){var a=this.ekgDoc.attrMap[i];if(o=(n=this.ekgDoc.relationships[a]).getAttributeGroup())for(var s=0;s<o.getAttributeList().length;s++){(p=o.getAttributeList()[s]).getId()===i&&p.setIri(r)}}else{var o,d=this.ekgDoc.attrMap[i];if(g=this.ekgDoc.concepts[d])if(o=g.getAttributeGroup())for(s=0;s<o.getAttributeList().length;s++){var p;(p=o.getAttributeList()[s]).getId()===i&&p.setIri(r)}}}else if("attributeGroups"===e){var g;if(this.logger.debug("*********** ATTRIBUTE GROUP CALLBACK**************"),i.startsWith("node"))(g=this.ekgDoc.concepts[i])&&g.getAttributeGroup()&&updateDiagramArrayForAttrGroup(g.getAttributeGroup(),r);else if(i.startsWith("link")){var n;(n=this.ekgDoc.relationships[i])&&n.getAttributeGroup()&&updateDiagramArrayForAttrGroup(n.getAttributeGroup(),r)}}},EkgApi.prototype.updateDiagramData=async function(t,e){return this.logger.debug("******************* ekgapi updateDiagramData ********************"),await this.docBuilder.addSlotDoc(t,e)&&await this.docBuilder.buildDoc()};var updateDiagramArrayForRelationship=function(t,e){for(var i=0;i<e.length;i++){var r=t.ekgDoc.relationships[e[i].linkId];r&&(r.setLabel(e[i].label),r.setDescription(e[i].description),r.setFields(e[i].fields),r.setLastUpdatedBy(e[i].lastUpdatedBy),e[i].color&&r.setColor(ColorPalette.resolveColor(e[i].color)),e[i].cardinalityType&&r.setCardinalityType(DataTypes.resolveCardinalityType(e[i].cardinalityType)),r.setCardinality(e[i].cardinality))}},updateDiagramArrayForAttrGroup=function(t,e){t.setLastUpdatedBy(e.lastUpdatedBy);for(var i=0;i<t.getAttributeList().length;i++)for(var r=t.getAttributeList()[i],a=0;a<e.attributeList.length;a++)r.getId()===e.attributeList[i]&&(r.setLabel(e.attributeList[i].label),r.setDescription(e.attributeList[i].description),r.setFields(e.attributeList[i].fields),r.setXsdDataType(DataTypes.resolveXsdDataType(e.attributeList[i].xsdDataType)),r.setCardinalityType(DataTypes.resolveCardinalityType(e.attributeList[i].cardinalityType)),r.setCardinality(e.attributeList[i].cardinality))},getObjectByName=function(t,e,i){for(var r in t.DiagramData)for(var a in t.DiagramData[r][i]){var s=t.DiagramData[r][i][a];if("nodes"===i){if(s.label===e)return t.logger.debug("Concept From Diagram Data ===> "),t.logger.debug(s),s}else if("attributeGroups"===i)for(var o in s.attributeList)if(s.attributeList[o].label===e)return t.logger.debug("Attribute From Diagram Data ===> "),t.logger.debug(s.attributeList[o]),s.attributeList[o]}};EkgApi.prototype.createRelationship=async function(t){return this.logger.debug("Creating Relationship with id: "+t.id),t.setLastUpdatedBy(this.userId),await this.shareDbAdapter.createRelationship(t),t},EkgApi.prototype.updateRelationship=async function(t,e){return this.logger.debug("Updating Relationship with id = "+t.getId()),t.setLastUpdatedBy(this.userId),await this.shareDbAdapter.updateRelationship(t,e),t};