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 | |
9529f69c |
27 | sub index :Path :Args(0) { |
28 | my( $self, $c ) = @_; |
b28e606e |
29 | $c->stash->{'template'} = 'relate.tt'; |
30 | } |
31 | |
9529f69c |
32 | =head2 definitions |
b28e606e |
33 | |
9c2e7b80 |
34 | GET relation/definitions |
b28e606e |
35 | |
36 | Returns a data structure giving the valid types and scopes for a relationship. |
37 | |
38 | =cut |
39 | |
9c2e7b80 |
40 | sub definitions :Local :Args(0) { |
b28e606e |
41 | my( $self, $c ) = @_; |
9529f69c |
42 | my $valid_relationships = [ qw/ spelling orthographic grammatical meaning |
43 | lexical transposition / ]; |
b28e606e |
44 | my $valid_scopes = [ qw/ local global / ]; |
45 | $c->stash->{'result'} = { 'types' => $valid_relationships, 'scopes' => $valid_scopes }; |
46 | $c->forward('View::JSON'); |
b8a92065 |
47 | } |
48 | |
9529f69c |
49 | =head2 text |
b28e606e |
50 | |
9529f69c |
51 | GET relation/$textid/ |
52 | |
53 | Runs the relationship mapper for the specified text ID. |
54 | |
b28e606e |
55 | =cut |
56 | |
9529f69c |
57 | sub text :Chained('/') :PathPart('relation') :CaptureArgs(1) { |
58 | my( $self, $c, $textid ) = @_; |
59 | $c->stash->{'tradition'} = $c->model('Directory')->tradition( $textid ); |
60 | } |
61 | |
62 | sub main :Chained('text') :PathPart('') :Args(0) { |
b28e606e |
63 | my( $self, $c ) = @_; |
9c2e7b80 |
64 | my $tradition = delete $c->stash->{'tradition'}; |
65 | my $collation = $tradition->collation; |
9529f69c |
66 | my $svg_str = $collation->as_svg; |
67 | $svg_str =~ s/\n//gs; |
68 | $c->stash->{'svg_string'} = $svg_str; |
69 | $c->stash->{'text_title'} = $tradition->name; |
70 | $c->stash->{'template'} = 'relate.tt'; |
71 | |
b28e606e |
72 | } |
73 | |
74 | =head2 relationships |
75 | |
9529f69c |
76 | GET $textid/relationships |
77 | |
78 | Returns the list of relationships defined for this text. |
b28e606e |
79 | |
9529f69c |
80 | POST $textid/relationships { request } |
81 | |
82 | Attempts to define the requested relationship within the text. Returns 200 on |
83 | success or 403 on error. |
b28e606e |
84 | |
9529f69c |
85 | DELETE $textid/relationships { request } |
86 | |
b28e606e |
87 | |
88 | =cut |
89 | |
9529f69c |
90 | sub relationships :Chained('text') :PathPart :Args(0) { |
b28e606e |
91 | my( $self, $c ) = @_; |
6d124a83 |
92 | my $tradition = delete $c->stash->{'tradition'}; |
93 | my $collation = $tradition->collation; |
cdd592f3 |
94 | my $m = $c->model('Directory'); |
9529f69c |
95 | if( $c->request->method eq 'GET' ) { |
96 | my @pairs = $collation->relationships; # returns the edges |
97 | my @all_relations; |
98 | foreach my $p ( @pairs ) { |
99 | my $relobj = $collation->relations->get_relationship( @$p ); |
100 | push( @all_relations, |
101 | { source => $p->[0], target => $p->[1], |
102 | type => $relobj->type, scope => $relobj->scope } ); |
103 | } |
104 | $c->stash->{'result'} = \@all_relations; |
105 | } elsif( $c->request->method eq 'POST' ) { |
106 | my $node = $c->request->param('source_id'); |
107 | my $target = $c->request->param('target_id'); |
108 | my $relation = $c->request->param('rel_type'); |
109 | my $note = $c->request->param('note'); |
110 | my $scope = $c->request->param('scope'); |
111 | |
112 | my $opts = { 'type' => $relation, |
113 | 'scope' => $scope }; |
114 | |
115 | try { |
116 | my @vectors = $collation->add_relationship( $node, $target, $opts ); |
117 | $c->stash->{'result'} = \@vectors; |
cdd592f3 |
118 | $m->save( $tradition ); |
9529f69c |
119 | } catch( Text::Tradition::Error $e ) { |
120 | $c->response->status( '403' ); |
121 | $c->stash->{'result'} = { 'error' => $e->message }; |
122 | } |
123 | } elsif( $c->request->method eq 'DELETE' ) { |
124 | my $node = $c->request->param('source_id'); |
125 | my $target = $c->request->param('target_id'); |
126 | |
127 | try { |
128 | my @vectors = $collation->del_relationship( $node, $target ); |
cdd592f3 |
129 | $m->save( $tradition ); |
9529f69c |
130 | $c->stash->{'result'} = \@vectors; |
131 | } catch( Text::Tradition::Error $e ) { |
132 | $c->response->status( '403' ); |
133 | $c->stash->{'result'} = { 'error' => $e->message }; |
134 | } |
b28e606e |
135 | } |
b28e606e |
136 | $c->forward('View::JSON'); |
137 | } |
9529f69c |
138 | |
b28e606e |
139 | |
b8a92065 |
140 | =head2 end |
141 | |
142 | Attempt to render a view, if needed. |
143 | |
144 | =cut |
145 | |
146 | sub end : ActionClass('RenderView') {} |
147 | |
148 | =head1 AUTHOR |
149 | |
150 | Tara L Andrews |
151 | |
152 | =head1 LICENSE |
153 | |
154 | This library is free software. You can redistribute it and/or modify |
155 | it under the same terms as Perl itself. |
156 | |
157 | =cut |
158 | |
159 | __PACKAGE__->meta->make_immutable; |
160 | |
161 | 1; |