openaire-library/searchPages/searchResearchResults.compo...

265 lines
11 KiB
TypeScript
Raw Normal View History

import {Component, Input, ViewChild} from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {AdvancedField} from './searchUtils/searchHelperClasses.class';
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 {SearchCustomFilter, SearchUtilsClass} from './searchUtils/searchUtils.class';
import {EnvProperties} from '../utils/properties/env-properties';
import {NewSearchPageComponent} from "./searchUtils/newSearchPage.component";
import {DOI} from "../utils/string-utils.class";
@Component({
selector: 'search-research-results',
template: `
<new-search-page
pageTitle="{{(simpleView?'':'Advanced ')}} Search for {{ getEntityName(resultType, true, true) | titlecase }}"
[entityType]="resultType"
[type]="getEntityName(resultType, true, true)"
[(results)]="results"
[(searchUtils)]="searchUtils"
[(fieldIds)]="fieldIds" [(fieldIdsMap)]="fieldIdsMap" [(selectedFields)]="selectedFields"
[csvParams]="csvParams" [csvPath]="getEntityName(resultType, true, false)"
[simpleSearchLink]="simpleSearchLink" [advancedSearchLink]="advancedSearchLink"
[disableForms]="disableForms"
[loadPaging]="loadPaging"
[oldTotalResults]="oldTotalResults"
[(openaireLink)]=openaireLink
[piwikSiteId]=piwikSiteId [hasPrefix]="hasPrefix"
searchFormClass="publicationsSearchForm"
[(sort)]=sort
[(filters)]="filters"
[simpleView]="simpleView" formPlaceholderText="Search by title, author, doi, abstract content..."
>
</new-search-page>
<!--
Field missing from simple search!
formPlaceholderText = "Search for {{ getEntityName(resultType, true, true) | titlecase }}"
[(filters)] = "filters"
[(baseUrl)] = baseUrl
[advancedSearchLink]="advancedSearchLink"
[(advancedSearchParameters)]=advancedSearchParameters-->
`
})
export class SearchResearchResultsComponent {
@Input() resultType: string = "publication";
simpleSearchLink: string = "";
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 searchFields: SearchFields = new SearchFields();
public fieldIds: string[] = this.searchFields.RESULT_ADVANCED_FIELDS;
public fieldIdsMap = this.searchFields.RESULT_FIELDS;
public selectedFields: AdvancedField[] = [];
public resourcesQuery = "((oaftype exact result) and (resulttypeid exact " + this.resultType + "))";
public csvParams: string;
public disableForms: boolean = false;
public loadPaging: boolean = true;
public oldTotalResults: number = 0;
@Input() openaireLink: string;
@Input() customFilter: SearchCustomFilter = null;
public pagingLimit: number = 0;
public isPiwikEnabled;
public sort: boolean = true;
properties: EnvProperties;
public refineFields: string[] = this.searchFields.RESULT_REFINE_FIELDS;
@ViewChild(NewSearchPageComponent) searchPage: NewSearchPageComponent;
@Input() simpleView: boolean = true;
constructor(private route: ActivatedRoute, private _searchResearchResultsService: SearchResearchResultsService) {
this.results = [];
this.errorCodes = new ErrorCodes();
this.errorMessages = new ErrorMessagesComponent();
this.searchUtils.status = this.errorCodes.LOADING;
}
ngOnInit() {
this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
this.properties = data.envSpecific;
this.pagingLimit = data.envSpecific.pagingLimit;
this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
// this.searchLink = "/search/research-results";
// this.searchUtils.baseUrl = this.searchLink;
this.simpleSearchLink = this.properties.searchLinkToResults;
this.advancedSearchLink = this.properties.searchLinkToAdvancedResults;
this.searchUtils.baseUrl = (this.simpleView)?this.simpleSearchLink:this.advancedSearchLink;
// } else if(this.resultType == "dataset") {
// this.simpleSearchLink = this.properties.searchLinkToDatasets;
// this.searchUtils.baseUrl = this.properties.searchLinkToAdvancedDatasets;
// } else if(this.resultType == "software") {
// this.simpleSearchLink = this.properties.searchLinkToSoftware;
// this.searchUtils.baseUrl = this.properties.searchLinkToAdvancedSoftware;
// } else if(this.resultType == "other") {
// this.simpleSearchLink = this.properties.searchLinkToOrps;
// this.searchUtils.baseUrl = this.properties.searchLinkToAdvancedOrps;
// }
});
this.searchUtils.status = this.errorCodes.LOADING;
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;
}
var refine = true;
if (this.searchUtils.page != ((params['page'] === undefined) ? 1 : +params['page']) && this.filters && !firstLoad) {
refine = false;
}
firstLoad = false;
let page = (params['page'] === undefined) ? 1 : +params['page'];
this.searchUtils.page = (page <= 0) ? 1 : 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.fieldIds = this.fieldIds;
this.selectedFields = [];
this.searchPage.selectedFields = this.selectedFields;
this.searchPage.refineFields = this.refineFields;
this.searchPage.fieldIdsMap = this.fieldIdsMap;
this.searchPage.customFilter = this.customFilter;
this.searchPage.createAdvancedSearchSelectedFiltersFromURLParameters(params);
this._getResults(this.searchPage.getSearchAPIQueryForAdvancedSearhFields(), this.searchUtils.page, this.searchUtils.size, this.searchUtils.sortBy, refine, this.searchPage.getSearchAPIQueryForRefineFields(params));
});
}
ngOnDestroy() {
this.sub.unsubscribe();
}
sub: any;
/*public getResults(parameters:string, page: number, size: number, sortBy: string, refine:boolean ) {
if (page > this.pagingLimit) {
size = 0;
}
if (page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
if (parameters != null && parameters != '') {
this.csvParams = "&fq=(" + this.resourcesQuery + " and (" + parameters + "))";
} else {
this.csvParams = "&fq=" + this.resourcesQuery;
}
this.searchUtils.status = this.errorCodes.LOADING;
this.disableForms = true;
this.results = [];
this.searchUtils.totalResults = 0;
this._getResults(parameters, page, size, sortBy, refine);
}
}*/
public _getResults(parameters: string, page: number, size: number, sortBy: string, refine: boolean, refineFieldsFilterQuery = null) {
if (page > this.pagingLimit) {
size = 0;
}
if (page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
// TODO update the following method to get more than one types
if (parameters != null && parameters != '') {
this.csvParams = "&fq=(" + this.resourcesQuery + " and (" + parameters + "))";
} else {
this.csvParams = "&fq=" + this.resourcesQuery;
}
this.csvParams += (refineFieldsFilterQuery?refineFieldsFilterQuery:'');
this.searchUtils.status = this.errorCodes.LOADING;
this.disableForms = true;
this.results = [];
this.searchUtils.totalResults = 0;
// TODO update the following method to get more than one types
this._searchResearchResultsService.advancedSearch(this.resultType, parameters, page, size, sortBy, this.properties, (refine) ? this.searchPage.getRefineFieldsQuery() : null, this.searchPage.getFields(), refineFieldsFilterQuery).subscribe(
data => {
this.searchUtils.totalResults = data[0];
this.results = data[1];
if (refine) {
this.filters = data[2];
console.info(this.filters);
}
this.searchPage.checkSelectedFilters(this.filters);
// this.searchPage.updateBaseUrlWithParameters(this.filters);
this.searchPage.updateBaseUrlWithParameters();
this.searchUtils.status = this.errorCodes.DONE;
if (this.searchUtils.totalResults == 0) {
this.searchUtils.status = this.errorCodes.NONE;
}
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 => {
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.NOT_AVAILABLE;
/*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;
}
);
}
}
private handleError(message: string, error) {
console.error(this.getEntityName(this.resultType, true, true) + " advanced Search Page: " + message, error);
}
public 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");
}
}
}