import {Component, Input, ViewChild} from '@angular/core'; import {ActivatedRoute} from '@angular/router'; import {Location} from '@angular/common'; import {SearchResearchResultsService} from '../../services/searchResearchResults.service'; import {ErrorCodes} from '../../utils/properties/errorCodes'; import {ErrorMessagesComponent} from '../../utils/errorMessages.component'; import {SearchFields} from '../../utils/properties/searchFields'; import {SearchPageComponent} from '../searchUtils/searchPage.component'; import {SearchCustomFilter, SearchUtilsClass} from '../searchUtils/searchUtils.class'; import {DOI} from '../../utils/string-utils.class'; import {EnvProperties} from '../../utils/properties/env-properties'; @Component({ selector: 'search-research-results', template: ` ` }) export class SearchResearchResultsComponent { @Input() resultType: string = "publication"; public advancedSearchLink: string = ""; private errorCodes: ErrorCodes; private errorMessages: ErrorMessagesComponent; @Input() piwikSiteId = null; @Input() hasPrefix: boolean = true; public results =[]; public filters =[]; public searchUtils:SearchUtilsClass = new SearchUtilsClass(); public baseUrl:string = ""; public sub: any; public subResults: any; public searchFields:SearchFields = new SearchFields(); public refineFields: string[] = this.searchFields.RESULT_REFINE_FIELDS; public fieldIdsMap=this.searchFields.RESULT_FIELDS; public urlParams : Map; @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ; public _location:Location; public CSV: any = { "columnNames": [ "Title", "Authors", "Publication Year", "DOI", "Funder", "Project Name (GA Number)", "Access" ], "export":[] }; public CSVDownloaded = false; public csvParams: string; public disableForms: boolean = false; public loadPaging: boolean = true; public oldTotalResults: number = 0; @Input() openaireLink: string; @Input() customFilter:SearchCustomFilter= null; @Input() advancedSearchParameters ; pagingLimit = 0; public sort: boolean = true; properties: EnvProperties; constructor (private route: ActivatedRoute, private _searchResearchResultsService: SearchResearchResultsService ) { this.errorCodes = new ErrorCodes(); this.errorMessages = new ErrorMessagesComponent(); this.searchUtils.status = this.errorCodes.LOADING; this.searchUtils.page =1; } public ngOnInit() { this.route.data .subscribe((data: { envSpecific: EnvProperties }) => { this.properties = data.envSpecific; this.pagingLimit = data.envSpecific.pagingLimit; if(this.resultType == "publication") { this.advancedSearchLink = this.properties.searchLinkToAdvancedPublications; this.baseUrl = this.properties.searchLinkToPublications; } else if(this.resultType == "dataset") { this.advancedSearchLink = this.properties.searchLinkToAdvancedDatasets; this.baseUrl = this.properties.searchLinkToDatasets; } else if(this.resultType == "software") { this.advancedSearchLink = this.properties.searchLinkToAdvancedSoftware; this.baseUrl = this.properties.searchLinkToSoftware; } else if(this.resultType == "other") { this.advancedSearchLink = this.properties.searchLinkToAdvancedOrps; this.baseUrl = this.properties.searchLinkToOrps; } }); this.searchPage.refineFields = this.refineFields; this.searchPage.fieldIdsMap = this.fieldIdsMap; this.searchPage.type = this.getEntityName(this.resultType, true, true); var firstLoad =true; this.sub = this.route.queryParams.subscribe(params => { if(params['page'] && this.searchUtils.page != params['page']) { this.loadPaging = false; this.oldTotalResults = this.searchUtils.totalResults; } this.searchUtils.keyword = (params['keyword']?params['keyword']:''); var refine = true; if(this.searchUtils.page != ((params['page']=== undefined)?1:+params['page']) && this.filters && !firstLoad){ refine = false; } firstLoad = false; this.searchUtils.page = (params['page']=== undefined)?1:+params['page']; this.searchUtils.size = (params['size']=== undefined)?10:+params['size']; if(this.searchUtils.size != 5 && this.searchUtils.size != 10 && this.searchUtils.size != 20 && this.searchUtils.size != 50) { this.searchUtils.size = 10; } this.searchUtils.sortBy = (params['sortBy'])?params['sortBy']:''; if(this.searchUtils.sortBy && this.searchUtils.sortBy != "resultdateofacceptance,descending" && this.searchUtils.sortBy != "resultdateofacceptance,ascending") { this.searchUtils.sortBy = ""; } this.searchPage.customFilter = this.customFilter; var queryParameters = this.searchPage.getQueryParametersFromUrl(params); this._getResults(queryParameters, refine, this.searchUtils.page, this.searchUtils.size, this.searchUtils.sortBy); }); } public ngOnDestroy() { if(this.sub){ this.sub.unsubscribe(); } if(this.subResults){ this.subResults.unsubscribe(); } } public getResults(keyword:string,refine:boolean, page: number, size: number, sortBy: string){ var parameters = ""; if(keyword.length > 0){ var DOIs:string[] = DOI.getDOIsFromString(keyword); var doisParams = ""; for(var i =0 ;i < DOIs.length; i++){ doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"'; } if(doisParams.length > 0){ parameters += "&"+doisParams; }else{ parameters = "q=" + keyword; } } this._getResults(parameters,refine,page,size,sortBy); } private _getResults(parameters:string,refine:boolean, page: number, size: number, sortBy: string){ if(page > this.pagingLimit) { size=0; } if(page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) { this.csvParams = parameters; this.searchUtils.status = this.errorCodes.LOADING; this.disableForms = true; this.results = []; this.searchUtils.totalResults = 0; this.subResults = this._searchResearchResultsService.search(this.resultType, parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, sortBy, this.searchPage.getFields(), this.properties).subscribe( data => { this.searchUtils.totalResults = data[0]; this.results = data[1]; if(refine){ this.filters = data[2]; } this.searchPage.checkSelectedFilters(this.filters); this.searchPage.updateBaseUrlWithParameters(this.filters); this.searchUtils.status = this.errorCodes.DONE; if(this.searchUtils.totalResults == 0 ){ this.searchUtils.status = this.errorCodes.NONE; } //this.searchPage.closeLoading(); this.disableForms = false; if(this.searchUtils.status == this.errorCodes.DONE) { // Page out of limit!!! let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size); if(!(Number.isInteger(totalPages))) { totalPages = (parseInt(totalPages, 10) + 1); } if(totalPages < page) { this.searchUtils.totalResults = 0; this.searchUtils.status = this.errorCodes.OUT_OF_BOUND; } } }, err => { //console.log(err); this.handleError("Error getting "+this.getEntityName(this.resultType, true, true), err); this.searchUtils.status = this.errorMessages.getErrorCode(err.status); //TODO check erros (service not available, bad request) // if( ){ // this.searchUtils.status = ErrorCodes.ERROR; // } //var errorCodes:ErrorCodes = new ErrorCodes(); //this.searchUtils.status = errorCodes.ERROR; /*if(err.status == '404') { this.searchUtils.status = this.errorCodes.NOT_FOUND; } else if(err.status == '500') { this.searchUtils.status = this.errorCodes.ERROR; } else { this.searchUtils.status = this.errorCodes.NOT_AVAILABLE; }*/ //this.searchPage.closeLoading(); this.disableForms = false; } ); } } public queryChanged($event) { this.loadPaging = true; } private handleError(message: string, error) { console.error(this.getEntityName(this.resultType, true, true)+" simple Search Page (SearchResearchResultsComponent): "+message, error); } private getEntityName (entityType:string, plural:boolean, full:boolean): string { if(entityType == "publication") { return "publication" + (plural ? "s" : ""); } else if(entityType == "dataset") { return (full ? "research data" : ("dataset" + (plural ? "s" : ""))); } else if(entityType == "software") { return "software"; } else if(entityType == "other") { return (full ? ("other research product" + (plural ? "s" : "")) : "other"); } } }