some more footling around with POI nodes in ways
[potlatch2.git] / net / systemeD / halcyon / connection / Entity.as
1 package net.systemeD.halcyon.connection {
2
3     import flash.events.EventDispatcher;
4     import flash.utils.Dictionary;
5
6     public class Entity extends EventDispatcher {
7         private var _id:Number;
8         private var _version:uint;
9         private var tags:Object = {};
10         private var modified:Boolean = false;
11                 private var _loaded:Boolean = true;
12                 private var parents:Dictionary = new Dictionary();
13
14         public function Entity(id:Number, version:uint, tags:Object, loaded:Boolean) {
15             this._id = id;
16             this._version = version;
17             this.tags = tags;
18                         this._loaded = loaded;
19             modified = id < 0;
20         }
21
22         public function get id():Number {
23             return _id;
24         }
25
26         public function get version():uint {
27             return _version;
28         }
29
30         public function get loaded():Boolean {
31             return _loaded;
32         }
33
34                 public function updateEntityProperties(version:uint, tags:Object, loaded:Boolean):void {
35                         _version=version; this.tags=tags; _loaded=loaded;
36                 }
37
38                 // Tag-handling methods
39
40         public function hasTags():Boolean {
41             for (var key:String in tags)
42                 return true;
43             return false;
44         }
45
46                 // ** we could do with hasInterestingTags - don't bother with source, created_by, any TIGER tags, etc.
47
48         public function getTag(key:String):String {
49             return tags[key];
50         }
51
52         public function setTag(key:String, value:String):void {
53             var old:String = tags[key];
54             if ( old != value ) {
55                 if ( value == null || value == "" )
56                     delete tags[key];
57                 else
58                     tags[key] = value;
59                 markDirty();
60                 dispatchEvent(new TagEvent(Connection.TAG_CHANGE, this, key, key, old, value));
61             }
62         }
63
64         public function renameTag(oldKey:String, newKey:String):void {
65             var value:String = tags[oldKey];
66             if ( oldKey != newKey ) {
67                 delete tags[oldKey];
68                 tags[newKey] = value;
69                 markDirty();
70                 dispatchEvent(new TagEvent(Connection.TAG_CHANGE, this, oldKey, newKey, value, value));
71             }
72         }
73
74         public function getTagList():TagList {
75             return new TagList(tags);
76         }
77
78         public function getTagsCopy():Object {
79             var copy:Object = {};
80             for (var key:String in tags )
81                 copy[key] = tags[key];
82             return copy;
83         }
84
85                 public function getTagsHash():Object {
86                         // hm, not sure we should be doing this, but for read-only purposes
87                         // it's faster than using getTagsCopy
88                         return tags;
89                 }
90
91         public function getTagArray():Array {
92             var copy:Array = [];
93             for (var key:String in tags )
94                 copy.push(new Tag(this, key, tags[key]));
95             return copy;
96         }
97
98                 // Clean/dirty methods
99
100         public function get isDirty():Boolean {
101             return modified;
102         }
103
104         public function markClean(newID:Number, newVersion:uint):void {
105             this._id = newID;
106             this._version = newVersion;
107             modified = false;
108         }
109
110         protected function markDirty():void {
111             modified = true;
112         }
113
114                 // Parent handling
115                 
116                 public function addParent(parent:Entity):void {
117                         parents[parent]=true;
118                 }
119
120                 public function removeParent(parent:Entity):void {
121                         delete parents[parent];
122                 }
123                 
124                 public function get parentWays():Array {
125                         var a:Array=[];
126                         for (var o:Object in parents) {
127                                 if (o is Way) { a.push(o); }
128                         }
129                         return a;
130                 }
131                 
132                 public function get parentRelations():Array {
133                         var a:Array=[];
134                         for (var o:Object in parents) {
135                                 if (o is Relation) { a.push(o); }
136                         }
137                         return a;
138                 }
139                 
140                 public function get parentObjects():Array {
141                         var a:Array=[];
142                         for (var o:Object in parents) { a.push(o); }
143                         return a;
144                 }
145                 
146                 public function hasParent(entity:Entity):Boolean {
147             return parents[entity] == true;
148         }
149         
150                 // To be overridden
151
152         public function getType():String {
153             return '';
154         }
155
156     }
157
158 }