Project

General

Profile

1
 import {SearchOrganizationsService} from '../../services/searchOrganizations.service';
2
 import { ErrorCodes} from '../../utils/properties/errorCodes';
3
 import {ErrorMessagesComponent}    from '../../utils/errorMessages.component';
4
 import {SearchUtilsClass } from '../../searchPages/searchUtils/searchUtils.class';
5
 import{EnvProperties} from '../../utils/properties/env-properties';
6
import {StringUtils} from '../../utils/string-utils.class';
7
export class FetchOrganizations {
8
  private errorCodes: ErrorCodes;
9
  private errorMessages: ErrorMessagesComponent;
10

    
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
    this.errorMessages = new ErrorMessagesComponent();
23
    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
    public getResultsByKeyword(keyword:string , page: number, size: number, properties:EnvProperties){
39
      var parameters = "";
40
      if(keyword.length > 0){
41
        parameters = "q=" + StringUtils.URIEncode(keyword);
42
      }
43

    
44
      //var errorCodes:ErrorCodes = new ErrorCodes();
45
      this.searchUtils.status = this.errorCodes.LOADING;
46

    
47
      this.subResults = this._searchOrganizationsService.searchOrganizations(parameters, null, page, size, [], properties).subscribe(
48
          data => {
49
              this.searchUtils.totalResults = data[0];
50
              //console.info("search Organizations: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
51
              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
              /*console.log(err);
61
               //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
              }*/
75
              this.handleError("Error getting organization for keyword: "+keyword, err);
76
              this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
77
           }
78
      );
79
    }
80

    
81
    private handleError(message: string, error) {
82
        console.error("Fetch Organizations (class): "+message, error);
83
    }
84
}
(3-3/7)