e6bdbd155389d00a3e82dc4886091c629835643f
[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, uid:Number = NaN, timestamp:String = null) {
10             super(id, version, tags, loaded, uid, timestamp);
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, parentsLoaded:Boolean, members:Array, uid:Number = NaN, timestamp:String = null):void {
17                         var member:RelationMember;
18                         for each (member in this.members)
19                             member.entity.removeParent(this);
20
21                         updateEntityProperties(version,tags,loaded,parentsLoaded,uid,timestamp);
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 get memberEntities():Array {
32                         var list:Array=[];
33                         for (var index:uint = 0; index < members.length; index++) {
34                                 var e:Entity=members[index].entity;
35                                 if (list.indexOf(e)==-1) list.push(e);
36                         }
37                         return list;
38                 }
39
40         public function findEntityMemberIndex(entity:Entity):int {
41             for (var index:uint = 0; index < members.length; index++) {
42                 var member:RelationMember = members[index];
43                 if ( member.entity == entity )
44                     return index;
45             }
46             return -1;
47         }
48
49         public function findEntityMemberIndexes(entity:Entity):Array {
50             var indexes:Array = [];
51             for (var index:uint = 0; index < members.length; index++) {
52                 var member:RelationMember = members[index];
53                 if ( member.entity == entity )
54                     indexes.push(index);
55             }
56             return indexes;
57         }
58         
59         public function getMember(index:uint):RelationMember {
60             return members[index];
61         }
62
63         public function setMember(index:uint, member:RelationMember):void {
64             var oldMember:RelationMember = getMember(index);
65             
66                         members.splice(index, 1, member);
67             oldMember.entity.removeParent(this);
68                         member.entity.addParent(this);
69                         markDirty();
70         }
71
72                 public function findMembersByRole(role:String, entityType:Class=null):Array {
73                         var a:Array=[];
74             for (var index:uint = 0; index < members.length; index++) {
75                 if (members[index].role==role && (!entityType || members[index].entity is entityType)) { a.push(members[index].entity); }
76             }
77                         return a;
78                 }
79
80                 public function hasMemberInRole(entity:Entity,role:String):Boolean {
81             for (var index:uint = 0; index < members.length; index++) {
82                                 if (members[index].role==role && members[index].entity == entity) { return true; }
83                         }
84                         return false;
85                 }
86                 
87         public function insertMember(index:uint, member:RelationMember, performAction:Function):void {
88             performAction(new AddMemberToRelationAction(this, index, member, members));
89         }
90
91         public function appendMember(member:RelationMember, performAction:Function):uint {
92             performAction(new AddMemberToRelationAction(this, -1, member, members));
93             return members.length + 1;
94         }
95
96                 public function removeMember(entity:Entity, performAction:Function):void {
97                         performAction(new RemoveEntityFromRelationAction(this, entity, members));
98                 }
99
100         public function removeMemberByIndex(index:uint, performAction:Function):void {
101             performAction(new RemoveMemberByIndexAction(this, members, index));
102         }
103
104                 public override function remove(performAction:Function):void {
105                         performAction(new DeleteRelationAction(this, setDeletedState, members));
106                 }
107
108                 public override function nullify():void {
109                         nullifyEntity();
110                         members=[];
111                 }
112                 
113                 internal override function isEmpty():Boolean {
114                         return (deleted || (members.length==0));
115                 }
116
117         public override function getDescription():String {
118             var desc:String = "";
119             var relTags:Object = getTagsHash();
120             if ( relTags["type"] ) {
121                 desc = relTags["type"];
122                 if ( relTags[desc] )
123                     desc += " " + relTags[desc];
124             }
125             if ( relTags["ref"] )
126                 desc += " " + relTags["ref"];
127             if ( relTags["name"] )
128                 desc += " " + relTags["name"];
129             return desc;
130         }
131
132                 public override function getType():String {
133                         return 'relation';
134                 }
135                 
136                 public override function toString():String {
137             return "Relation("+id+"@"+version+"): "+members.length+" members "+getTagList();
138         }
139
140     }
141
142 }