Merge branch 'master' into snapshotserver
[potlatch2.git] / net / systemeD / potlatch2 / utils / SnapshotConnection.as
1 package net.systemeD.potlatch2.utils {
2
3     import net.systemeD.halcyon.connection.*;
4     import flash.events.Event;
5     import flash.net.*;
6
7     public class SnapshotConnection extends XMLConnection {
8
9         public function SnapshotConnection(cname:String,api:String,policy:String,initparams:Object=null) {
10             super(cname,api,policy,initparams);
11             inlineStatus = true;
12         }
13
14         // As it stands, the following two functions could be refactored further.
15
16         /** Post a status update call to the server and update entity.status if successful */
17         public function markComplete(entity:Entity):void {
18             var urlReq:URLRequest;
19
20             if (entity is Node) {
21                 var node:Node = Node(entity);
22                 if (node == getNode(node.id)) { // confirm it's from this connection
23                     makeRequest(entity, 'complete');
24                 }
25
26             } else if (entity is Way) {
27                 var way:Way = Way(entity);
28                 if (way == getWay(way.id)) { // confirm it's from this connection
29                     makeRequest(entity, 'complete');
30                 }
31             }
32         }
33
34         /** Send a "complete" call to the server and update entity.status if successful */
35         public function markNotComplete(entity:Entity):void {
36             var urlReq:URLRequest;
37
38             if (entity is Node) {
39                 var node:Node = Node(entity);
40                 if (node == getNode(node.id)) { // confirm it's from this connection
41                     makeRequest(entity, 'incomplete');
42                 }
43
44             } else if (entity is Way) {
45                 var way:Way = Way(entity);
46                 if (way == getWay(way.id)) { // confirm it's from this connection
47                     makeRequest(entity, 'incomplete');
48                 }
49             }
50         }
51
52         private function makeRequest(entity:Entity, status:String):void {
53             var urlReq:URLRequest = new URLRequest(apiBaseURL+entity.getType()+"/"+entity.id+"/status");
54             urlReq.method = "POST";
55             urlReq.data = status;
56             var loader:URLLoader = new URLLoader();
57             loader.addEventListener(Event.COMPLETE, function(e:Event):void { updateStatus(entity, status) });
58             loader.load(urlReq);
59         }
60
61         private function updateStatus(e:Entity, s:String):void {
62             e.setStatus(s);
63         }
64
65     }
66 }