real delete support
[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.Globals;
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             var oauthPolicy:String = Connection.getParam("oauth_policy", "");
20             if ( oauthPolicy != "" ) {
21                 trace(oauthPolicy);
22                 Security.loadPolicyFile(oauthPolicy);
23             }
24                 }
25                 
26                 override public function loadBbox(left:Number,right:Number,
27                                                                 top:Number,bottom:Number):void {
28             var mapVars:URLVariables = new URLVariables();
29             mapVars.bbox= left+","+bottom+","+right+","+top;
30
31             var mapRequest:URLRequest = new URLRequest(Connection.apiBaseURL+"map");
32             mapRequest.data = mapVars;
33
34             var mapLoader:URLLoader = new URLLoader();
35             mapLoader.addEventListener(Event.COMPLETE, loadedMap);
36             mapLoader.addEventListener(IOErrorEvent.IO_ERROR, errorOnMapLoad);
37             mapLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, mapLoadStatus);
38             mapLoader.load(mapRequest);
39             dispatchEvent(new Event(LOAD_STARTED));
40                 }
41
42         private function parseTags(tagElements:XMLList):Object {
43             var tags:Object = {};
44             for each (var tagEl:XML in tagElements)
45                 tags[tagEl.@k] = tagEl.@v;
46             return tags;
47         }
48
49         private function errorOnMapLoad(event:Event):void {
50             trace("error loading map");
51         }
52         private function mapLoadStatus(event:HTTPStatusEvent):void {
53             trace("loading map status = "+event.status);
54         }
55         
56         private function loadedMap(event:Event):void {
57             dispatchEvent(new Event(LOAD_COMPLETED));
58
59             var map:XML = new XML(URLLoader(event.target).data);
60             var id:Number;
61             var version:uint;
62             var tags:Object;
63
64             for each(var nodeData:XML in map.node) {
65                 id = Number(nodeData.@id);
66                 version = uint(nodeData.@version);
67
68                 var node:Node = getNode(id);
69                 if ( node == null || !node.loaded ) {
70                     var lat:Number = Number(nodeData.@lat);
71                     var lon:Number = Number(nodeData.@lon);
72                     tags = parseTags(nodeData.tag);
73                     if ( node == null )
74                         setNode(new Node(id, version, tags, true, lat, lon),false);
75                     else {
76                         node.update(version, tags, true, lat, lon);
77                         sendEvent(new EntityEvent(NEW_NODE, node), false);
78                     }
79                 }
80             }
81
82             for each(var data:XML in map.way) {
83                 id = Number(data.@id);
84                 version = uint(data.@version);
85
86                 var way:Way = getWay(id);
87                 if ( way == null || !way.loaded ) {
88                     var nodes:Array = [];
89                     for each(var nd:XML in data.nd)
90                         nodes.push(getNode(Number(nd.@ref)));
91                     tags = parseTags(data.tag);
92                     if ( way == null )
93                         setWay(new Way(id, version, tags, true, nodes),false);
94                     else {
95                         way.update(version, tags, true, nodes);
96                         sendEvent(new EntityEvent(NEW_WAY, way), false);
97                     }
98                 }
99             }
100             
101             for each(var relData:XML in map.relation) {
102                 id = Number(relData.@id);
103                 version = uint(relData.@version);
104                 
105                 var rel:Relation = getRelation(id);
106                 if ( rel == null || !rel.loaded ) {
107                     tags = parseTags(relData.tag);
108                     var members:Array = [];
109                     for each(var memberXML:XML in relData.member) {
110                         var type:String = memberXML.@type.toLowerCase();
111                         var role:String = memberXML.@role;
112                         var memberID:Number = Number(memberXML.@ref);
113                         var member:Entity = null;
114                         if ( type == "node" ) {
115                             member = getNode(memberID);
116                             if ( member == null ) {
117                                 member = new Node(memberID,0,{},false,0,0);
118                                 setNode(Node(member),true);
119                             }
120                         } else if ( type == "way" ) {
121                             member = getWay(memberID);
122                             if (member == null) {
123                                 member = new Way(memberID,0,{},false,[]);
124                                 setWay(Way(member),true);
125                             }
126                         } else if ( type == "relation" ) {
127                             member = getRelation(memberID);
128                             if (member == null) {
129                                 member = new Relation(memberID,0,{},false,[]);
130                                 setRelation(Relation(member),true);
131                             }
132                         }
133                         
134                         if ( member != null )
135                             members.push(new RelationMember(member, role));
136                     }
137                     
138                     if ( rel == null )
139                         setRelation(new Relation(id, version, tags, true, members), false);
140                     else {
141                         rel.update(version,tags,true,members);
142                         sendEvent(new EntityEvent(NEW_RELATION, rel), false);
143                     }
144                 }
145             }
146             
147             registerPOINodes();
148         }
149         
150         protected function registerPOINodes():void {
151             for each (var nodeID:Number in getAllNodeIDs()) {
152                 var node:Node = getNode(nodeID);
153                 if (!node.hasParentWays)
154                     registerPOI(node);
155             }
156         }
157
158         protected var appID:OAuthConsumer;
159         protected var authToken:OAuthToken;
160         
161             override public function setAppID(id:Object):void {
162                 appID = OAuthConsumer(id);
163             }
164             
165             override public function setAuthToken(id:Object):void {
166                 authToken = OAuthToken(id);
167             }
168
169         private var httpStatus:int = 0;
170         
171         private function recordStatus(event:HTTPStatusEvent):void {
172             httpStatus = event.status;
173         }
174         
175         private var lastUploadedChangesetTags:Object;
176         
177         override public function createChangeset(tags:Object):void {
178             lastUploadedChangesetTags = tags;
179             
180                 var changesetXML:XML = <osm version="0.6"><changeset /></osm>;
181                 var changeset:XML = <changeset />;
182                 for (var tagKey:Object in tags) {
183               var tagXML:XML = <tag/>;
184               tagXML.@k = tagKey;
185               tagXML.@v = tags[tagKey];
186               changesetXML.changeset.appendChild(tagXML);
187             }        
188
189             // make an OAuth query
190             var sig:IOAuthSignatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
191             var url:String = Connection.apiBaseURL+"changeset/create";
192             //var params:Object = { _method: "PUT" };
193             var oauthRequest:OAuthRequest = new OAuthRequest("PUT", url, null, appID, authToken);
194             var urlStr:Object = oauthRequest.buildRequest(sig, OAuthRequest.RESULT_TYPE_URL_STRING)
195
196             // build the actual request
197             var urlReq:URLRequest = new URLRequest(String(urlStr));
198             urlReq.method = "POST";
199             urlReq.data = changesetXML.toXMLString();
200             urlReq.contentType = "application/xml";
201             urlReq.requestHeaders = new Array(new URLRequestHeader("X_HTTP_METHOD_OVERRIDE", "PUT"));
202             var loader:URLLoader = new URLLoader();
203             loader.addEventListener(Event.COMPLETE, changesetCreateComplete);
204             loader.addEventListener(IOErrorEvent.IO_ERROR, changesetCreateError);
205             loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, recordStatus);
206                 loader.load(urlReq);
207             }
208
209         private function changesetCreateComplete(event:Event):void {
210             // response should be a Number changeset id
211             var id:Number = Number(URLLoader(event.target).data);
212             
213             // which means we now have a new changeset!
214             setActiveChangeset(new Changeset(id, lastUploadedChangesetTags));
215         }
216
217         private function changesetCreateError(event:IOErrorEvent):void {
218             dispatchEvent(new Event(NEW_CHANGESET_ERROR));
219         }
220         
221         override public function uploadChanges():void {
222             var changeset:Changeset = getActiveChangeset();
223             var upload:XML = <osmChange version="0.6"/>
224             upload.appendChild(addCreated(changeset, getAllNodeIDs, getNode, serialiseNode));
225             upload.appendChild(addCreated(changeset, getAllWayIDs, getWay, serialiseWay));
226             upload.appendChild(addCreated(changeset, getAllRelationIDs, getRelation, serialiseRelation));
227             upload.appendChild(addDeleted(changeset, getAllRelationIDs, getRelation, serialiseEntityRoot));
228             upload.appendChild(addDeleted(changeset, getAllWayIDs, getWay, serialiseEntityRoot));
229             upload.appendChild(addDeleted(changeset, getAllNodeIDs, getNode, serialiseEntityRoot));
230             upload.appendChild(addModified(changeset, getAllNodeIDs, getNode, serialiseNode));
231             upload.appendChild(addModified(changeset, getAllWayIDs, getWay, serialiseWay));
232             upload.appendChild(addModified(changeset, getAllRelationIDs, getRelation, serialiseRelation));
233
234             // *** TODO *** deleting items
235             
236             // now actually upload them
237             // make an OAuth query
238             var sig:IOAuthSignatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
239             var url:String = Connection.apiBaseURL+"changeset/" + changeset.id + "/upload";
240             var oauthRequest:OAuthRequest = new OAuthRequest("POST", url, null, appID, authToken);
241             var urlStr:Object = oauthRequest.buildRequest(sig, OAuthRequest.RESULT_TYPE_URL_STRING)
242
243             // build the actual request
244             var urlReq:URLRequest = new URLRequest(String(urlStr));
245             urlReq.method = "POST";
246             urlReq.data = upload.toXMLString();
247             urlReq.contentType = "text/xml";
248             var loader:URLLoader = new URLLoader();
249             loader.addEventListener(Event.COMPLETE, diffUploadComplete);
250             loader.addEventListener(IOErrorEvent.IO_ERROR, diffUploadError);
251             loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, recordStatus);
252                 loader.load(urlReq);
253                 
254                 dispatchEvent(new Event(SAVE_STARTED));
255         }
256
257         private function diffUploadComplete(event:Event):void {
258             // response should be XML describing the progress
259             var results:XML = new XML((URLLoader(event.target).data));
260             
261             for each( var update:XML in results.child("*") ) {
262                 var oldID:Number = Number(update.@old_id);
263                 var newID:Number = Number(update.@new_id);
264                 var version:uint = uint(update.@new_version);
265                 var type:String = update.name();
266                 
267                 var entity:Entity;
268                 if ( type == "node" ) entity = getNode(oldID);
269                 else if ( type == "way" ) entity = getWay(oldID);
270                 else if ( type == "relation" ) entity = getRelation(oldID);
271                 entity.markClean(newID, version);
272                 
273                 if ( oldID != newID ) {
274                     if ( type == "node" ) renumberNode(oldID, entity as Node, false);
275                     else if ( type == "way" ) renumberWay(oldID, entity as Way, false);
276                     else if ( type == "relation" ) renumberRelation(oldID, entity as Relation, false);
277                 }
278                 // *** TODO *** handle deleting
279             }
280
281                 dispatchEvent(new SaveCompleteEvent(SAVE_COMPLETED, true));
282         }
283
284         private function diffUploadError(event:IOErrorEvent):void {
285             trace("error "+URLLoader(event.target).data + " "+httpStatus+ " " + event.text);
286
287                 dispatchEvent(new SaveCompleteEvent(SAVE_COMPLETED, false));
288         }
289
290         private function addCreated(changeset:Changeset, getIDs:Function, get:Function, serialise:Function):XML {
291             var create:XML = <create version="0.6"/>
292             for each( var id:Number in getIDs() ) {
293                 if ( id >= 0 )
294                     continue;
295                     
296                 var entity:Object = get(id);
297                 var xml:XML = serialise(entity);
298                 xml.@changeset = changeset.id;
299                 create.appendChild(xml);
300             }
301             return create.hasComplexContent() ? create : <!-- blank create section -->;
302         }
303
304                 private function addDeleted(changeset:Changeset, getIDs:Function, get:Function, serialise:Function):XML {
305             var del:XML = <delete version="0.6"/>
306             for each( var id:Number in getIDs() ) {
307                 var entity:Entity = get(id);
308                 // creates are already included
309                 if ( id < 0 || !entity.deleted )
310                     continue;
311                     
312                 var xml:XML = serialise(entity);
313                 xml.@changeset = changeset.id;
314                 del.appendChild(xml);
315             }
316             return del.hasComplexContent() ? del : <!-- blank delete section -->;
317                 }
318
319         private function addModified(changeset:Changeset, getIDs:Function, get:Function, serialise:Function):XML {
320             var modify:XML = <modify version="0.6"/>
321             for each( var id:Number in getIDs() ) {
322                 var entity:Entity = get(id);
323                 // creates and deletes are already included
324                 if ( id < 0 || entity.deleted || !entity.isDirty )
325                     continue;
326                     
327                 var xml:XML = serialise(entity);
328                 xml.@changeset = changeset.id;
329                 modify.appendChild(xml);
330             }
331             return modify.hasComplexContent() ? modify : <!-- blank modify section -->;
332         }
333
334         private function serialiseNode(node:Node):XML {
335             var xml:XML = serialiseEntityRoot(node); //<node/>
336             serialiseEntityTags(node, xml);
337             xml.@lat = node.lat;
338             xml.@lon = node.lon;
339             return xml;
340         }
341
342         private function serialiseWay(way:Way):XML {
343             var xml:XML = serialiseEntityRoot(way); //<node/>
344             serialiseEntityTags(way, xml);
345             for ( var i:uint = 0; i < way.length; i++ ) {
346                 var nd:XML = <nd/>
347                 nd.@ref = way.getNode(i).id;
348                 xml.appendChild(nd);
349             }
350             return xml;
351         }
352
353         private function serialiseRelation(relation:Relation):XML {
354             var xml:XML = serialiseEntityRoot(Relation); //<node/>
355             serialiseEntityTags(relation, xml);
356             for ( var i:uint = 0; i < relation.length; i++ ) {
357                 var relMember:RelationMember = relation.getMember(i);
358                 var member:XML = <member/>
359                 member.@ref = relMember.entity.id;
360                 member.@type = relMember.entity.getType();
361                 member.@role = relMember.role;
362                 xml.appendChild(member);
363             }
364             return xml;
365         }
366         
367                 private function serialiseEntityRoot(entity:Object):XML {
368                         var xml:XML;
369                         if      (entity is Way     ) { xml = <way/> }
370                         else if (entity is Node    ) { xml = <node/> }
371                         else if (entity is Relation) { xml = <relation/> }
372                         xml.@id = entity.id;
373                         xml.@version = entity.version;
374                         return xml;
375                 }
376
377         private function serialiseEntityTags(entity:Entity, xml:XML):void {
378             xml.@id = entity.id;
379             xml.@version = entity.version;
380             for each( var tag:Tag in entity.getTagArray() ) {
381               var tagXML:XML = <tag/>
382               tagXML.@k = tag.key;
383               tagXML.@v = tag.value;
384               xml.appendChild(tagXML);
385             }
386         }
387
388         }
389 }