explore-services/portal-2/src/app/claimPages/claim-utils/claimPublication.component.ts

293 lines
8.9 KiB
TypeScript

import {Component, Input, Output, EventEmitter} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import { ActivatedRoute } from '@angular/router';
import {SearchCrossrefService} from '../../services/searchCrossref.service';
import {SearchOrcidService} from '../../services/searchOrcid.service';
@Component({
selector: 'claim-publication',
templateUrl: 'claimPublication.component.html',
})
export class ClaimPublicationComponent {
constructor (private _searchCrossrefService: SearchCrossrefService,private _searchOrcidService: SearchOrcidService,
private route: ActivatedRoute) {
var myDate = new Date();
this.todayDate = myDate.getFullYear()+ "-" +(myDate.getMonth() + 1) + "-" + myDate.getDate() ;
this.nextDate = (myDate.getFullYear()+100)+ "-01-01" ;
}
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
let page = +params['page'];
let size = +params['size'];
this.page = ( page <= 0 ) ? 1 : page;
this.size = ( size <= 0 ) ? 10 : size;
if(this.keyword !=null && this.keyword.length > 0){
this.search(this.keyword,this.size,this.page);
}
});
}
ngOnDestroy() {
this.sub.unsubscribe();
}
sub: any;
page : number = 1;
size:number = 10;
@Input() public keyword:string = "";
navigateTo: string = "Search";
source: string = "crossref";
type : string = "publication";
@Input() public select:boolean = true ;
@Input() public selectedPublications = [] ;
@Output() publicationsChange = new EventEmitter();
crossrefResults=[];
crossrefResultsNum : Observable<number> ;
orcidResults: string[];
orcidResultsNum: number ;
totalPages: number;
orcidResultsToShow: string[];
authorId: string;
authorGivenName: string;
authorFamilyName: string;
authorIds: string[];
authorGivenNames: string[];
authorFamilyNames: string[];
authorsNum : number ;
public warningMessage = "";
public infoMessage = "";
todayDate = '';
nextDate = '';
private searchOrcid (term: string) {
this.authorIds = new Array<string>();
this.authorGivenNames = new Array<string>();
this.authorFamilyNames = new Array<string>();
this.getOrcidAuthor(term);
console.info('searchOrcid in searchOrcid file');
}
private readData(data: any) {
this.authorIds.push(data[2].path);
if(data[0] != null) {
this.authorGivenNames.push(data[0].value);
} else {
this.authorGivenNames.push("");
}
if(data[1] != null) {
this.authorFamilyNames.push(data[1].value);
} else {
this.authorFamilyNames.push("");
}
}
private getOrcidAuthor (term: string) {
this.orcidResultsNum = null;
//passing structures in order to fill them in service
this._searchOrcidService.searchOrcidAuthor(term, this.authorIds,
this.authorGivenNames, this.authorFamilyNames).subscribe(
data => {
if(data != null && data == true) {
this.getOrcidResultsById(0);
}
},
err => this.errorHandler(err, term)
);
}
private errorHandler(err: any, term: string) {
if(err.status == 404){
this.getOrcidAuthors(term);
} else {
console.error(err.status);
}
}
private getOrcidAuthors (term: string) {
this.orcidResultsNum = null;
//passing structures in order to fill them in service
this._searchOrcidService.searchOrcidAuthors(term, this.authorIds,
this.authorGivenNames, this.authorFamilyNames).subscribe(
data => {
if(data != null && data == true) {
this.getOrcidResultsById(0);
}
},
err => console.error(err.status)
);
}
private getOrcidResultsById (index:number) {
if(this.authorIds.length > index) {
let id = this.authorIds[index];
console.info("getOrcidResultsById: "+id);
this._searchOrcidService.searchOrcidPublications(id).subscribe(
data => {
if(data != null) {
this.orcidResults=data['orcid-work'];
this.orcidResultsNum = data['orcid-work'].length;
this.page = 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);
} else {
this.orcidResultsNum = 0;
this.totalPages=0;
}
this.authorGivenName = this.authorGivenNames[index];
this.authorFamilyName = this.authorFamilyNames[index];
this.authorId = id;
},
err => console.error(err.status)
);
console.info("totalPages = " +this.totalPages);
}
}
public search(term: string, size : number, page : number){
this.warningMessage = "";
this.infoMessage = "";
this.getCrossrefResults(term,size,page);
this.searchOrcid(term);
}
private getCrossrefResults (term: string, size : number, page : number) {
if(term.split(' ').length == 1 ){
this._searchCrossrefService.searchCrossrefByDOI(term).subscribe(
data => {
if(data != null) {
this.crossrefResults = data.items;
this.page=page;
this.crossrefResultsNum = data['total-results'];
if(data.items == 0){
this._searchCrossrefService.searchCrossrefResults(term, size, page).subscribe(
data => {
if(data != null) {
this.crossrefResults = data.items;
this.page=page;
this.crossrefResultsNum = data['total-results'];
}
},
err => console.error(err.status)
);
}
}
},
err => {
//console.error(err);
this._searchCrossrefService.searchCrossrefResults(term, size, page).subscribe(
data => {
this.crossrefResults = data.items;
this.page=page;
this.crossrefResultsNum = data['total-results'];
},
err => console.error(err.status)
);
}
);
}else{
this._searchCrossrefService.searchCrossrefResults(term, size, page).subscribe(
data => {
if(data != null) {
this.crossrefResults = data.items;
this.page=page;
this.crossrefResultsNum = data['total-results'];
}
},
err => console.error(err.status)
);
}
}
private add(item, id, itemSource, itemType, itemUrl, itemTitle, date){
var result ;
if(itemSource == 'crossref'){
date = (date == null) ? null : date.substring(0,10);
result = {id: id, type :itemType, source : itemSource, title: itemTitle,url: itemUrl, result: item, accessRights: 'OPEN', embargoEndDate: this.nextDate, date};
}else if (itemSource == 'orcid'){
date = (date == null) ? null : date + "-01.-01"
result = {id:id, type :itemType, source : itemSource, title: itemTitle,url: itemUrl, result: item, accessRights: 'OPEN', embargoEndDate: this.nextDate, date};
}else if (itemSource == 'openaire'){
}
console.info("Added result:"+result.date);
var found:boolean = this.isSelected(result.id);
this.warningMessage = "";
if (!found) {
this.selectedPublications.push(result);
this.publicationsChange.emit({
value: this.selectedPublications
});
}else{
this.warningMessage = "Publication already in selected list";
}
}
private remove(item){
this.warningMessage = "";
this.infoMessage = "";
var index:number =this.selectedPublications.indexOf(item);
item.selected=false;
if (index > -1) {
this.selectedPublications.splice(index, 1);
this.publicationsChange.emit({
value: this.selectedPublications
});
}
}
private pageChange($event) {
this.page=$event.value;
this.crossrefResults=[];
this.getCrossrefResults(this.keyword,this.size,this.page);
}
private orcidPageChange($event) {
this.page=$event.value;
this.orcidResultsToShow=[];
this.orcidResultsToShow = this.orcidResults.slice(($event.value-1)*this.size, $event.value*this.size);
}
private isSelected(id:string){
var found:boolean = false;
this.warningMessage = "";
for (var _i = 0; _i < this.selectedPublications.length; _i++) {
let item = this.selectedPublications[_i];
if(item.id == id){
found=true;
this.warningMessage = "Publication already in selected list";
}
}
return found;
}
}