Project

General

Profile

1 50169 argiro.kok
 import {SearchOrganizationsService} from '../../services/searchOrganizations.service';
2 50761 argiro.kok
 import { ErrorCodes} from '../../utils/properties/errorCodes';
3 54870 konstantin
 import {ErrorMessagesComponent}    from '../../utils/errorMessages.component';
4 50169 argiro.kok
 import {SearchUtilsClass } from '../../searchPages/searchUtils/searchUtils.class';
5 50586 argiro.kok
 import{EnvProperties} from '../../utils/properties/env-properties';
6 53593 argiro.kok
import {StringUtils} from '../../utils/string-utils.class';
7 50169 argiro.kok
export class FetchOrganizations {
8
  private errorCodes: ErrorCodes;
9 54870 konstantin
  private errorMessages: ErrorMessagesComponent;
10 50169 argiro.kok
11
  public results =[];
12
13
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
14
  public sub: any;
15
  public subResults: any;
16
17
18
19
  constructor ( private _searchOrganizationsService: SearchOrganizationsService ) {
20
21
    this.errorCodes = new ErrorCodes();
22 54870 konstantin
    this.errorMessages = new ErrorMessagesComponent();
23 50169 argiro.kok
    this.searchUtils.status = this.errorCodes.LOADING;
24
25
  }
26
27
28
  public ngOnDestroy() {
29
    if(this.sub){
30
      this.sub.unsubscribe();
31
    }
32
    if(this.subResults){
33
      this.subResults.unsubscribe();
34
    }
35
  }
36
37
38 50586 argiro.kok
    public getResultsByKeyword(keyword:string , page: number, size: number, properties:EnvProperties){
39 50169 argiro.kok
      var parameters = "";
40
      if(keyword.length > 0){
41 53593 argiro.kok
        parameters = "q=" + StringUtils.URIEncode(keyword);
42 50169 argiro.kok
      }
43
44
      //var errorCodes:ErrorCodes = new ErrorCodes();
45
      this.searchUtils.status = this.errorCodes.LOADING;
46
47 50586 argiro.kok
      this.subResults = this._searchOrganizationsService.searchOrganizations(parameters, null, page, size, [], properties).subscribe(
48 50169 argiro.kok
          data => {
49
              this.searchUtils.totalResults = data[0];
50 54775 konstantin
              //console.info("search Organizations: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
51 50169 argiro.kok
              this.results = data[1];
52
53
              //var errorCodes:ErrorCodes = new ErrorCodes();
54
              this.searchUtils.status = this.errorCodes.DONE;
55
              if(this.searchUtils.totalResults == 0 ){
56
                this.searchUtils.status = this.errorCodes.NONE;
57
              }
58
           },
59
          err => {
60 54870 konstantin
              /*console.log(err);
61 50169 argiro.kok
               //TODO check erros (service not available, bad request)
62
              // if( ){
63
              //   this.searchUtils.status = ErrorCodes.ERROR;
64
              // }
65
              //var errorCodes:ErrorCodes = new ErrorCodes();
66
              //this.searchUtils.status = errorCodes.ERROR;
67
68
              if(err.status == '404') {
69
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
70
              } else if(err.status == '500') {
71
                this.searchUtils.status = this.errorCodes.ERROR;
72
              } else {
73
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
74 54870 konstantin
              }*/
75
              this.handleError("Error getting organization for keyword: "+keyword, err);
76
              this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
77 50169 argiro.kok
           }
78
      );
79
    }
80
81 54870 konstantin
    private handleError(message: string, error) {
82
        console.error("Fetch Organizations (class): "+message, error);
83
    }
84 50169 argiro.kok
}