1
1

Relation.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. abstract class RelationHandler {
  2. protected relations : Array<Relation>;
  3. public constructor (...relations : Array <Relation>) {
  4. relations.forEach(relation => {
  5. relation.setHandler(this);
  6. });
  7. this.relations = relations;
  8. }
  9. public addRelation (relation : Relation) {
  10. this.relations.push(relation);
  11. relation.setHandler(this);
  12. }
  13. public abstract setRelation (relation : Relation, left : any, right : any, value? : any) : boolean;
  14. }
  15. abstract class Relation {
  16. public handler : RelationHandler;
  17. public setHandler (handler : RelationHandler) {
  18. this.handler = handler;
  19. }
  20. public hasHandler () {
  21. return this.handler != undefined;
  22. }
  23. public abstract setRelation (left : any, right : any, value? : any) : void;
  24. public abstract unsetRight (right : any) : void;
  25. public abstract unsetLeft (left : any) : void;
  26. public abstract getLeft (right : any) : any;
  27. public abstract getRight (left : any) : any;
  28. public static createString (obj1 : any, obj2 : any) : string {
  29. if (obj1 instanceof Object && obj2 instanceof Object) {
  30. let id1 = Relation.getId(obj1);
  31. let id2 = Relation.getId(obj2);
  32. return id1 < id2 ?
  33. (id1 + ";" + id2) :
  34. (id2 + ";" + id1);
  35. }
  36. }
  37. private static objectCount = 0;
  38. private static objectIdField = "_RELATIONINTERNALID";
  39. /**
  40. * if getId >= 0, then obj is NOT an Object
  41. * if getId < 0, then obj is an Object
  42. * @param obj
  43. * @returns {number}
  44. */
  45. public static getId (obj : Object) : number {
  46. if (obj[Relation.objectIdField] == undefined) {
  47. obj[Relation.objectIdField] = Relation.objectCount++;
  48. }
  49. return obj[Relation.objectIdField];
  50. }
  51. }