a2bbc68b3a35d75e580e37523d8db4225f8713b4
[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         import net.systemeD.halcyon.Globals;
8         import net.systemeD.halcyon.connection.actions.*;
9
10         public class Connection extends EventDispatcher {
11
12         private static var connectionInstance:Connection = null;
13
14         protected static var policyURL:String;
15         protected static var apiBaseURL:String;
16         protected static var params:Object;
17
18         public static function getConnection(initparams:Object=null):Connection {
19             if ( connectionInstance == null ) {
20             
21                 params = initparams == null ? new Object() : initparams;
22                 policyURL = getParam("policy", "http://127.0.0.1:3000/api/crossdomain.xml");
23                 apiBaseURL = getParam("api", "http://127.0.0.1:3000/api/0.6/");
24                 var connectType:String = getParam("connection", "XML");
25                 
26                 if ( connectType == "XML" )
27                     connectionInstance = new XMLConnection();
28                 else if ( connectType == "OSM" )
29                     connectionInstance = new OSMConnection();
30                 else
31                     connectionInstance = new AMFConnection();
32             }
33             return connectionInstance;
34         }
35
36         public static function getParam(name:String, defaultValue:String):String {
37             return params[name] == null ? defaultValue : params[name];
38         }
39
40         public function get apiBase():String {
41             return apiBaseURL;
42         }
43
44         public static function get serverName():String {
45             return getParam("serverName", "Localhost");
46         }
47                 
48                 public static function getConnectionInstance():Connection {
49             return connectionInstance;
50                 }
51
52                 public function getEnvironment(responder:Responder):void {}
53
54         // connection events
55         public static var LOAD_STARTED:String = "load_started";
56         public static var LOAD_COMPLETED:String = "load_completed";
57         public static var SAVE_STARTED:String = "save_started";
58         public static var SAVE_COMPLETED:String = "save_completed";
59         public static var DATA_DIRTY:String = "data_dirty";
60         public static var DATA_CLEAN:String = "data_clean";
61         public static var NEW_CHANGESET:String = "new_changeset";
62         public static var NEW_CHANGESET_ERROR:String = "new_changeset_error";
63         public static var NEW_NODE:String = "new_node";
64         public static var NEW_WAY:String = "new_way";
65         public static var NEW_RELATION:String = "new_relation";
66         public static var NEW_POI:String = "new_poi";
67         public static var NODE_RENUMBERED:String = "node_renumbered";
68         public static var WAY_RENUMBERED:String = "way_renumbered";
69         public static var RELATION_RENUMBERED:String = "relation_renumbered";
70         public static var TAG_CHANGED:String = "tag_change";
71         public static var NODE_MOVED:String = "node_moved";
72         public static var WAY_NODE_ADDED:String = "way_node_added";
73         public static var WAY_NODE_REMOVED:String = "way_node_removed";
74         public static var WAY_REORDERED:String = "way_reordered";
75         public static var WAY_DRAGGED:String = "way_dragged";
76                 public static var NODE_DELETED:String = "node_deleted";
77                 public static var WAY_DELETED:String = "way_deleted";
78                 public static var RELATION_DELETED:String = "relation_deleted";
79                 public static var RELATION_MEMBER_ADDED:String = "relation_member_added";
80                 public static var RELATION_MEMBER_REMOVED:String = "relation_member_deleted";
81                 public static var ADDED_TO_RELATION:String = "added_to_relation";
82                 public static var REMOVED_FROM_RELATION:String = "removed_from_relation";
83                 public static var SUSPEND_REDRAW:String = "suspend_redraw";
84                 public static var RESUME_REDRAW:String = "resume_redraw";
85         public static var TRACES_LOADED:String = "traces_loaded";
86
87         // store the data we download
88         private var negativeID:Number = -1;
89         private var nodes:Object = {};
90         private var ways:Object = {};
91         private var relations:Object = {};
92         private var pois:Array = [];
93         private var changeset:Changeset = null;
94                 private var changesetUpdated:Number;
95                 private var modified:Boolean = false;
96                 public var nodecount:int=0;
97                 public var waycount:int=0;
98                 public var relationcount:int=0;
99         private var traces:Array = [];
100         protected var traces_loaded:Boolean = false;
101
102         protected function get nextNegative():Number {
103             return negativeID--;
104         }
105
106         protected function setNode(node:Node, queue:Boolean):void {
107                         if (!nodes[node.id]) { nodecount++; }
108             nodes[node.id] = node;
109             if (node.loaded) { sendEvent(new EntityEvent(NEW_NODE, node),queue); }
110         }
111
112         protected function setWay(way:Way, queue:Boolean):void {
113                         if (!ways[way.id]) { waycount++; }
114             ways[way.id] = way;
115             if (way.loaded) { sendEvent(new EntityEvent(NEW_WAY, way),queue); }
116         }
117
118         protected function setRelation(relation:Relation, queue:Boolean):void {
119                         if (!relations[relation.id]) { relationcount++; }
120             relations[relation.id] = relation;
121             if (relation.loaded) { sendEvent(new EntityEvent(NEW_RELATION, relation),queue); }
122         }
123
124         protected function renumberNode(oldID:Number, node:Node, queue:Boolean):void {
125             nodes[node.id] = node;
126             if (node.loaded) { sendEvent(new EntityRenumberedEvent(NODE_RENUMBERED, node, oldID),queue); }
127             delete nodes[oldID];
128         }
129
130         protected function renumberWay(oldID:Number, way:Way, queue:Boolean):void {
131             ways[way.id] = way;
132             if (way.loaded) { sendEvent(new EntityRenumberedEvent(WAY_RENUMBERED, way, oldID),queue); }
133             delete ways[oldID];
134         }
135
136         protected function renumberRelation(oldID:Number, relation:Relation, queue:Boolean):void {
137             relations[relation.id] = relation;
138             if (relation.loaded) { sendEvent(new EntityRenumberedEvent(RELATION_RENUMBERED, relation, oldID),queue); }
139             delete relations[oldID];
140         }
141
142
143                 public function sendEvent(e:*,queue:Boolean):void {
144                         // queue is only used for AMFConnection
145                         dispatchEvent(e);
146                 }
147
148         public function registerPOI(node:Node):void {
149             if ( pois.indexOf(node) < 0 ) {
150                 pois.push(node);
151                 sendEvent(new EntityEvent(NEW_POI, node),false);
152             }
153         }
154
155         public function unregisterPOI(node:Node):void {
156             var index:uint = pois.indexOf(node);
157             if ( index >= 0 ) {
158                 pois.splice(index,1);
159             }
160         }
161
162         public function getNode(id:Number):Node {
163             return nodes[id];
164         }
165
166         public function getWay(id:Number):Way {
167             return ways[id];
168         }
169
170         public function getRelation(id:Number):Relation {
171             return relations[id];
172         }
173
174                 // Remove data from Connection
175                 // These functions are used only internally to stop redundant data hanging around
176                 // (either because it's been deleted on the server, or because we have panned away
177                 //  and need to reduce memory usage)
178
179                 protected function killNode(id:Number):void {
180                         if (!nodes[id]) return;
181             nodes[id].dispatchEvent(new EntityEvent(Connection.NODE_DELETED, nodes[id]));
182                         if (nodes[id].parentRelations.length>0) {
183                                 nodes[id]=new Node(id,0,{},false,0,0);
184                         } else {
185                                 delete nodes[id];
186                         }
187                         nodecount--;
188                 }
189
190                 protected function killWay(id:Number):void {
191                         if (!ways[id]) return;
192             ways[id].dispatchEvent(new EntityEvent(Connection.WAY_DELETED, ways[id]));
193                         if (ways[id].parentRelations.length>0) {
194                                 ways[id]=new Way(id,0,{},false,[]);
195                         } else {
196                                 delete ways[id];
197                         }
198                         waycount--;
199                 }
200
201                 protected function killRelation(id:Number):void {
202                         if (!relations[id]) return;
203             relations[id].dispatchEvent(new EntityEvent(Connection.RELATION_DELETED, relations[id]));
204                         if (relations[id].parentRelations.length>0) {
205                                 relations[id]=new Relation(id,0,{},false,[]);
206                         } else {
207                                 delete relations[id];
208                         }
209                         relationcount--;
210                 }
211
212                 protected function killWayWithNodes(id:Number):void {
213                         var way:Way=ways[id];
214                         var node:Node;
215                         for (var i:uint=0; i<way.length; i++) {
216                                 node=way.getNode(i);
217                                 if (node.isDirty) { continue; }
218                                 if (node.parentWays.length>1) {
219                                         node.removeParent(way);
220                                 } else {
221                                         killNode(node.id);
222                                 }
223                         }
224                         killWay(id);
225                 }
226                 
227
228
229         public function createNode(tags:Object, lat:Number, lon:Number, performCreate:Function):Node {
230             var node:Node = new Node(nextNegative, 0, tags, true, lat, lon);
231             performCreate(new CreateEntityAction(node, setNode));
232                         //markDirty();
233             return node;
234         }
235
236         public function createWay(tags:Object, nodes:Array, performCreate:Function):Way {
237             var way:Way = new Way(nextNegative, 0, tags, true, nodes.concat());
238             performCreate(new CreateEntityAction(way, setWay));
239                         //markDirty();
240             return way;
241         }
242
243         public function createRelation(tags:Object, members:Array, performCreate:Function):Relation {
244             var relation:Relation = new Relation(nextNegative, 0, tags, true, members.concat());
245             performCreate(new CreateEntityAction(relation, setRelation));
246                         //markDirty();
247             return relation;
248         }
249
250         public function getAllNodeIDs():Array {
251             var list:Array = [];
252             for each (var node:Node in nodes)
253                 list.push(node.id);
254             return list;
255         }
256
257         public function getAllWayIDs():Array {
258             var list:Array = [];
259             for each (var way:Way in ways)
260                 list.push(way.id);
261             return list;
262         }
263
264         public function getAllRelationIDs():Array {
265             var list:Array = [];
266             for each (var relation:Relation in relations)
267                 list.push(relation.id);
268             return list;
269         }
270
271         public function getMatchingRelationIDs(match:Object):Array {
272             var list:Array = [];
273                         var ok:Boolean;
274             for each (var relation:Relation in relations) {
275                                 ok=true;
276                                 if (relation.deleted) { ok=false; }
277                                 for (var k:String in match) {
278                                         if (!relation.getTagsHash()[k] || relation.getTagsHash()[k]!=match[k]) { ok=false; }
279                                 }
280                                 if (ok) { list.push(relation.id); }
281                         }
282             return list;
283         }
284
285                 public function getObjectsByBbox(left:Number, right:Number, top:Number, bottom:Number):Object {
286                         var o:Object = { nodesInside: [], nodesOutside: [], waysInside: [], waysOutside: [] };
287                         for each (var way:Way in ways) {
288                                 if (way.within(left,right,top,bottom)) { o.waysInside.push(way); }
289                                                                   else { o.waysOutside.push(way); }
290                         }
291                         for each (var node:Node in nodes) {
292                                 if (node.within(left,right,top,bottom)) { o.nodesInside.push(node); }
293                                                                    else { o.nodesOutside.push(node); }
294                         }
295                         return o;
296                 }
297
298                 public function purgeOutside(left:Number, right:Number, top:Number, bottom:Number):void {
299                         return;
300                         // ** this doesn't work - WayUIs stick around.
301                         // ** should purge POIs and relations too
302                         for each (var way:Way in ways) {
303                                 if (!way.within(left,right,top,bottom) && !way.isDirty) {
304                                         killWayWithNodes(way.id);
305                                 }
306                         }
307                 }
308
309                 public function markDirty():void {
310             if (!modified) { dispatchEvent(new Event(DATA_DIRTY)); }
311                         modified=true;
312                 }
313                 public function markClean():void {
314             if (modified) { dispatchEvent(new Event(DATA_CLEAN)); }
315                         modified=false;
316                 }
317                 public function get isDirty():Boolean {
318                         return modified;
319                 }
320
321                 // Changeset tracking
322
323         protected function setActiveChangeset(changeset:Changeset):void {
324             this.changeset = changeset;
325                         changesetUpdated = new Date().getTime();
326             sendEvent(new EntityEvent(NEW_CHANGESET, changeset),false);
327         }
328
329                 protected function freshenActiveChangeset():void {
330                         changesetUpdated = new Date().getTime();
331                 }
332                 
333                 protected function closeActiveChangeset():void {
334                         changeset = null;
335                 }
336         
337         public function getActiveChangeset():Changeset {
338                         if (changeset && (new Date().getTime()) > (changesetUpdated+58*60*1000)) {
339                                 closeActiveChangeset();
340                         }
341             return changeset;
342         }
343
344         protected function addTrace(t:Object):void {
345             traces.push(t);
346         }
347
348         protected function clearTraces():void {
349             traces = [];
350         }
351
352         public function getTraces():Array {
353             return traces;
354         }
355
356         // these are functions that the Connection implementation is expected to
357         // provide. This class has some generic helpers for the implementation.
358                 public function loadBbox(left:Number, right:Number,
359                                                                 top:Number, bottom:Number):void {
360             }
361             
362             public function setAppID(id:Object):void {}
363             public function setAuthToken(id:Object):void {}
364             public function createChangeset(tags:Object):void {}
365                 public function closeChangeset():void {}
366             public function uploadChanges():void {}
367         public function fetchUserTraces(refresh:Boolean=false):void {}
368         public function fetchTrace(id:Number, callback:Function):void {}
369     }
370
371 }
372