UNPKG

grafo-client

Version:

Grafo Client API Library

1 lines 44.5 kB
const uuidv4=require("uuid/v4");let async=require("async");const util=require("../util");let Concept=require("./concept"),Relationship=require("./relationship"),Attribute=require("./attribute"),AttributeGroup=require("./attribute_group"),DataTypes=require("./datatypes"),Validator=require("./validator"),Field=require("./custom_field"),Mapping=require("./mapping"),RelationshipBuilder=require("./relationship_builder.js");const MappingTypes=require("./mapping_types"),MappingStatus=require("./mapping_status"),GrafoErrorGlossary=require("../errors/GrafoErrorGlossary"),GenericGrafoClientError=require("../errors/GenericGrafoClientError"),GrafoCrudError=require("../errors/GrafoCrudError"),NonExistentEntityError=require("../errors/NonExistentEntityError"),DuplicateEntityError=require("../errors/DuplicateEntityError"),GenericValidationError=require("../errors/GenericValidationError"),coarseGrainedHelper=require("./ekgdoc_coarsegrained_helper");function EkgDocument(t,e){this.logger=e,this.shouldTrackDelta=!1,t&&(this.id=t.id,this.title=t.title,this.iri=t.iri,this.iriPrefix=t.iriPrefix,this.description=t.description,this.owner=t.owner,this.dateCreated=t.dateCreated,this.dateDeleted=t.dateDeleted,this.lastModified=t.lastModified,this.parent=t.parent,this.needForceDirectedLayout=t.needForceDirectedLayout,this.deleted=t.deleted),this.concepts={},this.relationships={}}module.exports=EkgDocument,EkgDocument.prototype.getId=function(){return this.id},EkgDocument.prototype.getConcepts=function(){return new Promise((t,e)=>{let r=[];async.forEachOfSeries(this.concepts,(t,e,i)=>{r.push(t.toObject()),async.setImmediate(function(){i()})},i=>{i?e(i):t(r)})})},EkgDocument.prototype.getRelationships=function(){return new Promise((t,e)=>{let r=[];async.forEachOfSeries(this.relationships,(t,e,i)=>{r.push(t.toObject()),async.setImmediate(function(){i()})},i=>{i?e(i):t(r)})})},EkgDocument.prototype.getRelationshipInternal=function(t){return this.relationships[t]},EkgDocument.prototype.getConcept=function(t){let e=this.concepts[t];if(e)return e;throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Concept Id "+t+" is Invalid or Concept does not exist")},EkgDocument.prototype.getRelationship=function(t){let e=this.relationships[t];if(e&&e.getRelationShipType()!==DataTypes.RelationshipType.SPECIALIZATION)return e;throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Relationship Id "+t+" is Invalid or Relationship does not exist")},EkgDocument.prototype.getSpecializations=function(){return new Promise((t,e)=>{let r=[];async.forEachOfSeries(this.relationships,(t,e,i)=>{t&&t.getRelationShipType()===DataTypes.RelationshipType.SPECIALIZATION&&r.push(t.toObject()),async.setImmediate(function(){i()})},i=>{i?e(i):t(r)})})},EkgDocument.prototype.getSpecialization=function(t){let e=this.relationships[t];if(e&&e.getRelationShipType()===DataTypes.RelationshipType.SPECIALIZATION)return e;throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Specialization Id "+t+" is Invalid or Specialization does not exist")},EkgDocument.prototype.getFields=function(t){if(this.concepts[t])return this.concepts[t].getFields()},EkgDocument.prototype.getConceptAttributes=function(t){return new Promise((e,r)=>{let i=this.concepts[t];i||r(new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Concept Id "+t+" is Invalid or Concept does not exist"));let o=[];async.eachSeries(i.getAttributeList(),(t,e)=>{o.push(t.toObject()),async.setImmediate(function(){e()})},t=>{t?r(t):e(o)})})},EkgDocument.prototype.getConceptAttribute=async function(t,e){let r=this.concepts[t];if(!r)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Concept Id "+t+" is Invalid or Concept does not exist");let i=await r.getAttribute(e);if(!i)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Attribute Id "+e+" is Invalid or Attribute does not exist");return i},EkgDocument.prototype.getRelationshipAttributes=function(t){return new Promise((e,r)=>{let i=this.relationships[t];i||r(new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Relationship Id "+t+" is Invalid or Relationship does not exist"));let o=[];async.eachSeries(i.getAttributeList(),(t,e)=>{o.push(t.toObject()),async.setImmediate(function(){e()})},t=>{t?r(t):e(o)})})},EkgDocument.prototype.getRelationshipAttribute=async function(t,e){let r=this.relationships[t];if(!r)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Relationship Id "+t+" is Invalid or Relationship does not exist");let i=await r.getAttribute(e);if(!i)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Attribute Id "+e+" is Invalid or Attribute does not exist");return i},EkgDocument.prototype.cloneConcept=function(t){return new Promise((e,r)=>{try{let i=this.getConcept(t);i||r(new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Concept Id "+t+" is Invalid or Concept does not exist")),e(Object.assign(new Concept,i))}catch(t){r(new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Clone Concept",t.message))}})},EkgDocument.prototype.addConcept=async function(t,e,r,i,o,n,a,s,l){let p=this;if(l||Validator.validateConcept({label:t,positionX:e,positionY:r,description:i,color:o,fields:n,iri:a,id:s}),s?p.concepts[s.trim()]:void 0)throw new DuplicateEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Add Concept. The Concept with Id "+s+" already exists");{let l=p.iri,d=new Concept(t,e,r,i,o,n,a,s,l);try{let t=await p.ekgApi.createConcept(d);return p.concepts[t.getId()]=t,t}catch(t){throw new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Create Concept",t.message)}}},EkgDocument.prototype.updateConcept=async function(t,e,r,i,o,n,a,s,l,p){let d=this;p||Validator.validateConcept({positionX:e,positionY:r,description:i,color:o,fields:n,iri:s},!0);let u=d.concepts[l];if(!u||u&&u.deleted)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Update Concept. The Concept with Id "+l+" is invalid or does not exist anymore");if(!(t&&t.trim()||e||r||i||o||n&&n.length>0||s||a))throw new GenericValidationError(GrafoErrorGlossary.ErrorLevel.INFO,"No data was available for update");{let l=Object.assign(new Concept,u);t&&t.trim()&&l.setLabel(t),e&&l.setPositionX(e),r&&l.setPositionY(r),i&&l.setDescription(i),o&&l.setColor(o),n&&l.setFields(n,p),s&&l.setIri(s),a&&l.setMappings(a);try{let t=await d.ekgApi.updateConcept(l,u);return d.concepts[t.getId()]=t,t}catch(t){throw new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Update Concept "+u.getLabel(),t.message)}}},EkgDocument.prototype.deleteConcept=function(t){let e=this;return new Promise((r,i)=>{let o=e.concepts[t];if(!o||o&&o.deleted)i(new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Delete Concept. The Concept with Id "+t+" is invalid or does not exist anymore"));else{let n=Object.assign(new Concept,o);n.setDeleted(!0),e.deleteConceptAttribute(t).then(()=>new Promise(function(t,r){async.eachSeries(o.getFromLinks(),function(t,r){e.deleteRelationship(t).then(function(){r()})},function(e){return t()})})).then(()=>new Promise(function(t,r){async.eachSeries(o.getToLinks(),function(t,r){e.deleteRelationship(t).then(function(){r()})},function(e){return t()})})).then(()=>e.ekgApi.updateConcept(n,o)).then(t=>{e.concepts[t.getId()]=t,r()}).catch(e=>{i(new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Delete Concept with Id "+t,e.message))})}})},EkgDocument.prototype.addConceptInternal=function(t){this.concepts[t.getId()]=t},EkgDocument.prototype.addRelationshipInternal=function(t){this.relationships[t.getId()]=t},EkgDocument.prototype.getConceptFields=function(t){let e=this.concepts[t];if(e)return e.getFields();throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"The Concept with Id "+t+" is invalid or does not exist anymore")},EkgDocument.prototype.getConceptField=function(t,e){let r=this.concepts[t];if(r){let t=r.getFields();for(let r=0;r<t.length;r++)if(e===t[r].id)return t[r];throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Field Not Found")}throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"The Concept with Id "+t+" is invalid or does not exist anymore")},EkgDocument.prototype.addConceptField=function(t,e,r){let i=[],o=new Field(t,e);return Validator.validateCustomField(o,!0),i.push(o),this.updateConcept(null,null,null,null,null,i,null,null,r,!0)},EkgDocument.prototype.updateConceptField=function(t,e,r,i){let o=[],n=new Field(t,e,r);return Validator.validateCustomField(n,!1),o.push(n),this.updateConcept(null,null,null,null,null,o,null,null,i,!0)},EkgDocument.prototype.deleteConceptField=async function(t,e){let r=this,i=r.concepts[t];if(!i||i&&i.deleted)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"The Concept with Id "+t+" is invalid or does not exist anymore");if(!i.fields)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"The Field with Id "+e+" is invalid or does not exist anymore");{let t=Object.assign(new Concept,i);if(-1===t.deleteField(e))throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"The Field with Id "+e+" is invalid or does not exist anymore");try{let o=await r.ekgApi.updateConcept(t,i);return r.concepts[o.getId()]=o,o}catch(t){throw new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Delete Field "+e,t.message)}}},EkgDocument.prototype.addConceptMappings=async function(t,e){let r=this,i=r.concepts[t];if(!i)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Add Mapping. This mapping belongs to Concept with Id "+t+". The concept is invalid or does not exist anymore");{let t=Object.assign(new Concept,i);t.addMappings(e);try{let e=await r.ekgApi.updateConcept(t,i);return r.concepts[e.getId()]=e,e}catch(t){throw new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Add Mapping for Concept "+i.getLabel(),t.message)}}},EkgDocument.prototype.deleteConceptMappings=async function(t,e,r){let i=this,o=i.concepts[t],n="Mapping";if(r&&(n="Imported "+n),!o)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Delete Mapping. This mapping belonged to Concept with Id "+t+". The concept is invalid or does not exist anymore");if(!o.mappings)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.INFO,"Cannot Delete Mapping. No Mappings exist for Concept "+o.getLabel()+" & id "+o.getId());{let t=Object.assign(new Concept,o),a=t.deleteMappings(e,r);if(noMappingsWhatsoeverExisted(a))throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.INFO,"Cannot Delete Mapping. No "+n+" exist for Concept "+o.getLabel()+" for the specified database identified by "+e);try{let r=await i.ekgApi.updateConcept(t,o);return i.concepts[r.getId()]=r,r}catch(t){throw new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Delete "+n+" for Concept "+o.getLabel()+" and database identified by "+e,t.message)}}};var noMappingsWhatsoeverExisted=function(t){for(var e=!0,r=0;r<t.length;r++){var i=t[r];if(!1===(e=e&&-1===i))return!1}return e};EkgDocument.prototype.getRelationshipFields=function(t){let e=this.relationships[t];if(e)return e.getFields();throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Relationship Not Found")},EkgDocument.prototype.getRelationshipField=function(t,e){let r=this.relationships[t];if(r){let t=r.getFields();for(let r=0;r<t.length;r++)if(e===t[r].id)return t[r];throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Field Not Found")}throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Relationship Not Found")},EkgDocument.prototype.addRelationshipField=function(t,e,r){let i=[],o=new Field(t,e);return Validator.validateCustomField(o,!0),i.push(o),this.updateRelationship(null,null,null,null,i,null,null,null,null,null,null,r,!0)},EkgDocument.prototype.updateRelationshipField=function(t,e,r,i){let o=[],n=new Field(t,e,r);return Validator.validateCustomField(n,!1),o.push(n),this.updateRelationship(null,null,null,null,o,null,null,null,null,null,null,i,!0)},EkgDocument.prototype.deleteRelationshipField=async function(t,e){let r=this,i=r.relationships[t];if(!i||i&&i.deleted)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Relationship Id "+t+" is Invalid or Relationship does not exist");if(!i.fields)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Field Id "+e+" is Invalid or Field does not exist");{let t=Object.assign(new Relationship,i);if(-1===t.deleteField(e))throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Field Id "+e+" is Invalid or Field does not exist");new RelationshipBuilder(r,t,!0).buildShareDbFormatRelationship(),new RelationshipBuilder(r,i,!1).buildShareDbFormatRelationship();try{let n=await r.ekgApi.updateRelationship(t,i);var o=new Relationship;return o.getRequiredPropertiesForDoc(n),r.relationships[n.getId()]=o,n}catch(t){throw new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Delete Field "+e,t.message)}}},EkgDocument.prototype.addRelationshipMappings=async function(t,e){let r=this,i=r.relationships[t];if(!i)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Add Mapping. This mapping belongs to Relationship with Id "+t+". The relationship is invalid or does not exist anymore");{let t=Object.assign(new Relationship,i);t.addMappings(e),new RelationshipBuilder(r,t,!0).buildShareDbFormatRelationship(),new RelationshipBuilder(r,i,!1).buildShareDbFormatRelationship();let o=await r.ekgApi.updateRelationship(t,i);try{let t=new Relationship;return t.getRequiredPropertiesForDoc(o),r.relationships[o.getId()]=t,o}catch(t){throw new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Add Mapping for Relationship "+i.getLabel(),t.message)}}},EkgDocument.prototype.deleteRelationshipMappings=async function(t,e,r){let i=this,o=i.relationships[t],n="Mapping";if(r&&(n="Imported "+n),!o)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Delete Mapping. This mapping belonged to Relationship with Id "+t+". The relationship is invalid or does not exist anymore");if(!o.mappings)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.INFO,"Cannot Delete Mapping. No Mappings exist for Relationship "+o.getLabel()+" & id "+o.getId());{let t=Object.assign(new Relationship,o),a=t.deleteMappings(e,r);if(noMappingsWhatsoeverExisted(a))throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.INFO,"Cannot Delete Mapping. No "+n+" exist for Relationship "+o.getLabel()+" for the specified database identified by "+e);new RelationshipBuilder(i,t,!0).buildShareDbFormatRelationship(),new RelationshipBuilder(i,o,!1).buildShareDbFormatRelationship();try{let r=await i.ekgApi.updateRelationship(t,o),a=new Relationship;return a.getRequiredPropertiesForDoc(r),i.relationships[r.getId()]=a,r}catch(t){throw new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Delete "+n+" for Relationship "+o.getLabel()+" and database identified by "+e,t.message)}}},EkgDocument.prototype.getConceptAttributeFields=async function(t,e){let r=this.concepts[t];if(r){let t=await r.getAttribute(e);if(t)return t.getFields();throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Attribute with Id "+e+" Not Found")}throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Concept with Id "+t+" Not Found")},EkgDocument.prototype.getConceptAttributeField=async function(t,e,r){let i=this.concepts[t];if(i){let t=await i.getAttribute(e);if(t){let e=await util.eachSeriesFindById(t.getFields(),r);if(e)return e;throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Field with Id "+r+" Not Found")}throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Attribute with Id "+e+" Not Found")}throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Concept with Id "+t+" Not Found")},EkgDocument.prototype.addConceptAttributeField=function(t,e,r,i){let o=[],n=new Field(t,e);return Validator.validateCustomField(n,!0),o.push(n),this.updateAttributeToConcept(null,null,o,null,null,null,null,null,i,r,!0)},EkgDocument.prototype.updateConceptAttributeField=function(t,e,r,i,o){let n=[],a=new Field(t,e,r);return Validator.validateCustomField(a,!1),n.push(a),this.updateAttributeToConcept(null,null,n,null,null,null,null,null,o,i,!0)},EkgDocument.prototype.deleteConceptAttributeField=function(t,e,r,i,o,n){let a=this,s=i||a.concepts[t];return new Promise((l,p)=>{if(!s||s&&s.deleted)p(new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Delete Concept Attribute Field. The Concept with Id "+t+" is invalid or does not exist anymore"));else if(!e||a.attrMap[e]&&a.attrMap[e]===t){let t=o||s.attrGroup;if(t){let o=n||util.deepCopy(t);async.eachSeries(o.getAttributeList(),(n,d)=>{if(n.getId()===e){if(n.fields){-1===n.deleteField(r)?p(new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Delete Concept Attribute Field. The Field with Id "+r+" is invalid or does not exist anymore")):(o.setAttributeChanged(!0),i?l():a.ekgApi.updateAttribute(o,t).then(t=>{a.concepts[s.getId()].attrGroup=t,l(t)}).catch(t=>{p(new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Delete Field "+r,t.message))}))}else p(new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Delete Concept Attribute Field. The Field with Id "+r+" is invalid or does not exist anymore"));let e=new Error;return e.breakOutOfLoop=!0,d(e)}d()},t=>{t&&!t.breakOutOfLoop&&p(new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Delete Field "+id,t.message))})}else p(new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Delete Concept Attribute Field. The Attribute with Id "+e+" is invalid or does not exist anymore"))}else p(new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Delete Concept Attribute Field. The Attribute with Id "+e+" is invalid or does not exist anymore"))})},EkgDocument.prototype.addConceptAttributeMappings=async function(t,e,r){let i=this,o=i.concepts[e];if(!o)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Add Mapping. This mapping belonged to Attribute with Id "+t+". Its Parent Concept with Id "+e+"is invalid or does not exist anymore");if(t&&(!i.attrMap[t]||i.attrMap[t]!==e))throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Add Mapping. This mapping belonged to Attribute with Id "+t+". The Attribute is invalid or does not exist anymore");{let n=o.attrGroup;if(!n)return;{let a=Object.assign(new AttributeGroup(e),n),s=a.getAttributeList();for(let e=0;e<s.length;e++)if(s[e].getId()===t){s[e].addMappings(r);break}a.setAttributeChanged(!0);try{let e=await i.ekgApi.updateAttribute(a,n);return i.concepts[o.getId()].attrGroup=e,e}catch(e){throw new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Add Mapping for Attribute "+t,e.message)}}}},EkgDocument.prototype.addAllConceptAttributeMappings=async function(t,e,r){let i=this,o=i.concepts[t];if(o){let r=o.attrGroup;if(r){let n=Object.assign(new AttributeGroup(t),r),a=n.getAttributeList(),s=!1;for(let t=0;t<a.length;t++){let r=e[a[t].id];r&&(s=!0,a[t].addMappings(r))}n.setAttributeChanged(s);let l=await i.ekgApi.updateAttribute(n,r);return i.concepts[o.getId()].attrGroup=l,l}}else r.push(new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Add Mapping. Its Parent Concept with Id "+t+"is invalid or does not exist anymore"))},EkgDocument.prototype.deleteConceptAttributeMappings=async function(t,e,r,i,o,n,a){let s="Mapping";i&&(s="Imported "+s);let l=o||this.concepts[e];if(!l)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Delete Mapping. This mapping belonged to Attribute with Id "+t+". Its Parent Concept with Id "+e+"is invalid or does not exist anymore");if(t&&(!this.attrMap[t]||this.attrMap[t]!==e))throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Delete Mapping. This mapping belonged to Attribute with Id "+t+". The Attribute is invalid or does not exist anymore");{let p=n||l.attrGroup;if(p){let n=a||Object.assign(new AttributeGroup(e),p),d=n.getAttributeList();for(let e=0;e<d.length;e++)if(d[e].getId()===t){if(!d[e].mappings)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.INFO,"Cannot Delete Mapping. No Mappings exist for Attribute "+d[e].getLabel()+" & id "+d[e].getId());{let t=d[e].deleteMappings(r,i);if(noMappingsWhatsoeverExisted(t))throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.INFO,"Cannot Delete Mapping. No "+s+" exist for Attribute "+d[e].getLabel()+" for the specified database identified by "+r);if(n.setAttributeChanged(!0),!o){let t=await this.ekgApi.updateAttribute(n,p);return this.concepts[l.getId()].attrGroup=t,t}}break}}}},EkgDocument.prototype.getRelationshipAttributeFields=async function(t,e){let r=this.relationships[t];if(r){let t=await r.getAttribute(e);if(t)return t.getFields();throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Attribute Not Found")}throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Relationship Not Found")},EkgDocument.prototype.getRelationshipAttributeField=async function(t,e,r){let i=this.relationships[t];if(i){let t=await i.getAttribute(e);if(t){let e=await util.eachSeriesFindById(t.getFields(),r);if(e)return e;throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Field Not Found")}throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Attribute Not Found")}throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Relationship Not Found")},EkgDocument.prototype.addRelationshipAttributeField=function(t,e,r,i){let o=[],n=new Field(t,e);return Validator.validateCustomField(n,!0),o.push(n),this.updateRelationshipAttribute(null,null,o,null,null,null,null,i,r,!0)},EkgDocument.prototype.updateRelationshipAttributeField=function(t,e,r,i,o){let n=[],a=new Field(t,e,r);return Validator.validateCustomField(a,!1),n.push(a),this.updateRelationshipAttribute(null,null,n,null,null,null,null,o,i,!0)},EkgDocument.prototype.deleteRelAttributeField=function(t,e,r,i,o,n){let a=this,s=i||a.relationships[t];return new Promise((l,p)=>{if(!s||s&&s.deleted)p(new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Delete Relationship Attribute Field. The Relationship with Id "+t+" is invalid or does not exist anymore"));else if(!e||a.attrMap[e]&&a.attrMap[e]===t){let t=o||s.attrGroup;if(t){let o=n||util.deepCopy(t);async.eachSeries(o.getAttributeList(),(n,d)=>{if(n.getId()===e)if(n.fields){-1===n.deleteField(r)?p(new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Delete Relationship Attribute Field. The Field with Id "+r+" is invalid or does not exist anymore")):(o.setAttributeChanged(!0),i?l():a.ekgApi.updateAttribute(o,t).then(t=>{a.relationships[s.getId()].attrGroup=t,l(t)}).catch(t=>{p(new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Delete Field "+r,t.message))}))}else p(new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Delete Relationship Attribute Field. The Field with Id "+r+" is invalid or does not exist anymore"));else d()},t=>{t&&p(new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Delete Field "+id,t.message))})}else p(new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Delete Relationship Attribute Field. The Attribute with Id "+e+" is invalid or does not exist anymore"))}else p(new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Delete Relationship Attribute Field. The Attribute with Id "+e+" is invalid or does not exist anymore"))})},EkgDocument.prototype.setEkgApi=function(t){this.ekgApi=t;for(let e in this.concepts)this.concepts[e].setEkgApi(t);for(let e in this.relationships)this.relationships[e].setEkgApi(t)},EkgDocument.prototype.onDocReady=function(){this.shouldTrackDelta=!0},EkgDocument.prototype.buildAttrMap=async function(){this.attrMap={},await util.forEachOfSeries(this.concepts,addToAttrMap,{attrMap:this.attrMap}),await util.forEachOfSeries(this.relationships,addToAttrMap,{attrMap:this.attrMap})};let addToAttrMap=function(t,e,r){let i=t.getAttributeList(),o=r.attrMap;return new Promise((t,r)=>{async.eachSeries(i,(t,r)=>{t.deleted||(o[t.id]=e),async.setImmediate(function(){r()})},(e,i)=>{e?r(e):t(i)})})};EkgDocument.prototype.initIriToElementIdMap=function(t){this.iriToElementIdMap=t},EkgDocument.prototype.addAttributeToConcept=async function(t,e,r,i,o,n,a,s,l,p,d,u,E){let c=this;c.attrMap||(c.attrMap={}),this.logger.debug("Adding Attribute to Concept "+l),p||Validator.validateAttribute({label:t,fields:r,iri:a,xsdDataType:i,cardinalityType:o,cardinality:n});let h=c.iri,g=d||c.concepts[l];if(!g||g&&g.deleted)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Add Attribute to Concept. The Concept with Id "+l+" is invalid or does not exist anymore");if(s&&c.attrMap[s])throw new DuplicateEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Attribute with Id "+s+" already exists");{let p=E||g.getAttributeGroup(),u=new Attribute(t,e,r,i,o,n,a,s,h);if(u.setGroupId(p.getId()),p.setAttributeChanged(!0),p.setAttributeIriChanged(!0),u.setIriChanged(!0),p.attributeList.push(u),d)return;try{let t=await c.ekgApi.addAttribute(p,u);return c.attrMap[u.getId()]=l,c.concepts[g.getId()].attrGroup=t,t}catch(t){throw new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to add Attribute to Concept",t.message)}}},EkgDocument.prototype.updateAttributeToConcept=function(t,e,r,i,o,n,a,s,l,p,d,u,E,c){let h=this;this.logger.debug("Updating Attribute to Concept "+p),d||Validator.validateAttribute({fields:r,iri:s,xsdDataType:o,cardinalityType:n,cardinality:a},!0);let g=u||h.concepts[p];return new Promise((y,f)=>{if(!g||g&&g.deleted)f(new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Update Attribute. The Concept with Id "+p+" is invalid or does not exist anymore"));else if(h.attrMap[l]&&h.attrMap[l]===p){let p,G,w=E||g.getAttributeGroup(),R=c||util.deepCopy(w);async.eachSeries(R.getAttributeList(),(u,E)=>{if(u.getId()===l){p=Object.assign(new Attribute,u),G=u,(t&&t.trim()||e||r&&r.length>0||o||n||a||i)&&R.setAttributeChanged(!0),s&&R.setAttributeIriChanged(!0),t&&t.trim()&&G.setLabel(t),e&&G.setDescription(e),r&&G.setFields(r,d),o&&G.setXsdDataType(o),n&&G.setCardinalityType(n),a&&G.setCardinality(a),s&&G.setIri(s),i&&G.setMappings(i);let l=new Error;return l.breakOutOfLoop=!0,E(l)}E()},t=>{t&&!t.breakOutOfLoop&&f(new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Update Attribute with Id "+l,t.message)),R.isAttrPropOrIriChanged()?u?y():h.ekgApi.updateAttribute(R,w).then(t=>{h.concepts[g.getId()].attrGroup=t,y(t)}).catch(t=>{throw new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Update Attribute with Id "+l,t.message)}):f(new GenericValidationError(GrafoErrorGlossary.ErrorLevel.INFO,"No data available for update"))})}else f(new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Update Attribute. This Attribute with Id "+l+" is invalid or does not exist anymore"))})},EkgDocument.prototype.deleteConceptAttribute=function(t,e,r,i,o){let n=this,a=r||n.concepts[t];return new Promise((s,l)=>{if(!a||a&&a.deleted)l(new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Delete Concept Attribute. The Concept with Id "+t+" is invalid or does not exist anymore"));else if(!e||n.attrMap[e]&&n.attrMap[e]===t){let t=i||a.attrGroup;if(t){let i=o||util.deepCopy(t),p=i.getAttributeList();e?async.forEachOfSeries(p,(t,r,o)=>{if(t.getId()===e){delete n.attrMap[e],i.setAttributeChanged(!0),p.splice(r,1);let t=new Error;return t.breakOutOfLoop=!0,o(t)}o()},o=>{o&&!o.breakOutOfLoop&&l(new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Update Attribute with Id "+e,o.message)),0===p.length&&i.setDeleted(!0),r?s():n.ekgApi.updateAttribute(i,t).then(t=>{n.concepts[a.getId()].attrGroup=t,s(t)})}):(i.setDeleted(!0),r?s():n.ekgApi.updateAttribute(i,t).then(t=>{n.concepts[a.getId()].attrGroup=t,s(t)}))}else s()}else l(new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Delete Concept Attribute. The Attribute with Id "+e+" is invalid or does not exist anymore"))})},EkgDocument.prototype.cloneRelationship=function(t){return new Promise((e,r)=>{try{let i=this.getRelationshipInternal(t);if(!i)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Relationship not found");e(Object.assign(new Relationship,i))}catch(t){r(new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Could not Clone Relationship",t.message))}})},EkgDocument.prototype.addRelationship=async function(t,e,r,i,o,n,a,s,l,p,d,u,E,c){this.logger.debug("Add relationship to document"),this.logger.debug(t),this.logger.debug(e),this.logger.debug(r);let h=this;c||Validator.validateRelationship({label:t,fromConceptId:e,toConceptId:r,specialization:i,description:o,color:n,fields:a,cardinalityType:s,cardinality:l,positionX:p,positionY:d,iri:u,id:E});let g=h.concepts[e],y=h.concepts[r];if(E?h.relationships[E.trim()]:void 0)throw new DuplicateEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Relationship already exists for the provided id");if(!g||g&&g.deleted)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"From Concept Id does not exist or it is already deleted. It is needed to add relationship");if(!y||y&&y.deleted)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"To Concept Id does not exist or it is already deleted. It is needed to add relationship");{let c=h.iri,G=new Relationship(t,e,r,i,o,n,a,s,l,p,d,u,E,c);new RelationshipBuilder(h,G,!1).buildShareDbFormatRelationship();try{let t=await h.ekgApi.createRelationship(G);g.setFromLinks(t.getId()),e!==r&&y.setToLinks(t.getId());var f=new Relationship;return f.getRequiredPropertiesForDoc(t),h.relationships[t.getId()]=f,t}catch(t){throw new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to create relationship",t.message)}}},EkgDocument.prototype.updateRelationship=async function(t,e,r,i,o,n,a,s,l,p,d,u,E){let c=this;if(e)throw new GenericValidationError(GrafoErrorGlossary.ErrorLevel.INFO,"Specialization cannot be updated");E||Validator.validateRelationship({label:t,specialization:e,description:r,color:i,fields:o,cardinalityType:a,cardinality:s,positionX:l,positionY:p,iri:d},!0);let h=this.relationships[u];if(!h||h&&h.deleted)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Cannot Update Relationship. The Relationship with Id "+u+" is invalid or does not exist anymore");if(!(t&&t.trim()||r||i||o||a||s||l||p||d||n))throw new GenericValidationError(GrafoErrorGlossary.ErrorLevel.INFO,"Relationship not updated. Parameters to update are not mentioned.");var g=Object.assign(new Relationship,h);t&&g.setLabel(t),r&&g.setDescription(r),i&&g.setColor(i),o&&g.setFields(o,E),a&&g.setCardinalityType(a),s&&g.setCardinality(s),l&&g.setPositionX(l),p&&g.setPositionY(p),d&&g.setIri(d),n&&g.setMappings(n),new RelationshipBuilder(c,g,!0).buildShareDbFormatRelationship(),new RelationshipBuilder(c,h,!1).buildShareDbFormatRelationship();try{let t=await c.ekgApi.updateRelationship(g,h),e=new Relationship;return e.getRequiredPropertiesForDoc(t),c.relationships[t.getId()]=e,t}catch(t){throw new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Update Relationship "+h.getLabel(),t.message)}},EkgDocument.prototype.deleteRelationship=async function(t){let e=this,r=e.relationships[t];if(!r||r&&r.deleted)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Requested relationship not found.");{let i=Object.assign(new Relationship,r);i.setDeleted(!0),new RelationshipBuilder(e,i,!0).buildShareDbFormatRelationship(),new RelationshipBuilder(e,r,!1).buildShareDbFormatRelationship();try{await e.deleteRelationshipAttribute(t);let o=await e.ekgApi.updateRelationship(i,r),n=new Relationship;n.getRequiredPropertiesForDoc(o),e.relationships[o.getId()]=n}catch(t){throw new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Delete Relationship",t.message)}}},EkgDocument.prototype.addRelationshipAttribute=async function(t,e,r,i,o,n,a,s,l,p,d,u,E){let c=this;c.attrMap||(c.attrMap={}),this.logger.debug("Adding Attribute to Relationship "+l),p||Validator.validateAttribute({label:t,fields:r,iri:a,xsdDataType:i,cardinalityType:o,cardinality:n});let h=c.iri,g=d||c.relationships[l];if(!g||g&&g.deleted)throw new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Relationship Id is Invalid or Relationship does not exist");if(g&&g.specialization)throw new GenericValidationError(GrafoErrorGlossary.ErrorLevel.INFO,"Attributes cannot be added to specialization relationship.");if(s&&c.attrMap[s])throw new DuplicateEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Attribute with Id "+s+" already exists");{let p=new Attribute(t,e,r,i,o,n,a,s,h,DataTypes.ResourceType.RELATIONSHIP),u=E||g.getAttributeGroup();if(p.setGroupId(u.getId()),p.setFromNodeIdAndToNodeId(g.getFromConceptId(),g.getToConceptId()),u.setAttributeChanged(!0),u.setAttributeIriChanged(!0),p.setIriChanged(!0),u.attributeList.push(p),d)return;try{let t=await c.ekgApi.addAttribute(u,p);return c.attrMap[p.getId()]=l,c.relationships[l].attrGroup=t,t}catch(t){throw new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to add Attribute to Relationship",t.message)}}},EkgDocument.prototype.updateRelationshipAttribute=function(t,e,r,i,o,n,a,s,l,p,d,u,E){let c=this;this.logger.debug("Updating Attribute to Relationship "+l),p||Validator.validateAttribute({label:t,fields:r,iri:a,xsdDataType:i,cardinalityType:o,cardinality:n},!0);let h=d||c.relationships[l];return new Promise((g,y)=>{if(!h||h&&h.deleted)y(new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Relationship Id is Invalid or Relationship does not exist"));else if(c.attrMap[s]&&c.attrMap[s]===l){let f,G,w=u||h.getAttributeGroup(),R=E||util.deepCopy(w);async.eachSeries(R.getAttributeList(),(l,d)=>{if(l.getId()===s){f=Object.assign(new Attribute,l),G=l,(t&&t.trim()||e||r&&r.length>0||i||o||n)&&R.setAttributeChanged(!0),a&&R.setAttributeIriChanged(!0),t&&t.trim()&&G.setLabel(t),e&&G.setDescription(e),r&&G.setFields(r,p),i&&G.setXsdDataType(i),o&&G.setCardinalityType(o),n&&G.setCardinality(n),a&&G.setIri(a);let s=new Error;return s.breakOutOfLoop=!0,d(s)}d()},t=>{t&&!t.breakOutOfLoop&&y(new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Update Attribute with Id "+s,t.message)),R.isAttrPropOrIriChanged()?d?g():c.ekgApi.updateAttribute(R,w).then(t=>{c.relationships[l].attrGroup=t,g(t)}).catch(t=>{g(new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Update Attribute with Id "+s,t.message))}):y(new GenericValidationError(GrafoErrorGlossary.ErrorLevel.INFO,"No data available for update"))})}else y(new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Attribute Id is Invalid or Attribute does not exist"))})},EkgDocument.prototype.deleteRelationshipAttribute=function(t,e,r,i,o){let n=this,a=r||n.relationships[t];return new Promise((s,l)=>{if(!a||a&&a.deleted)l(new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Relationship Id is Invalid or Relationship does not exist"));else if(!e||n.attrMap[e]&&n.attrMap[e]===t){let p=i||a.attrGroup;if(p){let i=o||util.deepCopy(p),a=i.getAttributeList();e?async.forEachOfSeries(a,(t,r,o)=>{if(t.getId()===e){delete n.attrMap[e],i.setAttributeChanged(!0),a.splice(r,1);let t=new Error;return t.breakOutOfLoop=!0,o(t)}o()},o=>{o&&!o.breakOutOfLoop&&l(new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Update Attribute with Id "+e,o.message)),0===a.length&&i.setDeleted(!0),r?s():n.ekgApi.updateAttribute(i,p).then(e=>{n.relationships[t].attrGroup=e,s(e)}).catch(t=>{l(new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Delete Attribute",t.message))})}):(i.setDeleted(!0),r?s():n.ekgApi.updateAttribute(i,p).then(e=>{n.relationships[t].attrGroup=e,s(e)}).catch(t=>{l(new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Failed to Delete Attribute",t.message))}))}else s()}else l(new NonExistentEntityError(GrafoErrorGlossary.ErrorLevel.WARNING,"Attribute Id is Invalid or Attribute does not exist"))})},EkgDocument.prototype.updateDbCollectionAndEKGDoc=async function(t){if(t){this.logger.debug("***********EKG Doc received for update**************************"),Validator.validateEkgDoc(t);try{if(t.concepts&&Object.keys(t.concepts).length>0){this.logger.debug("Updating concepts......");await coarseGrainedHelper.runAllConceptCrud(this,t.concepts);this.logger.debug("Done Updating Concepts....")}if(t.relationships&&Object.keys(t.relationships).length>0){this.logger.debug("Updating relationships......");await coarseGrainedHelper.runAllRelCrud(this,t.relationships);this.logger.debug("Done Updating relationships....")}}catch(t){throw new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Encountered error while updating the document ",t.message)}}},EkgDocument.prototype.createDbCollectionAndEKGDoc=async function(t){if(t){this.logger.debug("***********EKG Doc received for create**************************"),Validator.validateEkgDoc(t);try{if(t.concepts&&Object.keys(t.concepts).length>0){this.logger.debug("Adding concepts......");await coarseGrainedHelper.runAllConceptCrud(this,t.concepts,!0);this.logger.debug("Done Adding Concepts....")}if(t.relationships&&Object.keys(t.relationships).length>0){this.logger.debug("Adding relationships......");await coarseGrainedHelper.runAllRelCrud(this,t.relationships,!0);this.logger.debug("Done Adding relationships....")}}catch(t){throw new GrafoCrudError(GrafoErrorGlossary.ErrorLevel.ERROR,"Encountered error while creating the document ",t.message)}}},EkgDocument.prototype.addAllMappings=async function(t){let e={},r=[],i=Object.keys(t);for(const o of i){let i=t[o];await this.addOrCollectMappingsInternal(o,i,e,r)}let o=Object.keys(e);for(const t of o)try{console.log("handling all attributes for "+t),await this.addAllConceptAttributeMappings(t,e[t],r)}catch(t){console.log(t.stack)}},EkgDocument.prototype.addOrCollectMappingsInternal=async function(t,e,r,i){try{let o=this.iriToElementIdMap[t];if(!o)return void i.push(new MappingStatus({iri:t},MappingTypes.MappingStatusLevel.WARNING,MappingTypes.MappingLevelDetail.IRI_HAS_NO_MATCHING_GRAFO_ENTITY));let n=getType(o);if("node"===n)await this.addConceptMappings(o,e);else if("link"===n)await this.addRelationshipMappings(o,e);else if("link_attr"!==n){if("concept_attr"===n){let n=this.attrMap[o];if(n)putMappingsForConceptAttr(r,n,o,e);else{const e=new MappingStatus({iri:t,id:o},MappingTypes.MappingStatusLevel.ERROR,MappingTypes.MappingLevelDetail.ATTRIBUTE_PARENT_ENTITY_NOT_FOUND);i.push(e)}return}return void i.push(new MappingStatus({iri:t},MappingTypes.MappingStatusLevel.ERROR,MappingTypes.MappingLevelDetail.INVALID_ENITITY_ID))}i.push(new MappingStatus({iri:t},MappingTypes.MappingStatusLevel.SUCCESS))}catch(e){i.push(buildMappingStatus(t,e,GrafoErrorGlossary.OpType.ADD))}};let putMappingsForConceptAttr=function(t,e,r,i){let o=t[e];o||(o={},t[e]=o),t[e][r]=i};EkgDocument.prototype.addMappings=async function(t,e){try{let r=this.iriToElementIdMap[t];if(!r)return new MappingStatus({iri:t},MappingTypes.MappingStatusLevel.WARNING,MappingTypes.MappingLevelDetail.IRI_HAS_NO_MATCHING_GRAFO_ENTITY);let i=getType(r);if("node"===i)await this.addConceptMappings(r,e);else if("link"===i)await this.addRelationshipMappings(r,e);else if("link_attr"===i);else{if("concept_attr"!==i)return new MappingStatus({iri:t},MappingTypes.MappingStatusLevel.ERROR,MappingTypes.MappingLevelDetail.INVALID_ENITITY_ID);{let i=this.attrMap[r];if(!i)return new MappingStatus({iri:t,id:r},MappingTypes.MappingStatusLevel.ERROR,MappingTypes.MappingLevelDetail.ATTRIBUTE_PARENT_ENTITY_NOT_FOUND);await this.addConceptAttributeMappings(r,i,e)}}return new MappingStatus({iri:t},MappingTypes.MappingStatusLevel.SUCCESS)}catch(e){return buildMappingStatus(t,e,GrafoErrorGlossary.OpType.ADD)}},EkgDocument.prototype.deleteMappings=async function(t,e,r){try{let i=this.iriToElementIdMap[t];if(!i)return new MappingStatus({iri:t},MappingTypes.MappingStatusLevel.WARNING,MappingTypes.MappingLevelDetail.IRI_HAS_NO_MATCHING_GRAFO_ENTITY);let o=getType(i);if("node"===o)await this.deleteConceptMappings(i,e,r);else if("link"===o)await this.deleteRelationshipMappings(i,e,r);else if("link_attr"===o);else{if("concept_attr"!==o)return new MappingStatus({iri:t},MappingTypes.MappingStatusLevel.ERROR,MappingTypes.MappingLevelDetail.INVALID_ENITITY_ID);{let o=this.attrMap[i];if(!o)return new MappingStatus({iri:t,id:i},MappingTypes.MappingStatusLevel.ERROR,MappingTypes.MappingLevelDetail.ATTRIBUTE_PARENT_ENTITY_NOT_FOUND);await this.deleteConceptAttributeMappings(i,o,e,r)}}return new MappingStatus({iri:t},MappingTypes.MappingStatusLevel.SUCCESS)}catch(e){return buildMappingStatus(t,e,GrafoErrorGlossary.OpType.DELETE)}};let buildMappingStatus=function(t,e,r){return e instanceof GrafoCrudError?new MappingStatus({iri:t},e.level,null,e.message):r===GrafoErrorGlossary.OpType.DELETE?new MappingStatus({iri:t},MappingTypes.MappingStatusLevel.SEVERE,null,"Unexpected Error when deleting mapping for iri "+t+". Detail: "+e.message):r===GrafoErrorGlossary.OpType.ADD?new MappingStatus({iri:t},MappingTypes.MappingStatusLevel.SEVERE,null,"Unexpected Error when adding mapping for iri "+t+". Detail: "+e.message):r===GrafoErrorGlossary.OpType.ADD_UPDATE?new MappingStatus({iri:t},MappingTypes.MappingStatusLevel.SEVERE,null,"Unexpected Error when adding/updating mapping for iri "+t+". Detail: "+e.message):new MappingStatus({iri:t},MappingTypes.MappingStatusLevel.SEVERE,null,"Oh! Oh! Unhandled operation")};EkgDocument.prototype.getAllMappings=function(){let t=[];for(let e in this.concepts)util.customPush(t,this.concepts[e].getAllMappings());for(let e in this.relationships)util.customPush(t,this.relationships[e].getAllMappings());return t};let getType=function(t){return t.startsWith("node-")?"node":t.startsWith("link-")?"link":t.startsWith("linkattr-")?"link_attr":(t.startsWith("attr-"),"concept_attr")};EkgDocument.prototype.toObject=function(){let t={id:this.id,type:"document",title:this.title,iri:this.iri,iriPrefix:this.iriPrefix,description:this.description,owner:this.owner,dateCreated:this.dateCreated,dateDeleted:this.dateDeleted,lastModified:this.lastModified,deleted:!!this.deleted,parent:this.parent,concepts:{},relationships:{}};for(let e in this.concepts){let r=this.concepts[e];t.concepts[e]=r.toObject()}for(let e in this.relationships){let r=this.relationships[e];t.relationships[e]=r.toObject()}return t},EkgDocument.prototype.getAllIris=function(){return Object.keys(this.iriToElementIdMap)},EkgDocument.prototype.deletePriorImportedMappings=async function(t){let e=[],r=this.deleteAllImportedConceptMappings(t),i=this.deleteAllImportedRelMappings(t);return await r,await i,r.then(t=>{t.forEach(t=>e.push(t.toObject()))}),i.then(t=>{t.forEach(t=>e.push(t.toObject()))}),e},EkgDocument.prototype.deleteAllImportedConceptMappings=async function(t){let e=[];return Object.keys(this.concepts).forEach(async r=>{let i=this.concepts[r];try{await this.deleteConceptMappings(r,t,!0),e.push(new MappingStatus({iri:i.getIri()},MappingTypes.MappingStatusLevel.SUCCESS))}catch(t){e.push(buildMappingStatus(i.getIri(),t,GrafoErrorGlossary.OpType.DELETE))}let o=Object.assign(new AttributeGroup(i.getId()),i.attrGroup);i.getAttributeList().forEach(async n=>{try{await this.deleteConceptAttributeMappings(n.getId(),r,t,!0,i,i.attrGroup,o),e.push(new MappingStatus({iri:n.getIri()},MappingTypes.MappingStatusLevel.SUCCESS))}catch(t){e.push(buildMappingStatus(n.getIri(),t,GrafoErrorGlossary.OpType.DELETE))}});let n=await this.ekgApi.updateAttribute(o,i.attrGroup);this.concepts[i.getId()].attrGroup=n}),e},EkgDocument.prototype.deleteAllImportedRelMappings=async function(t){let e=[];return Object.keys(this.relationships).forEach(async r=>{let i=this.relationships[r];try{await this.deleteRelationshipMappings(r,t,!0),e.push(new MappingStatus({iri:i.getIri()},MappingTypes.MappingStatusLevel.SUCCESS))}catch(t){e.push(buildMappingStatus(i.getIri(),t,GrafoErrorGlossary.OpType.DELETE))}}),e};