Project

General

Profile

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 {SearchOrganizationsService} from '../../services/searchOrganizations.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-organizations',
15
    template: `
16

    
17
    <search-page pageTitle="Search Organizations"
18
                  formPlaceholderText = "Search for Organizations"
19
                 type="organizations" entityType="organization" [(filters)] = "filters"
20
                 [(results)] = "results"  [(searchUtils)] = "searchUtils"
21
                 [baseUrl] = "baseUrl" (queryChange)="queryChanged($event)"
22
                 [csvParams]="csvParams" csvPath="organizations" advancedSearchLink="/search/advanced/organizations"
23
                 [disableForms]="disableForms"
24
                 [loadPaging]="loadPaging"
25
                 [oldTotalResults]="oldTotalResults"
26
                 searchFormClass="organizationsSearchForm">
27
    </search-page>
28

    
29
    `
30

    
31
})
32
export class SearchOrganizationsComponent {
33
  private errorCodes: ErrorCodes;
34

    
35
  public results =[];
36
  public filters =[];
37
  public baseUrl:string;
38
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
39
  public sub: any;
40
  public subResults: any;
41
  public searchFields:SearchFields = new SearchFields();
42
  public refineFields: string[] =  this.searchFields.ORGANIZATION_REFINE_FIELDS;
43
  public fieldIdsMap = this.searchFields.ORGANIZATION_FIELDS;
44
  public urlParams : Map<string, string>;
45
  public _location:Location;
46
  public csvParams: string;
47
  public disableForms: boolean = false;
48
  public loadPaging: boolean = true;
49
  public oldTotalResults: number = 0;
50
  pagingLimit = 0;
51
properties: EnvProperties;
52
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
53

    
54
  constructor (private route: ActivatedRoute, private _searchOrganizationsService: SearchOrganizationsService ) {
55

    
56
    this.errorCodes = new ErrorCodes();
57
    this.searchUtils.status = this.errorCodes.LOADING;
58
    this.searchUtils.page =1;
59

    
60
  }
61

    
62
  public ngOnInit() {
63
    this.route.data
64
      .subscribe((data: { envSpecific: EnvProperties }) => {
65
        this.properties = data.envSpecific;
66
        this.baseUrl = data.envSpecific.searchLinkToOrganizations;
67
        this.pagingLimit = data.envSpecific.pagingLimit;
68

    
69
      });
70
    this.searchPage.refineFields = this.refineFields;
71
    this.searchPage.fieldIdsMap = this.fieldIdsMap;
72
    var firstLoad = true;
73
    this.sub =  this.route.queryParams.subscribe(params => {
74
      if(params['page'] && this.searchUtils.page != params['page']) {
75
        this.loadPaging = false;
76
        this.oldTotalResults = this.searchUtils.totalResults;
77
      }
78

    
79
      this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
80
      var refine = true;
81
      if(this.searchUtils.page != ((params['page']=== undefined)?1:+params['page']) && this.filters && !firstLoad){
82
        refine = false;
83

    
84
      }
85
      firstLoad = false;
86
      this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
87

    
88
      var queryParameters = this.searchPage.getQueryParametersFromUrl(params);
89
       this._getResults(queryParameters, refine, this.searchUtils.page, this.searchUtils.size);
90
    });
91
  }
92

    
93
  public ngOnDestroy() {
94
    if(this.sub){
95
      this.sub.unsubscribe();
96
    }
97
    if(this.subResults){
98
      this.subResults.unsubscribe();
99
    }
100
  }
101

    
102

    
103
    public getResults(keyword:string,refine:boolean, page: number, size: number){
104
      var parameters = "";
105
      if(keyword.length > 0){
106
        parameters = "q=" + keyword;
107
      }
108
      this._getResults(parameters,refine,page,this.searchUtils.size);
109
    }
110
    private _getResults(parameters:string,refine:boolean, page: number, size: number){
111
      if(page > this.pagingLimit) {
112
        size=0;
113
      }
114
      if(page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
115
        this.csvParams = parameters;
116

    
117
        this.searchUtils.status = this.errorCodes.LOADING;
118
        //this.searchPage.openLoading();
119
        this.disableForms = true;
120
        this.results = [];
121
        this.searchUtils.totalResults = 0;
122

    
123
        this.subResults = this._searchOrganizationsService.searchOrganizations(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, this.searchPage.getFields(), this.properties).subscribe(
124
          data => {
125
              this.searchUtils.totalResults = data[0];
126
              console.info("search Organizations: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
127
              this.results = data[1];
128
              if(refine){
129
                this.filters = data[2];
130
              }
131
              this.searchPage.checkSelectedFilters(this.filters);
132
              this.searchPage.updateBaseUrlWithParameters(this.filters);
133
              //var errorCodes:ErrorCodes = new ErrorCodes();
134
              this.searchUtils.status = this.errorCodes.DONE;
135
              if(this.searchUtils.totalResults == 0 ){
136
                this.searchUtils.status = this.errorCodes.NONE;
137
              }
138
              //this.searchPage.closeLoading();
139
              this.disableForms = false;
140

    
141
              if(this.searchUtils.status == this.errorCodes.DONE) {
142
                // Page out of limit!!!
143
                let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
144
                if(!(Number.isInteger(totalPages))) {
145
                    totalPages = (parseInt(totalPages, 10) + 1);
146
                }
147
                if(totalPages < page) {
148
                  this.searchUtils.totalResults = 0;
149
                  this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
150
                }
151
              }
152
          },
153
          err => {
154
              console.log(err);
155
               //TODO check erros (service not available, bad request)
156
              // if( ){
157
              //   this.searchUtils.status = ErrorCodes.ERROR;
158
              // }
159
              //var errorCodes:ErrorCodes = new ErrorCodes();
160
              //this.searchUtils.status = errorCodes.ERROR;
161
              if(err.status == '404') {
162
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
163
              } else if(err.status == '500') {
164
                this.searchUtils.status = this.errorCodes.ERROR;
165
              } else {
166
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
167
              }
168

    
169
              //this.searchPage.closeLoading();
170
              this.disableForms = false;
171
          }
172
        );
173
      }
174
    }
175

    
176

    
177
  public queryChanged($event) {
178
    this.loadPaging = true;
179

    
180
    var parameters = $event.value;
181
    console.info("queryChanged: Execute search query "+parameters);
182
    this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size);
183
  }
184
}
(5-5/12)