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