1 package net.systemeD.potlatch2 {
3 import net.systemeD.halcyon.Map;
4 import net.systemeD.halcyon.connection.*;
5 import net.systemeD.halcyon.connection.actions.*;
8 import flash.system.Security;
9 import com.adobe.serialization.json.JSON;
11 /** A Connection that can be used to load and display bugs from MapDust-compatible APIs.
12 See utils/BugLoader.as for the corresponding loader. */
14 public class BugConnection extends Connection {
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";
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"];
34 public function BugConnection(n:String, baseUrl:String, apiKey:String, detailsURL:String) {
35 this.baseUrl = baseUrl;
37 this.detailsUrl = detailsURL;
38 super(n, baseUrl, baseUrl+"crossdomain.xml", null);
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) {
49 comment ||= 'No Comment';
50 if (comment.length > 1000) {
51 comment = comment.substr(0,1000); // that's index, length
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();
59 loader.addEventListener(Event.COMPLETE, function(e:Event):void { bugClosed(e, m, status); } );
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
67 public override function loadBbox(left:Number, right:Number,
68 top:Number, bottom:Number):void {
70 // Should be guarded against multiple calls really.
71 if (policyURL != "") { Security.loadPolicyFile(policyURL); }
73 var loader:URLLoader = new URLLoader();
74 loader.addEventListener(Event.COMPLETE, parseJSON);
75 loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handleError);
76 loader.addEventListener(IOErrorEvent.IO_ERROR, handleError);
77 loader.load(new URLRequest(baseUrl+"getBugs?bbox="+left+","+bottom+","+right+","+top+"&key="+apiKey+"&filter_status="+filter_status+"&filter_type="+filter_type+commentType));
80 private function handleError(event:Event):void {
83 private function parseJSON(event:Event):void {
84 var result:String = String(event.target.data);
85 if (result) { // api returns 204 no content for no bugs, and the JSON parser treats '' as an error
86 var featureCollection:Object = JSON.decode(result);
88 for each (var feature:Object in featureCollection.features) {
89 // geoJSON spec is x,y,z i.e. lon, lat, ele
90 var lon:Number = feature.geometry.coordinates[0];
91 var lat:Number = feature.geometry.coordinates[1];
93 tags["name"] = String(feature.properties.description).substr(0,10)+'...';
94 tags["description"] = feature.properties.description;
95 tags["bug_id"] = feature.id;
96 tags["nickname"] = feature.properties.nickname;
97 tags["type"] = feature.properties.type;
98 tags["date_created"] = feature.properties.date_created;
99 tags["date_updated"] = feature.properties.date_updated;
100 tags["source"] = feature.properties.source;
101 tags["status"] = status[int(feature.properties.status)];
102 var marker:Marker = createMarker(tags, lat, lon, Number(feature.id));
107 public function bugDetailsUrl(m:Marker):String {
108 if (detailsUrl == '')
110 return detailsUrl+m.id;