Merge branch 'master' into history
[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(connection:Connection, id:Number, version:uint, tags:Object, loaded:Boolean, members:Array, uid:Number = NaN, timestamp:String = null, user:String = null) {
10             super(connection, id, version, tags, loaded, uid, timestamp, user);
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, user: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,user);
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, performAction:Function):void {
64             var composite:CompositeUndoableAction = new CompositeUndoableAction("Set Member at index "+index);
65             composite.push(new RemoveMemberByIndexAction(this, members, index));
66             composite.push(new AddMemberToRelationAction(this, index, member, members));
67             performAction(composite);
68         }
69
70                 public function findMembersByRole(role:String, entityType:Class=null):Array {
71                         var a:Array=[];
72             for (var index:uint = 0; index < members.length; index++) {
73                 if (members[index].role==role && (!entityType || members[index].entity is entityType)) { a.push(members[index].entity); }
74             }
75                         return a;
76                 }
77
78                 public function hasMemberInRole(entity:Entity,role:String):Boolean {
79             for (var index:uint = 0; index < members.length; index++) {
80                                 if (members[index].role==role && members[index].entity == entity) { return true; }
81                         }
82                         return false;
83                 }
84                 
85         public function insertMember(index:uint, member:RelationMember, performAction:Function):void {
86             performAction(new AddMemberToRelationAction(this, index, member, members));
87         }
88
89         public function appendMember(member:RelationMember, performAction:Function):uint {
90             performAction(new AddMemberToRelationAction(this, -1, member, members));
91             return members.length + 1;
92         }
93
94                 public function removeMember(entity:Entity, performAction:Function):void {
95                         performAction(new RemoveEntityFromRelationAction(this, entity, members));
96                 }
97
98         public function removeMemberByIndex(index:uint, performAction:Function):void {
99             performAction(new RemoveMemberByIndexAction(this, members, index));
100         }
101
102                 public override function remove(performAction:Function):void {
103                         performAction(new DeleteRelationAction(this, setDeletedState, members));
104                 }
105
106                 public override function nullify():void {
107                         nullifyEntity();
108                         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 }