Project

General

Profile

1
import {Component, Input, ViewChild} from '@angular/core';
2
import {Observable}       from 'rxjs/Observable';
3
import { Router, ActivatedRoute} from '@angular/router';
4
import {Filter, Value,AdvancedField} 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 {AdvancedSearchPageComponent} from '../searchUtils/advancedSearchPage.component';
9
import {SearchFields} from '../../utils/properties/searchFields';
10
import {SearchUtilsClass } from '../searchUtils/searchUtils.class';
11

    
12

    
13
@Component({
14
    selector: 'advanced-search-organizations',
15
    template: `
16
    <advanced-search-page pageTitle="Advanced Search for Organizations" entityType="organization"
17
                 type = "organizations"
18
                 [(results)] = "results"
19
                 [(searchUtils)] = "searchUtils"
20
                 [(fieldIds)]="fieldIds" [(fieldIdsMap)]="fieldIdsMap"   [(selectedFields)]="selectedFields"
21
                 (queryChange)="queryChanged($event)"
22
                 [csvParams]="csvParams" csvPath="resources" simpleSearchLink="/search/find/organizations"
23
                 [disableForms]="disableForms"
24
                 [loadPaging]="loadPaging"
25
                 [oldTotalResults]="oldTotalResults">
26
    </advanced-search-page>
27

    
28
    `
29
 })
30

    
31
export class AdvancedSearchOrganizationsComponent {
32
  private errorCodes: ErrorCodes;
33

    
34
  public results =[];
35
  public filters =[];
36
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
37
  public searchFields:SearchFields = new SearchFields();
38

    
39
  public fieldIds:  string[] = this.searchFields.ORGANIZATION_ADVANCED_FIELDS;
40
  public fieldIdsMap  = this.searchFields.ORGANIZATION_FIELDS;
41
  public selectedFields:AdvancedField[] =  [];
42
  public csvParams: string;
43
  public disableForms: boolean = false;
44
  public loadPaging: boolean = true;
45
  public oldTotalResults: number = 0;
46

    
47
  @ViewChild (AdvancedSearchPageComponent) searchPage : AdvancedSearchPageComponent ;
48

    
49
public resourcesQuery = "(oaftype exact organization)";
50
  constructor (private route: ActivatedRoute, private _searchOrganizationsService: SearchOrganizationsService ) {
51
    this.results =[];
52
    this.errorCodes = new ErrorCodes();
53
    this.searchUtils.status = this.errorCodes.LOADING;
54
    this.searchUtils.baseUrl = OpenaireProperties.searchLinkToAdvancedOrganizations;
55

    
56

    
57

    
58
  }
59
  ngOnInit() {
60
    //var errorCodes:ErrorCodes = new ErrorCodes();
61
    this.searchUtils.status = this.errorCodes.LOADING;
62

    
63
    this.sub =  this.route.queryParams.subscribe(params => {
64
      if(params['page'] && this.searchUtils.page != params['page']) {
65
        this.loadPaging = false;
66
        this.oldTotalResults = this.searchUtils.totalResults;
67
      }
68

    
69
      let page = (params['page']=== undefined)?1:+params['page'];
70
      this.searchUtils.page = ( page <= 0 ) ? 1 : page;
71
      this.searchPage.fieldIds = this.fieldIds;
72
      this.selectedFields =[];
73
      this.searchPage.selectedFields = this.selectedFields;
74
      this.searchPage.fieldIdsMap = this.fieldIdsMap;
75
      this.searchPage.getSelectedFiltersFromUrl(params);
76
      this.getResults(this.searchPage.createQueryParameters(),  this.searchUtils.page, this.searchUtils.size);
77

    
78
    });
79
  }
80
  ngOnDestroy() {
81
    this.sub.unsubscribe();
82
  }
83
  sub: any;
84
  public getResults(parameters:string, page: number, size: number){
85
      if(parameters!= null && parameters != ''  ) {
86
        this.csvParams ="&type=organizations&query=( "+this.resourcesQuery + "and (" + parameters + "))";
87
      }else{
88
        this.csvParams ="&type=organizations&query="+this.resourcesQuery;
89
      }
90

    
91
    //var errorCodes:ErrorCodes = new ErrorCodes();
92
    this.searchUtils.status = this.errorCodes.LOADING;
93
    //this.searchPage.openLoading();
94
    this.disableForms = true;
95
    this.results = [];
96
    this.searchUtils.totalResults = 0;
97

    
98
    console.info("Advanced Search for Organizations: Execute search query "+parameters);
99
     this._searchOrganizationsService.advancedSearchOrganizations(parameters, page, size).subscribe(
100
        data => {
101
            this.searchUtils.totalResults = data[0];
102
            console.info("search Organizations total="+this.searchUtils.totalResults);
103
            this.results = data[1];
104
            this.searchPage.updateBaseUrlWithParameters();
105
            //var errorCodes:ErrorCodes = new ErrorCodes();
106
            this.searchUtils.status = this.errorCodes.DONE;
107
            if(this.searchUtils.totalResults == 0 ){
108
              this.searchUtils.status = this.errorCodes.NONE;
109
            }
110
            //this.searchPage.closeLoading();
111
            this.disableForms = false;
112

    
113
            if(this.searchUtils.status == this.errorCodes.DONE) {
114
              // Page out of limit!!!
115
              let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
116
              if(!(Number.isInteger(totalPages))) {
117
                  totalPages = (parseInt(totalPages, 10) + 1);
118
              }
119
              if(totalPages < page) {
120
                this.searchUtils.totalResults = 0;
121
                this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
122
              }
123
            }
124
        },
125
        err => {
126
            console.log(err);
127
            console.info("error");
128
            //TODO check erros (service not available, bad request)
129
            // if( ){
130
            //   this.searchUtils.status = errorCodes.ERROR;
131
            // }
132
            //var errorCodes:ErrorCodes = new ErrorCodes();
133
            //this.searchUtils.status = errorCodes.NOT_AVAILABLE;
134
            if(err.status == '404') {
135
              this.searchUtils.status = this.errorCodes.NOT_FOUND;
136
            } else if(err.status == '500') {
137
              this.searchUtils.status = this.errorCodes.ERROR;
138
            } else {
139
              this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
140
            }
141

    
142
            //this.searchPage.closeLoading();
143
            this.disableForms = false;
144
        }
145
    );
146
  }
147
  private setFilters(){
148
    //TODO set filters from
149
  }
150

    
151
  public queryChanged($event) {
152
    this.loadPaging = true;
153
    
154
    var parameters = $event.value;
155
    this.getResults(parameters, this.searchUtils.page,this.searchUtils.size);
156
    console.info("queryChanged: Execute search query "+parameters);
157

    
158
  }
159

    
160

    
161
}
(8-8/18)