1 package net.systemeD.halcyon.connection {
5 import net.systemeD.halcyon.AttentionEvent;
7 public class StatusFetcher {
9 /* Class to fetch the status for newly loaded objects.
10 At present this is rather specialised to WTFE (wtfe.gryph.de).
13 public var _url:String;
14 public var conn:Connection;
16 private static var STATUSES:Array=["no","partial","unsure",''];
18 public function StatusFetcher(url:String, connection:Connection) {
23 public function fetch(entities:Array):void {
24 if (entities.length==0) return;
26 var vars:URLVariables = new URLVariables();
30 for each (var entity:Entity in entities) {
31 if (entity is Node) vars.nodes+=entity.id+",";
32 else if (entity is Way) vars.ways+=entity.id+",";
33 else if (entity is Relation) vars.relations+=entity.id+",";
35 if (vars.ways.substr(vars.ways.length-1,1)==',') vars.ways=vars.ways.substr(0,vars.ways.length-1);
36 if (vars.nodes.substr(vars.nodes.length-1,1)==',') vars.nodes=vars.nodes.substr(0,vars.nodes.length-1);
37 if (vars.relations.substr(vars.relations.length-1,1)==',') vars.relations=vars.relations.substr(0,vars.relations.length-1);
38 var request:URLRequest = new URLRequest(_url);
40 request.method = "POST";
43 var loader:URLLoader = new URLLoader();
44 loader.addEventListener(Event.COMPLETE, loadedFetch);
45 loader.addEventListener(IOErrorEvent.IO_ERROR, errorOnFetchLoad);
46 loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, fetchLoadStatus);
50 private function loadedFetch(event:Event):void {
51 var xml:XML=new XML(URLLoader(event.target).data);
52 var entity:Entity, status:String;
54 for each (var exml:XML in xml.*) {
55 switch (exml.name().localName) {
56 case "way": entity=conn.getWay(exml.@id); break;
57 case "relation":entity=conn.getRelation(exml.@id); break;
58 case "node": entity=conn.getNode(exml.@id); break;
61 // **** Specific WTFE-parsing code starts here
62 // FIXME: should be generalised
63 // if all users are "yes" or "auto", status is 'ok' (green)
64 // if first user is "no", status is 'no' (red)
65 // if any other users are no, status is 'partial' (softer red)
66 // otherwise, status is 'unsure' (yellow)
68 for each (var user:XML in exml.user) {
69 if (user.@decision=='no' && user.@version=='first') { s=0; } // no from v1
70 else if (user.@decision=='no') { s=Math.min(s,1); } // no from later version
71 else if (user.@decision=='undecided' || user.@decision=='anonymous') { s=Math.min(s,2); } // unsure
74 // **** Specific WTFE-parsing code ends here
75 entity.setStatus(status);
79 private function errorOnFetchLoad(event:Event):void {
80 conn.dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, "Couldn't load status information"));
82 private function fetchLoadStatus(event:HTTPStatusEvent):void { }