openaire-library/searchPages/advanced/advancedSearchDataProviders...

177 lines
7.1 KiB
TypeScript

import {Component, Input, ViewChild} from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {AdvancedField} from '../searchUtils/searchHelperClasses.class';
import {SearchDataprovidersService} from '../../services/searchDataproviders.service';
import {ErrorCodes} from '../../utils/properties/errorCodes';
import {ErrorMessagesComponent} from '../../utils/errorMessages.component';
import {AdvancedSearchPageComponent} from '../searchUtils/advancedSearchPage.component';
import {SearchFields} from '../../utils/properties/searchFields';
import {SearchCustomFilter, SearchUtilsClass} from '../searchUtils/searchUtils.class';
import {EnvProperties} from '../../utils/properties/env-properties';
@Component({
selector: 'advanced-search-dataprovider',
template: `
<advanced-search-page pageTitle="Advanced Search for Content Providers" entityType="dataprovider"
type = "content providers"
[(results)] = "results"
[(fieldIds)]="fieldIds" [(fieldIdsMap)]="fieldIdsMap" [(selectedFields)]="selectedFields"
[(searchUtils)] = "searchUtils"
[csvParams]="csvParams" csvPath="datasources" simpleSearchLink="/search/find/dataproviders"
[disableForms]="disableForms"
[loadPaging]="loadPaging"
[oldTotalResults]="oldTotalResults" [piwikSiteId]=piwikSiteId
searchFormClass="datasourcesSearchForm">
</advanced-search-page>
`
})
export class AdvancedSearchDataProvidersComponent {
private errorCodes: ErrorCodes;
private errorMessages: ErrorMessagesComponent;
@Input() piwikSiteId = null;
@Input() customFilter:SearchCustomFilter= null;
public results =[];
public filters =[];
public searchUtils:SearchUtilsClass = new SearchUtilsClass();
public searchFields:SearchFields = new SearchFields();
public fieldIds: string[] = this.searchFields.DATASOURCE_ADVANCED_FIELDS;
public fieldIdsMap= this.searchFields.DATASOURCE_FIELDS;
public selectedFields:AdvancedField[] = [];
@ViewChild (AdvancedSearchPageComponent) searchPage : AdvancedSearchPageComponent ;
public resourcesQuery = "(oaftype exact datasource)";
public csvParams: string;
public disableForms: boolean = false;
public loadPaging: boolean = true;
public oldTotalResults: number = 0;
public pagingLimit: number = 0;
public isPiwikEnabled;
properties:EnvProperties;
constructor (private route: ActivatedRoute, private _searchDataProvidersService: SearchDataprovidersService ) {
this.results =[];
this.errorCodes = new ErrorCodes();
this.errorMessages = new ErrorMessagesComponent();
this.searchUtils.status = this.errorCodes.LOADING;
//console.info("Con -base url:"+this.searchUtils.baseUrl );
}
ngOnInit() {
this.route.data
.subscribe((data: { envSpecific: EnvProperties }) => {
this.properties= data.envSpecific;
this.searchUtils.baseUrl = data.envSpecific.searchLinkToAdvancedDataProviders;
this.pagingLimit = data.envSpecific.pagingLimit;
this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
});
this.sub = this.route.queryParams.subscribe(params => {
this.loadPaging = true;
if(params['page'] && this.searchUtils.page != params['page']) {
this.loadPaging = false;
this.oldTotalResults = this.searchUtils.totalResults;
}
let page = (params['page']=== undefined)?0:+params['page'];
this.searchUtils.page = ( page < 1 ) ? 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.searchPage.fieldIds = this.fieldIds;
this.selectedFields =[];
this.searchPage.selectedFields = this.selectedFields;
this.searchPage.fieldIdsMap = this.fieldIdsMap;
this.searchPage.customFilter = this.customFilter;
this.searchPage.getSelectedFiltersFromUrl(params);
this.getResults(this.searchPage.createQueryParameters(), this.searchUtils.page, this.searchUtils.size);
});
}
ngOnDestroy() {
this.sub.unsubscribe();
}
sub: any;
public getResults(parameters:string, page: number, size: number){
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;
}
//var errorCodes:ErrorCodes = new ErrorCodes();
this.searchUtils.status = this.errorCodes.LOADING;
//this.searchPage.openLoading();
this.disableForms = true;
this.results = [];
this.searchUtils.totalResults = 0;
//console.info("Advanced Search for Content Providers: Execute search query "+parameters);
this._searchDataProvidersService.advancedSearchDataproviders(parameters, page, size, this.properties).subscribe(
data => {
this.searchUtils.totalResults = data[0];
this.results = data[1];
this.searchPage.updateBaseUrlWithParameters();
//var errorCodes:ErrorCodes = new ErrorCodes();
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 content providers", 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("Content Providers advanced Search Page: "+message, error);
}
}