first pass integration of relationship mapper
[scpubgit/stemmatology.git] / stemmaweb / lib / stemmaweb / Controller / Relation.pm
1 package stemmaweb::Controller::Relation;
2 use Moose;
3 use namespace::autoclean;
4 use TryCatch;
5
6 BEGIN { extends 'Catalyst::Controller' }
7
8
9 =head1 NAME
10
11 stemmaweb::Controller::Relation - Controller for the relationship mapper
12
13 =head1 DESCRIPTION
14
15 The reading relationship mapper with draggable nodes.
16
17 =head1 METHODS
18
19 =head2 index
20
21  GET relation/$textid
22  
23 Renders the application for the text identified by $textid.
24
25 =cut
26
27 sub index :Path :Args(1) {
28         my( $self, $c, $textid ) = @_;
29         my $m = $c->model('Directory');
30         my $tradition = $m->tradition( $textid );
31         my $collation = $tradition->collation;
32         my $svg_str = $collation->as_svg;
33         $svg_str =~ s/\n//gs;
34         $c->stash->{'svg_string'} = $svg_str;
35         $c->stash->{'template'} = 'relate.tt';
36 }
37
38 sub dispatcher :Path :Args(2) {
39         my( $self, $c, $textid, $forward ) = @_;
40         $c->stash->{'collation'} = $c->model('Directory')->tradition( $textid )->collation;
41         $c->forward( $forward );        
42 }
43
44 =head2 relationship_definition
45
46  GET relation/relationship_definition
47  
48 Returns a data structure giving the valid types and scopes for a relationship.
49
50 =cut
51
52 sub relationship_definition :Local :Args(0) {
53         my( $self, $c ) = @_;
54         my $valid_relationships = [ qw/ spelling orthographic grammatical meaning / ];
55         my $valid_scopes = [ qw/ local global / ];
56         $c->stash->{'result'} = { 'types' => $valid_relationships, 'scopes' => $valid_scopes };
57         $c->forward('View::JSON');
58 }
59
60 =head2 set_relationship
61
62  POST relation/$textid/relationship
63    source_id: $source, target_id: $target, rel_type: $type, scope: $scope
64    
65 Sets the specified relationship between the readings in $source and $target.
66 Returns 200 and a list of node pairs where the relationship was added on success;
67 returns 403 and an { error: message } struct on failure.
68
69 =cut
70
71 sub relationship :Private {
72         my( $self, $c ) = @_;
73         my $collation = delete $c->stash->{'collation'};
74         my $node = $c->request->param('source_id');
75         my $target = $c->request->param('target_id');
76         my $relation = $c->request->param('rel_type');
77         my $note = $c->request->param('note');
78         my $scope = $c->request->param('scope');
79
80         my $opts = { 'type' => $relation,
81                                  'scope' => $scope };
82         
83         try {
84                 my @vectors = $collation->add_relationship( $node, $target, $opts );
85                 $c->stash->{'result'} = \@vectors;
86         } catch( Text::Tradition::Error $e ) {
87                 $c->response->status( '403' );
88                 $c->stash->{'result'} = { 'error' => $e->message };
89         }
90         $c->forward('View::JSON');
91 }
92
93 =head2 relationships
94
95  GET relation/$textid/relationships
96
97 Returns a list of relationships that exist in the specified text. Each
98 relationship is returned in a struct that looks like:
99
100 { source: $sid, target: $tid, type: $rel_type, scope: $rel_scope }
101
102 =cut
103
104 sub get_relationships :Private {
105         my( $self, $c ) = @_;
106         my $collation = delete $c->stash->{'collation'};
107         # TODO make this API
108         my @pairs = $collation->relationships; # returns the edges
109         my @all_relations;
110         foreach my $p ( @pairs ) {
111                 my $relobj = $collation->relations->get_relationship( @$p );
112                 push( @all_relations, 
113                         { source => $p->[0], target => $p->[1], 
114                           type => $relobj->type, scope => $relobj->scope } );
115         }
116         $c->stash->{'result'} = \@all_relations;
117         $c->forward('View::JSON');
118 }               
119
120
121 =head2 end
122
123 Attempt to render a view, if needed.
124
125 =cut
126
127 sub end : ActionClass('RenderView') {}
128
129 =head1 AUTHOR
130
131 Tara L Andrews
132
133 =head1 LICENSE
134
135 This library is free software. You can redistribute it and/or modify
136 it under the same terms as Perl itself.
137
138 =cut
139
140 __PACKAGE__->meta->make_immutable;
141
142 1;