1 # The ChangesetController is the RESTful interface to Changeset objects
3 class ChangesetController < ApplicationController
6 before_filter :authorize, :only => [:create, :update, :delete, :upload]
7 before_filter :check_write_availability, :only => [:create, :update, :delete, :upload]
8 before_filter :check_read_availability, :except => [:create, :update, :delete, :upload]
9 after_filter :compress_output
11 # Create a changeset from XML.
14 cs = Changeset.from_xml(request.raw_post, true)
19 render :text => cs.id.to_s, :content_type => "text/plain"
21 render :nothing => true, :status => :bad_request
24 render :nothing => true, :status => :method_not_allowed
28 def create_prim(ids, prim, nd)
30 prim.user_id = @user.id
32 prim.save_with_history!
34 ids[nd['id'].to_i] = prim.id
37 def fix_way(w, node_ids)
38 w.nds = w.instance_eval { @nds }.
39 map { |nd| node_ids[nd] || nd }
44 r.members = r.instance_eval { @members }.
45 map { |memb| [memb[0], ids[memb[0]][memb[1].to_i] || memb[1], memb[2]] }
51 render :nothing => true, :status => :method_not_allowed
55 # FIXME: this should really be done without loading the whole XML file
58 p.string = request.raw_post
61 node_ids, way_ids, rel_ids = {}, {}, {}
62 ids = {"node"=>node_ids, "way"=>way_ids, "relation"=>rel_ids}
64 Changeset.transaction do
65 doc.find('//osm/create/node').each do |nd|
66 create_prim node_ids, Node.from_xml_node(nd, true), nd
68 doc.find('//osm/create/way').each do |nd|
69 way = Way.from_xml_node(nd, true)
70 fix_way(way, node_ids)
71 raise OSM::APIPreconditionFailedError.new if !way.preconditions_ok?
72 create_prim way_ids, way, nd
74 doc.find('//osm/create/relation').each do |nd|
75 relation = Relation.from_xml_node(nd, true)
76 fix_rel(relation, ids)
77 raise OSM::APIPreconditionFailedError.new if !relation.preconditions_ok?
78 create_prim rel_ids, relation, nd
81 doc.find('//osm/modify/node').each do |nd|
82 unless NodeController.new.update_internal nil, Node.from_xml_node(nd)
83 raise OSM::APIPreconditionFailedError.new
86 doc.find('//osm/modify/way').each do |nd|
87 unless WayController.update_internal nil, fix_way(Way.from_xml_node(nd), node_ids)
88 raise OSM::APIPreconditionFailedError.new
91 doc.find('//osm/modify/relation').each do |nd|
92 unless RelationController.update_internal nil, fix_rel(Relation.from_xml_node(nd), ids)
93 raise OSM::APIPreconditionFailedError.new
97 doc.find('//osm/delete/node').each do |nd|
98 unless NodeController.delete_internal nil, Node.from_xml_node(n)
99 raise OSM::APIPreconditionFailedError.new
102 doc.find('//osm/delete/way').each do |nd|
103 Way.from_xml_node(nd).delete_with_relations_and_history(@user)
105 doc.find('//osm/delete/relation').each do |nd|
106 unless RelationController.delete_internal nil, fix_rel(Relation.from_xml_node(nd), ids)
107 raise OSM::APIPreconditionFailedError.new
112 render :text => "Ok, Fine. Upload worked without errors.\n", :status => 200