import {Component, ViewChild} from '@angular/core'; import { ActivatedRoute} from '@angular/router'; import { Filter, Value} from '../../openaireLibrary/searchPages/searchUtils/searchHelperClasses.class'; import {ErrorCodes} from '../../openaireLibrary/utils/properties/errorCodes'; import {ErrorMessagesComponent} from '../../openaireLibrary/utils/errorMessages.component'; import {SearchFields} from '../../openaireLibrary/utils/properties/searchFields'; import {SearchPageTableViewComponent } from '../../openaireLibrary/searchPages/searchUtils/searchPageTableView.component'; import {SearchCustomFilter, SearchUtilsClass} from '../../openaireLibrary/searchPages/searchUtils/searchUtils.class'; import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties'; import {SearchCommunityProjectsService} from '../../openaireLibrary/connect/projects/searchProjects.service'; import {PiwikHelper} from '../../utils/piwikHelper'; import {ConnectHelper} from '../../openaireLibrary/connect/connectHelper'; import {properties} from "../../../environments/environment"; @Component({ selector: 'openaire-search-projects', template: ` ` }) export class OpenaireSearchProjectsComponent { private errorCodes: ErrorCodes; private errorMessages: ErrorMessagesComponent; public columnNames = ['Project', 'GrantId', 'Funder']; public results =[]; public filters =[]; public searchUtils:SearchUtilsClass = new SearchUtilsClass(); public sub: any; public subResults: any; public _location:Location; public searchFields:SearchFields = new SearchFields(); public refineFields: string[] = [];// = this.searchFields.JOURNAL_FIELDS; properties:EnvProperties; public disableForms: boolean = false; public enableSearchView: boolean = true; private communityId: string = ''; @ViewChild (SearchPageTableViewComponent) searchPage : SearchPageTableViewComponent ; piwikSiteId = null; customFilter: SearchCustomFilter = null; initialLoad = true; constructor (private route: ActivatedRoute, private _searchProjectsService: SearchCommunityProjectsService) { this.errorCodes = new ErrorCodes(); this.errorMessages = new ErrorMessagesComponent(); this.searchUtils.status = this.errorCodes.LOADING; } public ngOnInit() { this.properties = properties; this.sub = this.route.queryParams.subscribe(params => { this.communityId = ConnectHelper.getCommunityFromDomain(this.properties.domain); if(!this.communityId) { this.communityId = params['communityId']; } this.customFilter = new SearchCustomFilter("Community", "communityId", this.communityId, ""); this.piwikSiteId = PiwikHelper.getSiteId(this.communityId,this.properties.environment); this.searchUtils.keyword = (params['fv0']?params['fv0']:(params['keyword']?params['keyword']:'')); if(this.initialLoad) { this.initialLoad = false; this._getResults(params); }else{ this.searchPage.goTo(1); } }); } public ngOnDestroy() { if(this.sub){ this.sub.unsubscribe(); } if(this.subResults){ this.subResults.unsubscribe(); } } private _getResults(params){ this.searchUtils.status = this.errorCodes.LOADING; this.disableForms = true; this.enableSearchView = false; this.results = []; this.searchUtils.totalResults = 0; let size: number = 0; this.subResults = this._searchProjectsService.searchProjects(this.properties, this.communityId).subscribe( data => { // data=data.slice(0,150) this.filters = this.createFilters(data, params); this.searchUtils.totalResults = data.length; //console.info("search Projects [total results:"+this.searchUtils.totalResults+"]"); this.results = data; this.searchPage.checkSelectedFilters(this.filters); //var errorCodes:ErrorCodes = new ErrorCodes(); this.searchUtils.status = this.errorCodes.DONE; if(this.searchUtils.totalResults == 0 ){ this.searchUtils.status = this.errorCodes.NONE; } else { this.searchPage.triggerInitialLoad(); this.searchPage.transform(this.results, this.searchUtils); this.disableForms = false; } this.enableSearchView = true; }, err => { /* console.log(err); //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.handleError("Error getting projects for community with id: "+this.communityId, err); this.searchUtils.status = this.errorMessages.getErrorCode(err.status); this.enableSearchView = true; } ); } private setFilters(){ //TODO set filters from } private createFilters(data, params):Filter[] { let length = Array.isArray(data) ? data.length : 1; var filter_names=["Funder"]; var filter_ids=["relfunder"]; var searchFields = new SearchFields(); var filter_original_ids = ["relfunder"];//searchFields.JOURNAL_FIELDS; this.refineFields = ["relfunder"]; this.searchPage.refineFields = this.refineFields; this.searchPage.getParametersFromUrl(params); var value_names=[/*["Journal", "Journal Aggregator\/Publisher"]*/]; var value_original_ids=[/*["pubsrepository::journal", "aggregator::pubsrepository::journals"]*/]; var funders = new Set(); var value_name = []; var value_original_id = []; let i; for(i=0; i 0) { var values: Value[] = []; for (var j = 0; j < value_names[i].length; j++) { var value: Value = {name: value_names[i][j], id: value_original_ids[i][j], number: j, selected: false} values.push(value); } var filter: Filter = { title: filter_names[i], filterId: filter_ids[i], originalFilterId: filter_original_ids[i], values: values, countSelectedValues: 0, "filterOperator": 'or', valueIsExact: true, filterType: "checkbox" }; filters.push(filter); } } return filters; } private handleError(message: string, error) { console.error("Projects simple Search Page: "+message, error); } }