Update bug when it has been closed, using the closure approach
[potlatch2.git] / net / systemeD / potlatch2 / BugLayer.as
1 package net.systemeD.potlatch2 {
2
3     import net.systemeD.halcyon.VectorLayer;
4     import net.systemeD.halcyon.Map;
5     import net.systemeD.halcyon.connection.*;
6     import net.systemeD.halcyon.connection.actions.*;
7     import flash.net.*;
8     import flash.events.*;
9     import com.adobe.serialization.json.JSON;
10
11     public class BugLayer extends VectorLayer {
12
13         private var baseUrl:String;
14         private var apiKey:String;
15
16         // as strings, since that's how they are in tags and http calls
17         private var BUG_STATUS_OPEN:String = "1";
18         private var BUG_STATUS_FIXED:String = "2";
19         private var BUG_STATUS_INVALID:String = "3"; // or 'non-reproduceable'
20         private static const status:Array = ["", "open", "fixed", "invalid"];
21
22         public function BugLayer(n:String, map:Map, s:String, baseUrl:String, apiKey:String) {
23             this.baseUrl = baseUrl;
24             this.apiKey = apiKey;
25             super(n,map,s);
26         }
27
28         public function closeBug(m:Marker):void {
29             var id:String = m.getTag('bug_id');
30             var status:String = BUG_STATUS_FIXED;
31             var comment:String = "NoComment";
32             var nickname:String = "NoName";
33
34             //TODO urlencode stuff
35             var urlReq:URLRequest = new URLRequest(baseUrl+"changeBugStatus?id="+id+"&status="+status+"&comment="+comment+"&nickname="+nickname+"&key="+apiKey);
36             urlReq.method = "POST";
37             urlReq.data = '    '; // dear Adobe, this is nuts, kthxbye (you can't POST with an empty payload)
38             var loader:URLLoader = new URLLoader();
39             loader.load(urlReq);
40             loader.addEventListener(Event.COMPLETE, function(e:Event):void { bugClosed(e, m); } );
41         }
42
43         private function bugClosed(event:Event, marker:Marker):void {
44             var action:UndoableEntityAction = new SetTagAction(marker, "status", status[int(BUG_STATUS_FIXED)]);
45             action.doAction(); // just do it, don't add to undo stack
46         }
47
48         public override function loadBbox(left:Number, right:Number,
49                                 top:Number, bottom:Number):void {
50             var loader:URLLoader = new URLLoader();
51             loader.load(new URLRequest(baseUrl+"getBugs?bbox="+map.edge_l+","+map.edge_b+","+map.edge_r+","+map.edge_t+"&key="+apiKey));
52             loader.addEventListener(Event.COMPLETE, parseJSON);
53         }
54
55         private function parseJSON(event:Event):void {
56             var result:String = String(event.target.data);
57             if (result) { // api returns 204 no content for no bugs, and the JSON parser treats '' as an error
58               var featureCollection:Object = JSON.decode(result);
59
60               for each (var feature:Object in featureCollection.features) {
61                 // geoJSON spec is x,y,z i.e. lon, lat, ele
62                 var lon:Number = feature.geometry.coordinates[0];
63                 var lat:Number = feature.geometry.coordinates[1];
64                 var tags:Object = {};
65                 tags["name"] = String(feature.properties.description).substr(0,10)+'...';
66                 tags["description"] = feature.properties.description;
67                 tags["bug_id"] = feature.id;
68                 tags["nickname"] = feature.properties.nickname;
69                 tags["type"] = feature.properties.type;
70                 tags["date_created"] = feature.properties.date_created;
71                 tags["date_updated"] = feature.properties.date_updated;
72                 tags["source"] = feature.properties.source;
73                 tags["status"] = status[int(feature.properties.status)];
74                 var marker:Marker = createMarker(tags, lat, lon);
75               }
76               paint.updateEntityUIs(getObjectsByBbox(map.edge_l,map.edge_r,map.edge_t,map.edge_b), true, false);
77             }
78         }
79
80     }
81 }