myexperiment-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myexperiment-hackers] [2702] trunk: added write API for ontologies and


From: noreply
Subject: [myexperiment-hackers] [2702] trunk: added write API for ontologies and predicates
Date: Wed, 14 Sep 2011 10:58:40 -0400 (EDT)

Revision
2702
Author
dgc
Date
2011-09-14 10:58:39 -0400 (Wed, 14 Sep 2011)

Log Message

added write API for ontologies and predicates

Modified Paths

Diff

Modified: trunk/app/models/predicate.rb (2701 => 2702)


--- trunk/app/models/predicate.rb	2011-09-13 21:55:40 UTC (rev 2701)
+++ trunk/app/models/predicate.rb	2011-09-14 14:58:39 UTC (rev 2702)
@@ -10,6 +10,7 @@
   format_attribute(:description)
 
   validates_presence_of(:title)
+  validates_presence_of(:ontology)
 
 end
 

Modified: trunk/config/routes.rb (2701 => 2702)


--- trunk/config/routes.rb	2011-09-13 21:55:40 UTC (rev 2701)
+++ trunk/config/routes.rb	2011-09-14 14:58:39 UTC (rev 2702)
@@ -61,6 +61,9 @@
                    :render_output => :get }
   end
   
+  # Ontologies
+  map.resources :ontologies
+
   # mashup
   map.resource :mashup
   

Modified: trunk/config/tables.xml


(Binary files differ)

Modified: trunk/lib/authorization.rb (2701 => 2702)


--- trunk/lib/authorization.rb	2011-09-13 21:55:40 UTC (rev 2701)
+++ trunk/lib/authorization.rb	2011-09-14 14:58:39 UTC (rev 2702)
@@ -552,16 +552,12 @@
 
         case action
 
-          when "destroy"
-            # Users can delete their own ontologies
-            is_authorized = Authorization.is_owner?(user_id, thing_instance)
-
           when "view"
             # All users can view
             is_authorized = true
 
-          when "edit"
-            # Users can edit their own ontologies
+          when "edit", "destroy"
+            # Users can edit and destroy their own ontologies
             is_authorized = Authorization.is_owner?(user_id, thing_instance)
         end
 
@@ -697,6 +693,8 @@
         is_authorized = (thing.user_id == user_id)
       when "ClientApplication"
         is_authorized = (thing.user_id == user_id)
+      when "Ontology"
+        is_authorized = (thing.user_id == user_id)
       #else
         # do nothing -- unknown "thing" types are not authorized by default 
     end

Modified: trunk/lib/rest.rb (2701 => 2702)


--- trunk/lib/rest.rb	2011-09-13 21:55:40 UTC (rev 2701)
+++ trunk/lib/rest.rb	2011-09-14 14:58:39 UTC (rev 2702)
@@ -578,7 +578,7 @@
     when 'ContentType';            return content_type_url(ob)
     when 'License';                return license_url(ob)
     when 'CurationEvent';          return nil
-    when 'Ontology';               return nil
+    when 'Ontology';               return ontology_url(ob)
     when 'Predicate';              return nil
     when 'Relationship';           return nil
 
@@ -737,6 +737,7 @@
   return [TavernaEnactor, $1, is_local] if uri.path =~ /^\/runners\/([\d]+)$/
   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]+)$/
 
   nil
 
@@ -1946,6 +1947,129 @@
   tagging_aux('destroy', opts)
 end
 
