Request, and handle, errors in XML format when loading map data
[potlatch2.git] / net / systemeD / halcyon / connection / XMLConnection.as
index e0fc6a1..6a22bf4 100644 (file)
@@ -1,7 +1,8 @@
 package net.systemeD.halcyon.connection {
 
     import flash.events.*;
-
+       import mx.rpc.http.HTTPService;
+       import mx.rpc.events.*;
        import flash.system.Security;
        import flash.net.*;
     import org.iotashan.oauth.*;
@@ -15,8 +16,6 @@ package net.systemeD.halcyon.connection {
     */
        public class XMLConnection extends XMLBaseConnection {
 
-        //public var readConnection:NetConnection;
-
                public function XMLConnection() {
 
                        if (Connection.policyURL!='')
@@ -29,6 +28,15 @@ package net.systemeD.halcyon.connection {
                
                override public function loadBbox(left:Number,right:Number,
                                                                top:Number,bottom:Number):void {
+            purgeIfFull(left,right,top,bottom);
+            if (isBboxLoaded(left,right,top,bottom)) return;
+
+            // enlarge bbox by 20% on each edge
+            var xmargin:Number=(right-left)/5;
+            var ymargin:Number=(top-bottom)/5;
+            left-=xmargin; right+=xmargin;
+            bottom-=ymargin; top+=ymargin;
+
             var mapVars:URLVariables = new URLVariables();
             mapVars.bbox= left+","+bottom+","+right+","+top;
 
@@ -49,12 +57,14 @@ package net.systemeD.halcyon.connection {
                        mapLoader.addEventListener(Event.COMPLETE, loadedMap);
                        mapLoader.addEventListener(IOErrorEvent.IO_ERROR, errorOnMapLoad);
                        mapLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, mapLoadStatus);
+            request.requestHeaders.push(new URLRequestHeader("X-Error-Format", "XML"));
                        mapLoader.load(request);
                        dispatchEvent(new Event(LOAD_STARTED));
                }
 
         private function errorOnMapLoad(event:Event):void {
                        dispatchEvent(new MapEvent(MapEvent.ERROR, { message: "Couldn't load the map" } ));
+                       dispatchEvent(new Event(LOAD_COMPLETED));
         }
         private function mapLoadStatus(event:HTTPStatusEvent):void {
             trace("loading map status = "+event.status);
@@ -170,7 +180,8 @@ package net.systemeD.halcyon.connection {
             urlReq.method = "POST";
                        if (xml) { urlReq.data = xml.toXMLString(); } else { urlReq.data = true; }
             urlReq.contentType = "application/xml";
-            urlReq.requestHeaders = new Array(new URLRequestHeader("X_HTTP_METHOD_OVERRIDE", "PUT"));
+            urlReq.requestHeaders = [ new URLRequestHeader("X_HTTP_METHOD_OVERRIDE", "PUT"), 
+                                                 new URLRequestHeader("X-Error-Format", "XML") ];
             var loader:URLLoader = new URLLoader();
             loader.addEventListener(Event.COMPLETE, onComplete);
             loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
@@ -206,27 +217,26 @@ package net.systemeD.halcyon.connection {
 
             // now actually upload them
             // make an OAuth query
-
             var url:String = Connection.apiBaseURL+"changeset/" + changeset.id + "/upload";
 
             // build the actual request
-            var urlReq:URLRequest = new URLRequest(signedOAuthURL(url, "POST"));
-            urlReq.method = "POST";
-            urlReq.data = upload.toXMLString();
-            urlReq.contentType = "text/xml";
-            urlReq.requestHeaders = [new URLRequestHeader("X-Error-Format","xml")];
-            var loader:URLLoader = new URLLoader();
-            loader.dataFormat = URLLoaderDataFormat.BINARY;
-            loader.addEventListener(Event.COMPLETE, diffUploadComplete);
-            loader.addEventListener(IOErrorEvent.IO_ERROR, function(event:IOErrorEvent):void { trace(urlReq.data); diffUploadIOError(event); } );
-            loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, recordStatus);
-               loader.load(urlReq);
+                       var serv:HTTPService=new HTTPService();
+                       serv.method="POST";
+                       serv.url=signedOAuthURL(url, "POST");
+                       serv.contentType = "text/xml";
+                       serv.headers={'X-Error-Format':'xml'};
+                       serv.request=" ";
+                       serv.resultFormat="e4x";
+                       serv.requestTimeout=0;
+                       serv.addEventListener(ResultEvent.RESULT, diffUploadComplete);
+                       serv.addEventListener(FaultEvent.FAULT, diffUploadIOError);
+                       serv.send(upload);
                
                dispatchEvent(new Event(SAVE_STARTED));
         }
 
-        private function diffUploadComplete(event:Event):void {
-                       var results:XML = new XML(URLLoader(event.target).data);
+        private function diffUploadComplete(event:ResultEvent):void {
+                       var results:XML = XML(event.result);
 
                        // was it an error document?
                        if (results.name().localName=='osmError') {
@@ -263,10 +273,11 @@ package net.systemeD.halcyon.connection {
             MainUndoStack.getGlobalStack().breakUndo(); // so, for now, break the undo stack
         }
 
-        private function diffUploadIOError(event:IOErrorEvent):void {
-                       dispatchEvent(new MapEvent(MapEvent.ERROR, { message: "Couldn't upload data: "+httpStatus+" "+event.text } ));
-               dispatchEvent(new SaveCompleteEvent(SAVE_COMPLETED, false));
-        }
+               private function diffUploadIOError(event:FaultEvent):void {
+                       trace(event.fault);
+                       dispatchEvent(new MapEvent(MapEvent.ERROR, { message: "Couldn't upload data: "+event.fault.faultString } ));
+                       dispatchEvent(new SaveCompleteEvent(SAVE_COMPLETED, false));
+               }
 
                private function diffUploadAPIError(status:String, message:String):void {
                        var matches:Array;