seedlot upload with accession synonyms. seedlot upload works to update existing seedlots
[sgn.git] / lib / SGN / Controller / Publication.pm
blobd3c2c623075ce6576421f390cda59990a54f7da7
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;
109 $c->stash(
110 template => '/publication/index.mas',
111 pubref => {
112 pub_id => $found_pub_id ,
113 curator => $curator,
114 submitter => $submitter,
115 sequencer => $sequencer,
116 person_id => $person_id,
117 pub => $pub,
118 dbh => $dbh,
119 dbxrefs => $dbxrefs,
120 doi => $doi,
127 =head2 view_pub
129 Public path: /publication/<pub_id>/view
131 View a publication detail page.
133 Chained off of L</get_pub> below.
135 =cut
137 sub view_pub : Chained('get_pub') PathPart('view') Args(0) {
138 my ( $self, $c, $action) = @_;
139 my $pub = $c->stash->{pub};
142 my $logged_user = $c->user;
143 my $person_id = $logged_user->get_object->get_sp_person_id if $logged_user;
144 my $curator = $logged_user->check_roles('curator') if $logged_user;
145 my $submitter = $logged_user->check_roles('submitter') if $logged_user;
146 my $sequencer = $logged_user->check_roles('sequencer') if $logged_user;
147 my $dbh = $c->dbc->dbh;
149 ##################
151 ###Check if a publication page can be printed###
152 my $pub_id = $pub ? $pub->get_pub_id : undef ;
154 # print message if pub_id is not valid
155 unless ( ( $pub_id =~ m /^\d+$/ ) || ($action eq 'new' && !$pub_id) ) {
156 $c->throw_404( "No publication exists for that identifier." );
158 unless ( $pub || !$pub_id && $action && $action eq 'new' ) {
159 $c->throw_404( "No publication exists for that identifier." );
162 # print message if pub_id does not exist
163 if ( !$pub && $action ne 'new' && $action ne 'store' ) {
164 $c->throw_404('No publication exists for this identifier');
167 ####################
169 my $dbxrefs = $pub->get_dbxrefs;
171 #########
172 ################
173 $c->stash(
174 template => '/publication/index.mas',
175 pubref => {
176 action => $action,
177 pub_id => $pub_id ,
178 curator => $curator,
179 submitter => $submitter,
180 sequencer => $sequencer,
181 person_id => $person_id,
182 user => $logged_user,
183 pub => $pub,
184 dbh => $dbh,
185 dbxrefs => $dbxrefs,
191 =head1 PRIVATE ACTIONS
193 =head2 get_pub
195 Chain root for fetching a publication object to operate on.
197 Path part: /publication/<pub_id>
199 =cut
201 sub get_pub : Chained('/') PathPart('publication') CaptureArgs(1) {
202 my ($self, $c, $pub_id) = @_;
204 my $identifier_type = $c->stash->{identifier_type}
205 || $pub_id =~ /[^-\d]/ ? 'accession' : 'pub_id';
207 if( $identifier_type eq 'pub_id' ) {
208 if ( $pub_id == 0 ) {
209 $c->stash->{pub} = CXGN::Chado::Publication->new($c->dbc->dbh);
210 return 1;
212 $pub_id > 0
213 or $c->throw_client_error( public_message => 'Publication ID must be a positive integer.' );
215 my $matching_pubs;
217 if ($identifier_type eq 'pub_id' ) {
218 $matching_pubs = $self->schema->resultset('Pub::Pub')->search(
220 pub_id => $pub_id,
221 } );
223 if( $matching_pubs->count > 1 ) {
224 $c->throw_client_error( public_message => 'Multiple matching publications' );
227 my ( $publication ) = $matching_pubs->all
228 or $c->throw_404( "publication $pub_id not found" );
229 my $found_pub_id = $publication->pub_id;
231 $c->stash->{pub} = CXGN::Chado::Publication->new($c->dbc->dbh, $found_pub_id);
233 return 1;
237 =head2 doi_banner
239 Public path: /doibanner/
241 Return SGN logo if DOI exists, or a 1x1 empty pixel if does not exist.
242 Used by Elsevier for banner linking publications to solgenomics publication page /pub/doi/<doi>
244 =cut
247 sub doi_banner : Path('/doibanner/') CaptureArgs(3) {
248 my ($self, $c , @doi_parts) = @_;
249 my $doi = join '/' , @doi_parts;
250 my $matching_pubs = $self->schema->resultset('Pub::Pub')->search(
252 'dbxref.accession' => $doi,
253 }, {
254 join => { 'pub_dbxrefs' => 'dbxref' },
255 } );
256 my $pub_count = $matching_pubs->count;
257 if( $matching_pubs->count > 1 ) {
258 $c->throw_client_error( public_message => 'Multiple matching publications' );
261 my $image = $c->path_to('/documents/img/white_pixel.png');
263 if ($pub_count == 1 ) {
264 $image = $c->path_to( '/documents/img/sgn_logo_26x60.png') ;
267 my $image_file = read_file($image , { binmode => ':raw' } );
269 $c->response->content_type('image/png');
270 $c->response->body($image_file);
274 __PACKAGE__->meta->make_immutable;