]> git.openstreetmap.org Git - rails.git/blobdiff - app/controllers/amf_controller.rb
Make the polymorphic relation association work properly.
[rails.git] / app / controllers / amf_controller.rb
index fd0296ae5ecbd4ce91bfd471077028e1bb73f89d..359b45326b99137b89bfaaba944959b15a45d503 100644 (file)
@@ -30,6 +30,7 @@ class AmfController < ApplicationController
     req.read(2)                                                                        # Skip version indicator and client ID
     results={}                                                                 # Results of each body
     renumberednodes={}                                                 # Shared across repeated putways
+    renumberedways={}                                                  # Shared across repeated putways
 
     # -------------
     # Parse request
@@ -55,11 +56,16 @@ class AmfController < ApplicationController
       when 'whichways';                        results[index]=AMF.putdata(index,whichways(args))
       when 'whichways_deleted';        results[index]=AMF.putdata(index,whichways_deleted(args))
       when 'getway';                   results[index]=AMF.putdata(index,getway(args))
+      when 'getrelation';              results[index]=AMF.putdata(index,getrelation(args))
       when 'getway_old';               results[index]=AMF.putdata(index,getway_old(args))
       when 'getway_history';   results[index]=AMF.putdata(index,getway_history(args))
       when 'putway';                   r=putway(args,renumberednodes)
                                                                renumberednodes=r[3]
+                                                               if r[1] != r[2]
+                                                                       renumberedways[r[1]] = r[2]
+                                                               end
                                                                results[index]=AMF.putdata(index,r)
+      when 'putrelation';              results[index]=AMF.putdata(index,putrelation(args, renumberednodes, renumberedways))
       when 'deleteway';                        results[index]=AMF.putdata(index,deleteway(args))
       when 'putpoi';                   results[index]=AMF.putdata(index,putpoi(args))
       when 'getpoi';                   results[index]=AMF.putdata(index,getpoi(args))
@@ -116,7 +122,10 @@ class AmfController < ApplicationController
     nodes_not_used_in_area = nodes_in_area.select { |node| node.ways.empty? }
     points = nodes_not_used_in_area.collect { |n| [n.id, n.lon_potlatch(baselong,masterscale), n.lat_potlatch(basey,masterscale), n.tags_as_hash] }
 
-    [way_ids,points]
+    # find the relations used by those nodes and ways
+    relation_ids = (Relation.find_for_nodes_and_ways(nodes_in_area.collect {|n| n.id}, way_ids)).collect {|n| n.id}.uniq
+
+    [way_ids,points,relation_ids]
   end
 
   # ----- whichways_deleted
@@ -264,6 +273,80 @@ class AmfController < ApplicationController
     [history]
   end
 
+  # ----- getrelation
+  # Get a relation with all of it's tags, and member IDs
+  # The input is an array with the following components, in order:
+  # 0. relid - the ID of the relation to get
+  #
+  # The output is an array which contains:
+  # [0] relation id, [1] hash of tags, [2] list of members
+  def getrelation(args) #:doc:
+    relid = args[0]
+    relid = relid.to_i
+
+    RAILS_DEFAULT_LOGGER.info("  Message: getrel, id=#{relid}")
+
+    rel = Relation.find(relid)
+
+    [relid,rel.tags,rel.members]#nodes,ways]
+  end
+
+  # ----- getrelation
+  #              save relation to the database
+  #              in:   [0] user token (string),
+  #                            [1] original relation id (may be negative),
+  #                            [2] hash of tags, [3] list of members,
+  #                            [4] visible
+  #              out:  [0] 0 (success), [1] original relation id (unchanged),
+  #                            [2] new relation id
+  def putrelation(args, renumberednodes, renumberedways) #:doc:
+    usertoken,relid,tags,members,visible=args
+    uid=getuserid(usertoken)
+    if !uid then return -1,"You are not logged in, so the point could not be saved." end
+
+    relid = relid.to_i
+       visible = visible.to_i
+
+       # create a new relation, or find the existing one
+    if relid <= 0
+      rel = Relation.new
+    else
+      rel = Relation.find(relid)
+    end
+
+    # check the members are all positive, and correctly type
+    typedmembers = []
+    members.each do |m|
+      mid = m[1].to_i
+      if mid < 0
+        mid = renumberednodes[mid] if m[0] == 'node'
+        mid = renumberedways[mid] if m[0] == 'way'
+        if mid < 0
+          return -2, "Negative ID unresolved"
+        end
+      end
+      typedmembers << [m[0], mid, m[2]]
+    end
+
+       # assign new contents
+       rel.members = typedmembers
+       rel.tags = tags
+       rel.visible = visible
+       rel.user_id = uid
+
+    # check it then save it
+    # BUG: the following is commented out because it always fails on my
+    #  install. I think it's a Rails bug.
+
+    #if !rel.preconditions_ok?
+    #  return -2, "Relation preconditions failed"
+    #else
+      rel.save_with_history!
+    #end
+
+    [0,relid,rel.id]
+  end
+
   # ----- putway
   #              saves a way to the database
   #              in:   [0] user token (string),
@@ -293,10 +376,11 @@ class AmfController < ApplicationController
 
     RAILS_DEFAULT_LOGGER.info("  Message: putway, id=#{originalway}")
 
