Project

General

Profile

1 61381 k.triantaf
 import {SearchOrganizationsService} from '../../services/searchOrganizations.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
export class FetchOrganizations {
8
  private errorCodes: ErrorCodes;
9
  private errorMessages: ErrorMessagesComponent;
10
11
  public results =[];
12
13
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
14
   public subResults: any;
15
16
17
18
  constructor ( private _searchOrganizationsService: SearchOrganizationsService ) {
19
20
    this.errorCodes = new ErrorCodes();
21
    this.errorMessages = new ErrorMessagesComponent();
22
    this.searchUtils.status = this.errorCodes.LOADING;
23
24
  }
25
26
27
  public clearSubscriptions() {
28
29
    if(this.subResults){
30
      this.subResults.unsubscribe();
31
    }
32
  }
33
34
35
    public getResultsByKeyword(keyword:string , page: number, size: number, properties:EnvProperties,  customFilter:SearchCustomFilter=null){
36
      var parameters = "";
37
      if(keyword.length > 0){
38
        parameters = "q=" + StringUtils.URIEncode(keyword);
39
      }
40
41
      //var errorCodes:ErrorCodes = new ErrorCodes();
42
      this.searchUtils.status = this.errorCodes.LOADING;
43
      var refineParams = null;
44
      if(customFilter){
45
        refineParams = (refineParams?(refineParams+'&'):'')+"&fq="+StringUtils.URIEncode(customFilter.queryFieldName + " exact " + StringUtils.quote((customFilter.valueId )));
46
      }
47
      this.subResults = this._searchOrganizationsService.searchOrganizations(parameters, refineParams, 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
}