1
|
import {Component, Input, ViewChild} from '@angular/core';
|
2
|
import { ActivatedRoute} from '@angular/router';
|
3
|
import {Location} from '@angular/common';
|
4
|
import { Filter, Value} from './searchUtils/searchHelperClasses.class';
|
5
|
|
6
|
import {RefineResultsService} from '../services/servicesUtils/refineResuts.service';
|
7
|
import {SearchDatasetsService} from '../services/searchDatasets.service';
|
8
|
import {SearchResult} from '../utils/entities/searchResult';
|
9
|
import {OpenaireProperties, ErrorCodes} from '../utils/properties/openaireProperties';
|
10
|
import {SearchFields} from '../utils/properties/searchFields';
|
11
|
import {SearchPageComponent } from './searchUtils/searchPage.component';
|
12
|
@Component({
|
13
|
selector: 'search-datasets',
|
14
|
template: `
|
15
|
|
16
|
<search-page pageTitle="Search Datasets" type="datasource" [(filters)] = "filters"
|
17
|
[(results)] = "results" [(totalResults)] = "totalResults" [(keyword)] = "keyword"
|
18
|
[(page)] = "page" [(size)] = "size" [(status)] = "status" [baseUrl] = "baseUrl" (queryChange)="queryChanged($event)" >
|
19
|
</search-page>
|
20
|
|
21
|
`
|
22
|
|
23
|
})
|
24
|
export class SearchDatasetsComponent {
|
25
|
public results =[];
|
26
|
private filters: Filter[] =[];
|
27
|
public totalResults:number = 0 ;
|
28
|
private baseUrl:string;
|
29
|
public status:number;
|
30
|
private keyword = '';
|
31
|
private page :number = 1;
|
32
|
private size :number = 10;
|
33
|
private sub: any;
|
34
|
private refineFields = [];
|
35
|
private searchFields:SearchFields = new SearchFields();
|
36
|
private _location:Location;
|
37
|
|
38
|
|
39
|
@ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
|
40
|
constructor (private route: ActivatedRoute, private _searchDatasetsService: SearchDatasetsService, private _refineResultsService:RefineResultsService ) {
|
41
|
|
42
|
var errorCodes:ErrorCodes = new ErrorCodes();
|
43
|
this.status =errorCodes.LOADING;
|
44
|
this.baseUrl = OpenaireProperties.getLinkToSearchDatasets();
|
45
|
this.refineFields = this.searchFields.DATASET_FIELDS;
|
46
|
if(!this.searchPage){
|
47
|
this.searchPage = new SearchPageComponent(this._location);
|
48
|
}
|
49
|
}
|
50
|
|
51
|
private ngOnInit() {
|
52
|
this.searchPage.refineFields = this.refineFields;
|
53
|
|
54
|
this.sub = this.route.queryParams.subscribe(params => {
|
55
|
this.keyword = (params['keyword']?params['keyword']:'');
|
56
|
this.page = (params['page']=== undefined)?1:+params['page'];
|
57
|
// this.getRefineResults();
|
58
|
this.getResults(this.keyword, this.page, this.size, "searchPage");
|
59
|
|
60
|
});
|
61
|
}
|
62
|
|
63
|
private ngOnDestroy() {
|
64
|
this.sub.unsubscribe();
|
65
|
}
|
66
|
public getRefineResults (){
|
67
|
// this._refineResultsService.getRefineResults(this.searchPage.getFields()).subscribe(
|
68
|
this._refineResultsService.getRefineResults(["projectendyear","projectstartyear","funderid","projectecsc39"]).subscribe(
|
69
|
|
70
|
data => {
|
71
|
|
72
|
this.filters = data;
|
73
|
|
74
|
},
|
75
|
err => {
|
76
|
console.error(err);
|
77
|
//TODO check erros (service not available, bad request)
|
78
|
// if( ){
|
79
|
// this.status = ErrorCodes.ERROR;
|
80
|
// }
|
81
|
|
82
|
}
|
83
|
);
|
84
|
}
|
85
|
public getResults(parameters:string, page: number, size: number, flag: string){
|
86
|
console.info("Search Datasets: Execute search query "+parameters);
|
87
|
this._searchDatasetsService.searchDatasets(parameters+this.searchPage.getRefineFieldsQuery(), page, size,'searchPage', this.searchPage.getFields()).subscribe(
|
88
|
data => {
|
89
|
this.totalResults = data[0];
|
90
|
console.info("Search Datasets: results="+this.totalResults);
|
91
|
this.results = data[1];
|
92
|
this.filters = data[2];
|
93
|
var errorCodes:ErrorCodes = new ErrorCodes();
|
94
|
this.status = errorCodes.DONE;
|
95
|
if(this.totalResults == 0 ){
|
96
|
this.status = errorCodes.NONE;
|
97
|
}
|
98
|
},
|
99
|
err => {
|
100
|
console.error(err);
|
101
|
console.info("error");
|
102
|
//TODO check erros (service not available, bad request)
|
103
|
// if( ){
|
104
|
// this.status = ErrorCodes.ERROR;
|
105
|
// }
|
106
|
var errorCodes:ErrorCodes = new ErrorCodes();
|
107
|
this.status = errorCodes.NOT_AVAILABLE;
|
108
|
}
|
109
|
);
|
110
|
}
|
111
|
|
112
|
private setFilters(){
|
113
|
//TODO set filters from
|
114
|
}
|
115
|
|
116
|
private queryChanged($event) {
|
117
|
var parameters = $event.value;
|
118
|
this.getResults(parameters, this.page, this.size, "searchPage");
|
119
|
}
|
120
|
}
|