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 {SearchDatasetsService} from '../../services/searchDatasets.service';
|
7
|
import {SearchResult} from '../../utils/entities/searchResult';
|
8
|
import {OpenaireProperties, ErrorCodes} from '../../utils/properties/openaireProperties';
|
9
|
import {SearchFields} from '../../utils/properties/searchFields';
|
10
|
import {SearchPageComponent } from '../searchUtils/searchPage.component';
|
11
|
import {SearchUtilsClass } from '../searchUtils/searchUtils.class';
|
12
|
|
13
|
@Component({
|
14
|
selector: 'search-datasets',
|
15
|
template: `
|
16
|
|
17
|
<search-page pageTitle="Search Datasets" type="dataset" [(filters)] = "filters"
|
18
|
[(results)] = "results" [(searchUtils)] = "searchUtils"
|
19
|
[baseUrl] = "baseUrl" (queryChange)="queryChanged($event)" >
|
20
|
</search-page>
|
21
|
|
22
|
`
|
23
|
|
24
|
})
|
25
|
export class SearchDatasetsComponent {
|
26
|
public results =[];
|
27
|
private filters: Filter[] =[];
|
28
|
// public totalResults:number = 0 ;
|
29
|
private baseUrl:string;
|
30
|
|
31
|
public searchUtils:SearchUtilsClass = new SearchUtilsClass();
|
32
|
private sub: any;
|
33
|
private subResults: any;
|
34
|
private searchFields:SearchFields = new SearchFields();
|
35
|
private refineFields: string[] = this.searchFields.DATASET_INDEX;
|
36
|
private indexIdsMap: { [key:string]:string } = this.searchFields.DATASET_INDEX_PARAM_MAP;
|
37
|
private fieldIdsMap: { [key:string]:{ name:string, operator:string, type:string, indexField:string, equalityOperator:string }} = this.searchFields.DATASET_FIELDS_MAP;
|
38
|
private urlParams : Map<string, string>;
|
39
|
private _location:Location;
|
40
|
|
41
|
|
42
|
@ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
|
43
|
constructor (private route: ActivatedRoute, private _searchDatasetsService: SearchDatasetsService ) {
|
44
|
|
45
|
var errorCodes:ErrorCodes = new ErrorCodes();
|
46
|
this.searchUtils.status =errorCodes.LOADING;
|
47
|
this.baseUrl = OpenaireProperties.getLinkToSearchDatasets();
|
48
|
|
49
|
}
|
50
|
|
51
|
private ngOnInit() {
|
52
|
this.searchPage.refineFields = this.refineFields;
|
53
|
this.searchPage.indexIdsMap = this.indexIdsMap;
|
54
|
this.searchPage.fieldIdsMap = this.fieldIdsMap;
|
55
|
|
56
|
this.sub = this.route.queryParams.subscribe(params => {
|
57
|
this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
|
58
|
this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
|
59
|
// this.getRefineResults();
|
60
|
//this.getResults(this.searchUtils.keyword, this.searchUtils.page, this.searchUtils.size, "searchPage");
|
61
|
var queryParameters = this.searchPage.getQueryParametersFromUrl(params);
|
62
|
this._getResults(queryParameters, true, this.searchUtils.page, this.searchUtils.size);
|
63
|
|
64
|
});
|
65
|
}
|
66
|
|
67
|
private ngOnDestroy() {
|
68
|
if(this.sub){
|
69
|
this.sub.unsubscribe();
|
70
|
}
|
71
|
if(this.subResults){
|
72
|
this.subResults.unsubscribe();
|
73
|
}
|
74
|
}
|
75
|
|
76
|
|
77
|
public getResultsForEntity(entity:string, id:string, page: number, size: number){
|
78
|
var parameters = "";
|
79
|
|
80
|
if(entity == "project") {
|
81
|
parameters = "projects/"+id;
|
82
|
} else if(entity == "person") {
|
83
|
parameters = "people/"+id;
|
84
|
}
|
85
|
|
86
|
if(parameters != "") {
|
87
|
|
88
|
this._searchDatasetsService.searchDatasetsForEntity(parameters, page, size).subscribe(
|
89
|
data => {
|
90
|
this.searchUtils.totalResults = data[0];
|
91
|
console.info("search Datasets for "+entity+": [Parameters:"+parameters+" ] [total results:"+this.searchUtils.totalResults+"]");
|
92
|
this.results = data[1];
|
93
|
|
94
|
var errorCodes:ErrorCodes = new ErrorCodes();
|
95
|
this.searchUtils.status = errorCodes.DONE;
|
96
|
if(this.searchUtils.totalResults == 0 ){
|
97
|
this.searchUtils.status = errorCodes.NONE;
|
98
|
}
|
99
|
},
|
100
|
err => {
|
101
|
console.error(err);
|
102
|
//TODO check erros (service not available, bad request)
|
103
|
// if( ){
|
104
|
// this.searchUtils.status = ErrorCodes.ERROR;
|
105
|
// }
|
106
|
var errorCodes:ErrorCodes = new ErrorCodes();
|
107
|
this.searchUtils.status = errorCodes.ERROR;
|
108
|
}
|
109
|
);
|
110
|
}
|
111
|
}
|
112
|
|
113
|
public getResultsForDataproviders(id:string, resultsFrom:string, page: number, size: number){
|
114
|
var parameters;
|
115
|
if(resultsFrom == "collectedFrom") {
|
116
|
parameters = "datasets?fq=collectedfromdatasourceid exact "+'"'+id+'"';
|
117
|
} else if(resultsFrom == "hostedBy") {
|
118
|
parameters = "datasets?fq=resulthostingdatasourceid exact "+'"'+id+'"';
|
119
|
}
|
120
|
|
121
|
if(parameters != "") {
|
122
|
|
123
|
this._searchDatasetsService.searchDatasetsForDataproviders(parameters, page, size).subscribe(
|
124
|
data => {
|
125
|
this.searchUtils.totalResults = data[0];
|
126
|
console.info("search Datasets for Dataproviders: [Parameters:"+parameters+" ] [total results:"+this.searchUtils.totalResults+"]");
|
127
|
this.results = data[1];
|
128
|
|
129
|
var errorCodes:ErrorCodes = new ErrorCodes();
|
130
|
this.searchUtils.status = errorCodes.DONE;
|
131
|
if(this.searchUtils.totalResults == 0 ){
|
132
|
this.searchUtils.status = errorCodes.NONE;
|
133
|
}
|
134
|
},
|
135
|
err => {
|
136
|
console.error(err);
|
137
|
//TODO check erros (service not available, bad request)
|
138
|
// if( ){
|
139
|
// this.searchUtils.status = ErrorCodes.ERROR;
|
140
|
// }
|
141
|
var errorCodes:ErrorCodes = new ErrorCodes();
|
142
|
this.searchUtils.status = errorCodes.ERROR;
|
143
|
}
|
144
|
);
|
145
|
}
|
146
|
}
|
147
|
|
148
|
public getResults(keyword:string,refine:boolean, page: number, size: number){
|
149
|
var parameters = "";
|
150
|
if(keyword.length > 0){
|
151
|
parameters = "q=" + keyword + "&op=and";
|
152
|
}
|
153
|
this._getResults(parameters,refine,page,size);
|
154
|
}
|
155
|
private _getResults(parameters:string,refine:boolean, page: number, size: number){
|
156
|
if(!refine && !this.searchPage){
|
157
|
this.searchPage = new SearchPageComponent(this._location);
|
158
|
}
|
159
|
this.subResults = this._searchDatasetsService.searchDatasets(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, this.searchPage.getFields()).subscribe(
|
160
|
data => {
|
161
|
this.searchUtils.totalResults = data[0];
|
162
|
console.info("search Datasets: [Parameters:"+parameters+" ] [total results:"+this.searchUtils.totalResults+"]");
|
163
|
this.results = data[1];
|
164
|
this.filters = data[2];
|
165
|
this.searchPage.checkSelectedFilters(this.filters);
|
166
|
this.searchPage.updateBaseUrlWithParameters(this.filters);
|
167
|
var errorCodes:ErrorCodes = new ErrorCodes();
|
168
|
this.searchUtils.status = errorCodes.DONE;
|
169
|
if(this.searchUtils.totalResults == 0 ){
|
170
|
this.searchUtils.status = errorCodes.NONE;
|
171
|
}
|
172
|
},
|
173
|
err => {
|
174
|
console.error(err);
|
175
|
//TODO check erros (service not available, bad request)
|
176
|
// if( ){
|
177
|
// this.searchUtils.status = ErrorCodes.ERROR;
|
178
|
// }
|
179
|
var errorCodes:ErrorCodes = new ErrorCodes();
|
180
|
this.searchUtils.status = errorCodes.ERROR;
|
181
|
}
|
182
|
);
|
183
|
}
|
184
|
|
185
|
|
186
|
|
187
|
private setFilters(){
|
188
|
//TODO set filters from
|
189
|
}
|
190
|
|
191
|
private queryChanged($event) {
|
192
|
var parameters = $event.value;
|
193
|
//this.getResults(parameters, this.searchUtils.page, this.searchUtils.size, "searchPage");
|
194
|
this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size);
|
195
|
}
|
196
|
}
|