Commit | Line | Data |
b8a92065 |
1 | package stemmaweb::Controller::Relation; |
2 | use Moose; |
3 | use namespace::autoclean; |
b28e606e |
4 | use TryCatch; |
b8a92065 |
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 | |
b28e606e |
15 | The reading relationship mapper with draggable nodes. |
b8a92065 |
16 | |
17 | =head1 METHODS |
18 | |
b28e606e |
19 | =head2 index |
20 | |
b8a92065 |
21 | GET relation/$textid |
22 | |
23 | Renders the application for the text identified by $textid. |
24 | |
b8a92065 |
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 ); |
b28e606e |
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 ) = @_; |
9c2e7b80 |
40 | $c->stash->{'tradition'} = $c->model('Directory')->tradition( $textid ); |
b28e606e |
41 | $c->forward( $forward ); |
42 | } |
43 | |
44 | =head2 relationship_definition |
45 | |
9c2e7b80 |
46 | GET relation/definitions |
b28e606e |
47 | |
48 | Returns a data structure giving the valid types and scopes for a relationship. |
49 | |
50 | =cut |
51 | |
9c2e7b80 |
52 | sub definitions :Local :Args(0) { |
b28e606e |
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'); |
b8a92065 |
58 | } |
59 | |
9c2e7b80 |
60 | =head2 relationship |
b28e606e |
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 ) = @_; |
9c2e7b80 |
73 | my $tradition = delete $c->stash->{'tradition'}; |
74 | my $collation = $tradition->collation; |
b28e606e |
75 | my $node = $c->request->param('source_id'); |
76 | my $target = $c->request->param('target_id'); |
77 | my $relation = $c->request->param('rel_type'); |
78 | my $note = $c->request->param('note'); |
79 | my $scope = $c->request->param('scope'); |
80 | |
81 | my $opts = { 'type' => $relation, |
82 | 'scope' => $scope }; |
83 | |
84 | try { |
85 | my @vectors = $collation->add_relationship( $node, $target, $opts ); |
9c2e7b80 |
86 | my $m = $c->model('Directory'); |
87 | $m->save( $tradition ); |
b28e606e |
88 | $c->stash->{'result'} = \@vectors; |
89 | } catch( Text::Tradition::Error $e ) { |
90 | $c->response->status( '403' ); |
91 | $c->stash->{'result'} = { 'error' => $e->message }; |
92 | } |
93 | $c->forward('View::JSON'); |
94 | } |
95 | |
96 | =head2 relationships |
97 | |
98 | GET relation/$textid/relationships |
99 | |
100 | Returns a list of relationships that exist in the specified text. Each |
101 | relationship is returned in a struct that looks like: |
102 | |
103 | { source: $sid, target: $tid, type: $rel_type, scope: $rel_scope } |
104 | |
105 | =cut |
106 | |
9c2e7b80 |
107 | sub relationships :Private { |
b28e606e |
108 | my( $self, $c ) = @_; |
109 | my $collation = delete $c->stash->{'collation'}; |
110 | # TODO make this API |
111 | my @pairs = $collation->relationships; # returns the edges |
112 | my @all_relations; |
113 | foreach my $p ( @pairs ) { |
114 | my $relobj = $collation->relations->get_relationship( @$p ); |
115 | push( @all_relations, |
116 | { source => $p->[0], target => $p->[1], |
117 | type => $relobj->type, scope => $relobj->scope } ); |
118 | } |
119 | $c->stash->{'result'} = \@all_relations; |
120 | $c->forward('View::JSON'); |
121 | } |
122 | |
123 | |
b8a92065 |
124 | =head2 end |
125 | |
126 | Attempt to render a view, if needed. |
127 | |
128 | =cut |
129 | |
130 | sub end : ActionClass('RenderView') {} |
131 | |
132 | =head1 AUTHOR |
133 | |
134 | Tara L Andrews |
135 | |
136 | =head1 LICENSE |
137 | |
138 | This library is free software. You can redistribute it and/or modify |
139 | it under the same terms as Perl itself. |
140 | |
141 | =cut |
142 | |
143 | __PACKAGE__->meta->make_immutable; |
144 | |
145 | 1; |