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 {OpenaireProperties, ErrorCodes} from '../../utils/properties/openaireProperties';
8
import {SearchFields, FieldDetails} from '../../utils/properties/searchFields';
9
import {SearchPageComponent } from '../searchUtils/searchPage.component';
10
import {SearchUtilsClass } from '../searchUtils/searchUtils.class';
11

    
12
@Component({
13
    selector: 'search-organizations',
14
    template: `
15

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

    
28
    `
29

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

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

    
50
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
51

    
52
  constructor (private route: ActivatedRoute, private _searchOrganizationsService: SearchOrganizationsService ) {
53

    
54
    this.errorCodes = new ErrorCodes();
55
    this.searchUtils.status = this.errorCodes.LOADING;
56
    this.searchUtils.page =1;
57
    this.baseUrl = OpenaireProperties.getLinkToSearchOrganizations();
58

    
59
  }
60

    
61
  public ngOnInit() {
62
    this.searchPage.refineFields = this.refineFields;
63
    this.searchPage.fieldIdsMap = this.fieldIdsMap;
64
    var firstLoad = true;
65
    this.sub =  this.route.queryParams.subscribe(params => {
66
      if(params['page'] && this.searchUtils.page != params['page']) {
67
        this.loadPaging = false;
68
        this.oldTotalResults = this.searchUtils.totalResults;
69
      }
70

    
71
      this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
72
      var refine = true;
73
      if(this.searchUtils.page != ((params['page']=== undefined)?1:+params['page']) && this.filters && !firstLoad){
74
        refine = false;
75

    
76
      }
77
      firstLoad = false;
78
      this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
79

    
80
      var queryParameters = this.searchPage.getQueryParametersFromUrl(params);
81
       this._getResults(queryParameters, refine, this.searchUtils.page, this.searchUtils.size);
82
    });
83
  }
84

    
85
  public ngOnDestroy() {
86
    if(this.sub){
87
      this.sub.unsubscribe();
88
    }
89
    if(this.subResults){
90
      this.subResults.unsubscribe();
91
    }
92
  }
93

    
94

    
95
    public getResults(keyword:string,refine:boolean, page: number, size: number){
96
      var parameters = "";
97
      if(keyword.length > 0){
98
        parameters = "q=" + keyword;
99
      }
100
      this._getResults(parameters,refine,page,this.searchUtils.size);
101
    }
102
    private _getResults(parameters:string,refine:boolean, page: number, size: number){
103
        this.csvParams = parameters;
104

    
105
      // if(!refine && !this.searchPage){
106
      //     this.searchPage = new SearchPageComponent(this._location);
107
      // }
108
      //var errorCodes:ErrorCodes = new ErrorCodes();
109
      this.searchUtils.status = this.errorCodes.LOADING;
110
      //this.searchPage.openLoading();
111
      this.disableForms = true;
112
      this.results = [];
113
      this.searchUtils.totalResults = 0;
114

    
115
      this.subResults = this._searchOrganizationsService.searchOrganizations(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, this.searchPage.getFields()).subscribe(
116
          data => {
117
              this.searchUtils.totalResults = data[0];
118
              console.info("search Organizations: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
119
              this.results = data[1];
120
              if(refine){
121
                this.filters = data[2];
122
              }
123
              this.searchPage.checkSelectedFilters(this.filters);
124
              this.searchPage.updateBaseUrlWithParameters(this.filters);
125
              //var errorCodes:ErrorCodes = new ErrorCodes();
126
              this.searchUtils.status = this.errorCodes.DONE;
127
              if(this.searchUtils.totalResults == 0 ){
128
                this.searchUtils.status = this.errorCodes.NONE;
129
              }
130
              //this.searchPage.closeLoading();
131
              this.disableForms = false;
132

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

    
161
              //this.searchPage.closeLoading();
162
              this.disableForms = false;
163
          }
164
      );
165
    }
166

    
167

    
168
  public queryChanged($event) {
169
    this.loadPaging = true;
170
    
171
    var parameters = $event.value;
172
    console.info("queryChanged: Execute search query "+parameters);
173
    this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size);
174
  }
175
}
(5-5/12)