UNPKG

grafo-client

Version:

Grafo Client API Library

1 lines 6.11 kB
"use strict";const async=require("async");let Concept=require("./models/concept"),Relationship=require("./models/relationship"),util=require("./util");function DocBuilder(t,e,o){this.ekgDoc=t,this.ekgApi=e,this.slot0Filled=!1,this.slot1Filled=!1,this.IdToSlotMap={},this.iriToElementIdMap={},this.logger=o}module.exports=DocBuilder,DocBuilder.prototype.addSlotDoc=async function(t,e){if(e){if("data0"===t){this.slot0Filled=!0,this.slot0IriMap=e.iri,this.slot0NodeMap=e.nodes,this.slot0LinkMap=e.links,this.slot0CoordMap=e.coordinates,this.slot0AttrGrpMap=e.attributeGroups;let t=await fillIdToSlotMap(this,e,"data0"),o=await buildIriToElementIdMap(this,e.iri);return t&&o}{this.slot1Filled=!0,this.slot1IriMap=e.iri,this.slot1NodeMap=e.nodes,this.slot1LinkMap=e.links,this.slot1CoordMap=e.coordinates,this.slot1AttrGrpMap=e.attributeGroups;let t=await fillIdToSlotMap(this,e,"data1"),o=await buildIriToElementIdMap(this,e.iri);return t&&o}}},DocBuilder.prototype.areBothSlotsFilled=function(){return this.slot0Filled&&this.slot1Filled},DocBuilder.prototype.buildDoc=async function(){return this.areBothSlotsFilled()?(await parseNodes(this,this.slot0NodeMap),await parseNodes(this,this.slot1NodeMap),await parseLinks(this,this.slot0LinkMap),await parseLinks(this,this.slot1LinkMap),this.ekgDoc.initIriToElementIdMap(this.iriToElementIdMap),this.ekgDoc.setEkgApi(this.ekgApi),this.ekgDoc.onDocReady(),await this.ekgDoc.buildAttrMap()):Promise.resolve()},DocBuilder.prototype.getIdToSlotMap=function(t){return this.IdToSlotMap[t]},DocBuilder.prototype.getNodeSlot=function(){let t=0,e=0;for(let o in this.IdToSlotMap)o.startsWith("node")&&("data0"===this.IdToSlotMap[o]?t+=1:e+=1);return t<=e?0:1},DocBuilder.prototype.updateIdToSlotMap=function(t,e){this.IdToSlotMap[t]||(this.IdToSlotMap[t]=0===e?"data0":"data1")};let fillIdToSlotMap=async function(t,e,o){let i=await util.forEachOfSeriesUpdateCollectionWithSuppliedData(e.nodes,t.IdToSlotMap,o,{onKey:!0}),a=await util.forEachOfSeriesUpdateCollectionWithSuppliedData(e.links,t.IdToSlotMap,o,{onKey:!0}),r=await util.forEachOfSeriesUpdateCollectionWithSuppliedData(e.attributeGroups,t.IdToSlotMap,o,{onKey:!0});return i&&a&&r},parseNodes=async function(t,e){return await util.forEachOfSeries(e,parseNodesCbForAsyncIterator,{_this:t})},createConceptFromMongoDbData=async function(t,e){let o=lookupIriFor(t,e.id),i=lookupCoordinatesFor(t,e.id),a=lookupAttrGrpFor(t,e.id),r=new Concept;if(r.initUsingMongoDbStructure(o,e,i),a){let e=await getIriSubMapForAttributes(t,a);await r.addAttributesFromMongoDb(a,e)}return r},getIriSubMapForAttributes=function(t,e){return new Promise((o,i)=>{let a={};async.eachSeries(e.attributeList,function(e,o){"attribute"===e.nodeType&&(a[e.id]=lookupIriFor(t,e.id)),async.setImmediate(function(){o()})},function(t,e){t?i(t):o(a)})})},parseLinks=async function(t,e){return await util.forEachOfSeries(e,parseLinksCbForAsyncIterator,{_this:t})},createLinkFromMongoDbData=async function(t,e){let o=lookupIriFor(t,e.linkId),i=lookupAttrGrpFor(t,e.linkId),a=null;"self"===e.linkType&&(a=lookupCoordinatesFor(t,e.linkId));let r=new Relationship;if(r.initUsingMongoDbStructure(o,e,a),i){let e=await getIriSubMapForAttributes(t,i);await r.addAttributesFromMongoDb(i,e)}return r},lookupIriFor=function(t,e){let o=t.slot0IriMap[e];return o||(o=t.slot1IriMap[e]),o},lookupCoordinatesFor=function(t,e){let o=t.slot0CoordMap[e];return o||(o=t.slot1CoordMap[e]),o},lookupAttrGrpFor=function(t,e){let o=t.slot0AttrGrpMap[e];return o||(o=t.slot1AttrGrpMap[e]),o},lookupNodeFor=function(t,e){let o=t.slot0NodeMap[e];return o||(o=t.slot1NodeMap[e]),o},buildIriToElementIdMap=async function(t,e){return await util.forEachOfSeriesWithoutPromise(e,(t,e,o)=>{let i=o._this;t&&t.iri&&(i.iriToElementIdMap[t.iri]=e)},{_this:t})},parseNodesCbForAsyncIterator=async function(t,e,o){let i=o._this;if("concept"!==t.nodeType)throw new Error("Hey I am getting data from server to construct a ekg ekgDoc and I got a node whose node type is NOT concept. Can you do something please?");{let e=await createConceptFromMongoDbData(i,t);i.ekgDoc.addConceptInternal(e)}},parseLinksCbForAsyncIterator=async function(t,e,o){let i=o._this;if("link"!==t.nodeType)throw new Error("Hey I am getting data from server to construct a ekg ekgDoc and I got a link whose node type is NOT link. Can you do something please?");await util.eachSeries(t.peers,peerRelCbForAsyncIterator,{_this:i,lastUpdatedBy:t.lastUpdatedBy}),await util.eachSeries(t.self,selfRelCbForAsyncIterator,{_this:i,lastUpdatedBy:t.lastUpdatedBy}),await util.eachSeries(t.parents,parentRelCbForAsyncIterator,{_this:i,lastUpdatedBy:t.lastUpdatedBy}),await util.eachSeries(t.children,childRelCbForAsyncIterator,{_this:i,lastUpdatedBy:t.lastUpdatedBy})},peerRelCbForAsyncIterator=async function(t,e){let o=e._this;if(!o.ekgDoc.getRelationshipInternal(t.linkId)){let i=await createLinkFromMongoDbData(o,t);i.lastUpdatedBy=e.lastUpdatedBy,o.ekgDoc.addRelationshipInternal(i),o.ekgDoc.getConcept(i.fromConceptId)&&o.ekgDoc.getConcept(i.fromConceptId).setFromLinks(i.getId()),o.ekgDoc.getConcept(i.toConceptId)&&o.ekgDoc.getConcept(i.toConceptId).setToLinks(i.getId())}},selfRelCbForAsyncIterator=async function(t,e){let o=e._this;if(!o.ekgDoc.getRelationshipInternal(t.linkId)){let i=await createLinkFromMongoDbData(o,t);i.lastUpdatedBy=e.lastUpdatedBy,o.ekgDoc.addRelationshipInternal(i),o.ekgDoc.getConcept(i.fromConceptId)&&o.ekgDoc.getConcept(i.fromConceptId).setFromLinks(i.getId())}},parentRelCbForAsyncIterator=async function(t,e){let o=e._this,i=o.ekgDoc.getRelationshipInternal(t.linkId);i||((i=await createLinkFromMongoDbData(o,t)).lastUpdatedBy=e.lastUpdatedBy,o.ekgDoc.addRelationshipInternal(i)),o.ekgDoc.getConcept(i.fromConceptId)&&o.ekgDoc.getConcept(i.fromConceptId).setFromLinks(i.getId())},childRelCbForAsyncIterator=async function(t,e){let o=e._this,i=o.ekgDoc.getRelationshipInternal(t.linkId);i||((i=await createLinkFromMongoDbData(o,t)).lastUpdatedBy=e.lastUpdatedBy,o.ekgDoc.addRelationshipInternal(i)),o.ekgDoc.getConcept(i.toConceptId)&&o.ekgDoc.getConcept(i.toConceptId).setToLinks(i.getId())};