+# Ontologies
+
+def ontology_aux(action, opts)
+
+  # Obtain object
+
+  case action
+    when 'create':
+      return rest_response(401, :reason => "Not authorised to create an ontology") unless Authorization.is_authorized_for_type?('create', 'Ontology', opts[:user], nil)
+      ob = Ontology.new(:user => opts[:user])
+    when 'read', 'update', 'destroy':
+      ob = obtain_rest_resource('Ontology', 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
+
+    data = ""
+
+    title        = parse_element(data, :text, '/ontology/title')
+    description  = parse_element(data, :text, '/ontology/description')
+    uri          = parse_element(data, :text, '/ontology/uri')
+    prefix       = parse_element(data, :text, '/ontology/prefix')
+
+    # build the contributable
+
+    ob.title       = title       if title
+    ob.description = description if description
+    ob.uri         = uri         if uri
+    ob.prefix      = prefix      if prefix
+
+    if not ob.save
+      return rest_response(400, :object => ob)
+    end
+  end
+
+  rest_get_request(ob, "ontology", opts[:user],
+      rest_resource_uri(ob), "ontology", { "id" => ob.id.to_s })
+end
+
+def post_ontology(opts)
+  ontology_aux('create', opts)
+end
+
+def put_ontology(opts)
+  ontology_aux('update', opts)
+end
+
+def delete_ontology(opts)
+  ontology_aux('destroy', opts)
+end
+
+# Predicates
+
+def predicate_aux(action, opts)
+
+  if action != "destroy"
+
+    data = ""
+
+    title         = parse_element(data, :text,     '/predicate/title')
+     :resource, '/predicate/ontology')
+    description   = parse_element(data, :text,     '/predicate/description')
+    phrase        = parse_element(data, :text,     '/predicate/phrase')
+    equivalent_to = parse_element(data, :text,     '/predicate/equivalent-to')
+
+  end
+
+  # Obtain object
+
+  case action
+    when 'create':
+      return rest_response(401, :reason => "Not authorised to create a predicate") unless Authorization.is_authorized_for_type?('create', 'Predicate', opts[:user], ontology)
+      ob = Predicate.new
+    when 'read', 'update', 'destroy':
+      ob = obtain_rest_resource('Predicate', 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.title         = title         if title
+    ob.description   = description   if description
+    ob.phrase        = phrase        if phrase
+    ob.equivalent_to = equivalent_to if equivalent_to
+    ob.      if ontology
+
+    if not ob.save
+      return rest_response(400, :object => ob)
+    end
+  end
+
+  rest_get_request(ob, "predicate", opts[:user],
+      rest_resource_uri(ob), "predicate", { "id" => ob.id.to_s })
+end
+
+def post_predicate(opts)
+  predicate_aux('create', opts)
+end
+
+def put_predicate(opts)
+  predicate_aux('update', opts)
+end
+
+def delete_predicate(opts)
+  predicate_aux('destroy', opts)
+end
 # Call dispatcher
 
 def rest_call_request(req_uri, format, rules, user, query)

Modified: trunk/test/functional/api_controller_test.rb (2701 => 2702)


--- trunk/test/functional/api_controller_test.rb	2011-09-13 21:55:40 UTC (rev 2701)
+++ trunk/test/functional/api_controller_test.rb	2011-09-14 14:58:39 UTC (rev 2702)
@@ -3,12 +3,15 @@
 require 'xml/libxml'
 require 'lib/rest'
 
+include ActionView::Helpers::UrlHelper
+include ActionController::UrlWriter
+
 # Re-raise errors caught by the controller.
 class ApiController; def rescue_action(e) raise e end; end
 
 class ApiControllerTest < Test::Unit::TestCase
 
-  fixtures :workflows, :users, :content_types, :licenses
+  fixtures :workflows, :users, :content_types, :licenses, :ontologies
 
   def setup
     @controller = ApiController.new
@@ -694,6 +697,158 @@
     assert_response(:not_found)
   end
 
+  def test_ontologies
+
+    existing_ontologies = Ontology.find(:all)
+
+    login_as(:john)
+
+    title       = "Test ontology title"
+    title2      = "Updated test ontology title"
+    uri         = "http://example.com/ontology"
+    prefix      = "test prefix"
+    description = "A description of the ontology."
+
+    # post an ontology
+
+    rest_request(:post, 'ontology', "<?xml version='1.0'?>
+      <ontology>
+        <title>#{title}</title>
+        <description>#{description}</description>
+        <uri>#{uri}</uri>
+        <prefix>#{prefix}</prefix>
+      </ontology>")
+
+    assert_response(:success)
+
+    extra_ontologies = Ontology.find(:all) - existing_ontologies
+
+    assert_equal(extra_ontologies.length, 1)
+
+    
+
+    # get the ontology
+
+    response = rest_request(:get, 'ontology', nil, "id" => ontology.id,
+        "elements" => "title,description,uri,prefix")
+
+    assert_response(:success)
+
+    assert_equal(title,       response.find_first('/ontology/title').inner_xml)
+    assert_equal(description, response.find_first('/ontology/description').inner_xml)
+    assert_equal(uri,         response.find_first('/ontology/uri').inner_xml)
+    assert_equal(prefix,      response.find_first('/ontology/prefix').inner_xml)
+
+    # update the ontology
+
+    rest_request(:put, 'ontology', "<?xml version='1.0'?>
+      <ontology>
+        <title>#{title2}</title>
+      </ontology>", "id" => ontology.id)
+
+    assert_response(:success)
+
+    # get the updated ontology
+
+    response = rest_request(:get, 'ontology', nil, "id" => ontology.id,
+        "elements" => "title,description")
+
+    assert_response(:success)
+  
+    assert_equal(title2,      response.find_first('/ontology/title').inner_xml)
+    assert_equal(description, response.find_first('/ontology/description').inner_xml)
+
+    # delete the ontology
+
+    rest_request(:delete, 'ontology', nil, "id" => ontology.id)
+
+    assert_response(:success)
+
+    # try to get the deleted ontology
+
+    rest_request(:get, 'ontology', nil, "id" => ontology.id)
+
+    assert_response(:not_found)
+  end
+
+  def test_predicates
+
+    existing_predicates = Predicate.find(:all)
+
+    login_as(:john)
+
+    title            = "Test predicate title"
+    title2           = "Updated test predicate title"
+    
+    
+    phrase           = "test phrase"
+    description      = "A description of the predicate."
+    description_html = "<p>A description of the predicate.</p>"
+    equivalent_to    = "test equivalent_to"
+
+    # post a predicate
+
+    rest_request(:post, 'predicate', "<?xml version='1.0'?>
+      <predicate>
+        <title>#{title}</title>
+        <ontology resource='#{ontology_url}'/>
+        <phrase>#{phrase}</phrase>
+        <description>#{description}</description>
+        <equivalent-to>#{equivalent_to}</equivalent-to>
+      </predicate>")
+
+    assert_response(:success)
+
+    extra_predicates = Predicate.find(:all) - existing_predicates
+
+    assert_equal(extra_predicates.length, 1)
+
+    predicate = extra_predicates.first
+
+    # get the predicate
+
+    response = rest_request(:get, 'predicate', nil, "id" => predicate.id,
+        "elements" => "title,description,phrase,equivalent-to")
+
+    assert_response(:success)
+
+    assert_equal(title,         response.find_first('/predicate/title').inner_xml)
+    assert_equal(description,   response.find_first('/predicate/description').inner_xml)
+    assert_equal(phrase,        response.find_first('/predicate/phrase').inner_xml)
+    assert_equal(equivalent_to, response.find_first('/predicate/equivalent-to').inner_xml)
+
+    # update the predicate
+
+    rest_request(:put, 'predicate', "<?xml version='1.0'?>
+      <predicate>
+        <title>#{title2}</title>
+      </predicate>", "id" => predicate.id)
+
+    assert_response(:success)
+
+    # get the updated predicate
+
+    response = rest_request(:get, 'predicate', nil, "id" => predicate.id,
+        "elements" => "title,description")
+
+    assert_response(:success)
+  
+    assert_equal(title2,      response.find_first('/predicate/title').inner_xml)
+    assert_equal(description, response.find_first('/predicate/description').inner_xml)
+
+    # delete the predicate
+
+    rest_request(:delete, 'predicate', nil, "id" => predicate.id)
+
+    assert_response(:success)
+
+    # try to get the deleted predicate
+
+    rest_request(:get, 'predicate', nil, "id" => predicate.id)
+
+    assert_response(:not_found)
+  end
+
   private
 
   def rest_request(method, uri, data = "" query = {})

reply via email to

[Prev in Thread] Current Thread [Next in Thread]