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
|
import {SearchDataprovidersService} from '../../services/searchDataproviders.service';
|
6
|
import {SearchResult} from '../../utils/entities/searchResult';
|
7
|
import { ErrorCodes} from '../../utils/properties/errorCodes';
|
8
|
import {SearchFields, FieldDetails} from '../../utils/properties/searchFields';
|
9
|
import {SearchPageComponent } from '../searchUtils/searchPage.component';
|
10
|
import {SearchUtilsClass } from '../searchUtils/searchUtils.class';
|
11
|
import{EnvProperties} from '../../utils/properties/env-properties';
|
12
|
|
13
|
@Component({
|
14
|
selector: 'search-dataproviders',
|
15
|
template: `
|
16
|
|
17
|
<search-page pageTitle="Search Content Providers"
|
18
|
formPlaceholderText = "Search for Content Providers"
|
19
|
type="content providers" entityType="dataprovider" [(filters)] = "filters"
|
20
|
[(results)] = "results" [(searchUtils)] = "searchUtils" [baseUrl] = "baseUrl"
|
21
|
(queryChange)="queryChanged($event)"
|
22
|
[csvParams]="csvParams" csvPath="datasources" advancedSearchLink="/search/advanced/dataproviders"
|
23
|
[disableForms]="disableForms"
|
24
|
[loadPaging]="loadPaging"
|
25
|
[oldTotalResults]="oldTotalResults"
|
26
|
[piwikSiteId]=piwikSiteId
|
27
|
searchFormClass="datasourcesSearchForm">
|
28
|
</search-page>
|
29
|
|
30
|
`
|
31
|
})
|
32
|
export class SearchDataprovidersComponent {
|
33
|
private errorCodes: ErrorCodes;
|
34
|
@Input() piwikSiteId = null;
|
35
|
public results =[];
|
36
|
public filters =[];
|
37
|
public totalResults:number = 0 ;
|
38
|
public baseUrl:string;
|
39
|
public searchUtils:SearchUtilsClass = new SearchUtilsClass();
|
40
|
public sub: any; public subResults: any;
|
41
|
public _location:Location;
|
42
|
public searchFields:SearchFields = new SearchFields();
|
43
|
public refineFields: string[] = this.searchFields.DATASOURCE_REFINE_FIELDS;
|
44
|
public fieldIdsMap= this.searchFields.DATASOURCE_FIELDS;
|
45
|
public CSV: any = { "columnNames": [ "Title", "Type", "Coutries", "Compatibility" ],
|
46
|
"export":[]
|
47
|
};
|
48
|
public CSVDownloaded = false;
|
49
|
public csvParams: string;
|
50
|
|
51
|
public disableForms: boolean = false;
|
52
|
public loadPaging: boolean = true;
|
53
|
public oldTotalResults: number = 0;
|
54
|
pagingLimit = 0;
|
55
|
properties: EnvProperties;
|
56
|
@ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
|
57
|
|
58
|
constructor (private route: ActivatedRoute, private _searchDataprovidersService: SearchDataprovidersService ) {
|
59
|
this.errorCodes = new ErrorCodes();
|
60
|
this.searchUtils.status = this.errorCodes.LOADING;
|
61
|
this.searchUtils.page =1;
|
62
|
}
|
63
|
|
64
|
public ngOnInit() {
|
65
|
this.route.data
|
66
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
67
|
this.properties = data.envSpecific;
|
68
|
this.baseUrl = data.envSpecific.searchLinkToDataProviders;
|
69
|
this.pagingLimit = data.envSpecific.pagingLimit;
|
70
|
|
71
|
});
|
72
|
this.searchPage.refineFields = this.refineFields;
|
73
|
this.searchPage.fieldIdsMap = this.fieldIdsMap;
|
74
|
var firstLoad =true;
|
75
|
|
76
|
this.sub = this.route.queryParams.subscribe(params => {
|
77
|
if(params['page'] && this.searchUtils.page != params['page']) {
|
78
|
this.loadPaging = false;
|
79
|
this.oldTotalResults = this.searchUtils.totalResults;
|
80
|
}
|
81
|
|
82
|
this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
|
83
|
var refine = true;
|
84
|
if(this.searchUtils.page != ((params['page']=== undefined)?1:+params['page']) && this.filters && !firstLoad){
|
85
|
refine = false;
|
86
|
|
87
|
}
|
88
|
firstLoad = false;
|
89
|
this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
|
90
|
|
91
|
var queryParameters = this.searchPage.getQueryParametersFromUrl(params);
|
92
|
this._getResults(queryParameters, refine, this.searchUtils.page, this.searchUtils.size);
|
93
|
});
|
94
|
}
|
95
|
|
96
|
public ngOnDestroy() {
|
97
|
if(this.sub){
|
98
|
this.sub.unsubscribe();
|
99
|
}
|
100
|
if(this.subResults){
|
101
|
this.subResults.unsubscribe();
|
102
|
}
|
103
|
}
|
104
|
|
105
|
public getNumForEntity(entity: string, id:string) {
|
106
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
107
|
this.searchUtils.status = this.errorCodes.LOADING;
|
108
|
|
109
|
if(id != "" && entity != "") {
|
110
|
|
111
|
this._searchDataprovidersService.numOfEntityDataproviders(id, entity,this.properties).subscribe(
|
112
|
data => {
|
113
|
this.searchUtils.totalResults = data;
|
114
|
|
115
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
116
|
this.searchUtils.status = this.errorCodes.DONE;
|
117
|
if(this.searchUtils.totalResults == 0 ){
|
118
|
this.searchUtils.status = this.errorCodes.NONE;
|
119
|
}
|
120
|
},
|
121
|
err => {
|
122
|
console.log(err);
|
123
|
//TODO check erros (service not available, bad request)
|
124
|
// if( ){
|
125
|
// this.searchUtils.status = ErrorCodes.ERROR;
|
126
|
// }
|
127
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
128
|
//this.searchUtils.status = errorCodes.ERROR;
|
129
|
|
130
|
if(err.status == '404') {
|
131
|
this.searchUtils.status = this.errorCodes.NOT_FOUND;
|
132
|
} else if(err.status == '500') {
|
133
|
this.searchUtils.status = this.errorCodes.ERROR;
|
134
|
} else {
|
135
|
this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
|
136
|
}
|
137
|
}
|
138
|
);
|
139
|
}
|
140
|
}
|
141
|
|
142
|
public getNumForSearch(keyword: string) {
|
143
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
144
|
this.searchUtils.status = this.errorCodes.LOADING;
|
145
|
|
146
|
this._searchDataprovidersService.numOfDataproviders(keyword,this.properties).subscribe(
|
147
|
data => {
|
148
|
this.searchUtils.totalResults = data;
|
149
|
|
150
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
151
|
this.searchUtils.status = this.errorCodes.DONE;
|
152
|
if(this.searchUtils.totalResults == 0 ){
|
153
|
this.searchUtils.status = this.errorCodes.NONE;
|
154
|
}
|
155
|
},
|
156
|
err => {
|
157
|
console.log(err);
|
158
|
//TODO check erros (service not available, bad request)
|
159
|
// if( ){
|
160
|
// this.searchUtils.status = ErrorCodes.ERROR;
|
161
|
// }
|
162
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
163
|
//this.searchUtils.status = errorCodes.ERROR;
|
164
|
if(err.status == '404') {
|
165
|
this.searchUtils.status = this.errorCodes.NOT_FOUND;
|
166
|
} else if(err.status == '500') {
|
167
|
this.searchUtils.status = this.errorCodes.ERROR;
|
168
|
} else {
|
169
|
this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
|
170
|
}
|
171
|
}
|
172
|
);
|
173
|
}
|
174
|
|
175
|
public getResultsForDeposit(id:string, type:string, page: number, size: number){
|
176
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
177
|
//this.status = errorCodes.LOADING;
|
178
|
if(id != "") {
|
179
|
|
180
|
this._searchDataprovidersService.searchDataprovidersForDeposit(id,type, page, size,this.properties).subscribe(
|
181
|
data => {
|
182
|
this.searchUtils.totalResults = data[0];
|
183
|
console.info("search Dataproviders forDeposit: [id:"+id+", type:"+type+" ] [total results:"+this.totalResults+"]");
|
184
|
this.results = data[1];
|
185
|
|
186
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
187
|
this.searchUtils.status = this.errorCodes.DONE;
|
188
|
if(this.searchUtils.totalResults == 0 ){
|
189
|
this.searchUtils.status = this.errorCodes.NONE;
|
190
|
}
|
191
|
},
|
192
|
err => {
|
193
|
console.log(err);
|
194
|
//TODO check erros (service not available, bad request)
|
195
|
// if( ){
|
196
|
// this.searchUtils.status = ErrorCodes.ERROR;
|
197
|
// }
|
198
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
199
|
//this.searchUtils.status = errorCodes.ERROR;
|
200
|
if(err.status == '404') {
|
201
|
this.searchUtils.status = this.errorCodes.NOT_FOUND;
|
202
|
} else if(err.status == '500') {
|
203
|
this.searchUtils.status = this.errorCodes.ERROR;
|
204
|
} else {
|
205
|
this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
|
206
|
}
|
207
|
}
|
208
|
);
|
209
|
}
|
210
|
}
|
211
|
public getResultsForEntity(entity:string, id:string, page: number, size: number){
|
212
|
var parameters = "";
|
213
|
|
214
|
if(entity == "organization") {
|
215
|
parameters = "organizations/"+id;
|
216
|
}
|
217
|
|
218
|
if(parameters != "") {
|
219
|
|
220
|
this._searchDataprovidersService.searchDataprovidersForEntity(parameters, page, size,this.properties).subscribe(
|
221
|
data => {
|
222
|
this.searchUtils.totalResults = data[0];
|
223
|
console.info("search Dataproviders for "+entity+": [Parameters:"+parameters+" ] [total results:"+this.searchUtils.totalResults+"]");
|
224
|
this.results = data[1];
|
225
|
|
226
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
227
|
this.searchUtils.status = this.errorCodes.DONE;
|
228
|
if(this.searchUtils.totalResults == 0 ){
|
229
|
this.searchUtils.status = this.errorCodes.NONE;
|
230
|
}
|
231
|
},
|
232
|
err => {
|
233
|
console.log(err);
|
234
|
//TODO check erros (service not available, bad request)
|
235
|
// if( ){
|
236
|
// this.searchUtils.status = ErrorCodes.ERROR;
|
237
|
// }
|
238
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
239
|
//this.searchUtils.status = errorCodes.ERROR;
|
240
|
if(err.status == '404') {
|
241
|
this.searchUtils.status = this.errorCodes.NOT_FOUND;
|
242
|
} else if(err.status == '500') {
|
243
|
this.searchUtils.status = this.errorCodes.ERROR;
|
244
|
} else {
|
245
|
this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
|
246
|
}
|
247
|
}
|
248
|
);
|
249
|
}
|
250
|
}
|
251
|
|
252
|
public getResultsForDataproviders(id:string, page: number, size: number){
|
253
|
|
254
|
this._searchDataprovidersService.getDataProvidersforEntityRegistry(id, page, size,this.properties).subscribe(
|
255
|
data => {
|
256
|
this.searchUtils.totalResults = data[0];
|
257
|
console.info("search Dataproviders for Entity Registry: [Id:"+id+" ] [total results:"+this.searchUtils.totalResults+"]");
|
258
|
this.results = data[1];
|
259
|
|
260
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
261
|
this.searchUtils.status = this.errorCodes.DONE;
|
262
|
if(this.searchUtils.totalResults == 0 ){
|
263
|
this.searchUtils.status = this.errorCodes.NONE;
|
264
|
}
|
265
|
},
|
266
|
err => {
|
267
|
console.log(err);
|
268
|
//TODO check erros (service not available, bad request)
|
269
|
// if( ){
|
270
|
// this.searchUtils.status = ErrorCodes.ERROR;
|
271
|
// }
|
272
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
273
|
//this.searchUtils.status = errorCodes.ERROR;
|
274
|
if(err.status == '404') {
|
275
|
this.searchUtils.status = this.errorCodes.NOT_FOUND;
|
276
|
} else if(err.status == '500') {
|
277
|
this.searchUtils.status = this.errorCodes.ERROR;
|
278
|
} else {
|
279
|
this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
|
280
|
}
|
281
|
}
|
282
|
);
|
283
|
}
|
284
|
|
285
|
public getResults(keyword:string,refine:boolean, page: number, size: number){
|
286
|
var parameters = "";
|
287
|
if(keyword.length > 0){
|
288
|
parameters = "q="+ keyword;
|
289
|
}
|
290
|
this._getResults(parameters,refine,page,this.searchUtils.size);
|
291
|
}
|
292
|
private _getResults(parameters:string,refine:boolean, page: number, size: number){
|
293
|
if(page > this.pagingLimit) {
|
294
|
size=0;
|
295
|
}
|
296
|
if(page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
|
297
|
this.csvParams = parameters;
|
298
|
|
299
|
this.searchUtils.status = this.errorCodes.LOADING;
|
300
|
|
301
|
//this.searchPage.openLoading();
|
302
|
this.disableForms = true;
|
303
|
this.results = [];
|
304
|
this.searchUtils.totalResults = 0;
|
305
|
|
306
|
this.subResults = this._searchDataprovidersService.searchDataproviders(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, this.searchPage.getFields(),this.properties).subscribe(
|
307
|
data => {
|
308
|
this.searchUtils.totalResults = data[0];
|
309
|
console.info("search Content Providers: [Parameters:"+parameters+" ] [total results:"+this.searchUtils.totalResults+"]");
|
310
|
this.results = data[1];
|
311
|
if(refine){
|
312
|
this.filters = data[2];
|
313
|
}
|
314
|
this.searchPage.checkSelectedFilters(this.filters);
|
315
|
// this.filters = this.searchPage.checkSelectedFilters(data[2]);
|
316
|
this.searchPage.updateBaseUrlWithParameters(this.filters);
|
317
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
318
|
this.searchUtils.status = this.errorCodes.DONE;
|
319
|
if(this.searchUtils.totalResults == 0 ){
|
320
|
this.searchUtils.status = this.errorCodes.NONE;
|
321
|
}
|
322
|
//this.searchPage.closeLoading();
|
323
|
this.disableForms = false;
|
324
|
|
325
|
if(this.searchUtils.status == this.errorCodes.DONE) {
|
326
|
// Page out of limit!!!
|
327
|
let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
|
328
|
if(!(Number.isInteger(totalPages))) {
|
329
|
totalPages = (parseInt(totalPages, 10) + 1);
|
330
|
}
|
331
|
if(totalPages < page) {
|
332
|
this.searchUtils.totalResults = 0;
|
333
|
this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
|
334
|
}
|
335
|
}
|
336
|
},
|
337
|
err => {
|
338
|
console.log(err);
|
339
|
//TODO check erros (service not available, bad request)
|
340
|
// if( ){
|
341
|
// this.searchUtils.status = ErrorCodes.ERROR;
|
342
|
// }
|
343
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
344
|
//this.searchUtils.status = errorCodes.ERROR;
|
345
|
if(err.status == '404') {
|
346
|
this.searchUtils.status = this.errorCodes.NOT_FOUND;
|
347
|
} else if(err.status == '500') {
|
348
|
this.searchUtils.status = this.errorCodes.ERROR;
|
349
|
} else {
|
350
|
this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
|
351
|
}
|
352
|
|
353
|
//this.searchPage.closeLoading();
|
354
|
this.disableForms = false;
|
355
|
}
|
356
|
);
|
357
|
}
|
358
|
}
|
359
|
|
360
|
private setFilters(){
|
361
|
//TODO set filters from
|
362
|
}
|
363
|
|
364
|
public queryChanged($event) {
|
365
|
this.loadPaging = true;
|
366
|
|
367
|
var parameters = $event.value;
|
368
|
this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size);
|
369
|
}
|
370
|
}
|