6049c458b555a87cbcbbd07a3a8e11071508a894
[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 insertMember(index:uint, member:RelationMember, performAction:Function):void {
64             performAction(new AddMemberToRelationAction(this, index, member, members));
65         }
66
67         public function appendMember(member:RelationMember):uint {
68             members.push(member);
69                         member.entity.addParent(this);
70                         markDirty();
71
72                         dispatchEvent(new RelationMemberEvent(Connection.RELATION_MEMBER_ADDED, member.entity, this, members.length-1));
73             return members.length;
74         }
75
76                 public function removeMember(entity:Entity, performAction:Function):void {
77                         performAction(new RemoveEntityFromRelationAction(this, entity, members));
78                 }
79
80         public function removeMemberByIndex(index:uint):void {
81             var removed:Array=members.splice(index, 1);
82                         var entity:Entity=removed[0].entity;
83                         
84                         // only remove as parent if this was only reference
85                         if (findEntityMemberIndex(entity)==-1)
86                             entity.removeParent(this);
87                             
88                         markDirty();
89             dispatchEvent(new RelationMemberEvent(Connection.RELATION_MEMBER_REMOVED, entity, this, index));
90         }
91
92                 public override function remove(performAction:Function):void {
93                         performAction(new DeleteRelationAction(this, setDeletedState, members));
94                 }
95
96                 internal override function isEmpty():Boolean {
97                         return (deleted || (members.length==0));
98                 }
99
100         public function getDescription():String {
101             var desc:String = "";
102             var relTags:Object = getTagsHash();
103             if ( relTags["type"] ) {
104                 desc = relTags["type"];
105                 if ( relTags[desc] )
106                     desc += " " + relTags[desc];
107             }
108             if ( relTags["ref"] )
109                 desc += " " + relTags["ref"];
110             if ( relTags["name"] )
111                 desc += " " + relTags["name"];
112             return desc;
113         }
114
115                 public override function getType():String {
116                         return 'relation';
117                 }
118                 
119                 public override function toString():String {
120             return "Relation("+id+"@"+version+"): "+members.length+" members "+getTagList();
121         }
122
123     }
124
125 }