- # 2. new relation id.
-
- def putrelation(renumberednodes, renumberedways, usertoken, relid, tags, members, visible) #:doc:
- uid = getuserid(usertoken)
- if !uid then return -1,"You are not logged in, so the relation 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]
+ # 2. new relation id,
+ # 3. version.
+
+ def putrelation(renumberednodes, renumberedways, usertoken, changeset_id, version, relid, tags, members, visible) #:doc:
+ user = getuser(usertoken)
+ if !user then return -1,"You are not logged in, so the relation could not be saved." end
+
+ relid = relid.to_i
+ visible = (visible.to_i != 0)
+
+ new_relation = nil
+ relation = nil
+ Relation.transaction do
+ # create a new relation, or find the existing one
+ if relid > 0
+ relation = Relation.find(relid)
+ end
+ # We always need a new node, based on the data that has been sent to us
+ new_relation = Relation.new
+
+ # 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'
+ end
+ if mid
+ typedmembers << [m[0], mid, m[2]]
+ end
+ end
+
+ # assign new contents
+ new_relation.members = typedmembers
+ new_relation.tags = tags
+ new_relation.visible = visible
+ new_relation.changeset_id = changeset_id
+ new_relation.version = version
+
+ if relid <= 0
+ # We're creating the node
+ new_relation.create_with_history(user)
+ elsif visible
+ # We're updating the node
+ relation.update_from(new_relation, user)
+ else
+ # We're deleting the node
+ relation.delete_with_history!(new_relation, user)
+ end
+ end # transaction
+
+ if relid <= 0
+ return [0, relid, new_relation.id, new_relation.version]
+ else
+ return [0, relid, relid, relation.version]
+ end
+ rescue OSM::APIChangesetAlreadyClosedError => ex
+ return [-1, "The changeset #{ex.changeset.id} was closed at #{ex.changeset.closed_at}."]
+ rescue OSM::APIVersionMismatchError => ex
+ # Really need to check to see whether this is a server load issue, and the
+ # last version was in the same changeset, or belongs to the same user, then
+ # we can return something different
+ return [-3, "Sorry, someone else has changed this relation since you started editing. Please click the 'Edit' tab to reload the area."]
+ rescue OSM::APIAlreadyDeletedError => ex
+ return [-1, "The relation has already been deleted."]
+ rescue OSM::APIError => ex
+ # Some error that we don't specifically catch
+ return [-2, "An unusual error happened (in 'putrelation' #{relid})."]