remove mx.controls.Alert dependency from halcyon_viewer - Flex should only be used...
[potlatch2.git] / net / systemeD / halcyon / connection / XMLConnection.as
1 package net.systemeD.halcyon.connection {
2
3     import flash.events.*;
4
5         import flash.system.Security;
6         import flash.net.*;
7     import org.iotashan.oauth.*;
8
9         import net.systemeD.halcyon.MapEvent;
10
11         public class XMLConnection extends XMLBaseConnection {
12
13         //public var readConnection:NetConnection;
14
15                 public function XMLConnection() {
16
17                         if (Connection.policyURL!='')
18                 Security.loadPolicyFile(Connection.policyURL);
19             var oauthPolicy:String = Connection.getParam("oauth_policy", "");
20             if ( oauthPolicy != "" ) {
21                 Security.loadPolicyFile(oauthPolicy);
22             }
23                 }
24                 
25                 override public function loadBbox(left:Number,right:Number,
26                                                                 top:Number,bottom:Number):void {
27             var mapVars:URLVariables = new URLVariables();
28             mapVars.bbox= left+","+bottom+","+right+","+top;
29
30             var mapRequest:URLRequest = new URLRequest(Connection.apiBaseURL+"map");
31             mapRequest.data = mapVars;
32
33             var mapLoader:URLLoader = new URLLoader();
34             mapLoader.addEventListener(Event.COMPLETE, loadedMap);
35             mapLoader.addEventListener(IOErrorEvent.IO_ERROR, errorOnMapLoad);
36             mapLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, mapLoadStatus);
37             mapLoader.load(mapRequest);
38             dispatchEvent(new Event(LOAD_STARTED));
39                 }
40
41         private function errorOnMapLoad(event:Event):void {
42                         dispatchEvent(new MapEvent(MapEvent.ERROR, { message: "Couldn't load the map" } ));
43         }
44         private function mapLoadStatus(event:HTTPStatusEvent):void {
45             trace("loading map status = "+event.status);
46         }
47
48         protected var appID:OAuthConsumer;
49         protected var authToken:OAuthToken;
50         
51             override public function setAppID(id:Object):void {
52                 appID = OAuthConsumer(id);
53             }
54             
55             override public function setAuthToken(id:Object):void {
56                 authToken = OAuthToken(id);
57             }
58
59         private var httpStatus:int = 0;
60         
61         private function recordStatus(event:HTTPStatusEvent):void {
62             httpStatus = event.status;
63         }
64         
65         private var lastUploadedChangesetTags:Object;
66         
67         override public function createChangeset(tags:Object):void {
68             lastUploadedChangesetTags = tags;
69             
70                 var changesetXML:XML = <osm version="0.6"><changeset /></osm>;
71                 var changeset:XML = <changeset />;
72                 for (var tagKey:Object in tags) {
73               var tagXML:XML = <tag/>;
74               tagXML.@k = tagKey;
75               tagXML.@v = tags[tagKey];
76               changesetXML.changeset.appendChild(tagXML);
77             }        
78
79             // make an OAuth query
80             var sig:IOAuthSignatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
81             var url:String = Connection.apiBaseURL+"changeset/create";
82             //var params:Object = { _method: "PUT" };
83             var oauthRequest:OAuthRequest = new OAuthRequest("PUT", url, null, appID, authToken);
84             var urlStr:Object = oauthRequest.buildRequest(sig, OAuthRequest.RESULT_TYPE_URL_STRING)
85
86             // build the actual request
87             var urlReq:URLRequest = new URLRequest(String(urlStr));
88             urlReq.method = "POST";
89             urlReq.data = changesetXML.toXMLString();
90             urlReq.contentType = "application/xml";
91             urlReq.requestHeaders = new Array(new URLRequestHeader("X_HTTP_METHOD_OVERRIDE", "PUT"));
92             var loader:URLLoader = new URLLoader();
93             loader.addEventListener(Event.COMPLETE, changesetCreateComplete);
94             loader.addEventListener(IOErrorEvent.IO_ERROR, changesetCreateError);
95             loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, recordStatus);
96                 loader.load(urlReq);
97             }
98
99         private function changesetCreateComplete(event:Event):void {
100             // response should be a Number changeset id
101             var id:Number = Number(URLLoader(event.target).data);
102             
103             // which means we now have a new changeset!
104             setActiveChangeset(new Changeset(id, lastUploadedChangesetTags));
105         }
106
107         private function changesetCreateError(event:IOErrorEvent):void {
108             dispatchEvent(new Event(NEW_CHANGESET_ERROR));
109         }
110         
111         override public function uploadChanges():void {
112             var changeset:Changeset = getActiveChangeset();
113             var upload:XML = <osmChange version="0.6"/>
114             upload.appendChild(addCreated(changeset, getAllNodeIDs, getNode, serialiseNode));
115             upload.appendChild(addCreated(changeset, getAllWayIDs, getWay, serialiseWay));
116             upload.appendChild(addCreated(changeset, getAllRelationIDs, getRelation, serialiseRelation));
117             upload.appendChild(addModified(changeset, getAllNodeIDs, getNode, serialiseNode));
118             upload.appendChild(addModified(changeset, getAllWayIDs, getWay, serialiseWay));
119             upload.appendChild(addModified(changeset, getAllRelationIDs, getRelation, serialiseRelation));
120             upload.appendChild(addDeleted(changeset, getAllRelationIDs, getRelation, serialiseEntityRoot));
121             upload.appendChild(addDeleted(changeset, getAllWayIDs, getWay, serialiseEntityRoot));
122             upload.appendChild(addDeleted(changeset, getAllNodeIDs, getNode, serialiseEntityRoot));
123
124             // now actually upload them
125             // make an OAuth query
126             var sig:IOAuthSignatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
127             var url:String = Connection.apiBaseURL+"changeset/" + changeset.id + "/upload";
128             var oauthRequest:OAuthRequest = new OAuthRequest("POST", url, null, appID, authToken);
129             var urlStr:Object = oauthRequest.buildRequest(sig, OAuthRequest.RESULT_TYPE_URL_STRING)
130
131             // build the actual request
132             var urlReq:URLRequest = new URLRequest(String(urlStr));
133             urlReq.method = "POST";
134             urlReq.data = upload.toXMLString();
135             urlReq.contentType = "text/xml";
136             var loader:URLLoader = new URLLoader();
137             loader.addEventListener(Event.COMPLETE, diffUploadComplete);
138             loader.addEventListener(IOErrorEvent.IO_ERROR, diffUploadError);
139             loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, recordStatus);
140                 loader.load(urlReq);
141                 
142                 dispatchEvent(new Event(SAVE_STARTED));
143         }
144
145         private function diffUploadComplete(event:Event):void {
146             // response should be XML describing the progress
147             var results:XML = new XML((URLLoader(event.target).data));
148             
149             for each( var update:XML in results.child("*") ) {
150                 var oldID:Number = Number(update.@old_id);
151                 var newID:Number = Number(update.@new_id);
152                 var version:uint = uint(update.@new_version);
153                 var type:String = update.name();
154
155                                 if (newID==0) {
156                                         // delete
157                         if ( type == "node" ) killNode(oldID);
158                         else if ( type == "way" ) killWay(oldID);
159                         else if ( type == "relation" ) killRelation(oldID);
160                                         
161                                 } else {
162                                         // create/update
163                         var entity:Entity;
164                         if ( type == "node" ) entity = getNode(oldID);
165                         else if ( type == "way" ) entity = getWay(oldID);
166                         else if ( type == "relation" ) entity = getRelation(oldID);
167                         entity.markClean(newID, version);
168                 
169                         if ( oldID != newID ) {
170                             if ( type == "node" ) renumberNode(oldID, entity as Node, false);
171                             else if ( type == "way" ) renumberWay(oldID, entity as Way, false);
172                             else if ( type == "relation" ) renumberRelation(oldID, entity as Relation, false);
173                         }
174                                 }
175             }
176
177             dispatchEvent(new SaveCompleteEvent(SAVE_COMPLETED, true));
178             markClean(); // marks the connection clean. Pressing undo from this point on leads to unexpected results
179             MainUndoStack.getGlobalStack().breakUndo(); // so, for now, break the undo stack
180         }
181
182         private function diffUploadError(event:IOErrorEvent):void {
183                         dispatchEvent(new MapEvent(MapEvent.ERROR, { message: "Couldn't upload data: "+httpStatus+" "+event.text } ));
184                 dispatchEvent(new SaveCompleteEvent(SAVE_COMPLETED, false));
185         }
186
187         private function addCreated(changeset:Changeset, getIDs:Function, get:Function, serialise:Function):XML {
188             var create:XML = <create version="0.6"/>
189             for each( var id:Number in getIDs() ) {
190                 var entity:Entity = get(id);
191                 if ( id >= 0 || entity.deleted )
192                     continue;
193                     
194                 var xml:XML = serialise(entity);
195                 xml.@changeset = changeset.id;
196                 create.appendChild(xml);
197             }
198             return create.hasComplexContent() ? create : <!-- blank create section -->;
199         }
200
201                 private function addDeleted(changeset:Changeset, getIDs:Function, get:Function, serialise:Function):XML {
202             var del:XML = <delete version="0.6"/>
203             for each( var id:Number in getIDs() ) {
204                 var entity:Entity = get(id);
205                 // creates are already included
206                 if ( id < 0 || !entity.deleted )
207                     continue;
208                     
209                 var xml:XML = serialise(entity);
210                 xml.@changeset = changeset.id;
211                 del.appendChild(xml);
212             }
213             return del.hasComplexContent() ? del : <!-- blank delete section -->;
214                 }
215
216         private function addModified(changeset:Changeset, getIDs:Function, get:Function, serialise:Function):XML {
217             var modify:XML = <modify version="0.6"/>
218             for each( var id:Number in getIDs() ) {
219                 var entity:Entity = get(id);
220                 // creates and deletes are already included
221                 if ( id < 0 || entity.deleted || !entity.isDirty )
222                     continue;
223                     
224                 var xml:XML = serialise(entity);
225                 xml.@changeset = changeset.id;
226                 modify.appendChild(xml);
227             }
228             return modify.hasComplexContent() ? modify : <!-- blank modify section -->;
229         }
230
231         private function serialiseNode(node:Node):XML {
232             var xml:XML = serialiseEntityRoot(node); //<node/>
233             serialiseEntityTags(node, xml);
234             xml.@lat = node.lat;
235             xml.@lon = node.lon;
236             return xml;
237         }
238
239         private function serialiseWay(way:Way):XML {
240             var xml:XML = serialiseEntityRoot(way); //<node/>
241             serialiseEntityTags(way, xml);
242             for ( var i:uint = 0; i < way.length; i++ ) {
243                 var nd:XML = <nd/>
244                 nd.@ref = way.getNode(i).id;
245                 xml.appendChild(nd);
246             }
247             return xml;
248         }
249
250         private function serialiseRelation(relation:Relation):XML {
251             var xml:XML = serialiseEntityRoot(relation); //<node/>
252             serialiseEntityTags(relation, xml);
253             for ( var i:uint = 0; i < relation.length; i++ ) {
254                 var relMember:RelationMember = relation.getMember(i);
255                 var member:XML = <member/>
256                 member.@ref = relMember.entity.id;
257                 member.@type = relMember.entity.getType();
258                 member.@role = relMember.role;
259                 xml.appendChild(member);
260             }
261             return xml;
262         }
263         
264                 private function serialiseEntityRoot(entity:Object):XML {
265                         var xml:XML;
266                         if      (entity is Way     ) { xml = <way/> }
267                         else if (entity is Node    ) { xml = <node/> }
268                         else if (entity is Relation) { xml = <relation/> }
269                         xml.@id = entity.id;
270                         xml.@version = entity.version;
271                         return xml;
272                 }
273
274         private function serialiseEntityTags(entity:Entity, xml:XML):void {
275             xml.@id = entity.id;
276             xml.@version = entity.version;
277             for each( var tag:Tag in entity.getTagArray() ) {
278               var tagXML:XML = <tag/>
279               tagXML.@k = tag.key;
280               tagXML.@v = tag.value;
281               xml.appendChild(tagXML);
282             }
283         }
284
285         }
286 }