refactor the potlatch 2 code to look more MVC wrt the API parts
[potlatch2.git] / net / systemeD / halcyon / connection / XMLConnection.as
1 package net.systemeD.halcyon.connection {
2
3     import flash.events.Event;
4     import flash.net.URLLoader;
5     import flash.net.URLRequest;
6
7         import flash.system.Security;
8         import flash.net.*;
9
10
11         public class XMLConnection extends Connection {
12
13         public var readConnection:NetConnection;
14
15                 public function XMLConnection() {
16
17                         if (Connection.policyURL!='')
18                 Security.loadPolicyFile(Connection.policyURL);
19
20                         readConnection=new NetConnection();
21                         readConnection.objectEncoding = flash.net.ObjectEncoding.AMF0;
22                         readConnection.connect(Connection.apiBaseURL+"amf/read");
23                         
24                 }
25
26                 override public function getEnvironment(responder:Responder):void {
27                         readConnection.call("getpresets",responder,"en");
28                 }
29                 
30                 override public function loadBbox(left:Number,right:Number,
31                                                                 top:Number,bottom:Number):void {
32             var mapVars:URLVariables = new URLVariables();
33             mapVars.bbox= left+","+bottom+","+right+","+top;
34
35             var mapRequest:URLRequest = new URLRequest(Connection.apiBaseURL+"map");
36             mapRequest.data = mapVars;
37
38             var mapLoader:URLLoader = new URLLoader();
39             mapLoader.addEventListener(Event.COMPLETE, loadedMap);
40             mapLoader.load(mapRequest);
41                 }
42
43         private function parseTags(tagElements:XMLList):Object {
44             var tags:Object = {};
45             for each (var tagEl:XML in tagElements)
46                 tags[tagEl.@k] = tagEl.@v;
47             return tags;
48         }
49
50         private function loadedMap(event:Event):void {
51             var map:XML = new XML(URLLoader(event.target).data);
52             var id:Number;
53             var version:uint;
54             var tags:Object;
55
56             for each(var nodeData:XML in map.node) {
57                 id = Number(nodeData.@id);
58                 version = uint(nodeData.@version);
59
60                 var node:Node = getNode(id);
61                 if ( node == null ) {
62                     var lat:Number = Number(nodeData.@lat);
63                     var lon:Number = Number(nodeData.@lon);
64                     tags = parseTags(nodeData.tag);
65                     setNode(new Node(id, version, tags, lat, lon));
66                 }
67             }
68
69             for each(var data:XML in map.way) {
70                 id = Number(data.@id);
71                 version = uint(data.@version);
72
73                 var way:Way = getWay(id);
74                 if ( way == null ) {
75                     var nodes:Array = [];
76                     for each(var nd:XML in data.nd)
77                         nodes.push(getNode(Number(nd.@ref)));
78                     tags = parseTags(data.tag);
79                     setWay(new Way(id, version, tags, nodes));
80                 }
81             }
82         }
83
84         }
85 }