openaire-library/searchPages/advanced/advancedSearchOrps.componen...

171 lines
6.6 KiB
TypeScript

import {Component, Input, ViewChild} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import { Router, ActivatedRoute} from '@angular/router';
import {Filter, Value,AdvancedField} from '../searchUtils/searchHelperClasses.class';
import {SearchOrpsService} from '../../services/searchOrps.service';
import {SearchResult} from '../../utils/entities/searchResult';
import {ErrorCodes} from '../../utils/properties/errorCodes';
import {AdvancedSearchPageComponent} from '../searchUtils/advancedSearchPage.component';
import {SearchFields, FieldDetails} from '../../utils/properties/searchFields';
import {SearchUtilsClass } from '../searchUtils/searchUtils.class';
import{EnvProperties} from '../../utils/properties/env-properties';
@Component({
selector: 'advanced-search-orps',
template: `
<advanced-search-page pageTitle="Advanced Search for Other Research Products" entityType="other"
type = "other research products"
[(results)] = "results"
[(searchUtils)] = "searchUtils"
[(fieldIds)]="fieldIds" [(fieldIdsMap)]="fieldIdsMap" [(selectedFields)]="selectedFields"
(queryChange)="queryChanged($event)"
[csvParams]="csvParams" csvPath="other" simpleSearchLink="/search/find/other"
[disableForms]="disableForms"
[loadPaging]="loadPaging"
[oldTotalResults]="oldTotalResults" [(connectCommunityId)]=connectCommunityId [piwikSiteId]=piwikSiteId
searchFormClass="orpsSearchForm">
</advanced-search-page>
`
})
export class AdvancedSearchOrpsComponent {
private errorCodes: ErrorCodes;
properties:EnvProperties;
@Input() piwikSiteId = null;
@Input() connectCommunityId: string;
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[] = [];
@ViewChild (AdvancedSearchPageComponent) searchPage : AdvancedSearchPageComponent ;
public resourcesQuery = "( (oaftype exact result) and (resulttypeid exact other) )";
public csvParams: string;
public disableForms: boolean = false;
public loadPaging: boolean = true;
public oldTotalResults: number = 0;
public pagingLimit: number = 0;
public isPiwikEnabled;
constructor (private route: ActivatedRoute, private _searchOrpsService: SearchOrpsService ) {
this.results =[];
this.errorCodes = new ErrorCodes();
this.searchUtils.status = this.errorCodes.LOADING;
}
ngOnInit() {
this.route.data
.subscribe((data: { envSpecific: EnvProperties }) => {
this.properties= data.envSpecific;
this.searchUtils.baseUrl = data.envSpecific.searchLinkToAdvancedOrps;
this.pagingLimit = data.envSpecific.pagingLimit;
this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
});
this.searchUtils.status = this.errorCodes.LOADING;
this.sub = this.route.queryParams.subscribe(params => {
if(params['page'] && this.searchUtils.page != params['page']) {
this.loadPaging = false;
this.oldTotalResults = this.searchUtils.totalResults;
}
let page = (params['page']=== undefined)?1:+params['page'];
this.searchUtils.page = ( page <= 0 ) ? 1 : page;
this.searchPage.fieldIds = this.fieldIds;
this.selectedFields =[];
this.searchPage.selectedFields = this.selectedFields;
this.searchPage.fieldIdsMap = this.fieldIdsMap;
this.searchPage.connectCommunityId = this.connectCommunityId;
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;
}
this.searchUtils.status = this.errorCodes.LOADING;
this.disableForms = true;
this.results = [];
this.searchUtils.totalResults = 0;
console.info("Advanced Search for Other Research Products: Execute search query "+parameters);
this._searchOrpsService.advancedSearchOrps(parameters, page, size, this.properties).subscribe(
data => {
this.searchUtils.totalResults = data[0];
console.info("search Other Research Products total="+this.searchUtils.totalResults);
this.results = data[1];
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 => {
console.log(err);
console.info("error");
//TODO check erros (service not available, bad request)
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.disableForms = false;
}
);
}
}
private setFilters(){
//TODO set filters from
}
public queryChanged($event) {
this.loadPaging = true;
var parameters = $event.value;
this.getResults(parameters, this.searchUtils.page,this.searchUtils.size);
console.info("queryChanged: Execute search query "+parameters);
}
}