refactor the potlatch 2 code to look more MVC wrt the API parts
[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 CONNECTION_TYPE:String = "AMF";
11         private static var connectionInstance:Connection = null;
12
13         protected static var policyURL:String = "http://127.0.0.1:3000/api/crossdomain.xml";
14         protected static var apiBaseURL:String = "http://127.0.0.1:3000/api/0.6/";
15
16         public static function getConnection():Connection {
17             if ( connectionInstance == null ) {
18                 if ( CONNECTION_TYPE == "XML" )
19                     connectionInstance = new XMLConnection();
20                 else
21                     connectionInstance = new AMFConnection();
22             }
23             return connectionInstance;
24         }
25
26                 public function getEnvironment(responder:Responder):void {}
27
28         // connection events
29         public static var NEW_NODE:String = "new_node";
30         public static var NEW_WAY:String = "new_way";
31         public static var NEW_RELATION:String = "new_relation";
32         public static var NEW_POI:String = "new_poi";
33
34         // store the data we download
35         private var negativeID:Number = -1;
36         private var nodes:Object = {};
37         private var ways:Object = {};
38         private var relations:Object = {};
39         private var pois:Array = [];
40
41         protected function get nextNegative():Number {
42             return negativeID--;
43         }
44
45         protected function setNode(node:Node):void {
46             nodes[node.id] = node;
47             dispatchEvent(new EntityEvent(NEW_NODE, node));
48         }
49
50         protected function setWay(way:Way):void {
51             ways[way.id] = way;
52             dispatchEvent(new EntityEvent(NEW_WAY, way));
53         }
54
55         protected function setRelation(relation:Relation):void {
56             relations[relation.id] = relation;
57             dispatchEvent(new EntityEvent(NEW_RELATION, relation));
58         }
59
60         protected function registerPOI(node:Node):void {
61             if ( pois.indexOf(node) < 0 ) {
62                 pois.push(node);
63                 dispatchEvent(new EntityEvent(NEW_POI, node));
64             }
65         }
66
67         protected function unregisterPOI(node:Node):void {
68             var index:uint = pois.indexOf(node)
69             if ( index >= 0 ) {
70                 pois.splice(index,1);
71             }
72         }
73
74         public function getNode(id:Number):Node {
75             return nodes[id];
76         }
77
78         public function getWay(id:Number):Way {
79             return ways[id];
80         }
81
82         public function getRelation(id:Number):Relation {
83             return relations[id];
84         }
85
86         public function createNode(tags:Object, lat:Number, lon:Number):Node {
87             var node:Node = new Node(nextNegative, 0, tags, lat, lon);
88             setNode(node);
89             return node;
90         }
91
92         public function createWay(tags:Object, nodes:Array):Way {
93             var way:Way = new Way(nextNegative, 0, tags, nodes.concat());
94             setWay(way);
95             return way;
96         }
97
98         public function createRelation(tags:Object, members:Array):Relation {
99             var relation:Relation = new Relation(nextNegative, 0, tags, members.concat());
100             setRelation(relation);
101             return relation;
102         }
103
104         public function getAllNodeIDs():Array {
105             var list:Array = [];
106             for each (var node:Node in nodes)
107                 list.push(node.id);
108             return list;
109         }
110
111         public function getAllWayIDs():Array {
112             var list:Array = [];
113             for each (var way:Way in ways)
114                 list.push(way.id);
115             return list;
116         }
117
118         public function getAllRelationIDs():Array {
119             var list:Array = [];
120             for each (var relation:Relation in relations)
121                 list.push(relation.id);
122             return list;
123         }
124
125         // these are functions that the Connection implementation is expected to
126         // provide. This class has some generic helpers for the implementation.
127                 public function loadBbox(left:Number, right:Number,
128                                                                 top:Number, bottom:Number):void {
129             }
130     }
131
132 }
133
134