UNPKG

grafo-client

Version:

Grafo Client API Library

1 lines 4.63 kB
const uuidv4=require("uuid/v4"),async=require("async"),util=require("../util"),Iri=require("./iri"),Attribute=require("./attribute"),ShareDbUpdatePacket=require("./sharedb_update_packet"),ShareDbCreatePacket=require("./sharedb_create_packet"),GrafoErrorGlossary=require("../errors/GrafoErrorGlossary"),GenericValidationError=require("../errors/GenericValidationError");function AttributeGroup(t,e,r){if(this.nodeType="attrGrp",this.attributeChanged=!1,this.attributeIriChanged=!1,!t)throw new GenericValidationError(GrafoErrorGlossary.ErrorLevel.INFO,"Parent Id is required to create Attribute Group");this.parentId=t,this.groupId=e||"attrGrp-"+uuidv4(),this.attributeList=r||[]}module.exports=AttributeGroup,AttributeGroup.prototype.addAttributeFromMongoDb=async function(t,e){return await util.eachSeriesCbFunctionWithoutPromise(t,(t,e)=>{let r=e.iriMap,i=e._this,u=new Attribute;u.initUsingMongoDbStructure(t,r[t.id]),i.attributeList.push(u)},{iriMap:e,_this:this})},AttributeGroup.prototype.getId=function(){return this.groupId},AttributeGroup.prototype.getParentId=function(){return this.parentId},AttributeGroup.prototype.getAttributeList=function(){return this.attributeList},AttributeGroup.prototype.getAttributeById=async function(t){return await util.eachSeriesFindById(this.attributeList,t)},AttributeGroup.prototype.update=function(t){this.slot=t.slot,this.lastUpdatedBy=t.lastUpdatedBy},AttributeGroup.prototype.setLastUpdatedBy=function(t){this.lastUpdatedBy=t},AttributeGroup.prototype.setSlot=function(t){this.slot=t},AttributeGroup.prototype.getSlot=function(){return this.slot},AttributeGroup.prototype.setDeleted=function(t){this.attributeChanged=!0,this.deleted=t},AttributeGroup.prototype.getDeleted=function(){return this.deleted},AttributeGroup.prototype.setAttributeChanged=function(t){this.attributeChanged=t},AttributeGroup.prototype.setAttributeIriChanged=function(t){this.attributeIriChanged=t},AttributeGroup.prototype.isAttrPropOrIriChanged=function(){return this.attributeChanged||this.attributeIriChanged},AttributeGroup.prototype.getAttributeInShareDbFormat=async function(){let t={groupId:this.groupId,parentId:this.parentId,nodeType:this.nodeType,slot:this.slot,attributeList:[],lastUpdatedBy:this.lastUpdatedBy,deleted:this.deleted};await util.eachSeriesCbFunctionWithoutPromise(this.attributeList,(t,e)=>{let r=e._this,i=e.attrGroupObj;t.setGroupId(r.groupId),i.attributeList.push(t.getAttributeInShareDbFormat())},{_this:this,attrGroupObj:t});return t},AttributeGroup.prototype.getShareDbCreatePackets=function(){return new Promise((t,e)=>{let r=[];this.getAttributeInShareDbFormat().then(i=>{r.push(new ShareDbCreatePacket(this.slot,this.parentId,"attributeGroups",i)),async.eachSeries(this.attributeList,(t,e)=>{let i=t.getShareDbFormatIri();r.push(new ShareDbCreatePacket(this.slot,t.getId(),"iri",i)),e()},i=>{i?e():t(r)})})})},AttributeGroup.prototype.getShareDbUpdatePackets=function(t){let e=[];return new Promise((r,i)=>{let u;this.attributeChanged?this.getAttributeInShareDbFormat().then(e=>(u=e,t.getAttributeInShareDbFormat())).then(t=>{e.push(new ShareDbUpdatePacket(this.slot,this.parentId,"attributeGroups",u,t))}).then(()=>{r()}).catch(t=>{i(t)}):r()}).then(()=>new Promise((r,i)=>{this.attributeIriChanged?async.eachSeries(this.attributeList,(r,i)=>{r.iriChanged?getOldAttrIriFromOldAttrGroupInShareDbFormat(t,r.id).then(t=>{let u=r.getShareDbFormatIri();e.push(new ShareDbUpdatePacket(this.slot,r.getId(),"iri",u,t)),i()}):i()},t=>{t?i():r(e)}):r(e)}))};let getOldAttrIriFromOldAttrGroupInShareDbFormat=async function(t,e){let r=await util.eachSeriesFindById(t.attributeList,e);return r?r.getShareDbFormatIri():null};AttributeGroup.prototype.getIdsForCallbackRegistration=function(){return new Promise((t,e)=>{let r=[];this.attributeChanged&&r.push(this.parentId),async.eachSeries(this.attributeList,(t,e)=>{t.iriChanged&&r.push(t.getId()),e()},(i,u)=>{i?e():t(r)})})},AttributeGroup.prototype.getEventCount=function(){return this.getIdsForCallbackRegistration().length},AttributeGroup.prototype.getIriForAttr=function(t){for(let e=0;e<this.attributeList.length;e++)if(this.attributeList[e].id===t)return this.attributeList[e].getIri()},AttributeGroup.prototype.getAllMappings=function(){let t=[];if(this.attributeList)for(let e=0;e<this.attributeList.length;e++)util.customPush(t,this.attributeList[e].getAllMappings());return t},AttributeGroup.prototype.toObject=function(){let t={id:this.groupId,attributes:[],lastUpdatedBy:this.lastUpdatedBy,deleted:this.deleted};for(let e=0;e<this.attributeList.length;e++)t.attributes.push(this.attributeList[e].toObject());return t};