Don't load bugs that have the 'is default description' flag set.
[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     /** A VectorLayer that can be used to load and display bugs from MapDust-compatible APIs.
12         See utils/BugLoader.as for the corresponding loader. */
13
14     public class BugLayer extends VectorLayer {
15
16         private var baseUrl:String;
17         private var apiKey:String;
18         private var detailsUrl:String;
19         /** A comma-separated list of statuses that we wish to fetch. But TBH we only want open ones. */
20         private var filter_status:String = BUG_STATUS_OPEN;
21         /** A comma-separated list of types of bugs. We don't want ones classed as routing problems, they are likely to be skobbler-app specific. */
22         /* Possible values: wrong_turn,bad_routing,oneway_road,blocked_street,missing_street,wrong_roundabout,missing_speedlimit,other */
23         private var filter_type:String = "wrong_turn,oneway_road,blocked_street,missing_street,wrong_roundabout,missing_speedlimit,other";
24         /** Type of comments. "idd = 0" means no comments with default description, "idd = 1" means only comments with default description.
25         * Use empty string (i.e. don't pass any parameter) to indicate all comments. */
26         private var commentType:String = "&idd=0";
27
28         // as strings, since that's how they are in tags and http calls
29         public static var BUG_STATUS_OPEN:String = "1";
30         public static var BUG_STATUS_FIXED:String = "2";
31         public static var BUG_STATUS_INVALID:String = "3"; // or 'non-reproduceable'
32         public static const status:Array = ["", "open", "fixed", "invalid"];
33
34         public function BugLayer(n:String, map:Map, s:String, baseUrl:String, apiKey:String, detailsURL:String) {
35             this.baseUrl = baseUrl;
36             this.apiKey = apiKey;
37             this.detailsUrl = detailsURL;
38             super(n,map,s);
39         }
40
41         public function closeBug(m:Marker, nickname:String, comment:String, status:String = null):void {
42             var id:String = m.getTag('bug_id');
43             nickname ||= 'NoName';
44             // nicknames have length and character restictions. The character restrictions should be taken care of
45             // by the BugPanel.mxml restriction.
46             if (nickname.length < 3 || nickname.length > 16) {
47               nickname = 'NoName';
48             }
49             comment ||= 'No Comment';
50             if (comment.length > 1000) {
51               comment = comment.substr(0,1000); // that's index, length
52             }
53             status ||= BUG_STATUS_FIXED;
54             var urlReq:URLRequest = new URLRequest(baseUrl+"changeBugStatus?id="+id+"&status="+status+"&comment="+encodeURIComponent(comment)+"&nickname="+encodeURIComponent(nickname)+"&key="+apiKey);
55             urlReq.method = "POST";
56             urlReq.data = '    '; // dear Adobe, this is nuts, kthxbye (you can't POST with an empty payload)
57             var loader:URLLoader = new URLLoader();
58             loader.load(urlReq);
59             loader.addEventListener(Event.COMPLETE, function(e:Event):void { bugClosed(e, m, status); } );
60         }
61
62         private function bugClosed(event:Event, marker:Marker, s:String):void {
63             var action:UndoableEntityAction = new SetTagAction(marker, "status", status[int(s)]);
64             action.doAction(); // just do it, don't add to undo stack
65         }
66
67         public override function loadBbox(left:Number, right:Number,
68                                 top:Number, bottom:Number):void {
69             var loader:URLLoader = new URLLoader();
70             loader.load(new URLRequest(baseUrl+"getBugs?bbox="+map.edge_l+","+map.edge_b+","+map.edge_r+","+map.edge_t+"&key="+apiKey+"&filter_status="+filter_status+"&filter_type="+filter_type+commentType));
71             loader.addEventListener(Event.COMPLETE, parseJSON);
72             loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handleError);
73             loader.addEventListener(IOErrorEvent.IO_ERROR, handleError);
74         }
75
76                 private function handleError(event:Event):void {
77                 }
78
79         private function parseJSON(event:Event):void {
80             var result:String = String(event.target.data);
81             if (result) { // api returns 204 no content for no bugs, and the JSON parser treats '' as an error
82               var featureCollection:Object = JSON.decode(result);
83
84               for each (var feature:Object in featureCollection.features) {
85                 // geoJSON spec is x,y,z i.e. lon, lat, ele
86                 var lon:Number = feature.geometry.coordinates[0];
87                 var lat:Number = feature.geometry.coordinates[1];
88                 var tags:Object = {};
89                 tags["name"] = String(feature.properties.description).substr(0,10)+'...';
90                 tags["description"] = feature.properties.description;
91                 tags["bug_id"] = feature.id;
92                 tags["nickname"] = feature.properties.nickname;
93                 tags["type"] = feature.properties.type;
94                 tags["date_created"] = feature.properties.date_created;
95                 tags["date_updated"] = feature.properties.date_updated;
96                 tags["source"] = feature.properties.source;
97                 tags["status"] = status[int(feature.properties.status)];
98                 var marker:Marker = createMarker(tags, lat, lon, Number(feature.id));
99               }
100               paint.updateEntityUIs(getObjectsByBbox(map.edge_l,map.edge_r,map.edge_t,map.edge_b), true, false);
101             }
102         }
103
104         public function bugDetailsUrl(m:Marker):String {
105             if (detailsUrl == '')
106               return null;
107             return detailsUrl+m.id;
108         }
109
110     }
111 }