46758302958e7461596f43be91aec2e88738dd48
[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             return params[name] == null ? defaultValue : params[name];
34         }
35
36         public static function get apiBase():String {
37             return apiBaseURL;
38         }
39
40         public static function get serverName():String {
41             return getParam("serverName", "Localhost");
42         }
43                 
44                 public static function getConnectionInstance():Connection {
45             return connectionInstance;
46                 }
47
48                 public function getEnvironment(responder:Responder):void {}
49
50         // connection events
51         public static var LOAD_STARTED:String = "load_started";
52         public static var LOAD_COMPLETED:String = "load_completed";
53         public static var SAVE_STARTED:String = "save_started";
54         public static var SAVE_COMPLETED:String = "save_completed";
55         public static var NEW_CHANGESET:String = "new_changeset";
56         public static var NEW_CHANGESET_ERROR:String = "new_changeset_error";
57         public static var NEW_NODE:String = "new_node";
58         public static var NEW_WAY:String = "new_way";
59         public static var NEW_RELATION:String = "new_relation";
60         public static var NEW_POI:String = "new_poi";
61         public static var TAG_CHANGE:String = "tag_change";
62
63         // store the data we download
64         private var negativeID:Number = -1;
65         private var nodes:Object = {};
66         private var ways:Object = {};
67         private var relations:Object = {};
68         private var pois:Array = [];
69         private var changeset:Changeset = null;
70
71         protected function get nextNegative():Number {
72             return negativeID--;
73         }
74
75         protected function setNode(node:Node):void {
76             nodes[node.id] = node;
77             dispatchEvent(new EntityEvent(NEW_NODE, node));
78         }
79
80         protected function setWay(way:Way):void {
81             ways[way.id] = way;
82             dispatchEvent(new EntityEvent(NEW_WAY, way));
83         }
84
85         protected function setRelation(relation:Relation):void {
86             relations[relation.id] = relation;
87             dispatchEvent(new EntityEvent(NEW_RELATION, relation));
88         }
89
90         protected function registerPOI(node:Node):void {
91             if ( pois.indexOf(node) < 0 ) {
92                 pois.push(node);
93                 dispatchEvent(new EntityEvent(NEW_POI, node));
94             }
95         }
96
97         protected function unregisterPOI(node:Node):void {
98             var index:uint = pois.indexOf(node);
99             if ( index >= 0 ) {
100                 pois.splice(index,1);
101             }
102         }
103
104         protected function setActiveChangeset(changeset:Changeset):void {
105             this.changeset = changeset;
106             dispatchEvent(new EntityEvent(NEW_CHANGESET, changeset));
107         }
108         
109         public function getNode(id:Number):Node {
110             return nodes[id];
111         }
112
113         public function getWay(id:Number):Way {
114             return ways[id];
115         }
116
117         public function getRelation(id:Number):Relation {
118             return relations[id];
119         }
120
121         public function createNode(tags:Object, lat:Number, lon:Number):Node {
122             var node:Node = new Node(nextNegative, 0, tags, lat, lon);
123             setNode(node);
124             return node;
125         }
126
127         public function createWay(tags:Object, nodes:Array):Way {
128             var way:Way = new Way(nextNegative, 0, tags, nodes.concat());
129             setWay(way);
130             return way;
131         }
132
133         public function createRelation(tags:Object, members:Array):Relation {
134             var relation:Relation = new Relation(nextNegative, 0, tags, members.concat());
135             setRelation(relation);
136             return relation;
137         }
138
139         public function getAllNodeIDs():Array {
140             var list:Array = [];
141             for each (var node:Node in nodes)
142                 list.push(node.id);
143             return list;
144         }
145
146         public function getAllWayIDs():Array {
147             var list:Array = [];
148             for each (var way:Way in ways)
149                 list.push(way.id);
150             return list;
151         }
152
153         public function getAllRelationIDs():Array {
154             var list:Array = [];
155             for each (var relation:Relation in relations)
156                 list.push(relation.id);
157             return list;
158         }
159
160         public function getActiveChangeset():Changeset {
161             return changeset;
162         }
163         
164         // these are functions that the Connection implementation is expected to
165         // provide. This class has some generic helpers for the implementation.
166                 public function loadBbox(left:Number, right:Number,
167                                                                 top:Number, bottom:Number):void {
168             }
169             
170             public function setAppID(id:Object):void {}
171             public function setAuthToken(id:Object):void {}
172             public function createChangeset(tags:Object):void {}
173             public function uploadChanges():void {}
174     }
175
176 }
177