connect/src/app/searchPages/simple/searchProjects.component.ts

214 lines
8.6 KiB
TypeScript

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";
import {CommunitiesService} from "../../openaireLibrary/connect/communities/communities.service";
import {CommunityService} from "../../openaireLibrary/connect/community/community.service";
import {Subscriber} from "rxjs";
@Component({
selector: 'openaire-search-projects',
template: `
<search-page-table pageTitle="Search Projects"
type="projects" entityType="project" [filters] = "filters"
[results] = "results" [searchUtils] = "searchUtils"
[columnNames]="columnNames"
[showResultCount]=false
[openaireLink]="'https://' + (properties.environment == 'production'?'':'beta.') + 'explore.openaire.eu/search/find/projects'"
[disableForms]="disableForms"
[enableSearchView]="enableSearchView"
searchFormClass="projectsTableSearchForm"
formPlaceholderText="Search by project title, grant id, funder... "
[piwikSiteId]="piwikSiteId" [hasPrefix]="false" [customFilter]="customFilter" [enableEntitySelection]="true"
[includeOnlyResultsAndFilter]="false" [showBreadcrumb]="true">
</search-page-table>
`
})
export class OpenaireSearchProjectsComponent {
private errorCodes: ErrorCodes;
private errorMessages: ErrorMessagesComponent;
public columnNames = ['Project', 'GrantId', 'Funder'];
public results =[];
public filters =[];
public searchUtils:SearchUtilsClass = new SearchUtilsClass();
subscriptions = [];
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, private _communitiesService: CommunitiesService, private _communityService: CommunityService) {
this.errorCodes = new ErrorCodes();
this.errorMessages = new ErrorMessagesComponent();
this.searchUtils.status = this.errorCodes.LOADING;
}
public ngOnInit() {
this.properties = properties;
this.subscriptions.push(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.subscriptions.push(this._communityService.getCommunityByState(this.properties, this.properties.communityAPI + this.communityId).subscribe(community =>{
if(community != null){
this.customFilter.valueName = community.shortTitle;
}
}));
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() {
this.subscriptions.forEach(subscription => {
if (subscription instanceof Subscriber) {
subscription.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.subscriptions.push(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<String>();
var value_name = [];
var value_original_id = [];
let i;
for(i=0; i<length; i++) {
let resData = Array.isArray(data) ? data[i] : data;
if(resData.funder && !funders.has(resData.funder)) {
funders.add(resData.funder);
value_name.push(resData.funder);
value_original_id.push(resData.funder.trim());
}
}
value_names.push(value_name);
value_original_ids.push(value_original_id);
var filters: Filter[] =[];
for(i =0 ; i < filter_names.length;i++){
if(value_names[i].length > 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);
}
}