-    # -- Check for null IDs or short ways
+    # -- Check for null IDs, short ways or lats=90
 
     points.each do |a|
       if a[2]==0 or a[2].nil? then return -2,"Server error - node with id 0 found in way #{originalway}." end
+      if coord2lat(a[1],masterscale,basey)==90 then return -2,"Server error - node with lat -90 found in way #{originalway}." end
     end
     
     if points.length<2 then return -2,"Server error - way is only #{points.length} points long." end
@@ -487,7 +571,7 @@ class AmfController < ApplicationController
     
     n = Node.find(id.to_i)
     if n
-      return [n.id, n.long_potlatch(baselong,masterscale), n.lat_potlatch(basey,masterscale), n.tags_as_hash]
+      return [n.id, n.lon_potlatch(baselong,masterscale), n.lat_potlatch(basey,masterscale), n.tags_as_hash]
     else
       return [nil,nil,nil,'']
     end
@@ -499,21 +583,35 @@ class AmfController < ApplicationController
   #              does: deletes way from db and any constituent nodes not used elsewhere
   #                            also removes ways/nodes from any relations they're in
   #              out:  [0] 0 (success), [1] way id (unchanged)
+
   def deleteway(args) #:doc:
     usertoken,way_id=args
     RAILS_DEFAULT_LOGGER.info("  Message: deleteway, id=#{way_id}")
     uid=getuserid(usertoken)
     if !uid then return -1,"You are not logged in, so the way could not be deleted." end
 
-    user = User.find(uid)
+       # FIXME
+       # the next bit removes the way from any relations
+       # the delete_with_relations_and_nodes_and_history method should do this,
+       #   but at present it just throws a 'precondition failed'
+    way=way.to_i 
+    db_now='@now'+(rand*100).to_i.to_s+uid.to_s+way.abs.to_s+Time.new.to_i.to_s
+       db_uqn='unin'+(rand*100).to_i.to_s+uid.to_s+way.abs.to_s+Time.new.to_i.to_s
+    ActiveRecord::Base.connection.execute("SET #{db_now}=NOW()")
+       createuniquenodes(way,db_uqn,[])
+       deleteuniquenoderelations(db_uqn,uid,db_now)
+    deleteitemrelations(way_id,'way',uid,db_now)
+    ActiveRecord::Base.connection.execute("DROP TEMPORARY TABLE #{db_uqn}")
+       # end of FIXME
 
+       # now delete the way
+    user = User.find(uid)
     way = Way.find(way_id)
-
     way.delete_with_relations_and_nodes_and_history(user)  
-
     return [0,way_id]
   end
 
+
   def readwayquery(id,insistonvisible) #:doc:
     sql=<<-EOF
     SELECT latitude*0.0000001 AS latitude,longitude*0.0000001 AS longitude,current_nodes.id,tags,visible 
@@ -541,8 +639,8 @@ class AmfController < ApplicationController
     #   which means the SWF needs to allocate new ids
     # - if it's an invisible node, we can reuse the old node id
 
-    # get node list from specified version of way,
-    # and the _current_ lat/long/tags of each node
+    # -----    get node list from specified version of way,
+    #          and the _current_ lat/long/tags of each node
 
     row=ActiveRecord::Base.connection.select_one("SELECT timestamp FROM ways WHERE version=#{version} AND id=#{id}")
     waytime=row['timestamp']
@@ -557,31 +655,31 @@ class AmfController < ApplicationController
   EOF
     rows=ActiveRecord::Base.connection.select_all(sql)
 
-    # if historic (full revert), get the old version of each node
-    # - if it's in another way now, generate a new id
-    # - if it's not in another way, use the old ID
+    # -----    if historic (full revert), get the old version of each node
+    #          - if it's in another way now, generate a new id
+    #          - if it's not in another way, use the old ID
+
     if historic then
       rows.each_index do |i|
         sql=<<-EOF
     SELECT latitude*0.0000001 AS latitude,longitude*0.0000001 AS longitude,tags,cwn.id AS currentway 
       FROM nodes n
  LEFT JOIN current_way_nodes cwn
-      ON cwn.node_id=n.id
+ LEFT JOIN current_way_nodes cwn
+        ON cwn.node_id=n.id AND cwn.id!=#{id} 
      WHERE n.id=#{rows[i]['id']} 
        AND n.timestamp<="#{waytime}" 
-     AND cwn.id!=#{id} 
-     ORDER BY n.timestamp DESC 
+  ORDER BY n.timestamp DESC 
      LIMIT 1
     EOF
         row=ActiveRecord::Base.connection.select_one(sql)
-        unless row.nil? then
-          nx=row['longitude'].to_f
-          ny=row['latitude'].to_f
+        nx=row['longitude'].to_f
+        ny=row['latitude'].to_f
+        if (!row.nil?)
           if (row['currentway'] && (nx!=rows[i]['longitude'].to_f or ny!=rows[i]['latitude'].to_f or row['tags']!=rows[i]['tags'])) then rows[i]['id']=-1 end
-          rows[i]['longitude']=nx
-          rows[i]['latitude' ]=ny
-          rows[i]['tags'     ]=row['tags']
-        end
+               end
+        rows[i]['longitude']=nx
+        rows[i]['latitude' ]=ny
+        rows[i]['tags'     ]=row['tags']
       end
     end
     rows