2020-02-19 16:35:48 +01:00
|
|
|
import {Component, EventEmitter, Input, Output, ViewChild} from '@angular/core';
|
2020-02-11 15:56:20 +01:00
|
|
|
import {ActivatedRoute} from '@angular/router';
|
|
|
|
import {AdvancedField} from './searchUtils/searchHelperClasses.class';
|
|
|
|
import {SearchOrganizationsService} from '../services/searchOrganizations.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";
|
|
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'search-organizations',
|
|
|
|
template: `
|
|
|
|
<new-search-page
|
|
|
|
pageTitle="{{(simpleView?'':'Advanced ')}} Search for {{ 'organizations' | titlecase }}"
|
|
|
|
entityType="organization"
|
|
|
|
type="organizations"
|
2020-02-19 16:35:48 +01:00
|
|
|
[results]="results"
|
|
|
|
[searchUtils]="searchUtils"
|
|
|
|
[fieldIds]="fieldIds" [fieldIdsMap]="fieldIdsMap" [selectedFields]="selectedFields"
|
2020-02-11 15:56:20 +01:00
|
|
|
[csvParams]="csvParams" csvPath="organizations"
|
|
|
|
[simpleSearchLink]="simpleSearchLink" [advancedSearchLink]="advancedSearchLink"
|
|
|
|
[disableForms]="disableForms"
|
|
|
|
[loadPaging]="loadPaging"
|
|
|
|
[oldTotalResults]="oldTotalResults"
|
2020-02-19 16:35:48 +01:00
|
|
|
[openaireLink]=openaireLink
|
2020-02-11 15:56:20 +01:00
|
|
|
[piwikSiteId]=piwikSiteId [hasPrefix]="hasPrefix"
|
|
|
|
searchFormClass="organizationSearchForm"
|
2020-02-19 16:35:48 +01:00
|
|
|
[includeOnlyResultsAndFilter]="includeOnlyResultsAndFilter"
|
|
|
|
|
|
|
|
[filters]="filters"
|
2020-02-11 15:56:20 +01:00
|
|
|
[simpleView]="simpleView" formPlaceholderText="Search by title, author, doi, abstract content..."
|
|
|
|
>
|
|
|
|
</new-search-page>
|
|
|
|
`
|
|
|
|
})
|
|
|
|
|
|
|
|
export class SearchOrganizationsComponent {
|
|
|
|
private errorCodes: ErrorCodes;
|
|
|
|
private errorMessages: ErrorMessagesComponent;
|
|
|
|
properties:EnvProperties;
|
|
|
|
@Input() piwikSiteId = null;
|
|
|
|
public results =[];
|
|
|
|
public filters =[];
|
|
|
|
public searchUtils:SearchUtilsClass = new SearchUtilsClass();
|
|
|
|
public searchFields:SearchFields = new SearchFields();
|
|
|
|
|
|
|
|
public fieldIds: string[] = this.searchFields.ORGANIZATION_ADVANCED_FIELDS;
|
|
|
|
public fieldIdsMap = this.searchFields.ORGANIZATION_FIELDS;
|
|
|
|
public selectedFields:AdvancedField[] = [];
|
|
|
|
public csvParams: string;
|
|
|
|
public disableForms: boolean = false;
|
|
|
|
public loadPaging: boolean = true;
|
|
|
|
public oldTotalResults: number = 0;
|
|
|
|
public pagingLimit: number = 0;
|
|
|
|
public isPiwikEnabled;
|
|
|
|
|
|
|
|
@Input() customFilter:SearchCustomFilter= null;
|
|
|
|
public refineFields: string[] = this.searchFields.ORGANIZATION_REFINE_FIELDS;
|
|
|
|
@ViewChild(NewSearchPageComponent) searchPage: NewSearchPageComponent;
|
|
|
|
@Input() simpleView: boolean = true;
|
2020-02-19 16:35:48 +01:00
|
|
|
@Input() simpleSearchLink: string = "";
|
2020-02-11 15:56:20 +01:00
|
|
|
advancedSearchLink: string = "";
|
|
|
|
@Input() hasPrefix: boolean = true;
|
|
|
|
@Input() openaireLink: string;
|
2020-02-19 16:35:48 +01:00
|
|
|
@Input() includeOnlyResultsAndFilter: boolean = false;
|
|
|
|
@Output() searchPageUpdates = new EventEmitter();
|
2020-02-11 15:56:20 +01:00
|
|
|
public resourcesQuery = "(oaftype exact organization)";
|
|
|
|
constructor (private route: ActivatedRoute, private _searchOrganizationsService: SearchOrganizationsService ) {
|
|
|
|
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;
|
2020-02-19 16:35:48 +01:00
|
|
|
if (!this.simpleSearchLink) {
|
|
|
|
this.simpleSearchLink = this.properties.searchLinkToProjects;
|
|
|
|
} this.advancedSearchLink = this.properties.searchLinkToAdvancedProjects;
|
2020-02-11 15:56:20 +01:00
|
|
|
this.searchUtils.baseUrl = (this.simpleView)?this.simpleSearchLink:this.advancedSearchLink;
|
|
|
|
this.pagingLimit = data.envSpecific.pagingLimit;
|
|
|
|
this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
|
|
|
|
|
|
|
|
});
|
|
|
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
|
|
|
this.searchUtils.status = this.errorCodes.LOADING;
|
|
|
|
var firstLoad = true;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
var refine = true;
|
|
|
|
if (this.searchUtils.page != ((params['page'] === undefined) ? 1 : +params['page']) && this.filters && !firstLoad) {
|
|
|
|
refine = 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.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);
|
2020-02-17 15:19:14 +01:00
|
|
|
this.searchPage.prepareSearchPage(this.fieldIds, this.selectedFields, this.refineFields, [], this.fieldIdsMap,this.customFilter,params, "organization");
|
2020-02-11 15:56:20 +01:00
|
|
|
|
|
|
|
this.getResults(this.searchPage.getSearchAPIQueryForAdvancedSearhFields(), this.searchUtils.page, this.searchUtils.size, refine, this.searchPage.getSearchAPIQueryForRefineFields(params, firstLoad));
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
|
|
|
sub: any;
|
|
|
|
public getResults(parameters:string, page: number, size: number, refine: boolean, refineFieldsFilterQuery = null){
|
|
|
|
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;
|
2020-02-19 16:35:48 +01:00
|
|
|
this.searchPageUpdates.emit({disableForms: this.disableForms,searchUtils: this.searchUtils});
|
|
|
|
|
2020-02-11 15:56:20 +01:00
|
|
|
this.results = [];
|
|
|
|
this.searchUtils.totalResults = 0;
|
|
|
|
|
|
|
|
//console.info("Advanced Search for Organizations: Execute search query "+parameters);
|
|
|
|
this._searchOrganizationsService.advancedSearchOrganizations(parameters, page, size, 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 = this.searchPage.prepareFiltersToShow(data[2]);
|
|
|
|
}else{
|
2020-02-17 15:19:14 +01:00
|
|
|
this.searchPage.buildPageURLParameters(this.filters, [],false);
|
2020-02-11 15:56:20 +01:00
|
|
|
}
|
|
|
|
// 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;
|
2020-02-19 16:35:48 +01:00
|
|
|
this.searchPageUpdates.emit({disableForms: this.disableForms,searchUtils: this.searchUtils});
|
2020-02-11 15:56:20 +01:00
|
|
|
|
2020-02-19 16:35:48 +01:00
|
|
|
if(this.searchUtils.status == this.errorCodes.DONE) {
|
2020-02-11 15:56:20 +01:00
|
|
|
// 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 organizations", 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;
|
2020-02-19 16:35:48 +01:00
|
|
|
this.searchPageUpdates.emit({disableForms: this.disableForms,searchUtils: this.searchUtils});
|
|
|
|
|
2020-02-11 15:56:20 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private handleError(message: string, error) {
|
|
|
|
console.error("Organizations advanced Search Page: "+message, error);
|
|
|
|
}
|
|
|
|
}
|