507900892bd604bad7ab258861a13f9723654c5c
[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 NODE_DELETED:String = "node_deleted";
71                 public static var WAY_DELETED:String = "way_deleted";
72                 public static var RELATION_DELETED:String = "relation_deleted";
73                 public static var RELATION_MEMBER_ADDED:String = "relation_member_added";
74                 public static var RELATION_MEMBER_REMOVED:String = "relation_member_deleted";
75                 public static var ADDED_TO_RELATION:String = "added_to_relation";
76                 public static var REMOVED_FROM_RELATION:String = "removed_from_relation";
77                 public static var SUSPEND_REDRAW:String = "suspend_redraw";
78                 public static var RESUME_REDRAW:String = "resume_redraw";
79
80         // store the data we download
81         private var negativeID:Number = -1;
82         private var nodes:Object = {};
83         private var ways:Object = {};
84         private var relations:Object = {};
85         private var pois:Array = [];
86         private var changeset:Changeset = null;
87
88         protected function get nextNegative():Number {
89             return negativeID--;
90         }
91
92         protected function setNode(node:Node, queue:Boolean):void {
93             nodes[node.id] = node;
94             if (node.loaded) { sendEvent(new EntityEvent(NEW_NODE, node),queue); }
95         }
96
97         protected function setWay(way:Way, queue:Boolean):void {
98             ways[way.id] = way;
99             if (way.loaded) { sendEvent(new EntityEvent(NEW_WAY, way),queue); }
100         }
101
102         protected function setRelation(relation:Relation, queue:Boolean):void {
103             relations[relation.id] = relation;
104             if (relation.loaded) { sendEvent(new EntityEvent(NEW_RELATION, relation),queue); }
105         }
106
107         protected function renumberNode(oldID:Number, node:Node, queue:Boolean):void {
108             nodes[node.id] = node;
109             if (node.loaded) { sendEvent(new EntityRenumberedEvent(NODE_RENUMBERED, node, oldID),queue); }
110             delete nodes[oldID];
111         }
112
113         protected function renumberWay(oldID:Number, way:Way, queue:Boolean):void {
114             ways[way.id] = way;
115             if (way.loaded) { sendEvent(new EntityRenumberedEvent(WAY_RENUMBERED, way, oldID),queue); }
116             delete ways[oldID];
117         }
118
119         protected function renumberRelation(oldID:Number, relation:Relation, queue:Boolean):void {
120             relations[relation.id] = relation;
121             if (relation.loaded) { sendEvent(new EntityRenumberedEvent(RELATION_RENUMBERED, relation, oldID),queue); }
122             delete relations[oldID];
123         }
124
125
126                 public function sendEvent(e:*,queue:Boolean):void {
127                         // queue is only used for AMFConnection
128                         dispatchEvent(e);
129                 }
130
131         public function registerPOI(node:Node):void {
132             if ( pois.indexOf(node) < 0 ) {
133                 pois.push(node);
134                 sendEvent(new EntityEvent(NEW_POI, node),false);
135             }
136         }
137
138         public function unregisterPOI(node:Node):void {
139             var index:uint = pois.indexOf(node);
140             if ( index >= 0 ) {
141                 pois.splice(index,1);
142             }
143         }
144
145         protected function setActiveChangeset(changeset:Changeset):void {
146             this.changeset = changeset;
147             sendEvent(new EntityEvent(NEW_CHANGESET, changeset),false);
148         }
149         
150         public function getNode(id:Number):Node {
151             return nodes[id];
152         }
153
154         public function getWay(id:Number):Way {
155             return ways[id];
156         }
157
158         public function getRelation(id:Number):Relation {
159             return relations[id];
160         }
161
162         public function createNode(tags:Object, lat:Number, lon:Number):Node {
163             var node:Node = new Node(nextNegative, 0, tags, true, lat, lon);
164             setNode(node,false);
165             return node;
166         }
167
168         public function createWay(tags:Object, nodes:Array):Way {
169             var way:Way = new Way(nextNegative, 0, tags, true, nodes.concat());
170             setWay(way,false);
171             return way;
172         }
173
174         public function createRelation(tags:Object, members:Array):Relation {
175             var relation:Relation = new Relation(nextNegative, 0, tags, true, members.concat());
176             setRelation(relation,false);
177             return relation;
178         }
179
180         public function getAllNodeIDs():Array {
181             var list:Array = [];
182             for each (var node:Node in nodes)
183                 list.push(node.id);
184             return list;
185         }
186
187         public function getAllWayIDs():Array {
188             var list:Array = [];
189             for each (var way:Way in ways)
190                 list.push(way.id);
191             return list;
192         }
193
194         public function getAllRelationIDs():Array {
195             var list:Array = [];
196             for each (var relation:Relation in relations)
197                 list.push(relation.id);
198             return list;
199         }
200
201         public function getActiveChangeset():Changeset {
202             return changeset;
203         }
204         
205         // these are functions that the Connection implementation is expected to
206         // provide. This class has some generic helpers for the implementation.
207                 public function loadBbox(left:Number, right:Number,
208                                                                 top:Number, bottom:Number):void {
209             }
210             
211             public function setAppID(id:Object):void {}
212             public function setAuthToken(id:Object):void {}
213             public function createChangeset(tags:Object):void {}
214             public function uploadChanges():void {}
215     }
216
217 }
218