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