a9743b970f17f2522907775fb37782e6455bba3c
[potlatch2.git] / net / systemeD / halcyon / connection / Relation.as
1 package net.systemeD.halcyon.connection {
2
3     public class Relation extends Entity {
4         private var members:Array;
5                 public static var entity_type:String = 'relation';
6
7         public function Relation(id:Number, version:uint, tags:Object, loaded:Boolean, members:Array) {
8             super(id, version, tags, loaded);
9             this.members = members;
10                         for each (var member:RelationMember in members)
11                             member.entity.addParent(this);
12         }
13
14         public function update(version:uint, tags:Object, loaded:Boolean, members:Array):void {
15                         var member:RelationMember;
16                         for each (member in this.members)
17                             member.entity.removeParent(this);
18
19                         updateEntityProperties(version,tags,loaded);
20                         this.members=members;
21                         for each (member in members)
22                             member.entity.addParent(this);
23                 }
24                 
25         public function get length():uint {
26             return members.length;
27         }
28
29         public function findEntityMemberIndex(entity:Entity):int {
30             for (var index:uint = 0; index < members.length; index++) {
31                 var member:RelationMember = members[index];
32                 if ( member.entity == entity )
33                     return index;
34             }
35             return -1;
36         }
37
38         public function findEntityMemberIndexes(entity:Entity):Array {
39             var indexes:Array = [];
40             for (var index:uint = 0; index < members.length; index++) {
41                 var member:RelationMember = members[index];
42                 if ( member.entity == entity )
43                     indexes.push(index);
44             }
45             return indexes;
46         }
47         
48         public function getMember(index:uint):RelationMember {
49             return members[index];
50         }
51
52         public function setMember(index:uint, member:RelationMember):void {
53             var oldMember:RelationMember = getMember(index);
54             
55                         members.splice(index, 1, member);
56             oldMember.entity.removeParent(this);
57                         member.entity.addParent(this);
58                         markDirty();
59         }
60
61         public function insertMember(index:uint, member:RelationMember):void {
62             members.splice(index, 0, member);
63                         member.entity.addParent(this);
64                         markDirty();
65                         
66                         dispatchEvent(new RelationMemberEvent(Connection.RELATION_MEMBER_ADDED, member.entity, this, index));
67         }
68
69         public function appendMember(member:RelationMember):uint {
70             members.push(member);
71                         member.entity.addParent(this);
72                         markDirty();
73
74                         dispatchEvent(new RelationMemberEvent(Connection.RELATION_MEMBER_ADDED, member.entity, this, members.length-1));
75             return members.length;
76         }
77
78                 public function removeMember(entity:Entity):void {
79                         var i:int;
80                         var lastRemoved:int = -1;
81                         while ((i=findEntityMemberIndex(entity))>-1) {
82                                 members.splice(i, 1);
83                                 lastRemoved = i;
84                         }
85                         entity.removeParent(this);
86                         markDirty();
87                         
88                         if ( lastRemoved >= 0 )
89                             dispatchEvent(new RelationMemberEvent(Connection.RELATION_MEMBER_REMOVED, entity, this, lastRemoved));
90                 }
91
92         public function removeMemberByIndex(index:uint):void {
93             var removed:Array=members.splice(index, 1);
94                         var entity:Entity=removed[0].entity;
95                         
96                         // only remove as parent if this was only reference
97                         if (findEntityMemberIndex(entity)==-1)
98                             entity.removeParent(this);
99                             
100                         markDirty();
101             dispatchEvent(new RelationMemberEvent(Connection.RELATION_MEMBER_REMOVED, entity, this, index));
102         }
103
104                 public override function remove():void {
105                         removeFromParents();
106                         for each (var member:RelationMember in members) { member.entity.removeParent(this); }
107                         members=[];
108                         deleted=true;
109             dispatchEvent(new EntityEvent(Connection.RELATION_DELETED, this));
110                 }
111
112                 internal override function isEmpty():Boolean {
113                         return (deleted || (members.length==0));
114                 }
115
116         public function getDescription():String {
117             var desc:String = "";
118             var relTags:Object = getTagsHash();
119             if ( relTags["type"] ) {
120                 desc = relTags["type"];
121                 if ( relTags[desc] )
122                     desc += " " + relTags[desc];
123             }
124             if ( relTags["ref"] )
125                 desc += " " + relTags["ref"];
126             if ( relTags["name"] )
127                 desc += " " + relTags["name"];
128             return desc;
129         }
130
131                 public override function getType():String {
132                         return 'relation';
133                 }
134                 
135                 public override function toString():String {
136             return "Relation("+id+"@"+version+"): "+members.length+" members "+getTagList();
137         }
138
139     }
140
141 }