change relation.appendMember to use the undo system, and update various places as...
[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, 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,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 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, performAction:Function):uint {
83             performAction(new AddMemberToRelationAction(this, -1, member, members));
84             return members.length + 1;
85         }
86
87                 public function removeMember(entity:Entity, performAction:Function):void {
88                         performAction(new RemoveEntityFromRelationAction(this, entity, members));
89                 }
90
91         public function removeMemberByIndex(index:uint):void {
92             var removed:Array=members.splice(index, 1);
93                         var entity:Entity=removed[0].entity;
94                         
95                         // only remove as parent if this was only reference
96                         if (findEntityMemberIndex(entity)==-1)
97                             entity.removeParent(this);
98                             
99                         markDirty();
100             dispatchEvent(new RelationMemberEvent(Connection.RELATION_MEMBER_REMOVED, entity, this, index));
101         }
102
103                 public override function remove(performAction:Function):void {
104                         performAction(new DeleteRelationAction(this, setDeletedState, members));
105                 }
106
107                 public override function nullify():void {
108                         nullifyEntity();
109                         members=[];
110                 }
111                 
112                 internal override function isEmpty():Boolean {
113                         return (deleted || (members.length==0));
114                 }
115
116         public override 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 }