Merge branch 'master' of github.com:systemed/potlatch2
[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                 /** Is there an entity member in this specific role? */
87                 public function hasMemberInRole(entity:Entity,role:String):Boolean {
88             for (var index:uint = 0; index < members.length; index++) {
89                                 if (members[index].entity == entity && members[index].role==role) { return true; }
90                         }
91                         return false;
92                 }
93                 
94         public function insertMember(index:uint, member:RelationMember, performAction:Function):void {
95             performAction(new AddMemberToRelationAction(this, index, member, members));
96         }
97
98         public function appendMember(member:RelationMember, performAction:Function):uint {
99             performAction(new AddMemberToRelationAction(this, -1, member, members));
100             return members.length + 1;
101         }
102
103                 public function removeMember(entity:Entity, performAction:Function):void {
104                         if (length>1) {
105                                 performAction(new RemoveEntityFromRelationAction(this, entity, members));
106                         } else {
107                                 performAction(new DeleteRelationAction(this, setDeletedState, members));
108                         }
109                 }
110
111         public function removeMemberByIndex(index:uint, performAction:Function):void {
112                         if (length>1) {
113                                 performAction(new RemoveMemberByIndexAction(this, members, index));
114                         } else {
115                                 performAction(new DeleteRelationAction(this, setDeletedState, members));
116                         }
117         }
118
119                 public override function remove(performAction:Function):void {
120                         performAction(new DeleteRelationAction(this, setDeletedState, members));
121                 }
122
123                 public override function nullify():void {
124                         nullifyEntity();
125                         members=[];
126                 }
127                 
128                 internal override function isEmpty():Boolean {
129                         return (deleted || (members.length==0));
130                 }
131
132                 public override function getDescription():String {
133                         var desc:String = "";
134                         var relTags:Object = getTagsHash();
135                         var named:Boolean = false;
136                         if ( relTags["type"] ) {
137                                 // type=route                           --> "route"
138                                 desc = relTags["type"];
139                                 // type=route, route=bicycle--> "route bicycle"
140                                 if (relTags[desc]) { desc += " " + relTags[desc]; }
141                         }
142                         // type=route, route=bicycle, network=ncn, ref=54 -> "route bicycle ncn 54"
143                         if ( relTags["network"]) { desc += " " + relTags["network"]; }
144                         if ( relTags["ref"]    ) { desc += " " + relTags["ref"];  named=true; }
145                         if ( relTags["name"]   ) { desc += " " + relTags["name"]; named=true; }
146                         // handle node->node routes
147                         if ( !named && relTags["type"] && relTags["type"]=="route" ) {
148                                 var firstName:String=getSignificantName(getFirstMember().entity);
149                                 var lastName:String=getSignificantName(getLastMember().entity);
150                                 if ((firstName+lastName)!='') desc+=" "+firstName+"-"+lastName;
151                         }
152                         return desc;
153                 }
154                 
155                 public function getRelationType():String {
156                         var relTags:Object = getTagsHash();
157                         return relTags["type"] ? relTags["type"] : getType();
158                 }
159                 
160                 public function hasKeysOtherThanType():Boolean {
161                         // used for detecting old-style vs new-style multipolygons
162                         for (var key:String in getTagsHash()) {
163                                 if (key!='type' && key!='created_by' && key!='source') { return false; }
164                         }
165                         return true;
166                 }
167
168                 private function getSignificantName(entity:Entity):String {
169                         if (!entity.loaded || (entity is Relation)) return '';
170
171                         var t:String;
172                         if (entity is Way) {
173                                 t=getSignificantName(Way(entity).getFirstNode());
174                                 if (t=='') t=getSignificantName(Way(entity).getLastNode());
175                                 return t;
176                         }
177                         t=Node(entity).getTag('name');
178                         if (!t) t=Node(entity).getTagByRegex(/ref$/);
179                         return t ? t : '';
180                 }
181
182                 public override function getType():String {
183                         return 'relation';
184                 }
185                 
186                 public override function toString():String {
187             return "Relation("+id+"@"+version+"): "+members.length+" members "+getTagList();
188         }
189
190     }
191
192 }