Merge branch 'master' into history
[potlatch2.git] / net / systemeD / halcyon / connection / StatusFetcher.as
1 package net.systemeD.halcyon.connection {
2
3     import flash.net.*;
4     import flash.events.*;
5     import net.systemeD.halcyon.AttentionEvent;
6
7         public class StatusFetcher {
8
9                 /* Class to fetch the status for newly loaded objects.
10                    At present this is rather specialised to WTFE (wtfe.gryph.de).
11                 */
12
13                 public var _url:String;
14                 public var conn:Connection;
15                 
16                 private static var STATUSES:Array=["no","partial","unsure",''];
17                 
18                 public function StatusFetcher(url:String, connection:Connection) {
19                         _url=url;
20                         conn=connection;
21                 }
22
23                 public function fetch(entities:Array):void {
24                         if (entities.length==0) return;
25                         // Create URL request
26             var vars:URLVariables = new URLVariables();
27                         vars.nodes='';
28                         vars.ways='';
29                         vars.relations='';
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+",";
34                         }
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);
39             request.data = vars;
40                         request.method = "POST";
41
42                         // Make request
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);
47                         loader.load(request);
48                 }
49                 
50                 private function loadedFetch(event:Event):void {
51                         var xml:XML=new XML(URLLoader(event.target).data);
52                         var entity:Entity, status:String;
53
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;
59                                 }
60
61                                 // **** Specific WTFE-parsing code starts here
62                                 // FIXME: should be generalised
63                                 //              if all users are "yes" or "auto", status is 'ok' (3, green)
64                                 //              if first user is "no", status is 'no' (0, red)
65                                 //              if any other users are no, status is 'partial' (1, softer red)
66                                 //              otherwise, status is 'unsure' (2, yellow)
67                                 var s:uint=3;   // ok
68                                 for each (var user:XML in exml.user) {
69                                         if (user.@severity=='harmless' || user.@severity=='none') continue;     // ignore if harmless
70                                         if (user.@decision=='no' && user.@version=='first') { s=0; }            // no from v1
71                                         else if (user.@decision=='no') { s=Math.min(s,1); }                                     // no from later version
72                                         else if (user.@decision=='undecided' || user.@decision=='anonymous') { s=Math.min(s,2); }       // unsure
73                                 }
74                                 status=STATUSES[s];
75                                 // **** Specific WTFE-parsing code ends here
76                                 entity.setStatus(status);
77                         }
78                 }
79
80         private function errorOnFetchLoad(event:Event):void {
81                         conn.dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, "Couldn't load status information"));
82         }
83         private function fetchLoadStatus(event:HTTPStatusEvent):void { }
84
85         }
86 }