package EPrints::Plugin::Screen::MSAcad_Locate; @ISA = ( 'EPrints::Plugin::Screen' ); use strict; use JSON; # Make the plug-in sub new { my( $class, %params ) = @_; $params{ua} = LWP::UserAgent->new; $params{ua}->env_proxy; my $self = $class->SUPER::new(%params); # Where the button to access the screen appears if anywhere, and what priority $self->{actions} = [qw(submit_publication)]; $self->{appears} = [ { place => "admin_actions", position => 1247, },]; return $self; } sub allow_submit_publication { my ( $self ) = @_; return $self->can_be_viewed(); } sub action_submit_publication { my ( $self ) = @_; my $repository = $self->{repository}; my $tmpfile = File::Temp->new; binmode($tmpfile, ":utf8"); print $tmpfile scalar($self->{repository}->param( "msacad_publication_id" )); seek($tmpfile, 0, 0); my $dataset = $repository->get_dataset( "inbox" ); my $plugin = $self->{session}->plugin( "Import::MSAcad", session => $self->{session}, dataset => $self->{processor}->{dataset}, processor => $self->{processor}, ); my $list = $plugin->input_fh( dataset=>$dataset, fh=>$tmpfile, user=>$repository->current_user, ); if( $repository ) { # screen to go to after a single item import $self->{post_import_screen} = $self->param( "post_import_screen" ); $self->{post_import_screen} ||= "EPrint::Edit"; } my $n = $list->count; my $processor = $self->{processor}; if( $n == 1 ) { my( $eprint ) = $list->get_records( 0, 1 ); # add in eprint/eprintid for backwards compatibility $processor->{dataobj} = $processor->{eprint} = $eprint; $processor->{dataobj_id} = $processor->{eprintid} = $eprint->get_id; $processor->{screenid} = $self->{post_import_screen}; } } sub redirect_to_me_url { } # ONLY logged in users who can deposit should be able to view this screen. sub can_be_viewed { my( $self ) = @_; return $self->allow( "create_eprint" ); } # What to display sub render { my( $self ) = @_; # Get the current repository object (so we can access the users, eprints information about things in this repository) my $repository = $self->{repository}; # Create an XML element to return to our screen my $user = $repository->current_user; my $frag = $repository->xml->create_document_fragment(); if ((length($user->{data}->{name}->{given}) < 1) or (length($user->{data}->{name}->{family}) < 1)) { $self->{processor}->add_message( "error", $repository->xml->create_text_node("No Author Name in User Profile to Search From")); return $frag; } my $author_name = $user->{data}->{name}->{given} . "+" . $user->{data}->{name}->{family}; my $author = $self->getAuthor($author_name); my $div = $self->createAuthorDiv($author); $frag->appendChild($div); my $pubs = $self->getPubsForAuthor($author); foreach my $pub(@{$pubs}) { my $div = $self->createPublicationsDiv($pub); $frag->appendChild($div); } return $frag; } sub createPublicationsDiv { my ( $self, $pub ) = @_; my $repository = $self->{repository}; my $div = $repository->xml->create_element( "div", class=>"msacad_publication_search" ); my $epdata = $pub->{epdata}; $epdata->{eprint_status} = "inbox"; #use Data::Dumper; #print Dumper($epdata); my $ds = $repository->get_dataset( "eprint" ); my $temp_print = EPrints::DataObj::EPrint->new_from_data( $repository, $epdata, $ds ); my $citation = $temp_print->render_citation( "default" ); # NAME my $citation_div = $repository->xml->create_element("div", class=>"msacad_citation_div"); $citation_div->appendChild($citation); $div->appendChild($citation_div); # Full Text Box my $fulltext = $repository->xml->create_element("div", class=>"msacad_info_box"); my $fulltext_span =$repository->xml->create_element("span", class=>"msacad_info_span"); $fulltext->appendChild($fulltext_span); $fulltext_span->appendChild($self->html_phrase( "full_text_available" )); if (length($pub->{FullVersionURL}) > 1) { my $img = $repository->xml->create_element("img", class=>"msacad_info_img", src=>"/style/images/message.png", alt=>"Yes"); $fulltext->appendChild($img); } else { my $img = $repository->xml->create_element("img", class=>"msacad_info_img", src=>"/style/images/error.png", alt=>"No"); $fulltext->appendChild($img); } $div->appendChild($fulltext); # In Repository Box my $fulltext = $repository->xml->create_element("div", class=>"msacad_info_box"); my $fulltext_span =$repository->xml->create_element("span", class=>"msacad_info_span"); $fulltext->appendChild($fulltext_span); $fulltext_span->appendChild($self->html_phrase( "in_repository" )); if ($self->publicationExists($pub)) { my $img = $repository->xml->create_element("img", class=>"msacad_info_img", src=>"/style/images/message.png", alt=>"Yes"); $fulltext->appendChild($img); } else { my $img = $repository->xml->create_element("img", class=>"msacad_info_img", src=>"/style/images/error.png", alt=>"No"); $fulltext->appendChild($img); } $div->appendChild($fulltext); my $screen_id = "Screen::".$self->{processor}->{screenid}; my $screen = $self->{session}->plugin( $screen_id, processor => $self->{processor} ); my $form_div = $repository->xml->create_element("div", class=>"msacad_import_record"); my $form = $repository->render_form("POST"); my $field = $repository->make_element( "input", name=> "msacad_publication_id", value=> $pub->{ID}, type=> "hidden" ); $form->appendChild($field); my $button = $screen->render_action_button( { action => "submit_publication", screen => $screen, screen_id => $screen_id, } ); $form->appendChild($button); $form_div->appendChild($form); $div->appendChild($form_div); return $div; } sub publicationExists { my ( $self, $pub ) = @_; my $repository = $self->{repository}; my $eprint_dataset = $repository->dataset( "eprint" ); my @filters = ( { meta_fields => [qw( title )], value => $pub->{Title} }, ); my $eprints = $eprint_dataset->search( filters => \@filters, limit => 10, ); my $count = 0; $eprints->map( sub { my( undef, undef, $eprint ) = @_; $count += 1; print STDERR "FOUND EPRINT " . $eprint->id . "\n\n"; }); if ($count < 1) { return 0; } return 1; } sub createAuthorDiv { my ( $self, $author ) = @_; my $repository = $self->{repository}; my $frag = $repository->xml->create_element("div", class=>"msacad_author"); my $div = $repository->xml->create_element("div", class=>"msacad_author_search"); # NAME my $h2 = $repository->xml->create_element("div", class=>"msacad_author_name"); my $name = $repository->xml->create_text_node( $author->{FirstName} . " " . $author->{LastName} ); $h2->appendChild($name); $div->appendChild($h2); # Affiliation my $affil = $repository->xml->create_element("div", class =>"msacad_author_affiliation"); my $name = $repository->xml->create_text_node( $author->{Affiliation} ); $affil->appendChild($name); #$div->appendChild($affil); # Stats my $stats = $repository->xml->create_element("div", class => "msacad_author_stats"); # Publication Count my $stat = $repository->xml->create_element("i"); $stat->appendChild($self->html_phrase("publications")); $stats->appendChild($stat); my $stat = $repository->xml->create_element("b"); $stat->appendChild($repository->xml->create_text_node( $author->{PublicationCount} )); $stats->appendChild($stat); $stats->appendChild($repository->xml->create_text_node( " | " )); # Citation Count my $stat = $repository->xml->create_element("i"); $stat->appendChild($self->html_phrase( "citations" )); $stats->appendChild($stat); my $stat = $repository->xml->create_element("b"); $stat->appendChild($repository->xml->create_text_node( $author->{CitationCount} )); $stats->appendChild($stat); $stats->appendChild($repository->xml->create_text_node( " | " )); # G Index my $stat = $repository->xml->create_element("i"); $stat->appendChild($self->html_phrase( "gindex" )); $stats->appendChild($stat); my $stat = $repository->xml->create_element("b"); $stat->appendChild($repository->xml->create_text_node( $author->{GIndex} )); $stats->appendChild($stat); $stats->appendChild($repository->xml->create_text_node( " | " )); # H Index my $stat = $repository->xml->create_element("i"); $stat->appendChild($self->html_phrase( "hindex" )); $stats->appendChild($stat); my $stat = $repository->xml->create_element("b"); $stat->appendChild($repository->xml->create_text_node( $author->{HIndex} )); $stats->appendChild($stat); $div->appendChild($stats); # H Index my $homepage_div = $repository->xml->create_element("div", class => "msacad_author_homepage"); my $stat = $repository->xml->create_element("i"); $stat->appendChild($self->html_phrase( "homepage" )); $homepage_div->appendChild($stat); my $stat = $repository->xml->create_element("b"); my $ref = $repository->xml->create_element( "a", href=>$author->{HomepageURL}, target => "_blank" ); $ref->appendChild($repository->xml->create_text_node( $author->{HomepageURL} )); $stat->appendChild($ref); $homepage_div->appendChild($stat); $div->appendChild($homepage_div); #$author_hash{HomepageURL} = $author->{HomepageURL}; $frag->appendChild($div); # IMAGE my $img = $repository->xml->create_element("img", src=>$author->{DisplayPhotoURL}, class=>"msacad_author_image"); $frag->appendChild($img); return $frag; } sub getPubsFromJson { my ( $self, $content ) = @_; my $json = JSON->new->allow_nonref; #my $json_text = $json->allow_nonref->utf8->relaxed->loose->allow_singlequote->allow_barekey->decode($content); my $json_text = eval { $json->utf8()->decode($content); }; if ($@) { print STDERR "SOMETHING WENT WRONG \n\n"; } my @ret_publications; my $publications = $json_text->{d}->{Publication}->{Result}; foreach my $publication(@{$publications}) { #use Data::Dumper; #print Dumper($publication); my %phash = (); my $epdata = (); $phash{ID} = $publication->{ID}; $phash{Title} = $publication->{Title}; $epdata->{title} = $publication->{Title}; if (!($self->publicationExists($publication))) { $phash{Abstract} = $publication->{Abstract}; $epdata->{abstract} = $publication->{Abstract}; $phash{Year} = $publication->{Year}; $epdata->{date} = $publication->{Year}; $phash{CitationCount} = $publication->{CitationCount}; foreach my $journal($publication->{Journal}) { if (defined($phash{Journal})) { $phash{Journal} .= ", "; } $phash{Journal} .= $journal->{FullName}; $epdata->{type} = "article"; $epdata->{publication} = $phash{Journal}; } my $author_count = 0; foreach my $author(@{$publication->{Author}}) { if (defined($phash{Author})) { $phash{Author} .= ", "; } $epdata->{creators}->[$author_count]->{name}->{family} = $author->{LastName}; $epdata->{creators}->[$author_count]->{name}->{given} = $author->{FirstName} . " " . $author->{MiddleName}; $phash{Author} .= $author->{LastName} . ". " . $author->{FirstName} . ". " . $author->{MiddleName}; $author_count += 1; } $phash{FullVersionURL} = $publication->{FullVersionURL}; $phash{epdata} = $epdata; push(@ret_publications, \%phash); } } return \@ret_publications; } sub getAuthorsFromJson { my ( $self, $content ) = @_; my $json = JSON->new->allow_nonref; #my $json_text = $json->allow_nonref->utf8->relaxed->loose->allow_singlequote->allow_barekey->decode($content); my $json_text = eval { $json->utf8()->decode($content); }; if ($@) { print STDERR "SOMETHING WENT WRONG \n\n"; } my @ret_authors; my $authors = $json_text->{d}->{Author}->{Result}; foreach my $author(@{$authors}) { my %author_hash = (); $author_hash{ID} = $author->{ID}; $author_hash{FirstName} = $author->{FirstName}; $author_hash{LastName} = $author->{LastName}; $author_hash{MiddleName} = $author->{MiddleName}; $author_hash{PublicationCount} = $author->{PublicationCount}; $author_hash{CitationCount} = $author->{CitationCount}; $author_hash{HomepageURL} = $author->{HomepageURL}; $author_hash{HIndex} = $author->{HIndex}; $author_hash{GIndex} = $author->{GIndex}; $author_hash{DisplayPhotoURL} = $author->{DisplayPhotoURL}; foreach my $affiliation($author->{Affiliation}) { if (defined($author_hash{Affiliation})) { $author_hash{Affiliation} .= ", "; $author_hash{AffiliationID} .= ", "; } $author_hash{Affiliation} .= $affiliation->{Name}; $author_hash{AffiliationID} .= $affiliation->{ID}; } my $interests = $author->{ResearchInterestDomain}; foreach my $interest(@{$interests}) { if (defined($author_hash{Interest})) { $author_hash{Interest} .= ", "; } $author_hash{Interest} .= $interest->{Name}; } push(@ret_authors, \%author_hash); } return \@ret_authors; } sub getPubsForAuthor { my ( $self, $author ) = @_; my $repo = $self->{repository}; use HTTP::Request::Common qw(GET); my $ua = LWP::UserAgent->new; my $app_id = $repo->get_conf("msacad_api_key"); my $author_id = $author->{ID}; my $url = $repo->get_conf("msacad_api_url") . '?AppId='.$app_id.'&AuthorID='.$author_id.'&ResultObjects=Publication&StartIdx=1&EndIdx=10&orderBy=year'; my $req = GET $url; my $res = $ua->request($req); if ($res->is_success) { my $content = $res->content; my $publications = $self->getPubsFromJson($content); return $publications; } else { print STDERR "\n\n MSAcad_Locate (query error): " . $res->status_line . "\n\n"; } return undef; } sub getAuthor { my( $self, $author_name ) = @_; my $repo = $self->{repository}; use HTTP::Request::Common qw(GET); my $ua = LWP::UserAgent->new; my $app_id = $repo->get_conf("msacad_api_key"); my $organisation_id = $repo->get_conf("msacad_institution_id"); my $url = $repo->get_conf("msacad_api_url") . '?AppId='.$app_id.'&FullTextQuery='.$author_name.'&OrganisationID=3146&ResultObjects=Author&StartIdx=1&EndIdx=20'; my $req = GET $url; my $res = $ua->request($req); if (!$res->is_success) { $self->{processor}->add_message( "error", $repo->xml->create_text_node("No authors found in academic search (Query 1)")); return undef; } my $content = $res->content; my $authors = $self->getAuthorsFromJson($content); foreach my $author(@{$authors}) { my $affiliation_ids = $author->{AffiliationID}; if ($affiliation_ids =~ m/$organisation_id/) { return $author; } } $self->{processor}->add_message( "error", $repo->xml->create_text_node("No authors found in academic search (Query 2)")); return undef; } 1;