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 | |
e847b186 |
32 | =head2 help |
33 | |
34 | GET relation/help |
35 | |
36 | Returns the help window HTML. |
37 | |
38 | =cut |
39 | |
40 | sub help :Local :Args(0) { |
41 | my( $self, $c ) = @_; |
42 | $c->stash->{'template'} = 'relatehelp.tt'; |
43 | } |
44 | |
9529f69c |
45 | =head2 definitions |
b28e606e |
46 | |
9c2e7b80 |
47 | GET relation/definitions |
b28e606e |
48 | |
49 | Returns a data structure giving the valid types and scopes for a relationship. |
50 | |
51 | =cut |
52 | |
9c2e7b80 |
53 | sub definitions :Local :Args(0) { |
b28e606e |
54 | my( $self, $c ) = @_; |
17b660e6 |
55 | my $valid_relationships = [ qw/ spelling orthographic grammatical lexical transposition / ]; |
b28e606e |
56 | my $valid_scopes = [ qw/ local global / ]; |
57 | $c->stash->{'result'} = { 'types' => $valid_relationships, 'scopes' => $valid_scopes }; |
58 | $c->forward('View::JSON'); |
b8a92065 |
59 | } |
60 | |
9529f69c |
61 | =head2 text |
b28e606e |
62 | |
9529f69c |
63 | GET relation/$textid/ |
64 | |
65 | Runs the relationship mapper for the specified text ID. |
66 | |
b28e606e |
67 | =cut |
68 | |
9529f69c |
69 | sub text :Chained('/') :PathPart('relation') :CaptureArgs(1) { |
70 | my( $self, $c, $textid ) = @_; |
13aa153c |
71 | # If the tradition has more than 500 ranks or so, split it up. |
72 | my $tradition = $c->model('Directory')->tradition( $textid ); |
d58766c0 |
73 | # See how big the tradition is. Edges are more important than nodes |
74 | # when it comes to rendering difficulty. |
75 | my $numnodes = scalar $tradition->collation->readings; |
76 | my $numedges = scalar $tradition->collation->paths; |
13aa153c |
77 | my $length = $tradition->collation->end->rank; |
d58766c0 |
78 | # We should display no more than roughly 500 nodes, or roughly 700 |
79 | # edges, at a time. |
80 | my $segments = $numnodes / 500; |
81 | if( $numedges / 700 > $segments ) { |
82 | $segments = $numedges / 700; |
83 | } |
84 | my $segsize = sprintf( "%.0f", $length / $segments ); |
85 | my $margin = sprintf( "%.0f", $segsize / 10 ); |
86 | if( $segments > 1 ) { |
13aa153c |
87 | # Segment the tradition in order not to overload the browser. |
13aa153c |
88 | my @divs; |
89 | my $r = 0; |
d58766c0 |
90 | while( $r + $margin < $length ) { |
13aa153c |
91 | push( @divs, $r ); |
d58766c0 |
92 | $r += $segsize; |
13aa153c |
93 | } |
94 | $c->stash->{'textsegments'} = []; |
d58766c0 |
95 | $c->stash->{'segsize'} = $segsize; |
96 | $c->stash->{'margin'} = $margin; |
ea8e8b3c |
97 | foreach my $i ( 0..$#divs ) { |
98 | my $seg = { 'start' => $divs[$i] }; |
99 | $seg->{'display'} = "Segment " . ($i+1); |
13aa153c |
100 | push( @{$c->stash->{'textsegments'}}, $seg ); |
101 | } |
102 | } |
ea8e8b3c |
103 | $DB::single = 1; |
13aa153c |
104 | $c->stash->{'textid'} = $textid; |
105 | $c->stash->{'tradition'} = $tradition; |
9529f69c |
106 | } |
107 | |
108 | sub main :Chained('text') :PathPart('') :Args(0) { |
b28e606e |
109 | my( $self, $c ) = @_; |
13aa153c |
110 | my $startseg = $c->req->param('start'); |
9c2e7b80 |
111 | my $tradition = delete $c->stash->{'tradition'}; |
112 | my $collation = $tradition->collation; |
13aa153c |
113 | my $svgopts; |
114 | if( $startseg ) { |
d58766c0 |
115 | # Only render the subgraph from startseg to endseg or to END, |
13aa153c |
116 | # whichever is less. |
d58766c0 |
117 | my $endseg = $startseg + $c->stash->{'segsize'} + $c->stash->{'margin'}; |
13aa153c |
118 | $svgopts = { 'from' => $startseg }; |
d58766c0 |
119 | $svgopts->{'to'} = $endseg if $endseg < $collation->end->rank; |
13aa153c |
120 | } elsif( exists $c->stash->{'textsegments'} ) { |
121 | # This is the unqualified load of a long tradition. We implicitly start |
122 | # at zero, but go only as far as 550. |
d58766c0 |
123 | my $endseg = $c->stash->{'segsize'} + $c->stash->{'margin'}; |
ea8e8b3c |
124 | $startseg = 0; |
d58766c0 |
125 | $svgopts = { 'to' => $endseg }; |
13aa153c |
126 | } |
127 | my $svg_str = $collation->as_svg( $svgopts ); |
9529f69c |
128 | $svg_str =~ s/\n//gs; |
ea8e8b3c |
129 | $c->stash->{'startseg'} = $startseg if defined $startseg; |
9529f69c |
130 | $c->stash->{'svg_string'} = $svg_str; |
131 | $c->stash->{'text_title'} = $tradition->name; |
132 | $c->stash->{'template'} = 'relate.tt'; |
b28e606e |
133 | } |
134 | |
135 | =head2 relationships |
136 | |
13aa153c |
137 | GET relation/$textid/relationships |
9529f69c |
138 | |
139 | Returns the list of relationships defined for this text. |
b28e606e |
140 | |
13aa153c |
141 | POST relation/$textid/relationships { request } |
9529f69c |
142 | |
143 | Attempts to define the requested relationship within the text. Returns 200 on |
144 | success or 403 on error. |
b28e606e |
145 | |
13aa153c |
146 | DELETE relation/$textid/relationships { request } |
9529f69c |
147 | |
b28e606e |
148 | |
149 | =cut |
150 | |
9529f69c |
151 | sub relationships :Chained('text') :PathPart :Args(0) { |
b28e606e |
152 | my( $self, $c ) = @_; |
6d124a83 |
153 | my $tradition = delete $c->stash->{'tradition'}; |
154 | my $collation = $tradition->collation; |
cdd592f3 |
155 | my $m = $c->model('Directory'); |
9529f69c |
156 | if( $c->request->method eq 'GET' ) { |
157 | my @pairs = $collation->relationships; # returns the edges |
158 | my @all_relations; |
159 | foreach my $p ( @pairs ) { |
160 | my $relobj = $collation->relations->get_relationship( @$p ); |
545163a2 |
161 | next if $relobj->type eq 'collated'; # Don't show these |
69a19c91 |
162 | my $relhash = { source => $p->[0], target => $p->[1], |
163 | type => $relobj->type, scope => $relobj->scope }; |
164 | $relhash->{'note'} = $relobj->annotation if $relobj->has_annotation; |
165 | push( @all_relations, $relhash ); |
9529f69c |
166 | } |
167 | $c->stash->{'result'} = \@all_relations; |
168 | } elsif( $c->request->method eq 'POST' ) { |
169 | my $node = $c->request->param('source_id'); |
170 | my $target = $c->request->param('target_id'); |
171 | my $relation = $c->request->param('rel_type'); |
172 | my $note = $c->request->param('note'); |
173 | my $scope = $c->request->param('scope'); |
174 | |
175 | my $opts = { 'type' => $relation, |
69a19c91 |
176 | 'scope' => $scope }; |
177 | $opts->{'annotation'} = $note if $note; |
9529f69c |
178 | |
179 | try { |
180 | my @vectors = $collation->add_relationship( $node, $target, $opts ); |
181 | $c->stash->{'result'} = \@vectors; |
cdd592f3 |
182 | $m->save( $tradition ); |
9529f69c |
183 | } catch( Text::Tradition::Error $e ) { |
184 | $c->response->status( '403' ); |
185 | $c->stash->{'result'} = { 'error' => $e->message }; |
186 | } |
187 | } elsif( $c->request->method eq 'DELETE' ) { |
188 | my $node = $c->request->param('source_id'); |
189 | my $target = $c->request->param('target_id'); |
190 | |
191 | try { |
192 | my @vectors = $collation->del_relationship( $node, $target ); |
cdd592f3 |
193 | $m->save( $tradition ); |
9529f69c |
194 | $c->stash->{'result'} = \@vectors; |
195 | } catch( Text::Tradition::Error $e ) { |
196 | $c->response->status( '403' ); |
197 | $c->stash->{'result'} = { 'error' => $e->message }; |
198 | } |
b28e606e |
199 | } |
b28e606e |
200 | $c->forward('View::JSON'); |
201 | } |
9529f69c |
202 | |
b28e606e |
203 | |
b8a92065 |
204 | =head2 end |
205 | |
206 | Attempt to render a view, if needed. |
207 | |
208 | =cut |
209 | |
210 | sub end : ActionClass('RenderView') {} |
211 | |
212 | =head1 AUTHOR |
213 | |
214 | Tara L Andrews |
215 | |
216 | =head1 LICENSE |
217 | |
218 | This library is free software. You can redistribute it and/or modify |
219 | it under the same terms as Perl itself. |
220 | |
221 | =cut |
222 | |
223 | __PACKAGE__->meta->make_immutable; |
224 | |
225 | 1; |