Merge branch 'master' into history
[potlatch2.git] / net / systemeD / halcyon / connection / Entity.as
index b02b9747c27c8c2bc08d8f7c75a8301e567eeb6f..2a47935de4c2c5bd8e900f381edce64ea937bc46 100644 (file)
@@ -1,5 +1,6 @@
 package net.systemeD.halcyon.connection {
 
+    import flash.events.Event;
     import flash.events.EventDispatcher;
     import flash.utils.Dictionary;
     
@@ -8,6 +9,7 @@ package net.systemeD.halcyon.connection {
     /** An Entity is an object stored in the map database, and therefore uploaded and downloaded. This includes Nodes, Ways, 
     *   Relations but also Changesets etc. */
     public class Entity extends EventDispatcher {
+               private var _connection:Connection;
         private var _id:Number;
         private var _version:uint;
         private var _uid:Number;
@@ -17,13 +19,15 @@ package net.systemeD.halcyon.connection {
         private var modified:Boolean = false;
         private var _loaded:Boolean = true;
         private var parents:Dictionary = new Dictionary();
+               public var status:String;
         /** Lock against purging when off-screen */
         public var locked:Boolean = false;
         public var deleted:Boolean = false;
         /** Have all its parents (ie, relations that contain this object as a member, ways that contain this node) been loaded into memory */
         public var parentsLoaded:Boolean = true;
 
-        public function Entity(id:Number, version:uint, tags:Object, loaded:Boolean, uid:Number, timestamp:String, user:String) {
+        public function Entity(connection:Connection, id:Number, version:uint, tags:Object, loaded:Boolean, uid:Number, timestamp:String, user:String) {
+                       this._connection = connection;
             this._id = id;
             this._version = version;
             this._uid = uid;
@@ -69,6 +73,11 @@ package net.systemeD.halcyon.connection {
             return _user;
         }
 
+               /** Connection to which this entity belongs. */
+               public function get connection():Connection {
+                       return _connection;
+               }
+
         /** Set a bunch of properties in one hit. Implicitly makes entity not deleted. */
         public function updateEntityProperties(version:uint, tags:Object, loaded:Boolean, parentsLoaded:Boolean, uid:Number, timestamp:String, user:String):void {
             _version=version; this.tags=tags; _loaded=loaded; this.parentsLoaded=parentsLoaded; _uid = uid; _timestamp = timestamp; _user = user;
@@ -101,6 +110,16 @@ package net.systemeD.halcyon.connection {
             return false;
         }
 
+               /** Compare tags between two entities. */
+               public function sameTags(entity:Entity):Boolean {
+                       var o:Object=entity.getTagsHash();
+                       for (var k:String in tags)
+                               if (!o[k] || o[k]!=tags[k]) return false;
+                       for (k in o)
+                               if (!tags[k] || tags[k]!=o[k]) return false;
+                       return true;
+               }
+
         /** Rough function to detect entities untouched since TIGER import. */
         public function isUneditedTiger():Boolean {
             // todo: make this match the rules from the tiger edited map
@@ -168,6 +187,14 @@ package net.systemeD.halcyon.connection {
             return copy;
         }
 
+               /** Change entity status. */
+               public function setStatus(s:String):void {
+                       if (s=='') s=null;
+                       if (s==status) return;
+                       status=s;
+                       dispatchEvent(new EntityEvent(Connection.STATUS_CHANGED,this));
+               }
+
                // Clean/dirty methods
 
         /** Check if entity is modified since last markClean(). */
@@ -202,9 +229,9 @@ package net.systemeD.halcyon.connection {
             if (this is Node) {
                 var n:Node = Node(this);
                 if (isDeleted) {
-                    Connection.getConnection().removeDupe(n);
+                    connection.removeDupe(n);
                 } else {
-                    Connection.getConnection().addDupe(n);
+                    connection.addDupe(n);
                 }
             }
         }
@@ -384,7 +411,7 @@ package net.systemeD.halcyon.connection {
                }
 
 
-                /** Basic description of Entity - should be overriden by subclass. */
+               /** Basic description of Entity - should be overriden by subclass. */
                public function getDescription():String {
                        var basic:String=this.getType()+" "+_id;
                        if (tags['ref'] && tags['name']) { return tags['ref']+' '+tags['name']+' ('+basic+')'; }
@@ -405,31 +432,22 @@ package net.systemeD.halcyon.connection {
                
         /** Copy tags from another entity into this one, creating "key=value1; value2" pairs if necessary.
         * * @return Array of keys that require manual merging, in order to warn the user. */ 
-        public function mergeTags(source: Entity, performAction:Function):Array {
+        public function mergeTags(source: Entity, performAction:Function):Boolean {
             var sourcetags:Object = source.getTagsHash();
-            var problem_keys:Array=new Array(); 
+            var conflict:Boolean = false;
             for (var k:String in sourcetags) {
                 var v1:String = tags[k];
                 var v2:String = sourcetags[k];
                 if ( v1 && v1 != v2) {
-                    // This can create broken tags (does anything support "highway=residential; tertiary"?). 
-                    // Probably better to do something like:
-                    // highway=residential
-                    // highway:tomerge=tertiary
-                    
                     setTag(k, v1+"; "+v2, performAction);
-                    problem_keys.push(k);
+                    conflict=true;
                 } else {
                     setTag(k, v2, performAction);
                 }
             }
-            if (problem_keys.length > 0)
-                return problem_keys;
-            else 
-                return null;
+            return conflict;
         }
 
-
     }
 
 }