Commit | Line | Data |
5c9ecf66 |
1 | package stemmaweb::Controller::Relation; |
2376359f |
2 | use Moose; |
3 | use namespace::autoclean; |
581aee24 |
4 | use TryCatch; |
2376359f |
5 | |
6 | BEGIN { extends 'Catalyst::Controller' } |
7 | |
8 | |
9 | =head1 NAME |
10 | |
5c9ecf66 |
11 | stemmaweb::Controller::Relation - Controller for the relationship mapper |
2376359f |
12 | |
13 | =head1 DESCRIPTION |
14 | |
581aee24 |
15 | The reading relationship mapper with draggable nodes. |
2376359f |
16 | |
17 | =head1 METHODS |
18 | |
581aee24 |
19 | =head2 index |
20 | |
2376359f |
21 | GET relation/$textid |
22 | |
23 | Renders the application for the text identified by $textid. |
24 | |
2376359f |
25 | =cut |
26 | |
72874569 |
27 | sub index :Path :Args(0) { |
28 | my( $self, $c ) = @_; |
581aee24 |
29 | $c->stash->{'template'} = 'relate.tt'; |
30 | } |
31 | |
72874569 |
32 | =head2 definitions |
581aee24 |
33 | |
1c0900ef |
34 | GET relation/definitions |
581aee24 |
35 | |
36 | Returns a data structure giving the valid types and scopes for a relationship. |
37 | |
38 | =cut |
39 | |
1c0900ef |
40 | sub definitions :Local :Args(0) { |
581aee24 |
41 | my( $self, $c ) = @_; |
72874569 |
42 | my $valid_relationships = [ qw/ spelling orthographic grammatical meaning |
43 | lexical transposition / ]; |
581aee24 |
44 | my $valid_scopes = [ qw/ local global / ]; |
45 | $c->stash->{'result'} = { 'types' => $valid_relationships, 'scopes' => $valid_scopes }; |
46 | $c->forward('View::JSON'); |
2376359f |
47 | } |
48 | |
72874569 |
49 | =head2 text |
581aee24 |
50 | |
72874569 |
51 | GET relation/$textid/ |
52 | |
53 | Runs the relationship mapper for the specified text ID. |
54 | |
581aee24 |
55 | =cut |
56 | |
72874569 |
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) { |
581aee24 |
63 | my( $self, $c ) = @_; |
1c0900ef |
64 | my $tradition = delete $c->stash->{'tradition'}; |
65 | my $collation = $tradition->collation; |
72874569 |
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 | |
581aee24 |
72 | } |
73 | |
74 | =head2 relationships |
75 | |
72874569 |
76 | GET $textid/relationships |
77 | |
78 | Returns the list of relationships defined for this text. |
581aee24 |
79 | |
72874569 |
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. |
581aee24 |
84 | |
72874569 |
85 | DELETE $textid/relationships { request } |
86 | |
581aee24 |
87 | |
88 | =cut |
89 | |
72874569 |
90 | sub relationships :Chained('text') :PathPart :Args(0) { |
581aee24 |
91 | my( $self, $c ) = @_; |
bff7bb42 |
92 | my $tradition = delete $c->stash->{'tradition'}; |
93 | my $collation = $tradition->collation; |
7c280843 |
94 | my $m = $c->model('Directory'); |
72874569 |
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 ); |
31aaf446 |
100 | my $relhash = { source => $p->[0], target => $p->[1], |
101 | type => $relobj->type, scope => $relobj->scope }; |
102 | $relhash->{'note'} = $relobj->annotation if $relobj->has_annotation; |
103 | push( @all_relations, $relhash ); |
72874569 |
104 | } |
105 | $c->stash->{'result'} = \@all_relations; |
106 | } elsif( $c->request->method eq 'POST' ) { |
107 | my $node = $c->request->param('source_id'); |
108 | my $target = $c->request->param('target_id'); |
109 | my $relation = $c->request->param('rel_type'); |
110 | my $note = $c->request->param('note'); |
111 | my $scope = $c->request->param('scope'); |
112 | |
113 | my $opts = { 'type' => $relation, |
31aaf446 |
114 | 'scope' => $scope }; |
115 | $opts->{'annotation'} = $note if $note; |
72874569 |
116 | |
117 | try { |
118 | my @vectors = $collation->add_relationship( $node, $target, $opts ); |
119 | $c->stash->{'result'} = \@vectors; |
7c280843 |
120 | $m->save( $tradition ); |
72874569 |
121 | } catch( Text::Tradition::Error $e ) { |
122 | $c->response->status( '403' ); |
123 | $c->stash->{'result'} = { 'error' => $e->message }; |
124 | } |
125 | } elsif( $c->request->method eq 'DELETE' ) { |
126 | my $node = $c->request->param('source_id'); |
127 | my $target = $c->request->param('target_id'); |
128 | |
129 | try { |
130 | my @vectors = $collation->del_relationship( $node, $target ); |
7c280843 |
131 | $m->save( $tradition ); |
72874569 |
132 | $c->stash->{'result'} = \@vectors; |
133 | } catch( Text::Tradition::Error $e ) { |
134 | $c->response->status( '403' ); |
135 | $c->stash->{'result'} = { 'error' => $e->message }; |
136 | } |
581aee24 |
137 | } |
581aee24 |
138 | $c->forward('View::JSON'); |
139 | } |
72874569 |
140 | |
581aee24 |
141 | |
2376359f |
142 | =head2 end |
143 | |
144 | Attempt to render a view, if needed. |
145 | |
146 | =cut |
147 | |
148 | sub end : ActionClass('RenderView') {} |
149 | |
150 | =head1 AUTHOR |
151 | |
152 | Tara L Andrews |
153 | |
154 | =head1 LICENSE |
155 | |
156 | This library is free software. You can redistribute it and/or modify |
157 | it under the same terms as Perl itself. |
158 | |
159 | =cut |
160 | |
161 | __PACKAGE__->meta->make_immutable; |
162 | |
163 | 1; |