Merge pull request #5205 from solgenomics/topic/generic_trial_upload
[sgn.git] / lib / SGN / Controller / Publication.pm
blob91cdfea28ce95981bba246290749f1016403e50a
1 package SGN::Controller::Publication;
3 =head1 NAME
5 SGN::Controller::Publication - Catalyst controller for the SGN publication page
6 (replacing the old cgi script)
8 =cut
10 use Moose;
11 use namespace::autoclean;
12 use File::Slurp;
14 use URI::FromHash 'uri';
16 use CXGN::Chado::Publication ;
18 BEGIN { extends 'Catalyst::Controller' }
19 with 'Catalyst::Component::ApplicationAttribute';
22 has 'schema' => (
23 is => 'rw',
24 isa => 'DBIx::Class::Schema',
25 lazy_build => 1,
30 sub pub_search : Path('/search/publication') Args(0) {
31 my $self = shift;
32 my $c = shift;
34 $c->stash(
35 template => '/search/pub.mas',
41 sub _build_schema {
42 shift->_app->dbic_schema( 'Bio::Chado::Schema', 'sgn_chado' )
46 =head2 new_pub
48 Public path: /pub/0/new
50 Create a new publication.
52 Chained off of L</get_pub> below.
54 =cut
56 sub new_pub : Chained('get_pub') PathPart('new') Args(0) {
57 my ( $self, $c ) = @_;
58 $c->stash(
59 template => '/publication/index.mas',
61 pubref => {
62 action => "new",
63 pub_id => 0 ,
64 pub => $c->stash->{pub},
65 schema => $self->schema,
70 =head2 view_by_doi
72 Public path: /doi/pub/
74 View publication by doi.
76 Since DOIs can have "/" in them this path captures 3 args and then concatenates to recreate the DOI (hopefully no DOI has more than 3 slashes! )
78 =cut
81 sub view_by_doi : Path('/doi/pub/') CaptureArgs(3) {
82 my ($self, $c , @doi_parts) = @_;
83 my $doi = join '/' , @doi_parts;
84 my $matching_pubs = $self->schema->resultset('Pub::Pub')->search(
86 'dbxref.accession' => $doi,
87 }, {
88 join => { 'pub_dbxrefs' => 'dbxref' },
89 } );
91 if( $matching_pubs->count > 1 ) {
92 $c->throw_client_error( public_message => 'Multiple matching publications' );
95 my ( $publication ) = $matching_pubs->all
96 or $c->throw_404( "publication $doi not found" );
97 my $found_pub_id = $publication->pub_id;
98 my $pub = CXGN::Chado::Publication->new($c->dbc->dbh, $found_pub_id);
99 $c->{stash}->{pub} = $pub;
101 my $logged_user = $c->user;
102 my $person_id = $logged_user->get_object->get_sp_person_id if $logged_user;
103 my $curator = $logged_user->check_roles('curator') if $logged_user;
104 my $submitter = $logged_user->check_roles('submitter') if $logged_user;
105 my $sequencer = $logged_user->check_roles('sequencer') if $logged_user;
106 my $dbh = $c->dbc->dbh;
107 my $dbxrefs = $pub->get_dbxrefs;
108 my $stocks = $self->_get_stocks( $c);
110 $c->stash(
111 template => '/publication/index.mas',
112 pubref => {
113 pub_id => $found_pub_id ,
114 curator => $curator,
115 submitter => $submitter,
116 sequencer => $sequencer,
117 person_id => $person_id,
118 pub => $pub,
119 dbh => $dbh,
120 dbxrefs => $dbxrefs,
121 doi => $doi,
122 stocks => $stocks,
129 =head2 view_pub
131 Public path: /publication/<pub_id>/view
133 View a publication detail page.
135 Chained off of L</get_pub> below.
137 =cut
139 sub view_pub : Chained('get_pub') PathPart('view') Args(0) {
140 my ( $self, $c, $action) = @_;
141 my $pub = $c->stash->{pub};
144 my $logged_user = $c->user;
145 my $person_id = $logged_user->get_object->get_sp_person_id if $logged_user;
146 my $curator = $logged_user->check_roles('curator') if $logged_user;
147 my $submitter = $logged_user->check_roles('submitter') if $logged_user;
148 my $sequencer = $logged_user->check_roles('sequencer') if $logged_user;
149 my $dbh = $c->dbc->dbh;
151 ##################
153 ###Check if a publication page can be printed###
154 my $pub_id = $pub ? $pub->get_pub_id : undef ;
156 # print message if pub_id is not valid
157 unless ( ( $pub_id =~ m /^\d+$/ ) || ($action eq 'new' && !$pub_id) ) {
158 $c->throw_404( "No publication exists for that identifier." );
160 unless ( $pub || !$pub_id && $action && $action eq 'new' ) {
161 $c->throw_404( "No publication exists for that identifier." );
164 # print message if pub_id does not exist
165 if ( !$pub && $action ne 'new' && $action ne 'store' ) {
166 $c->throw_404('No publication exists for this identifier');
169 ####################
171 my $dbxrefs = $pub->get_dbxrefs;
172 my $stocks = $self->_get_stocks( $c );
173 #########
174 ################
175 $c->stash(
176 template => '/publication/index.mas',
177 pubref => {
178 action => $action,
179 pub_id => $pub_id ,
180 curator => $curator,
181 submitter => $submitter,
182 sequencer => $sequencer,
183 person_id => $person_id,
184 user => $logged_user,
185 pub => $pub,
186 dbh => $dbh,
187 dbxrefs => $dbxrefs,
188 stocks => $stocks,
194 =head1 PRIVATE ACTIONS
196 =head2 get_pub
198 Chain root for fetching a publication object to operate on.
200 Path part: /publication/<pub_id>
202 =cut
204 sub get_pub : Chained('/') PathPart('publication') CaptureArgs(1) {
205 my ($self, $c, $pub_id) = @_;
207 my $identifier_type = $c->stash->{identifier_type}
208 || $pub_id =~ /[^-\d]/ ? 'accession' : 'pub_id';
210 if( $identifier_type eq 'pub_id' ) {
211 if ( $pub_id == 0 ) {
212 $c->stash->{pub} = CXGN::Chado::Publication->new($c->dbc->dbh);
213 return 1;
215 $pub_id > 0
216 or $c->throw_client_error( public_message => 'Publication ID must be a positive integer.' );
218 my $matching_pubs;
220 if ($identifier_type eq 'pub_id' ) {
221 $matching_pubs = $self->schema->resultset('Pub::Pub')->search(
223 pub_id => $pub_id,
224 } );
226 if( $matching_pubs->count > 1 ) {
227 $c->throw_client_error( public_message => 'Multiple matching publications' );
230 my ( $publication ) = $matching_pubs->all
231 or $c->throw_404( "publication $pub_id not found" );
232 my $found_pub_id = $publication->pub_id;
234 $c->stash->{pub} = CXGN::Chado::Publication->new($c->dbc->dbh, $found_pub_id);
236 return 1;
241 sub _get_stocks {
242 my ( $self, $c) = @_;
243 my $pub = $c->stash->{pub};
244 my $schema = $self->schema;
245 my @stock_ids = $pub->get_stock_ids;
246 my @stocks;
247 foreach my $stock_id (@stock_ids) {
248 my $stock = CXGN::Stock->new( { schema => $schema, stock_id => $stock_id } ) ;
249 push @stocks, $stock;
251 return \@stocks;
256 =head2 doi_banner
258 Public path: /doibanner/
260 Return SGN logo if DOI exists, or a 1x1 empty pixel if does not exist.
261 Used by Elsevier for banner linking publications to solgenomics publication page /pub/doi/<doi>
263 =cut
266 sub doi_banner : Path('/doibanner/') CaptureArgs(3) {
267 my ($self, $c , @doi_parts) = @_;
268 my $doi = join '/' , @doi_parts;
269 my $matching_pubs = $self->schema->resultset('Pub::Pub')->search(
271 'dbxref.accession' => $doi,
272 }, {
273 join => { 'pub_dbxrefs' => 'dbxref' },
274 } );
275 my $pub_count = $matching_pubs->count;
276 if( $matching_pubs->count > 1 ) {
277 $c->throw_client_error( public_message => 'Multiple matching publications' );
280 my $image = $c->path_to('/documents/img/white_pixel.png');
282 if ($pub_count == 1 ) {
283 $image = $c->path_to( '/documents/img/sgn_logo_26x60.png') ;
286 my $image_file = read_file($image , { binmode => ':raw' } );
288 $c->response->content_type('image/png');
289 $c->response->body($image_file);
293 __PACKAGE__->meta->make_immutable;