openaire-library/claims/claim-utils/claimResultSearchForm.compo...

720 lines
26 KiB
TypeScript

import {Component, Input, Output, EventEmitter} from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import {SearchCrossrefService} from '../claim-utils/service/searchCrossref.service';
import {SearchOrcidService} from '../claim-utils/service/searchOrcid.service';
import {SearchPublicationsService} from '../../services/searchPublications.service';
import { SearchDataciteService } from '../claim-utils/service/searchDatacite.service';
import {SearchDatasetsService} from '../../services/searchDatasets.service';
import {SearchSoftwareService} from '../../services/searchSoftware.service';
import {SearchOrpsService} from '../../services/searchOrps.service';
import { ErrorCodes} from '../../utils/properties/errorCodes';
import {ClaimResult} from '../claim-utils/claimEntities.class';
import{DOI} from '../../utils/string-utils.class';
import{EnvProperties} from '../../utils/properties/env-properties';
declare var UIkit:any;
@Component({
selector: 'claim-result-search-form',
templateUrl: 'claimResultSearchForm.component.html',
})
export class ClaimResultSearchFormComponent {
constructor (private _searchDataciteService: SearchDataciteService, private _searchDatasetsService:SearchDatasetsService, private _searchSoftwareService:SearchSoftwareService,
private _searchCrossrefService: SearchCrossrefService,private _searchOrcidService: SearchOrcidService, private _searchPublicationsService: SearchPublicationsService,
private _searchORPService:SearchOrpsService,
private route: ActivatedRoute) {
var myDate = new Date();
this.todayDate = myDate.getFullYear()+ "-" +(myDate.getMonth() + 1) + "-" + myDate.getDate() ;
this.nextDate = (myDate.getFullYear()+100)+ "-" +(myDate.getMonth() + 1) + "-" + myDate.getDate() ;
}
ngOnInit() {
console.log("Init of ClaimResultSearchFormComponent");
console.log(this.properties);
if(this.keyword !=null && this.keyword.length > 0){
this.search(false);
}
}
page : number = 1;
size:number = 5;
navigateTo: string = "Search";
source: string = "datacite";
type : string = "dataset";
showSearchResults:boolean=false;
// searchType ="publication";
@Input() public select:boolean = true ;
@Input() public keyword:string = '';
@Input() public selectedResults:ClaimResult[];
@Input() public properties:EnvProperties;
@Input() localStoragePrefix:string = "";
// @Output() datasetsChange = new EventEmitter();
// @Output() publicationsChange = new EventEmitter();
// @Output() resultsChange = new EventEmitter();
public errorCodes:ErrorCodes = new ErrorCodes();
dataciteResults=[];
dataciteResultsNum:number = null;
// dataciteResultsNum : Observable<number> = null;
dataciteStatus = this.errorCodes.NONE;
datacitePage : number = 1;
openaireData=[];
openaireDataNum:number = 0 ;
openaireDataStatus = this.errorCodes.NONE;
openaireDataPage : number = 1;
openaireSoftware=[];
openaireSoftwareNum:number = 0 ;
openaireSoftwareStatus = this.errorCodes.NONE;
openaireSoftwarePage : number = 1;
openaireORP=[];
openaireORPNum:number = 0 ;
openaireORPStatus = this.errorCodes.NONE;
openaireORPPage : number = 1;
public warningMessage = "";
public infoMessage = "";
public todayDate = '';
public nextDate = '';
public DOIs:string[] = [];
sub: any;
public searchSource:string = "openaire"
public activeTab:string = "openairePub"
crossrefResults=[];
crossrefResultsNum : number = null;
crossrefPage : number = 1;
crossrefStatus:number = this.errorCodes.NONE;
openairePubs = [];
openairePubsNum: number ;
openairePubsPage : number = 1;
openairePubsStatus:number = this.errorCodes.NONE;
orcidResults: string[];
orcidResultsNum: number ;
totalPages: number;
orcidResultsToShow: string[];
orcidPage : number = 1;
orcidStatus:number = this.errorCodes.NONE;
authorId: string;
selectAuthorId: string = "0";
authorGivenName: string;
authorFamilyName: string;
authorIds: string[];
authors=[];
authorsNum : number ;
reloadOpenaire:boolean = true;
reloadCrossref:boolean = false;
reloadDatacite:boolean = false;
reloadOrcid:boolean = false;
search(sourceChanged){
this.warningMessage = "";
this.infoMessage = "";
if(!sourceChanged){
this.DOIs = DOI.getDOIsFromString(this.keyword);
this.reloadOpenaire = true;
this.reloadCrossref = true;
this.reloadDatacite = true;
this.reloadOrcid = true;
}
if((this.searchSource == "all" || this.searchSource == "openaire") && this.reloadOpenaire){
this.openairePubs = [];
this.openaireData = [];
this.openaireSoftware = [];
this.openaireORP = [];
this.searchOpenairePubs(this.keyword, this.size, 1);
this.searchOpenaireData(this.keyword,this.size,1);
this.searchOpenaireSoftware(this.keyword,this.size,1);
this.searchOpenaireORP(this.keyword,this.size,1);
this.reloadOpenaire = false;
}
if((this.searchSource == "all" || this.searchSource == "crossref")&&this.reloadCrossref){
this.crossrefResults = [];
this.getCrossrefResults(this.keyword, this.size,1);
this.reloadCrossref = false;
}
if((this.searchSource == "all" || this.searchSource == "datacite")&& this.reloadDatacite){
this.dataciteResults =[];
this.searchDatacite(this.keyword,this.size,1);
this.reloadDatacite = false;
}
if((this.searchSource == "all" || this.searchSource == "orcid")&& this.reloadOrcid){
this.orcidResultsToShow=[];
this.searchOrcid(this.keyword);
this.reloadOrcid = false;
}
this.showSearchResults = true;
}
private getCrossrefResults (term: string, size : number, page : number) {
this.crossrefStatus = this.errorCodes.LOADING;
if( this.DOIs.length > 0 ){
this._searchCrossrefService.searchCrossrefByDOIs(this.DOIs, this.properties.searchCrossrefAPIURL,true).subscribe(
data => {
if(data != null) {
this.crossrefResults = data;
this.crossrefPage=page;
this.crossrefResultsNum =data.length;
if(data.items == 0){
this._searchCrossrefService.searchCrossrefResults(term, size, page, this.properties.searchCrossrefAPIURL, true).subscribe(
data => {
if(data != null) {
this.crossrefResults = data;
this.crossrefPage=page;
this.crossrefResultsNum = data.length;
this.crossrefStatus = this.errorCodes.DONE;
}else{
this.crossrefStatus = this.errorCodes.ERROR;
}
},
err =>{
console.log(err.status);
this.crossrefStatus = this.errorCodes.ERROR;
}
);
}else{
this.crossrefStatus = this.errorCodes.DONE;
}
}
},
err => {
this._searchCrossrefService.searchCrossrefResults(term, size, page, this.properties.searchCrossrefAPIURL, true).subscribe(
data => {
this.crossrefResults = data;
this.crossrefPage=page;
this.crossrefResultsNum = data.length;
this.crossrefStatus = this.errorCodes.DONE;
},
err => {
console.log(err.status);
this.crossrefStatus = this.errorCodes.ERROR;
}
);
}
);
}else{
this._searchCrossrefService.searchCrossrefResults(term, size, page, this.properties.searchCrossrefAPIURL, true).subscribe(
data => {
if(data != null) {
this.crossrefResults = data;
this.crossrefPage=page;
this.crossrefResultsNum = data.length;
this.crossrefStatus = this.errorCodes.DONE;
}else{
this.crossrefStatus = this.errorCodes.ERROR;
}
},
err => {
console.log(err.status);
this.crossrefStatus = this.errorCodes.ERROR;
}
);
}
}
private searchOpenairePubs(term: string, size : number, page : number) {
if(this.DOIs.length > 0 ){
this.openairePubsStatus = this.errorCodes.LOADING;
this._searchPublicationsService.searchPublicationsByDois(this.DOIs, null, page, size, [], this.properties).subscribe(
data => {
if(data != null) {
this.openairePubsPage=page;
this.openairePubsNum = data[0];
this.openairePubs = this.openaire2ClaimResults(data[1],"publication");
this.openairePubsStatus = this.errorCodes.DONE;
if(this.openairePubsNum == 0){
this.openairePubsStatus = this.errorCodes.NONE;
}
}else {
this.openairePubsStatus = this.errorCodes.ERROR;
}
},
err => {
this.openairePubsStatus = this.errorCodes.ERROR;
console.log(err.status);
}
);
}else{
this.openairePubsStatus = this.errorCodes.LOADING;
this._searchPublicationsService.searchPublications('q='+term, null, page, size, "", [], this.properties).subscribe(
data => {
if(data != null) {
this.openairePubsPage=page;
this.openairePubsNum = data[0];
this.openairePubs = this.openaire2ClaimResults(data[1],"publication");
this.openairePubsStatus = this.errorCodes.DONE;
if(this.openairePubsNum == 0){
this.openairePubsStatus = this.errorCodes.NONE;
}
}else {
this.openairePubsStatus = this.errorCodes.ERROR;
}
},
err => {
this.openairePubsStatus = this.errorCodes.ERROR;
console.log(err.status);
}
);
}
}
private searchOrcid (term: string) {
if(this.DOIs.length > 0){
this.orcidStatus = this.errorCodes.NONE;
return;
}
this.orcidStatus = this.errorCodes.LOADING;
this.authorIds = new Array<string>();
this.authors =[]
this.getOrcidAuthor(term,true);
}
private readData(data: any) {
this.authorIds.push(data[2].path);
var author ={};
author['id']=data[2].path;
if(data[0] != null) {
author['authorGivenName']=data[0].value;
} else {
author['authorGivenName']="";
}
if(data[1] != null) {
author['authorFamilyName']=data[1].value;
} else {
author['authorFamilyName']="";
}
this.authors.push(author);
}
private getOrcidAuthor (term: string, addId) {
this.orcidResultsNum = null;
//passing structures in order to fill them in service
this._searchOrcidService.searchOrcidAuthor(term.replace(/\s/g, ""), this.authorIds,
this.authors, this.properties, addId).subscribe(
data => {
if(data != null && data == true && addId) {
this.getOrcidResults(0);
}
this.orcidStatus = this.errorCodes.NONE;
},
err => this.errorHandler(err, term)
);
}
private errorHandler(err: any, term: string) {
if(err.status == 404){
this.getOrcidAuthors(term);
} else {
this.orcidStatus = this.errorCodes.ERROR;
console.log(err.status);
}
}
private getOrcidAuthors (term: string) {
this.orcidResultsNum = null;
this.selectAuthorId = "0";
this.orcidStatus = this.errorCodes.LOADING;
//passing structures in order to fill them in service
this._searchOrcidService.searchOrcidAuthors(term, this.authorIds, this.properties).subscribe(
data => {
if(data != null && data == true) {
for(var i=0;i<this.authorIds.length;i++){
this.getOrcidAuthor(this.authorIds[i], (i==0?true:false));
}
}else{
this.orcidStatus = this.errorCodes.ERROR;
}
},
err => {
this.orcidStatus = this.errorCodes.ERROR;
console.log(err.status);
}
);
}
private getOrcidResultsById (id) {
for(var i=0; i<this.authors.length;i++){
if(this.authors[i].id == id){
this.getOrcidResults(i);
}
}
}
private getOrcidResults (index) {
this.authorGivenName =this.authors[index].authorGivenName;
this.authorFamilyName = this.authors[index].authorFamilyName;
this.authorId = this.authors[index].id;
this.orcidStatus = this.errorCodes.LOADING;
this._searchOrcidService.searchOrcidPublications(this.authors[index].id, this.properties, true).subscribe(
data => {
if(data != null) {
this.orcidResults=data;
this.orcidResultsNum = data.length;
this.orcidPage = 1;
if((this.orcidResultsNum % this.size) == 0){
this.totalPages=parseInt(''+(this.orcidResultsNum/this.size));
} else{
this.totalPages=parseInt(''+(this.orcidResultsNum/this.size+1));
}
this.orcidResultsToShow = this.orcidResults.slice(0,10);
this.orcidStatus = this.errorCodes.DONE;
if(this.orcidResultsNum == 0){
this.orcidStatus = this.errorCodes.NONE;
}
for(var i=0;i<this.orcidResults.length; i++){
var result = this.orcidResults[i];
result['authors'].push(this.authorFamilyName + ', '+this.authorGivenName);
if(result['DOI'] !=null){
this.enhanceInfoFromDOI(result);
}
}
} else {
this.orcidResultsNum = 0;
this.totalPages=0;
this.orcidStatus = this.errorCodes.NONE;
}
},
err => {
console.log(err.status);
this.orcidStatus = this.errorCodes.ERROR;
}
);
}
private enhanceInfoFromDOI(result){
if(result['DOI'] !=null){
this._searchCrossrefService.searchCrossrefByDOIs([result['DOI']], this.properties.searchCrossrefAPIURL, true).subscribe(
data => {
if(data != null || data.length > 0 ) {
var crossrefResult = data[0];
if(result['title'] == null ||result['title'] == ""){
result['title'] = crossrefResult['title'];
}
if(crossrefResult['authors'].length > 0){
result['authors'] = [];
for(var i =0; i <crossrefResult['authors'].length; i++ ){
result['authors'].push(crossrefResult['authors'][i]);
}
}
if(result['journal'] == null ||result['journal'] == ""){
result['journal'] = crossrefResult['journal'];
}
if(result['publisher'] == null ||result['publisher'] == ""){
result['publisher'] = crossrefResult['publisher'];
}
if(result['date'] == null ||result['date'] == ""){
result['date'] = crossrefResult['date'];
}
}else{
}
},
err =>{
}
);
}
}
/*
Is it USED???
private remove(item){
this.warningMessage = "";
this.infoMessage = "";
var index:number =this.selectedResults.indexOf(item);
item.selected=false;
if (index > -1) {
this.selectedResults.splice(index, 1);
// this.publicationsChange.emit({
// value: this.selectedResults
// });
}
}*/
private crossrefPageChange($event) {
this.crossrefPage=$event.value;
this.crossrefResults=[];
console.log("Crossref chaenged "+this.crossrefPage);
this.getCrossrefResults(this.keyword,this.size,this.crossrefPage);
}
private orcidPageChange($event) {
this.orcidPage=$event.value;
this.orcidResultsToShow=[];
this.orcidResultsToShow = this.orcidResults.slice(($event.value-1)*this.size, $event.value*this.size);
}
private openairePubsPageChange($event) {
this.openairePubsPage=$event.value;
this.openairePubs = [];
this.searchOpenairePubs(this.keyword,this.size,this.openairePubsPage);
}
datacitePageChange($event) {
this.datacitePage=$event.value;
this.dataciteResults=[];
this.searchDatacite(this.keyword,10,this.datacitePage);
this.warningMessage = "";
this.infoMessage = "";
}
openaireDataPageChange($event) {
this.openaireDataPage=$event.value;
this.openaireData=[];
this.searchOpenaireData(this.keyword,10,this.openaireDataPage);
this.warningMessage = "";
this.infoMessage = "";
}
openaireSoftwarePageChange($event) {
this.openaireSoftwarePage=$event.value;
this.openaireSoftware=[];
this.searchOpenaireSoftware(this.keyword,10,this.openaireSoftwarePage);
this.warningMessage = "";
this.infoMessage = "";
}
openaireORPPageChange($event) {
this.openaireORPPage=$event.value;
this.openaireORP=[];
this.searchOpenaireORP(this.keyword,10,this.openaireORPPage);
this.warningMessage = "";
this.infoMessage = "";
}
private searchDatacite (term: string, size : number, page : number) {
this.getDataciteResults(term,size,page);
this.warningMessage = "";
this.infoMessage = "";
}
private searchOpenaireData (term: string, size : number, page : number) {
if(this.DOIs.length > 0 ){
this.openaireDataStatus = this.errorCodes.LOADING;
this._searchDatasetsService.searchDatasetsByDois(this.DOIs, null, page, size, [], this.properties).subscribe(
data => {
if(data != null) {
this.openaireDataPage=page;
this.openaireDataNum = data[0];
this.openaireData = this.openaire2ClaimResults(data[1],"dataset");
this.openaireDataStatus = this.errorCodes.DONE;
if(this.openaireDataNum == 0){
this.openaireDataStatus = this.errorCodes.NONE;
}
}
},
err => {
this.openaireDataStatus = this.errorCodes.ERROR;
console.log(err.status);
}
);
}else{
this._searchDatasetsService.searchDatasets('q='+term+'', null, page, size, "", [], this.properties).subscribe(
data => {
if(data != null) {
this.openaireDataPage=page;
this.openaireDataNum = data[0];
this.openaireData = this.openaire2ClaimResults(data[1],"dataset");
this.openaireDataStatus = this.errorCodes.DONE;
if(this.openaireDataNum == 0){
this.openaireDataStatus = this.errorCodes.NONE;
}
}
},
err => {
this.openaireDataStatus = this.errorCodes.ERROR;
console.log(err.status);
}
);
}
this.warningMessage = "";
this.infoMessage = "";
}
private searchOpenaireSoftware (term: string, size : number, page : number) {
if(this.DOIs.length > 0 ){
this.openaireSoftwareStatus = this.errorCodes.LOADING;
this._searchSoftwareService.searchSoftwareByDois(this.DOIs, null, page, size, [], this.properties).subscribe(
data => {
if(data != null) {
this.openaireSoftwarePage=page;
this.openaireSoftwareNum = data[0];
this.openaireSoftware = this.openaire2ClaimResults(data[1],"software");
this.openaireSoftwareStatus = this.errorCodes.DONE;
if(this.openaireSoftwareNum == 0){
this.openaireSoftwareStatus = this.errorCodes.NONE;
}
}
},
err => {
this.openaireSoftwareStatus = this.errorCodes.ERROR;
console.log(err.status);
}
);
}else{
this._searchSoftwareService.searchSoftware('q='+term+'', null, page, size, "", [], this.properties).subscribe(
data => {
if(data != null) {
this.openaireSoftwarePage=page;
this.openaireSoftwareNum = data[0];
this.openaireSoftware = this.openaire2ClaimResults(data[1],"software");
this.openaireSoftwareStatus = this.errorCodes.DONE;
if(this.openaireSoftwareNum == 0){
this.openaireSoftwareStatus = this.errorCodes.NONE;
}
}
},
err => {
this.openaireSoftwareStatus = this.errorCodes.ERROR;
console.log(err.status);
}
);
}
this.warningMessage = "";
this.infoMessage = "";
}
private searchOpenaireORP (term: string, size : number, page : number) {
if(this.DOIs.length > 0 ){
this.openaireORPStatus = this.errorCodes.LOADING;
this._searchORPService.searchOrpsByDois(this.DOIs, null, page, size, [], this.properties).subscribe(
data => {
if(data != null) {
this.openaireORPPage=page;
this.openaireORPNum = data[0];
this.openaireORP = this.openaire2ClaimResults(data[1],"other");
this.openaireORPStatus = this.errorCodes.DONE;
if(this.openaireORPNum == 0){
this.openaireORPStatus = this.errorCodes.NONE;
}
}
},
err => {
this.openaireORPStatus = this.errorCodes.ERROR;
console.log(err.status);
}
);
}else{
this._searchORPService.searchOrps('q='+term+'', null, page, size, "", [], this.properties).subscribe(
data => {
if(data != null) {
this.openaireORPPage=page;
this.openaireORPNum = data[0];
this.openaireORP = this.openaire2ClaimResults(data[1],"other");
this.openaireORPStatus = this.errorCodes.DONE;
if(this.openaireORPNum == 0){
this.openaireORPStatus = this.errorCodes.NONE;
}
}
},
err => {
this.openaireORPStatus = this.errorCodes.ERROR;
console.log(err.status);
}
);
}
this.warningMessage = "";
this.infoMessage = "";
}
private openaire2ClaimResults(data,type):ClaimResult[]{
var claimResults = [];
for(var i=0; i< data.length; i++){
var item = data[i];
var result: ClaimResult = new ClaimResult();
result.publisher = (item.publisher && item.publisher != "")?item.publisher:null;
result.journal = null;
result.DOI = null;
result.id = item.id;
result.title = item.title.name;
result.url = item.title.url;
result.source = "openaire";
result.date = (item.year && item.year != "")?item.year:null;
result.accessRights = item.title.accessMode;
result.type = type;
result.embargoEndDate = (item.embargoEndDate && item.embargoEndDate != "")?item.embargoEndDate:null;
result.publisher = item.publisher;
result.result = item;
if(item.authors){
for(var j=0; j<item.authors.length; j++){
result.authors.push(item.authors[j]);
}
}
claimResults.push(result);
}
return claimResults;
}
private getDataciteResults (term: string, size : number, page : number) {
console.log("getDataciteResults + "+this.properties.searchDataciteAPIURL)
this._searchDataciteService.searchDataciteResults(term, size, page, this.properties, true).subscribe(
data => {
this.dataciteResults = data;
this.datacitePage=page;
this.dataciteResultsNum = data.length;
this.dataciteStatus = this.errorCodes.DONE;
if(this.dataciteResultsNum == 0){
this.dataciteStatus = this.errorCodes.NONE;
}
},
err => {
this.dataciteStatus = this.errorCodes.ERROR;
console.log(err);
}
);
}
public searchSourceChanged(source){
this.searchSource = source;
this.activeTab = (source == "openaire" || source == "all")?"openairePub":source;
if(this.keyword && this.keyword.length > 0){
this.search(true);
}
}
public clickTab(tab){
this.activeTab = tab;
}
}