diff --git a/config/routes.rb b/config/routes.rb index 871a5b40..d6a77c3b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -64,6 +64,9 @@ # Ontologies map.resources :ontologies + # Predicates + map.resources :predicates + # mashup map.resource :mashup diff --git a/config/tables.xml b/config/tables.xml index 9bc2a40a..a6aa7111 100644 --- a/config/tables.xml +++ b/config/tables.xml @@ -42,6 +42,7 @@ 19440 510 135 + 1 False False @@ -111,7 +112,7 @@ - @@ -1785,17 +1786,58 @@ yes no + + POST + relationship + user + view + context + yes + call + relationship + relationship + no + no + post_relationship + + + PUT + relationship + user + view + context + yes + call + relationship + relationship + required + no + put_relationship + + + DELETE + relationship + user + view + context + yes + call + relationship + relationship + required + no + delete_relationship +
80 - 1 - 153 + 160 2 - 2 + 6 0 @@ -1803,6 +1845,7 @@ 1 + 2 2 @@ -1810,8 +1853,8 @@ 0 - 174 - 0 + 191 + 12 False @@ -1820,7 +1863,7 @@
- @@ -5926,19 +5969,6 @@ yes user - - relationship - phrase - Relationship - predicate.phrase - no - text - no - yes - no - yes - user - relationship created-at @@ -5962,10 +5992,11 @@ 600 80 + 1 - 186 + 305 2 2 0 @@ -5982,8 +6013,9 @@ 0 - 217 - 4 + 324 + 0 + R325 False diff --git a/lib/rest.rb b/lib/rest.rb index 4f0b3ba7..47cb8cc2 100644 --- a/lib/rest.rb +++ b/lib/rest.rb @@ -579,7 +579,7 @@ def rest_resource_uri(ob) when 'License'; return license_url(ob) when 'CurationEvent'; return nil when 'Ontology'; return ontology_url(ob) - when 'Predicate'; return nil + when 'Predicate'; return predicate_url(ob) when 'Relationship'; return nil when 'Creditation'; return nil @@ -738,6 +738,7 @@ def parse_resource_uri(str) return [Job, $1, is_local] if uri.path =~ /^\/jobs\/([\d]+)$/ return [Download, $1, is_local] if uri.path =~ /^\/downloads\/([\d]+)$/ return [Ontology, $1, is_local] if uri.path =~ /^\/ontologies\/([\d]+)$/ + return [Predicate, $1, is_local] if uri.path =~ /^\/predicates\/([\d]+)$/ nil @@ -2070,6 +2071,69 @@ def put_predicate(opts) def delete_predicate(opts) predicate_aux('destroy', opts) end + +# Relationships + +def relationship_aux(action, opts) + + if action != "destroy" + + data = LibXML::XML::Parser.string(request.raw_post).parse + + subject = parse_element(data, :resource, '/relationship/subject') + predicate = parse_element(data, :resource, '/relationship/predicate') + objekt = parse_element(data, :resource, '/relationship/object') + context = parse_element(data, :resource, '/relationship/context') + end + + # Obtain object + + case action + when 'create': + return rest_response(401, :reason => "Not authorised to create a relationship") unless Authorization.is_authorized_for_type?('create', 'Relationship', opts[:user], context) + ob = Relationship.new(:user => opts[:user]) + when 'read', 'update', 'destroy': + ob = obtain_rest_resource('Relationship', opts[:query]['id'], opts[:query]['version'], opts[:user], action) + else + raise "Invalid action '#{action}'" + end + + return if ob.nil? # appropriate rest response already given + + if action == "destroy" + + ob.destroy + + else + + # build it + + ob.subject = subject if subject + ob.predicate = predicate if predicate + ob.objekt = objekt if objekt + ob.context = context if context + + if not ob.save + return rest_response(400, :object => ob) + end + end + + rest_get_request(ob, "relationship", opts[:user], + rest_resource_uri(ob), "relationship", { "id" => ob.id.to_s }) +end + +def post_relationship(opts) + relationship_aux('create', opts) +end + +def put_relationship(opts) + relationship_aux('update', opts) +end + +def delete_relationship(opts) + relationship_aux('destroy', opts) +end + # Call dispatcher def rest_call_request(req_uri, format, rules, user, query) diff --git a/test/functional/api_controller_test.rb b/test/functional/api_controller_test.rb index 82a353bf..8b751cfe 100644 --- a/test/functional/api_controller_test.rb +++ b/test/functional/api_controller_test.rb @@ -11,7 +11,7 @@ class ApiController; def rescue_action(e) raise e end; end class ApiControllerTest < Test::Unit::TestCase - fixtures :workflows, :users, :content_types, :licenses, :ontologies + fixtures :workflows, :users, :content_types, :licenses, :ontologies, :predicates, :packs def setup @controller = ApiController.new @@ -849,6 +849,60 @@ def test_predicates assert_response(:not_found) end + def test_relationships + + existing_relationships = Relationship.find(:all) + + login_as(:john) + + subject_url = rest_resource_uri(workflows(:workflow_branch_choice)) + predicate_url = rest_resource_uri(predicates(:test_predicate_1)) + objekt_url = rest_resource_uri(workflows(:workflow_dilbert)) + context_url = rest_resource_uri(packs(:pack_1)) + + # post a relationship + + rest_request(:post, 'relationship', " + + + + + + ") + + assert_response(:success) + + extra_relationships = Relationship.find(:all) - existing_relationships + + assert_equal(extra_relationships.length, 1) + + relationship = extra_relationships.first + + # get the relationship + + response = rest_request(:get, 'relationship', nil, "id" => relationship.id, + "elements" => "user,subject,predicate,object,context") + + assert_response(:success) + + assert_equal(subject_url, response.find_first('/relationship/subject/*/@resource').value) + assert_equal(predicate_url, response.find_first('/relationship/predicate/@resource').value) + assert_equal(objekt_url, response.find_first('/relationship/object/*/@resource').value) + assert_equal(context_url, response.find_first('/relationship/context/*/@resource').value) + + # delete the relationship + + rest_request(:delete, 'relationship', nil, "id" => relationship.id) + + assert_response(:success) + + # try to get the deleted relationship + + rest_request(:get, 'relationship', nil, "id" => relationship.id) + + assert_response(:not_found) + end + private def rest_request(method, uri, data = nil, query = {})