Microservice call to parse a collation and return SVG
[scpubgit/stemmatology.git] / TreeOfTexts / lib / TreeOfTexts / Controller / Root.pm
1 package TreeOfTexts::Controller::Root;
2 use Moose;
3 use namespace::autoclean;
4 use Text::Tradition::Analysis qw/ run_analysis /;
5
6
7 BEGIN { extends 'Catalyst::Controller' }
8
9 #
10 # Sets the actions in this controller to be registered with no prefix
11 # so they function identically to actions created in MyApp.pm
12 #
13 __PACKAGE__->config(namespace => '');
14
15 =head1 NAME
16
17 TreeOfTexts::Controller::Root - Root Controller for TreeOfTexts
18
19 =head1 DESCRIPTION
20
21 [enter your description here]
22
23 =head1 METHODS
24
25 =head2 index
26
27 The root page (/).  Lists the traditions available in the DB to work on,
28 and should also eventually have an 'Upload new' interface.
29
30 =cut
31
32 sub index :Path :Args(0) {
33     my ( $self, $c ) = @_;
34
35     my $m = $c->model('Directory');
36     my @all_texts;
37     foreach my $id ( $m->tradition_ids ) {
38         my $data = { 
39                 'id' => $id,
40                 'name' => $m->name( $id ),
41         };
42         push( @all_texts, $data );
43     }
44     
45     $c->stash->{texts} = \@all_texts;
46     $c->stash->{template} = 'frontpage.tt';
47 }
48
49 =head2 tradition (TODO)
50
51 The main page for a tradition, with information about it and links to the
52 available tools.
53
54 =head2 relationships
55
56 The relationship editor tool.
57
58 =cut
59
60 sub relationships :Local {
61         my( $self, $c ) = @_;
62         my $m = $c->model('Directory');
63         my $tradition = $m->tradition( $c->request->params->{'textid'} );
64         my $table = $tradition->collation->make_alignment_table();
65         my $witlist = map { $_->{'witness'} } @{$table->{'alignment'}};
66         $c->stash->{witnesses} = $witlist;
67         $c->stash->{alignment} = $table;
68         $c->stash->{template} = 'relate.tt';    
69 }
70
71 =head2 stexaminer
72
73 The stemma analysis tool with the pretty colored table.
74
75 =cut
76
77 sub stexaminer :Local {
78     my( $self, $c ) = @_;
79     my $m = $c->model('Directory');
80         my $tradition = $m->tradition( $c->request->params->{'textid'} );
81         my $stemma = $tradition->stemma;
82         # TODO Think about caching the stemma in a session 
83         $c->stash->{svg} = $stemma->as_svg;
84         $c->stash->{text_title} = $tradition->name;
85         $c->stash->{template} = 'index.tt'; 
86         # TODO Run the analysis as AJAX from the loaded page.
87         my $t = run_analysis( $tradition );
88         $c->stash->{variants} = $t->{'variants'};
89         $c->stash->{total} = $t->{'variant_count'};
90         $c->stash->{genealogical} = $t->{'genealogical_count'};
91         $c->stash->{conflict} = $t->{'conflict_count'};
92 }
93
94 =head2 alignment_table 
95
96 Return a JSON alignment table of a given text.
97
98 =cut
99
100 sub alignment_table :Local {
101         my( $self, $c ) = @_;
102         my $m = $c->model( 'Directory' );
103         my $tradition = $m->tradition( $c->request->params->{'textid'} );
104         my $table = $tradition->collation->make_alignment_table();
105         $c->stash->{'result'} = $table;
106         $c->forward-( 'View::JSON' );
107 }
108
109 =head1 MICROSERVICE CALLS
110
111 =head2 renderSVG
112
113 Parse the passed collation data and return an SVG of the collated text.  Takes
114 the following parameters:
115
116 =over 4
117
118 =item * data - The collation data itself.
119
120 =item * input - The data format.  Valid values include CollateX, Self, TEI (for parallel segmentation) eventually Tabular.
121
122 =item * name - A name for the text. Not so important for this function.
123
124 =cut
125
126 # Utility function to render SVG from a graph input.
127 sub renderSVG :Local {
128         my( $self, $c ) = @_;
129         my $format = $c->request->param('format') || 'string';
130         my $type = $c->request->body_params->{'type'};
131         my $name = $c->request->param('name') || 'Collation graph';
132         my $data = $c->request->body_params->{'data'};
133         $c->log->debug( $data );
134         my $tradition = Text::Tradition->new( 
135                 'name' => $name,
136                 'input' => $type,
137                 $format => $data,
138                 );
139         $c->log->debug( "Got tradition with " . $tradition->collation->readings . " readings" );
140         $c->stash->{'result'} = $tradition->collation->as_svg;
141         $c->forward('View::SVG');
142 }
143
144
145 =head1 OPENSOCIAL URLs
146
147 =head2 view_table
148
149 Simple gadget to return the analysis table for the stexaminer
150
151 =cut
152
153 sub view_table :Local {
154     my( $self, $c ) = @_;
155     my $m = $c->model('Directory');
156         my $id = $c->request->params->{'textid'};
157         my $t = run_analysis( $m->tradition( $id ), $m->stemma( $id ) );
158         $c->stash->{variants} = $t->{'variants'};
159     $c->stash->{template} = 'table_gadget.tt';
160 }
161
162 =head2 view_svg
163
164 Simple gadget to return the SVG for a given stemma
165
166 =cut
167
168 sub view_svg :Local {
169     my( $self, $c ) = @_;
170     my $m = $c->model('Directory');
171     my $stemma = $m->tradition( $c->request->params->{'textid'} )->stemma;
172         if( $stemma ) {
173                 $c->stash->{svg} = $stemma->as_svg;
174         }
175     $c->stash->{template} = 'stemma_gadget.tt';
176 }
177
178 =head2 default
179
180 Standard 404 error page
181
182 =cut
183
184 sub default :Path {
185     my ( $self, $c ) = @_;
186     $c->response->body( 'Page not found' );
187     $c->response->status(404);
188 }
189
190 =head2 end
191
192 Attempt to render a view, if needed.
193
194 =cut
195
196 sub end : ActionClass('RenderView') {}
197
198 =head1 AUTHOR
199
200 Tara L Andrews
201
202 =head1 LICENSE
203
204 This library is free software. You can redistribute it and/or modify
205 it under the same terms as Perl itself.
206
207 =cut
208
209 __PACKAGE__->meta->make_immutable;
210
211 1;