delete ways, too
[potlatch2.git] / net / systemeD / halcyon / connection / Connection.as
1 package net.systemeD.halcyon.connection {
2
3     import flash.net.*;
4
5     import flash.events.EventDispatcher;
6     import flash.events.Event;
7
8         public class Connection extends EventDispatcher {
9
10         private static var connectionInstance:Connection = null;
11
12         protected static var policyURL:String;
13         protected static var apiBaseURL:String;
14         protected static var params:Object;
15
16         public static function getConnection(initparams:Object=null):Connection {
17             if ( connectionInstance == null ) {
18             
19                 params = initparams == null ? new Object() : initparams;
20                 policyURL = getParam("policy", "http://127.0.0.1:3000/api/crossdomain.xml");
21                 apiBaseURL = getParam("api", "http://127.0.0.1:3000/api/0.6/");
22                 var connectType:String = getParam("connection", "XML");
23                 
24                 if ( connectType == "XML" )
25                     connectionInstance = new XMLConnection();
26                 else
27                     connectionInstance = new AMFConnection();
28             }
29             return connectionInstance;
30         }
31
32         public static function getParam(name:String, defaultValue:String):String {
33             trace("Returning param "+name+" as "+(params[name] == null ? defaultValue : params[name]));
34             return params[name] == null ? defaultValue : params[name];
35         }
36
37         public static function get apiBase():String {
38             return apiBaseURL;
39         }
40
41         public static function get serverName():String {
42             return getParam("serverName", "Localhost");
43         }
44                 
45                 public static function getConnectionInstance():Connection {
46             return connectionInstance;
47                 }
48
49                 public function getEnvironment(responder:Responder):void {}
50
51         // connection events
52         public static var LOAD_STARTED:String = "load_started";
53         public static var LOAD_COMPLETED:String = "load_completed";
54         public static var SAVE_STARTED:String = "save_started";
55         public static var SAVE_COMPLETED:String = "save_completed";
56         public static var NEW_CHANGESET:String = "new_changeset";
57         public static var NEW_CHANGESET_ERROR:String = "new_changeset_error";
58         public static var NEW_NODE:String = "new_node";
59         public static var NEW_WAY:String = "new_way";
60         public static var NEW_RELATION:String = "new_relation";
61         public static var NEW_POI:String = "new_poi";
62         public static var TAG_CHANGE:String = "tag_change";
63         public static var NODE_MOVED:String = "node_moved";
64         public static var WAY_NODE_ADDED:String = "way_node_added";
65         public static var WAY_NODE_REMOVED:String = "way_node_removed";
66                 public static var NODE_DELETED:String = "node_deleted";
67                 public static var WAY_DELETED:String = "way_deleted";
68                 public static var RELATION_DELETED:String = "relation_deleted";
69                 public static var RELATION_MEMBER_ADDED:String = "relation_member_added";
70                 public static var RELATION_MEMBER_REMOVED:String = "relation_member_deleted";
71                 public static var ADDED_TO_RELATION:String = "added_to_relation";
72                 public static var REMOVED_FROM_RELATION:String = "removed_from_relation";
73
74         // store the data we download
75         private var negativeID:Number = -1;
76         private var nodes:Object = {};
77         private var ways:Object = {};
78         private var relations:Object = {};
79         private var pois:Array = [];
80         private var changeset:Changeset = null;
81
82         protected function get nextNegative():Number {
83             return negativeID--;
84         }
85
86         protected function setNode(node:Node,queue:Boolean):void {
87             nodes[node.id] = node;
88             if (node.loaded) { sendEvent(new EntityEvent(NEW_NODE, node),queue); }
89         }
90
91         protected function setWay(way:Way,queue:Boolean):void {
92             ways[way.id] = way;
93             if (way.loaded) { sendEvent(new EntityEvent(NEW_WAY, way),queue); }
94         }
95
96         protected function setRelation(relation:Relation,queue:Boolean):void {
97             relations[relation.id] = relation;
98             if (relation.loaded) { sendEvent(new EntityEvent(NEW_RELATION, relation),queue); }
99         }
100
101         protected function renumberNode(oldID:Number, node:Node):void {
102             nodes[node.id] = node;
103             delete nodes[oldID];
104         }
105
106         protected function renumberWay(oldID:Number, way:Way):void {
107             ways[way.id] = way;
108             delete ways[oldID];
109         }
110
111         protected function renumberRelation(oldID:Number, relation:Relation):void {
112             relations[relation.id] = relation;
113             delete relations[oldID];
114         }
115
116
117                 public function sendEvent(e:*,queue:Boolean):void {
118                         // queue is only used for AMFConnection
119                         dispatchEvent(e);
120                 }
121
122         public function registerPOI(node:Node):void {
123             if ( pois.indexOf(node) < 0 ) {
124                 pois.push(node);
125                 sendEvent(new EntityEvent(NEW_POI, node),false);
126             }
127         }
128
129         protected function unregisterPOI(node:Node):void {
130             var index:uint = pois.indexOf(node);
131             if ( index >= 0 ) {
132                 pois.splice(index,1);
133             }
134         }
135
136         protected function setActiveChangeset(changeset:Changeset):void {
137             this.changeset = changeset;
138             sendEvent(new EntityEvent(NEW_CHANGESET, changeset),false);
139         }
140         
141         public function getNode(id:Number):Node {
142             return nodes[id];
143         }
144
145         public function getWay(id:Number):Way {
146             return ways[id];
147         }
148
149         public function getRelation(id:Number):Relation {
150             return relations[id];
151         }
152
153         public function createNode(tags:Object, lat:Number, lon:Number):Node {
154             var node:Node = new Node(nextNegative, 0, tags, true, lat, lon);
155             setNode(node,false);
156             return node;
157         }
158
159         public function createWay(tags:Object, nodes:Array):Way {
160             var way:Way = new Way(nextNegative, 0, tags, true, nodes.concat());
161             setWay(way,false);
162             return way;
163         }
164
165         public function createRelation(tags:Object, members:Array):Relation {
166             var relation:Relation = new Relation(nextNegative, 0, tags, true, members.concat());
167             setRelation(relation,false);
168             return relation;
169         }
170
171         public function getAllNodeIDs():Array {
172             var list:Array = [];
173             for each (var node:Node in nodes)
174                 list.push(node.id);
175             return list;
176         }
177
178         public function getAllWayIDs():Array {
179             var list:Array = [];
180             for each (var way:Way in ways)
181                 list.push(way.id);
182             return list;
183         }
184
185         public function getAllRelationIDs():Array {
186             var list:Array = [];
187             for each (var relation:Relation in relations)
188                 list.push(relation.id);
189             return list;
190         }
191
192         public function getActiveChangeset():Changeset {
193             return changeset;
194         }
195         
196         // these are functions that the Connection implementation is expected to
197         // provide. This class has some generic helpers for the implementation.
198                 public function loadBbox(left:Number, right:Number,
199                                                                 top:Number, bottom:Number):void {
200             }
201             
202             public function setAppID(id:Object):void {}
203             public function setAuthToken(id:Object):void {}
204             public function createChangeset(tags:Object):void {}
205             public function uploadChanges():void {}
206     }
207
208 }
209