Make it much less likely to trigger bugClosed errors, but errors still aren't handled...
[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, comment:String, status:String = null):void {
29             var id:String = m.getTag('bug_id');
30             nickname ||= 'NoName';
31             // nicknames have length and character restictions. The character restrictions should be taken care of
32             // by the BugPanel.mxml restriction.
33             if (nickname.length < 3 || nickname.length > 16) {
34               nickname = 'NoName';
35             }
36             comment ||= 'No Comment';
37             if (comment.length > 1000) {
38               comment = comment.substr(0,1000); // that's index, length
39             }
40             status ||= BUG_STATUS_FIXED;
41             var urlReq:URLRequest = new URLRequest(baseUrl+"changeBugStatus?id="+id+"&status="+status+"&comment="+encodeURIComponent(comment)+"&nickname="+encodeURIComponent(nickname)+"&key="+apiKey);
42             urlReq.method = "POST";
43             urlReq.data = '    '; // dear Adobe, this is nuts, kthxbye (you can't POST with an empty payload)
44             var loader:URLLoader = new URLLoader();
45             loader.load(urlReq);
46             loader.addEventListener(Event.COMPLETE, function(e:Event):void { bugClosed(e, m, status); } );
47         }
48
49         private function bugClosed(event:Event, marker:Marker, s:String):void {
50             var action:UndoableEntityAction = new SetTagAction(marker, "status", status[int(s)]);
51             action.doAction(); // just do it, don't add to undo stack
52         }
53
54         public override function loadBbox(left:Number, right:Number,
55                                 top:Number, bottom:Number):void {
56             var loader:URLLoader = new URLLoader();
57             loader.load(new URLRequest(baseUrl+"getBugs?bbox="+map.edge_l+","+map.edge_b+","+map.edge_r+","+map.edge_t+"&key="+apiKey));
58             loader.addEventListener(Event.COMPLETE, parseJSON);
59         }
60
61         private function parseJSON(event:Event):void {
62             var result:String = String(event.target.data);
63             if (result) { // api returns 204 no content for no bugs, and the JSON parser treats '' as an error
64               var featureCollection:Object = JSON.decode(result);
65
66               for each (var feature:Object in featureCollection.features) {
67                 // geoJSON spec is x,y,z i.e. lon, lat, ele
68                 var lon:Number = feature.geometry.coordinates[0];
69                 var lat:Number = feature.geometry.coordinates[1];
70                 var tags:Object = {};
71                 tags["name"] = String(feature.properties.description).substr(0,10)+'...';
72                 tags["description"] = feature.properties.description;
73                 tags["bug_id"] = feature.id;
74                 tags["nickname"] = feature.properties.nickname;
75                 tags["type"] = feature.properties.type;
76                 tags["date_created"] = feature.properties.date_created;
77                 tags["date_updated"] = feature.properties.date_updated;
78                 tags["source"] = feature.properties.source;
79                 tags["status"] = status[int(feature.properties.status)];
80                 var marker:Marker = createMarker(tags, lat, lon, Number(feature.id));
81               }
82               paint.updateEntityUIs(getObjectsByBbox(map.edge_l,map.edge_r,map.edge_t,map.edge_b), true, false);
83             }
84         }
85
86     }
87 }