Project

General

Profile

1 61381 k.triantaf
import {SearchDataprovidersService} from '../../services/searchDataproviders.service';
2
import {ErrorCodes} from '../../utils/properties/errorCodes';
3
import {ErrorMessagesComponent} from '../../utils/errorMessages.component';
4
import {SearchCustomFilter, SearchUtilsClass} from '../../searchPages/searchUtils/searchUtils.class';
5
import {EnvProperties} from '../../utils/properties/env-properties';
6
import {StringUtils} from '../../utils/string-utils.class';
7
import {Subscriber} from "rxjs";
8
9
export class FetchDataproviders {
10
  private errorCodes: ErrorCodes;
11
  private errorMessages: ErrorMessagesComponent;
12
13
  public results = [];
14
  public searchUtils: SearchUtilsClass = new SearchUtilsClass();
15
  subscriptions = [];
16
  public CSV: any = {
17
    "columnNames": ["Title", "Type", "Coutries", "Compatibility"],
18
    "export": []
19
  };
20
  public CSVDownloaded = false;
21
  public csvParams: string;
22
  public loadPaging: boolean = true;
23
  public oldTotalResults: number = 0;
24
25
  constructor(private _searchDataprovidersService: SearchDataprovidersService) {
26
    this.errorCodes = new ErrorCodes();
27
    this.errorMessages = new ErrorMessagesComponent();
28
    this.searchUtils.status = this.errorCodes.LOADING;
29
  }
30
31
32
  public clearSubscriptions() {
33
34
    this.subscriptions.forEach(subscription => {
35
      if (subscription instanceof Subscriber) {
36
        subscription.unsubscribe();
37
      }
38
    });
39
  }
40
41
  public getResultsByKeyword(keyword: string, page: number, size: number, properties: EnvProperties, customFilter: SearchCustomFilter = null) {
42
    var parameters = "";
43
    if (keyword.length > 0) {
44
      parameters = "q=" + StringUtils.URIEncode(keyword);
45
    }
46
47
    //var errorCodes:ErrorCodes = new ErrorCodes();
48
    this.searchUtils.status = this.errorCodes.LOADING;
49
50
    var refineParams = null;
51
    if (customFilter) {
52
      refineParams = (refineParams ? (refineParams + '&') : '') + "&fq=" + StringUtils.URIEncode(customFilter.queryFieldName + " exact " + StringUtils.quote((customFilter.valueId)));
53
    }
54
    this.subscriptions.push(this._searchDataprovidersService.searchDataproviders(parameters, refineParams, page, size, [], properties).subscribe(
55
      data => {
56
        this.searchUtils.totalResults = data[0];
57
        //console.info("search Content Providers: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
58
        this.results = data[1];
59
60
        //ar errorCodes:ErrorCodes = new ErrorCodes();
61
        this.searchUtils.status = this.errorCodes.DONE;
62
        if (this.searchUtils.totalResults == 0) {
63
          this.searchUtils.status = this.errorCodes.NONE;
64
        }
65
      },
66
      err => {
67
        /*console.log(err);
68
         //TODO check erros (service not available, bad request)
69
        // if( ){
70
        //   this.searchUtils.status = ErrorCodes.ERROR;
71
        // }
72
        //var errorCodes:ErrorCodes = new ErrorCodes();
73
        //this.searchUtils.status = errorCodes.ERROR;
74
        if(err.status == '404') {
75
          this.searchUtils.status = this.errorCodes.NOT_FOUND;
76
        } else if(err.status == '500') {
77
          this.searchUtils.status = this.errorCodes.ERROR;
78
        } else {
79
          this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
80
        }
81
        */
82
        this.handleError("Error getting content providers for keyword: " + keyword, err);
83
        this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
84
      }
85
    ));
86
  }
87
88
  public getResultsForHome(size: number, properties: EnvProperties) {
89
    let page = 1;
90
    var parameters = "&sortBy=resultdateofacceptance,descending";//"orderby=date";
91
92
    this.searchUtils.status = this.errorCodes.LOADING;
93
94
    this.subscriptions.push(this._searchDataprovidersService.searchDataproviders(parameters, null, page, size, [], properties).subscribe(
95
      data => {
96
        this.searchUtils.totalResults = data[0];
97
        //console.info("search Content Providers: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
98
        this.results = data[1];
99
100
        this.searchUtils.status = this.errorCodes.DONE;
101
        if (this.searchUtils.totalResults == 0) {
102
          this.searchUtils.status = this.errorCodes.NONE;
103
        }
104
      },
105
      err => {
106
        /*console.log(err);
107
         //TODO check erros (service not available, bad request)
108
        if(err.status == '404') {
109
          this.searchUtils.status = this.errorCodes.NOT_FOUND;
110
        } else if(err.status == '500') {
111
          this.searchUtils.status = this.errorCodes.ERROR;
112
        } else {
113
          this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
114
        }*/
115
        this.handleError("Error getting content providers with parameters: " + parameters + " for Home", err);
116
        this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
117
      }
118
    ));
119
  }
120
121
  public getNumForEntity(entity: string, id: string, properties: EnvProperties) {
122
    //var errorCodes:ErrorCodes = new ErrorCodes();
123
    this.searchUtils.status = this.errorCodes.LOADING;
124
125
    if (id != "" && entity != "") {
126
127
      this.subscriptions.push(this._searchDataprovidersService.numOfEntityDataproviders(id, entity, properties).subscribe(
128
        data => {
129
          this.searchUtils.totalResults = data;
130
131
          //var errorCodes:ErrorCodes = new ErrorCodes();
132
          this.searchUtils.status = this.errorCodes.DONE;
133
          if (this.searchUtils.totalResults == 0) {
134
            this.searchUtils.status = this.errorCodes.NONE;
135
          }
136
        },
137
        err => {
138
          /*console.log(err);
139
           //TODO check erros (service not available, bad request)
140
          // if( ){
141
          //   this.searchUtils.status = ErrorCodes.ERROR;
142
          // }
143
          //var errorCodes:ErrorCodes = new ErrorCodes();
144
          //this.searchUtils.status = errorCodes.ERROR;
145
          if(err.status == '404') {
146
            this.searchUtils.status = this.errorCodes.NOT_FOUND;
147
          } else if(err.status == '500') {
148
            this.searchUtils.status = this.errorCodes.ERROR;
149
          } else {
150
            this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
151
          }*/
152
          this.handleError("Error getting content providers for " + entity + " with id: " + id, err);
153
          this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
154
        }
155
      ));
156
    }
157
  }
158
159
  public getNumForSearch(keyword: string, properties: EnvProperties, refineParams: string = null) {
160
    //var errorCodes:ErrorCodes = new ErrorCodes();
161
    this.searchUtils.status = this.errorCodes.LOADING;
162
163
    this.subscriptions.push(this._searchDataprovidersService.numOfSearchDataproviders(keyword, properties, refineParams).subscribe(
164
      data => {
165
        this.searchUtils.totalResults = data;
166
        this.searchUtils.status = this.errorCodes.DONE;
167
168
        if (this.searchUtils.totalResults == 0) {
169
          this.searchUtils.status = this.errorCodes.NONE;
170
        }
171
      },
172
      err => {
173
        /*console.log(err);
174
         //TODO check erros (service not available, bad request)
175
        // if( ){
176
        //   this.searchUtils.status = ErrorCodes.ERROR;
177
        // }
178
        //var errorCodes:ErrorCodes = new ErrorCodes();
179
        //this.searchUtils.status = errorCodes.ERROR;
180
        if(err.status == '404') {
181
          this.searchUtils.status = this.errorCodes.NOT_FOUND;
182
        } else if(err.status == '500') {
183
          this.searchUtils.status = this.errorCodes.ERROR;
184
        } else {
185
          this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
186
        }*/
187
        this.handleError("Error getting number of content providers for keyword: " + keyword, err);
188
        this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
189
      }
190
    ));
191
  }
192
193
  public getResultsForDeposit(id: string, type: string, page: number, size: number, properties: EnvProperties) {
194
    //var errorCodes:ErrorCodes = new ErrorCodes();
195
    this.searchUtils.status = this.errorCodes.LOADING;
196
    this.results = [];
197
    this.searchUtils.totalResults = 0;
198
    this.loadPaging = false;
199
200
    if (id != "") {
201
202
      this.subscriptions.push(this._searchDataprovidersService.searchDataprovidersForDeposit(id, type, page, size, properties).subscribe(
203
        data => {
204
          this.searchUtils.totalResults = data[0];
205
          //console.info("search Dataproviders forDeposit: [id:"+id+", type:"+type+" ]  [total results:"+this.searchUtils.totalResults+"]");
206
          this.results = data[1];
207
208
          //var errorCodes:ErrorCodes = new ErrorCodes();
209
          this.searchUtils.status = this.errorCodes.DONE;
210
          if (this.searchUtils.totalResults == 0) {
211
            this.searchUtils.status = this.errorCodes.NONE;
212
          }
213
214
          if (this.searchUtils.status == this.errorCodes.DONE) {
215
            // Page out of limit!!!
216
            let totalPages: any = this.searchUtils.totalResults / (this.searchUtils.size);
217
            if (!(Number.isInteger(totalPages))) {
218
              totalPages = (parseInt(totalPages, 10) + 1);
219
            }
220
            if (totalPages < page) {
221
              this.searchUtils.totalResults = 0;
222
              this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
223
            }
224
          }
225
          this.loadPaging = true;
226
          this.oldTotalResults = this.searchUtils.totalResults;
227
        },
228
        err => {
229
          /*console.log(err);
230
           //TODO check erros (service not available, bad request)
231
          // if( ){
232
          //   this.searchUtils.status = ErrorCodes.ERROR;
233
          // }
234
          //var errorCodes:ErrorCodes = new ErrorCodes();
235
          //this.searchUtils.status = errorCodes.ERROR;
236
          if(err.status == '404') {
237
            this.searchUtils.status = this.errorCodes.NOT_FOUND;
238
          } else if(err.status == '500') {
239
            this.searchUtils.status = this.errorCodes.ERROR;
240
          } else {
241
            this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
242
          }*/
243
          this.handleError("Error getting content providers in share " + type + " for organization with id: " + id, err);
244
          this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
245
246
          this.loadPaging = true;
247
          this.oldTotalResults = 0;
248
        }
249
      ));
250
    }
251
  }
252
253
  public getResultsBySubjectsForDeposit(subject: string, type: string, page: number, size: number, properties: EnvProperties) {
254
    //var errorCodes:ErrorCodes = new ErrorCodes();
255
256
    this.searchUtils.status = this.errorCodes.LOADING;
257
    this.results = [];
258
    this.searchUtils.totalResults = 0;
259
    this.loadPaging = false;
260
261
    this.subscriptions.push(this._searchDataprovidersService.searchDataProvidersBySubjects(subject, type, page, size, properties).subscribe(
262
      data => {
263
        this.searchUtils.totalResults = data[0];
264
        //console.info("search Dataproviders forDeposit: [subject:"+subject+", type:"+type+" ]  [total results:"+this.searchUtils.totalResults+"]");
265
        this.results = data[1];
266
267
        //var errorCodes:ErrorCodes = new ErrorCodes();
268
        this.searchUtils.status = this.errorCodes.DONE;
269
        if (this.searchUtils.totalResults == 0) {
270
          this.searchUtils.status = this.errorCodes.NONE;
271
        }
272
        if (this.searchUtils.status == this.errorCodes.DONE) {
273
          // Page out of limit!!!
274
          let totalPages: any = this.searchUtils.totalResults / (this.searchUtils.size);
275
          if (!(Number.isInteger(totalPages))) {
276
            totalPages = (parseInt(totalPages, 10) + 1);
277
          }
278
          if (totalPages < page) {
279
            this.searchUtils.totalResults = 0;
280
            this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
281
          }
282
        }
283
        this.loadPaging = true;
284
        this.oldTotalResults = this.searchUtils.totalResults;
285
      },
286
      err => {
287
        /*console.log(err);
288
         //TODO check erros (service not available, bad request)
289
        // if( ){
290
        //   this.searchUtils.status = ErrorCodes.ERROR;
291
        // }
292
        //var errorCodes:ErrorCodes = new ErrorCodes();
293
        //this.searchUtils.status = errorCodes.ERROR;
294
        if(err.status == '404') {
295
          this.searchUtils.status = this.errorCodes.NOT_FOUND;
296
        } else if(err.status == '500') {
297
          this.searchUtils.status = this.errorCodes.ERROR;
298
        } else {
299
          this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
300
        }*/
301
        this.handleError("Error getting content providers in share " + type + " by subject: " + subject, err);
302
        this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
303
304
        this.loadPaging = true;
305
        this.oldTotalResults = 0;
306
      }
307
    ));
308
309
  }
310
311
  public getResultsForEntity(entity: string, id: string, page: number, size: number, properties: EnvProperties) {
312
    //var errorCodes:ErrorCodes = new ErrorCodes();
313
    this.searchUtils.status = this.errorCodes.LOADING;
314
315
    var parameters = "";
316
317
    if (entity == "organization") {
318
      parameters = "organizations/" + id;
319
    }
320
321
    if (parameters != "") {
322
323
      this.subscriptions.push(this._searchDataprovidersService.searchDataprovidersForEntity(parameters, page, size, properties).subscribe(
324
        data => {
325
          this.searchUtils.totalResults = data[0];
326
          //console.info("search Dataproviders for "+entity+": [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
327
          this.results = data[1];
328
329
          //var errorCodes:ErrorCodes = new ErrorCodes();
330
          this.searchUtils.status = this.errorCodes.DONE;
331
          if (this.searchUtils.totalResults == 0) {
332
            this.searchUtils.status = this.errorCodes.NONE;
333
          }
334
        },
335
        err => {
336
          /*console.log(err);
337
           //TODO check erros (service not available, bad request)
338
          // if( ){
339
          //   this.searchUtils.status = ErrorCodes.ERROR;
340
          // }
341
          //var errorCodes:ErrorCodes = new ErrorCodes();
342
          //this.searchUtils.status = errorCodes.ERROR;
343
          if(err.status == '404') {
344
            this.searchUtils.status = this.errorCodes.NOT_FOUND;
345
          } else if(err.status == '500') {
346
            this.searchUtils.status = this.errorCodes.ERROR;
347
          } else {
348
            this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
349
          }*/
350
          this.handleError("Error getting content providers for " + entity + " with id: " + id, err);
351
          this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
352
        }
353
      ));
354
    }
355
  }
356
357
  public getResultsForDataproviders(id: string, page: number, size: number, properties: EnvProperties) {
358
    //var errorCodes:ErrorCodes = new ErrorCodes();
359
    this.searchUtils.status = this.errorCodes.LOADING;
360
361
    this.subscriptions.push(this._searchDataprovidersService.getDataProvidersforEntityRegistry(id, page, size, properties).subscribe(
362
      data => {
363
        this.searchUtils.totalResults = data[0];
364
        //console.info("search Dataproviders for Entity Registry: [Id:"+id+" ]  [total results:"+this.searchUtils.totalResults+"]");
365
        this.results = data[1];
366
367
        //var errorCodes:ErrorCodes = new ErrorCodes();
368
        this.searchUtils.status = this.errorCodes.DONE;
369
        if (this.searchUtils.totalResults == 0) {
370
          this.searchUtils.status = this.errorCodes.NONE;
371
        }
372
      },
373
      err => {
374
        /*console.log(err);
375
         //TODO check erros (service not available, bad request)
376
        // if( ){
377
        //   this.searchUtils.status = ErrorCodes.ERROR;
378
        // }
379
        //var errorCodes:ErrorCodes = new ErrorCodes();
380
        //this.searchUtils.status = errorCodes.ERROR;
381
        if(err.status == '404') {
382
          this.searchUtils.status = this.errorCodes.NOT_FOUND;
383
        } else if(err.status == '500') {
384
          this.searchUtils.status = this.errorCodes.ERROR;
385
        } else {
386
          this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
387
        }*/
388
        this.handleError("Error getting content providers for entity registry with id: " + id, err);
389
        this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
390
      }
391
    ));
392
  }
393
394
  private handleError(message: string, error) {
395
    console.error("Fetch Content Providers (class): " + message, error);
396
  }
397
}