Don't duplicate bugs. The complete reimplementation of Connection in VectorLayer...
[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         public static var BUG_STATUS_OPEN:String = "1";
18         public static var BUG_STATUS_FIXED:String = "2";
19         public static var BUG_STATUS_INVALID:String = "3"; // or 'non-reproduceable'
20         public 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, nickname:String = "NoName", comment:String = "No Comment", status:String = null):void {
29             var id:String = m.getTag('bug_id');
30             status ||= BUG_STATUS_FIXED;
31             var urlReq:URLRequest = new URLRequest(baseUrl+"changeBugStatus?id="+id+"&status="+status+"&comment="+encodeURIComponent(comment)+"&nickname="+encodeURIComponent(nickname)+"&key="+apiKey);
32             urlReq.method = "POST";
33             urlReq.data = '    '; // dear Adobe, this is nuts, kthxbye (you can't POST with an empty payload)
34             var loader:URLLoader = new URLLoader();
35             loader.load(urlReq);
36             loader.addEventListener(Event.COMPLETE, function(e:Event):void { bugClosed(e, m, status); } );
37         }
38
39         private function bugClosed(event:Event, marker:Marker, s:String):void {
40             var action:UndoableEntityAction = new SetTagAction(marker, "status", status[int(s)]);
41             action.doAction(); // just do it, don't add to undo stack
42         }
43
44         public override function loadBbox(left:Number, right:Number,
45                                 top:Number, bottom:Number):void {
46             var loader:URLLoader = new URLLoader();
47             loader.load(new URLRequest(baseUrl+"getBugs?bbox="+map.edge_l+","+map.edge_b+","+map.edge_r+","+map.edge_t+"&key="+apiKey));
48             loader.addEventListener(Event.COMPLETE, parseJSON);
49         }
50
51         private function parseJSON(event:Event):void {
52             var result:String = String(event.target.data);
53             if (result) { // api returns 204 no content for no bugs, and the JSON parser treats '' as an error
54               var featureCollection:Object = JSON.decode(result);
55
56               for each (var feature:Object in featureCollection.features) {
57                 // geoJSON spec is x,y,z i.e. lon, lat, ele
58                 var lon:Number = feature.geometry.coordinates[0];
59                 var lat:Number = feature.geometry.coordinates[1];
60                 var tags:Object = {};
61                 tags["name"] = String(feature.properties.description).substr(0,10)+'...';
62                 tags["description"] = feature.properties.description;
63                 tags["bug_id"] = feature.id;
64                 tags["nickname"] = feature.properties.nickname;
65                 tags["type"] = feature.properties.type;
66                 tags["date_created"] = feature.properties.date_created;
67                 tags["date_updated"] = feature.properties.date_updated;
68                 tags["source"] = feature.properties.source;
69                 tags["status"] = status[int(feature.properties.status)];
70                 var marker:Marker = createMarker(tags, lat, lon, Number(feature.id));
71               }
72               paint.updateEntityUIs(getObjectsByBbox(map.edge_l,map.edge_r,map.edge_t,map.edge_b), true, false);
73             }
74         }
75
76     }
77 }