Skip to content

Commit

Permalink
added relationships API
Browse files Browse the repository at this point in the history
git-svn-id: http://myexperiment.rubyforge.org/svn/trunk@2709 4f7b4649-9ca9-420a-81b3-55a188e4d46f
  • Loading branch information
dgc committed Sep 20, 2011
1 parent fd7beb9 commit 470ca51
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 25 deletions.
3 changes: 3 additions & 0 deletions config/routes.rb
Expand Up @@ -64,6 +64,9 @@
# Ontologies
map.resources :ontologies

# Predicates
map.resources :predicates

# mashup
map.resource :mashup

Expand Down
78 changes: 55 additions & 23 deletions config/tables.xml
Expand Up @@ -42,6 +42,7 @@
<WindowWidth>19440</WindowWidth>
<WindowTopX>510</WindowTopX>
<WindowTopY>135</WindowTopY>
<ActiveSheet>1</ActiveSheet>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
Expand Down Expand Up @@ -111,7 +112,7 @@
</Style>
</Styles>
<Worksheet ss:Name="REST">
<Table ss:ExpandedColumnCount="20" ss:ExpandedRowCount="179" x:FullColumns="1"
<Table ss:ExpandedColumnCount="20" ss:ExpandedRowCount="185" x:FullColumns="1"
x:FullRows="1" ss:StyleID="s62" ss:DefaultRowHeight="15">
<Column ss:StyleID="s66" ss:Width="42"/>
<Column ss:StyleID="s66" ss:AutoFitWidth="0" ss:Width="109.5"/>
Expand Down Expand Up @@ -1785,33 +1786,75 @@
<Cell ss:Index="15"><Data ss:Type="String">yes</Data></Cell>
<Cell><Data ss:Type="String">no</Data></Cell>
</Row>
<Row ss:Index="181" ss:AutoFitHeight="0">
<Cell><Data ss:Type="String">POST</Data></Cell>
<Cell><Data ss:Type="String">relationship</Data></Cell>
<Cell ss:Index="4"><Data ss:Type="String">user</Data></Cell>
<Cell><Data ss:Type="String">view</Data></Cell>
<Cell><Data ss:Type="String">context</Data></Cell>
<Cell ss:Index="8"><Data ss:Type="String">yes</Data></Cell>
<Cell ss:Index="11"><Data ss:Type="String">call</Data></Cell>
<Cell><Data ss:Type="String">relationship</Data></Cell>
<Cell><Data ss:Type="String">relationship</Data></Cell>
<Cell ss:Index="15"><Data ss:Type="String">no</Data></Cell>
<Cell><Data ss:Type="String">no</Data></Cell>
<Cell><Data ss:Type="String">post_relationship</Data></Cell>
</Row>
<Row ss:Index="183" ss:AutoFitHeight="0">
<Cell><Data ss:Type="String">PUT</Data></Cell>
<Cell><Data ss:Type="String">relationship</Data></Cell>
<Cell ss:Index="4"><Data ss:Type="String">user</Data></Cell>
<Cell><Data ss:Type="String">view</Data></Cell>
<Cell><Data ss:Type="String">context</Data></Cell>
<Cell ss:Index="8"><Data ss:Type="String">yes</Data></Cell>
<Cell ss:Index="11"><Data ss:Type="String">call</Data></Cell>
<Cell><Data ss:Type="String">relationship</Data></Cell>
<Cell><Data ss:Type="String">relationship</Data></Cell>
<Cell ss:Index="15"><Data ss:Type="String">required</Data></Cell>
<Cell><Data ss:Type="String">no</Data></Cell>
<Cell><Data ss:Type="String">put_relationship</Data></Cell>
</Row>
<Row ss:Index="185" ss:AutoFitHeight="0">
<Cell><Data ss:Type="String">DELETE</Data></Cell>
<Cell><Data ss:Type="String">relationship</Data></Cell>
<Cell ss:Index="4"><Data ss:Type="String">user</Data></Cell>
<Cell><Data ss:Type="String">view</Data></Cell>
<Cell><Data ss:Type="String">context</Data></Cell>
<Cell ss:Index="8"><Data ss:Type="String">yes</Data></Cell>
<Cell ss:Index="11"><Data ss:Type="String">call</Data></Cell>
<Cell><Data ss:Type="String">relationship</Data></Cell>
<Cell><Data ss:Type="String">relationship</Data></Cell>
<Cell ss:Index="15"><Data ss:Type="String">required</Data></Cell>
<Cell><Data ss:Type="String">no</Data></Cell>
<Cell><Data ss:Type="String">delete_relationship</Data></Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<Unsynced/>
<Zoom>80</Zoom>
<Selected/>
<FreezePanes/>
<FrozenNoSplit/>
<SplitHorizontal>1</SplitHorizontal>
<TopRowBottomPane>153</TopRowBottomPane>
<TopRowBottomPane>160</TopRowBottomPane>
<SplitVertical>2</SplitVertical>
<LeftColumnRightPane>2</LeftColumnRightPane>
<LeftColumnRightPane>6</LeftColumnRightPane>
<ActivePane>0</ActivePane>
<Panes>
<Pane>
<Number>3</Number>
</Pane>
<Pane>
<Number>1</Number>
<ActiveCol>2</ActiveCol>
</Pane>
<Pane>
<Number>2</Number>
<ActiveRow>1</ActiveRow>
</Pane>
<Pane>
<Number>0</Number>
<ActiveRow>174</ActiveRow>
<ActiveCol>0</ActiveCol>
<ActiveRow>191</ActiveRow>
<ActiveCol>12</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
Expand All @@ -1820,7 +1863,7 @@
<x:WorksheetOptions/>
</Worksheet>
<Worksheet ss:Name="Model">
<Table ss:ExpandedColumnCount="27" ss:ExpandedRowCount="326" x:FullColumns="1"
<Table ss:ExpandedColumnCount="27" ss:ExpandedRowCount="325" x:FullColumns="1"
x:FullRows="1" ss:StyleID="s62" ss:DefaultRowHeight="15">
<Column ss:StyleID="s66" ss:Width="111.75"/>
<Column ss:StyleID="s66" ss:Width="116.25"/>
Expand Down Expand Up @@ -5926,19 +5969,6 @@
<Cell ss:Index="12"><Data ss:Type="String">yes</Data></Cell>
<Cell ss:Index="14"><Data ss:Type="String">user</Data></Cell>
</Row>
<Row ss:AutoFitHeight="0">
<Cell><Data ss:Type="String">relationship</Data></Cell>
<Cell><Data ss:Type="String">phrase</Data></Cell>
<Cell ss:Index="4"><Data ss:Type="String">Relationship</Data></Cell>
<Cell><Data ss:Type="String">predicate.phrase</Data></Cell>
<Cell><Data ss:Type="String">no</Data></Cell>
<Cell><Data ss:Type="String">text</Data></Cell>
<Cell><Data ss:Type="String">no</Data></Cell>
<Cell><Data ss:Type="String">yes</Data></Cell>
<Cell><Data ss:Type="String">no</Data></Cell>
<Cell ss:Index="12"><Data ss:Type="String">yes</Data></Cell>
<Cell ss:Index="14"><Data ss:Type="String">user</Data></Cell>
</Row>
<Row ss:AutoFitHeight="0">
<Cell><Data ss:Type="String">relationship</Data></Cell>
<Cell><Data ss:Type="String">created-at</Data></Cell>
Expand All @@ -5962,10 +5992,11 @@
<VerticalResolution>600</VerticalResolution>
</Print>
<Zoom>80</Zoom>
<Selected/>
<FreezePanes/>
<FrozenNoSplit/>
<SplitHorizontal>1</SplitHorizontal>
<TopRowBottomPane>186</TopRowBottomPane>
<TopRowBottomPane>305</TopRowBottomPane>
<SplitVertical>2</SplitVertical>
<LeftColumnRightPane>2</LeftColumnRightPane>
<ActivePane>0</ActivePane>
Expand All @@ -5982,8 +6013,9 @@
</Pane>
<Pane>
<Number>0</Number>
<ActiveRow>217</ActiveRow>
<ActiveCol>4</ActiveCol>
<ActiveRow>324</ActiveRow>
<ActiveCol>0</ActiveCol>
<RangeSelection>R325</RangeSelection>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
Expand Down
66 changes: 65 additions & 1 deletion lib/rest.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
56 changes: 55 additions & 1 deletion test/functional/api_controller_test.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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', "<?xml version='1.0'?>
<relationship>
<subject resource='#{subject_url}'/>
<predicate resource='#{predicate_url}'/>
<object resource='#{objekt_url}'/>
<context resource='#{context_url}'/>
</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 = {})
Expand Down

0 comments on commit 470ca51

Please sign in to comment.