+ # Save a way to the database, including all nodes. Any nodes in the previous
+ # version and no longer used are deleted.
+ #
+ # Parameters:
+ # 0. hash of renumbered nodes (added by amf_controller)
+ # 1. current user token (for authentication)
+ # 2. current changeset
+ # 3. new way version
+ # 4. way ID
+ # 5. list of nodes in way
+ # 6. hash of way tags
+ # 7. array of nodes to change (each one is [lon,lat,id,version,tags]),
+ # 8. hash of nodes to delete (id->version).
+ #
+ # Returns:
+ # 0. '0' (code for success),
+ # 1. original way id (unchanged),
+ # 2. new way id,
+ # 3. hash of renumbered nodes (old id=>new id),
+ # 4. way version,
+ # 5. hash of node versions (node=>version)
+
+ def putway(renumberednodes, usertoken, changeset_id, wayversion, originalway, pointlist, attributes, nodes, deletednodes) #:doc:
+
+ # -- Initialise
+
+ user = getuser(usertoken)
+ if !user then return -1,"You are not logged in, so the way could not be saved." end
+ if pointlist.length < 2 then return -2,"Server error - way is only #{points.length} points long." end
+
+ originalway = originalway.to_i
+logger.info("received #{pointlist} for #{originalway}")
+ pointlist.collect! {|a| a.to_i }
+logger.info("converted to #{pointlist}")
+
+ way=nil # this is returned, so scope it outside the transaction
+ nodeversions = {}
+ Way.transaction do
+
+ # -- Update each changed node
+
+ nodes.each do |a|
+ lon = a[0].to_f
+ lat = a[1].to_f
+ id = a[2].to_i
+ version = a[3].to_i
+ if id == 0 then return -2,"Server error - node with id 0 found in way #{originalway}." end
+ if lat== 90 then return -2,"Server error - node with latitude -90 found in way #{originalway}." end
+ if renumberednodes[id] then id = renumberednodes[id] end
+
+ node = Node.new
+ node.changeset_id = changeset_id
+ node.lat = lat
+ node.lon = lon
+ node.tags = a[4]
+ node.tags.delete('created_by')
+ node.version = version
+ if id <= 0
+ # We're creating the node
+ node.create_with_history(user)
+ renumberednodes[id] = node.id
+ nodeversions[node.id] = node.version
+ else
+ # We're updating an existing node
+ previous=Node.find(id)
+ node.id=id
+ previous.update_from(node, user)
+ nodeversions[previous.id] = previous.version
+ end
+ end