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 getFirstMember():RelationMember {
64                         return members[0];
65                 }
66
67                 public function getLastMember():RelationMember {
68                         return members[members.length-1];
69                 }
70
71         public function setMember(index:uint, member:RelationMember, performAction:Function):void {
72             var composite:CompositeUndoableAction = new CompositeUndoableAction("Set Member at index "+index);
73             composite.push(new RemoveMemberByIndexAction(this, members, index));
74             composite.push(new AddMemberToRelationAction(this, index, member, members));
75             performAction(composite);
76         }
77
78                 public function findMembersByRole(role:String, entityType:Class=null):Array {
79                         var a:Array=[];
80             for (var index:uint = 0; index < members.length; index++) {
81                 if (members[index].role==role && (!entityType || members[index].entity is entityType)) { a.push(members[index].entity); }
82             }
83                         return a;
84                 }
85
86                 public function hasMemberInRole(entity:Entity,role:String):Boolean {
87             for (var index:uint = 0; index < members.length; index++) {
88                                 if (members[index].role==role && members[index].entity == entity) { return true; }
89                         }
90                         return false;
91                 }
92                 
93         public function insertMember(index:uint, member:RelationMember, performAction:Function):void {
94             performAction(new AddMemberToRelationAction(this, index, member, members));
95         }
96
97         public function appendMember(member:RelationMember, performAction:Function):uint {
98             performAction(new AddMemberToRelationAction(this, -1, member, members));
99             return members.length + 1;
100         }
101
102                 public function removeMember(entity:Entity, performAction:Function):void {
103                         if (length>1) {
104                                 performAction(new RemoveEntityFromRelationAction(this, entity, members));
105                         } else {
106                                 performAction(new DeleteRelationAction(this, setDeletedState, members));
107                         }
108                 }
109
110         public function removeMemberByIndex(index:uint, performAction:Function):void {
111                         if (length>1) {
112                                 performAction(new RemoveMemberByIndexAction(this, members, index));
113                         } else {
114                                 performAction(new DeleteRelationAction(this, setDeletedState, members));
115                         }
116         }
117
118                 public override function remove(performAction:Function):void {
119                         performAction(new DeleteRelationAction(this, setDeletedState, members));
120                 }
121
122                 public override function nullify():void {
123                         nullifyEntity();
124                         members=[];
125                 }
126                 
127                 internal override function isEmpty():Boolean {
128                         return (deleted || (members.length==0));
129                 }
130
131                 public override function getDescription():String {
132                         var desc:String = "";
133                         var relTags:Object = getTagsHash();
134                         var named:Boolean = false;
135                         if ( relTags["type"] ) {
136                                 // type=route                           --> "route"
137                                 desc = relTags["type"];
138                                 // type=route, route=bicycle--> "route bicycle"
139                                 if (relTags[desc]) { desc += " " + relTags[desc]; }
140                         }
141                         // type=route, route=bicycle, network=ncn, ref=54 -> "route bicycle ncn 54"
142                         if ( relTags["network"]) { desc += " " + relTags["network"]; }
143                         if ( relTags["ref"]    ) { desc += " " + relTags["ref"];  named=true; }
144                         if ( relTags["name"]   ) { desc += " " + relTags["name"]; named=true; }
145                         // handle node->node routes
146                         if ( !named && relTags["type"] && relTags["type"]=="route" ) {
147                                 var firstName:String=getSignificantName(getFirstMember().entity);
148                                 var lastName:String=getSignificantName(getLastMember().entity);
149                                 if ((firstName+lastName)!='') desc+=" "+firstName+"-"+lastName;
150                         }
151                         return desc;
152                 }
153                 
154                 public function getRelationType():String {
155                         var relTags:Object = getTagsHash();
156                         return relTags["type"] ? relTags["type"] : getType();
157                 }
158                 
159                 private function getSignificantName(entity:Entity):String {
160                         if (!entity.loaded || (entity is Relation)) return '';
161
162                         var t:String;
163                         if (entity is Way) {
164                                 t=getSignificantName(Way(entity).getFirstNode());
165                                 if (t=='') t=getSignificantName(Way(entity).getLastNode());
166                                 return t;
167                         }
168                         t=Node(entity).getTag('name');
169                         if (!t) t=Node(entity).getTagByRegex(/ref$/);
170                         return t ? t : '';
171                 }
172
173                 public override function getType():String {
174                         return 'relation';
175                 }
176                 
177                 public override function toString():String {
178             return "Relation("+id+"@"+version+"): "+members.length+" members "+getTagList();
179         }
180
181     }
182
183 }