grafo-client
Version:
Grafo Client API Library
1 lines • 15.6 kB
JavaScript
const uuidv4=require("uuid/v4"),util=require("../util");var Iri=require("./iri"),ColorPalette=require("../models/color_palette"),DataTypes=require("../models/datatypes");const Attribute=require("./attribute"),AttributeGroup=require("./attribute_group"),ShareDbCreatePacket=require("./sharedb_create_packet"),ShareDbUpdatePacket=require("./sharedb_update_packet"),CustomField=require("./custom_field"),Validator=require("./validator"),Mapping=require("./mapping"),GrafoErrorGlossary=require("../errors/GrafoErrorGlossary"),GenericValidationError=require("../errors/GenericValidationError");function Relationship(t,i,e,o,r,s,n,a,p,h,l,d,c,u){if(this.nodeType=DataTypes.ResourceType.RELATIONSHIP,this.deleted=!1,this.iriChanged=!1,this.attrGrpChanged=!1,this.directPropsChanged=!1,this.coordsChanged=!1,t||o){if(o&&i==e)throw new GenericValidationError(GrafoErrorGlossary.ErrorLevel.INFO,"Its a specialization and fromConceptId and toConceptId are same. Only a self link has fromConceptId and toConceptId equal");if(this.id=c,this.specialization=o,this.fromConceptId=i,this.toConceptId=e,this.fields=[],this.specialization)this.label="label",this.description="";else{if(this.label=t,this.description=r,a&&(Object.values(DataTypes.CardinalityType).includes(a)?this.cardinalityType=a:this.cardinalityType="string"==typeof a?DataTypes.resolveCardinalityTypeByTitle(a):DataTypes.resolveCardinalityType(a)),this.cardinality=p,n)for(var y=0;y<n.length;y++)n[y]&&n[y].title&&n[y].title.trim()&&this.fields.push(new CustomField(n[y].title,n[y].text,n[y].id));s&&Object.values(ColorPalette.color).includes(s)&&(this.color=s),this.label||(this.label="label"),this.description||(this.description="")}this.id||(this.id="link-"+uuidv4()),d?(this.iri=new Iri,this.iri.initWithIri(d)):this.iri=new Iri(u,this.id,this.label),this.fromConceptId===this.toConceptId?(this.positionX=h,this.positionY=l,this.positionX||(this.positionX=100),this.positionY||(this.positionY=173),this.relationshipType=DataTypes.RelationshipType.SELFRELATIONSHIP):this.relationshipType=o?DataTypes.RelationshipType.SPECIALIZATION:DataTypes.RelationshipType.PEERRELATIONSHIP}}module.exports=Relationship,Relationship.prototype.initUsingMongoDbStructure=function(t,i,e){if(this.iri=new Iri,t&&this.iri.initWithIri(t.iri,t.hasEdited),this.id=i.linkId,i.deleted&&(this.deleted=i.deleted),this.relationshipType=DataTypes.resolveRelationshipType(i.linkType),"parent"===i.linkType||"child"===i.linkType?(this.fromConceptId=i.nodeId,this.toConceptId=i.sourceNodeId,this.specialization=!0):(this.fromConceptId=i.sourceNodeId,this.toConceptId=i.nodeId,this.specialization=!1),"link"!==this.nodeType)throw new GenericValidationError(GrafoErrorGlossary.ErrorLevel.INFO,"Hey you got wrong data for node type for link with id = "+i.linkId+". Please handle for nodeType = "+this.nodeType);if(this.label=i.label,this.description=i.description,this.fields=[],i.fields)for(var o=0;o<i.fields.length;o++)this.fields.push(new CustomField(i.fields[o].title,i.fields[o].text,i.fields[o].id));if(i.mappings){this.mappings=[];for(o=0;o<i.mappings.length;o++){var r=i.mappings[o],s=new Mapping(r.mappingId,r.dbId,r.mappingType,r.imported,r.id);s.setMappingDetails(r),this.mappings.push(s)}}i.cardinalityType?this.cardinalityType=DataTypes.resolveCardinalityType(i.cardinalityType):this.cardinalityType=null,this.cardinality=i.cardinality,i.color&&Object.values(ColorPalette.color).includes(i.color)&&(this.color=i.color),e&&(this.positionX=e.positionX,this.positionY=e.positionY)},Relationship.prototype.addAttributesFromMongoDb=async function(t,i){t&&(this.attrGroup||(this.attrGroup=new AttributeGroup(this.id,t.groupId)),this.attrGroup.setSlot(t.slot),this.attrGroup.setLastUpdatedBy(t.lastUpdatedBy),this.attrGroup.setDeleted(t.deleted),await this.attrGroup.addAttributeFromMongoDb(t.attributeList,i))},Relationship.prototype.getId=function(){return this.id},Relationship.prototype.getSlot=function(){return this.slot},Relationship.prototype.getRelationShipType=function(){return this.relationshipType},Relationship.prototype.getShareDbCreatePackets=function(){var t=this.getShareDbFormatLinkObjForFromConcept(),i=this.getShareDbFormatLinkObjForToConcept(),e=this.iri.getIri();this.eventCountForCreatePacket=0;var o=[];if(this.fromConceptId===this.toConceptId){var r=this.getShareDbFormatCoordObj();r.slot=t.slot,o.push(new ShareDbCreatePacket(t.slot,this.id,"coordinates",r))}return t&&(this.eventCountForCreatePacket+=2,o.push(new ShareDbCreatePacket(t.slot,t.nodeId,"links",t)),o.push(new ShareDbCreatePacket(t.slot,this.id,"iri",e))),i&&(this.eventCountForCreatePacket+=1,o.push(new ShareDbCreatePacket(i.slot,i.nodeId,"links",i)),t.slot!==i.slot&&(this.eventCountForCreatePacket+=1,o.push(new ShareDbCreatePacket(i.slot,this.id,"iri",e)))),o},Relationship.prototype.getShareDbUpdatePackets=function(t){var i=this.getShareDbFormatCoordObj(),e=this.getShareDbFormatLinkObjForFromConcept(),o=this.getShareDbFormatLinkObjForToConcept(),r=this.iri.getIri(),s=t.getShareDbFormatCoordObj(),n=t.getShareDbFormatLinkObjForFromConcept(),a=t.getShareDbFormatLinkObjForToConcept(),p=t.iri.getIri();this.eventCountForUpdatePacket=0;var h=[];return this.coordsChanged&&(i.slot=e.slot,s.slot=n.slot,h.push(new ShareDbUpdatePacket(e.slot,this.id,"coordinates",i,s))),e&&(this.directPropsChanged&&(this.eventCountForUpdatePacket+=1,h.push(new ShareDbUpdatePacket(e.slot,e.nodeId,"links",e,n))),this.iriChanged&&(this.eventCountForUpdatePacket+=1,h.push(new ShareDbUpdatePacket(e.slot,this.id,"iri",r,p)))),o&&(this.directPropsChanged&&(this.eventCountForUpdatePacket+=1,h.push(new ShareDbUpdatePacket(o.slot,o.nodeId,"links",o,a))),this.iriChanged&&e.slot!==o.slot&&(this.eventCountForUpdatePacket+=1,h.push(new ShareDbUpdatePacket(o.slot,this.id,"iri",r,p)))),h},Relationship.prototype.createShareDbFormatLinkObj=function(t){return this.linkObj={nodeType:this.nodeType,nodeId:t.getId(),slot:t.slot,peers:[],self:[],parents:[],children:[],lastUpdatedBy:this.lastUpdatedBy}},Relationship.prototype.getShareDbFormatLinkObjForFromConcept=function(){return this.linkObjForFromConcept},Relationship.prototype.getShareDbFormatLinkObjForToConcept=function(){return this.linkObjForToConcept},Relationship.prototype.setShareDbFormatLinkObjForFromConcept=function(t){this.linkObjForFromConcept=t},Relationship.prototype.setShareDbFormatLinkObjForToConcept=function(t,i){this.linkObjForToConcept=t},Relationship.prototype.getShareDbFormatLinkData=function(t){var i={linkId:this.id,nodeId:this.specialization?this.fromConceptId:this.toConceptId,myNodeId:t.getId(),sourceNodeId:this.specialization?this.toConceptId:this.fromConceptId,label:this.specialization?"":this.label,description:this.description,arrayName:this.specialization?this.getRelTypeForSpecialization(t).arrayName:this.getRelationShipType().arrayName,linkType:this.specialization?this.getRelTypeForSpecialization(t).linkType:this.getRelationShipType().linkType,cardinalityType:this.cardinalityType,cardinality:this.cardinality,fields:[],color:this.color,deleted:this.deleted?this.deleted:void 0};if(this.fields)for(var e=0;e<this.fields.length;e++)i.fields.push(this.fields[e].getCustomField());if(this.mappings&&this.mappings.length>0){i.mappings=[];for(e=0;e<this.mappings.length;e++)i.mappings.push(this.mappings[e].toObject())}return i},Relationship.prototype.getShareDbFormatCoordObj=function(){return{nodeType:"self-link",sourceNodeId:this.fromConceptId,positionX:this.positionX,positionY:this.positionY,slot:this.slot,lastUpdatedBy:this.lastUpdatedBy}},Relationship.prototype.getIri=function(){return this.iri.iri},Relationship.prototype.setEkgApi=function(t){this.ekgApi=t},Relationship.prototype.setLastUpdatedBy=function(t){this.lastUpdatedBy=t,this.linkObjForFromConcept&&(this.linkObjForFromConcept.lastUpdatedBy=t),this.linkObjForToConcept&&(this.linkObjForToConcept.lastUpdatedBy=t)},Relationship.prototype.getFromConceptId=function(){return this.fromConceptId},Relationship.prototype.getToConceptId=function(){return this.toConceptId},Relationship.prototype.getLabel=function(){return this.label},Relationship.prototype.setLabel=function(t){if(this.specialization)throw new GenericValidationError(GrafoErrorGlossary.ErrorLevel.INFO,"Label cannot be set on specialization relationship");this.directPropsChanged=!0,this.label=t},Relationship.prototype.setDescription=function(t){if(this.specialization)throw new GenericValidationError(GrafoErrorGlossary.ErrorLevel.INFO,"Description cannot be added to specialization relationship");this.directPropsChanged=!0,this.description=t},Relationship.prototype.setFields=function(t,i){if(this.specialization)throw new GenericValidationError(GrafoErrorGlossary.ErrorLevel.INFO,"Fields cannot be added to specialization relationship");if(this.directPropsChanged=!0,t){this.fields||(this.fields=[]);for(var e=0;e<t.length;e++){var o=getFieldIndex(this,t[e]);o>-1?updateField(this,t[e],o):addField(this,t[e],i)}}},Relationship.prototype.getFields=function(){return this.fields};var addField=function(t,i,e){e||Validator.validateCustomField(i,!0),t.fields.push(new CustomField(i.title,i.text,i.id))},updateField=function(t,i,e){var o=t.fields[e];i.title&&i.title.trim()&&o.setTitle(i.title),i.text&&i.text.trim()&&o.setText(i.text)},getFieldIndex=function(t,i){for(var e=0;e<t.fields.length;e++)if(i.id===t.fields[e].getId())return e;return-1};Relationship.prototype.deleteField=function(t){this.directPropsChanged=!0;for(var i=-1,e=0;e<this.fields.length;e++)t===this.fields[e].getId()&&(i=e);return i>-1&&this.fields.splice(i,1),i},Relationship.prototype.setMappings=function(t,i){if(this.directPropsChanged=!0,t)for(var e=0;e<t.length;e++){var o=getMappingIndex(this,t[e]);o>-1?updateMapping(this,t[e],o):addMapping(this,t[e],i)}},Relationship.prototype.getMappings=function(){return this.mappings},Relationship.prototype.getAllMappings=function(){var t=[];if(this.mappings)for(var i=0;i<this.mappings.length;i++)t.push(this.mappings[i].toObject());return this.attrGroup&&util.customPush(t,this.attrGroup.getAllMappings()),t},Relationship.prototype.addMappings=function(t,i){if(t){this.directPropsChanged=!0,this.mappings||(this.mappings=[]);for(var e=0;e<t.length;e++){var o=t[e];if(o){var r=new Mapping(o.mappingId,o.dbId,o.mappingType,o.imported,o.id);r.setMappingDetails(o),this.mappings.push(r)}}}};var addMapping=function(t,i,e){var o=new Mapping(i.mappingId,i.dbId,i.mappingType,i.imported,i.id);o.setMappingDetails(i),t.mappings.push(o)},updateMapping=function(t,i,e){var o=new Mapping(i.mappingId,i.dbId,i.mappingType,i.imported,i.id);o.setMappingDetails(i),t.mappings[e]=o},getMappingIndex=function(t,i){for(var e=0;e<t.mappings.length;e++)if(i.imported){if(t.mappings[e].dbId===i.dbId&&t.mappings[e].imported)return e}else if(i.id===t.mappings[e].getId())return e;return-1};Relationship.prototype.deleteMappings=function(t,i){this.directPropsChanged=!0;for(var e=[],o=0;o<this.mappings.length;o++)t===this.mappings[o].getDbId()&&(i?i===this.mappings[o].isImported()&&e.push(o):e.push(o));e.sort(function(t,i){return i-t});for(o=0;o<e.length;o++){var r=e[o];r>-1&&this.mappings.splice(r,1)}return e},Relationship.prototype.setIri=function(t){if(this.specialization)throw new GenericValidationError(GrafoErrorGlossary.ErrorLevel.INFO,"Iri cannot be set on specialization relationship");this.iriChanged=!0,this.iri=new Iri,t.iri?this.iri.initWithIri(t.iri,t.hasEdited):this.iri.initWithIri(t,!0)},Relationship.prototype.setColor=function(t){if(this.specialization)throw new GenericValidationError(GrafoErrorGlossary.ErrorLevel.INFO,"Color cannot be set on specialization relationship");this.directPropsChanged=!0,this.color=t},Relationship.prototype.setCardinalityType=function(t){if(this.specialization)throw new GenericValidationError(GrafoErrorGlossary.ErrorLevel.INFO,"Cardinality Type cannot be set on specialization relationship");this.directPropsChanged=!0,this.cardinalityType=t},Relationship.prototype.setCardinality=function(t){if(this.specialization)throw new GenericValidationError(GrafoErrorGlossary.ErrorLevel.INFO,"Cardinality cannot be set on specialization relationship");this.directPropsChanged=!0,this.cardinality=t},Relationship.prototype.setPositionX=function(t){if(this.specialization)throw new GenericValidationError(GrafoErrorGlossary.ErrorLevel.INFO,"Position X cannot be added on specialization relationship");if(this.fromConceptId!==this.toConceptId)throw new GenericValidationError(GrafoErrorGlossary.ErrorLevel.INFO,"Position X cannot be added on peer relationship");this.coordsChanged=!0,this.positionX=t},Relationship.prototype.setPositionY=function(t){if(this.specialization)throw new GenericValidationError(GrafoErrorGlossary.ErrorLevel.INFO,"Position Y cannot be added on specialization relationship");if(this.fromConceptId!==this.toConceptId)throw new GenericValidationError(GrafoErrorGlossary.ErrorLevel.INFO,"Position Y cannot be added on peer relationship");this.coordsChanged=!0,this.positionY=t},Relationship.prototype.setDeleted=function(t){this.directPropsChanged=!0,this.deleted=t},Relationship.prototype.getAttributeGroup=function(){return this.attrGroup||(this.attrGroup=new AttributeGroup(this.id)),this.attrGroup},Relationship.prototype.getAttributeList=function(){return this.attrGroup?this.attrGroup.getAttributeList():[]},Relationship.prototype.getAttribute=async function(t){return this.attrGroup?await this.attrGroup.getAttributeById(t):null},Relationship.prototype.createAttributeGroup=function(t){this.attrGroup=new AttributeGroup(this.id,t)},Relationship.prototype.getEventCount=function(){return this.eventCountForUpdatePacket},Relationship.prototype.getExpectedEventCountForCreate=function(){return this.eventCountForCreatePacket},Relationship.prototype.getRelTypeForSpecialization=function(t){var i=DataTypes.RelationshipType.PARENTRELATIONSHIP;return this.toConceptId===t.getId()&&(i=DataTypes.RelationshipType.CHILDRELATIONSHIP),i},Relationship.prototype.toObject=function(){var t={id:this.id,type:this.nodeType,fromConceptId:this.fromConceptId?this.fromConceptId:null,toConceptId:this.toConceptId?this.toConceptId:null,label:this.label,description:this.description,iri:this.iri.iri,lastUpdatedBy:this.lastUpdatedBy,cardinalityType:this.cardinalityType,cardinality:this.cardinality,specialization:this.specialization,color:this.color,positionX:this.positionX?this.positionX:null,positionY:this.positionY?this.positionY:null,deleted:this.deleted};if(this.fields&&this.fields.length>0){t.fields=[];for(var i=0;i<this.fields.length;i++)t.fields.push(this.fields[i].getCustomField())}if(this.attrGroup&&this.attrGroup.getAttributeList()&&this.attrGroup.getAttributeList().length>0&&(t.attrGroup=this.attrGroup.toObject()),this.mappings&&this.mappings.length>0){t.mappings=[];for(i=0;i<this.mappings.length;i++)t.mappings.push(this.mappings[i].toObject())}return t},Relationship.prototype.getRequiredPropertiesForDoc=function(t){this.id=t.id,this.label=t.label,this.description=t.description,this.fromConceptId=t.fromConceptId,this.toConceptId=t.toConceptId,this.specialization=t.specialization,this.relationshipType=t.relationshipType,this.iri=t.iri,this.fields=t.fields,this.mappings=t.mappings,this.cardinalityType=t.cardinalityType,this.cardinality=t.cardinality,this.color=t.color,this.deleted=t.deleted,this.positionX=t.positionX,this.positionY=t.positionY,this.attrGroup=t.attrGroup,this.lastUpdatedBy=t.lastUpdatedBy,this.attrGroup=t.attrGroup};var convertDegreesToRadians=function(t){return t*(Math.PI/180)},getAngleFromCoord=function(t,i){return Math.atan2(i,t)*(180/Math.PI)};