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