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 {SearchPeopleService} from '../../services/searchPeople.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 People" entityType="person"
17
                 type = "people"
18
                 [(results)] = "results"
19
                 [(searchUtils)] = "searchUtils"
20
                 [(fieldIds)]="fieldIds" [(fieldIdsMap)]="fieldIdsMap"   [(selectedFields)]="selectedFields"
21
                 (queryChange)="queryChanged($event)"
22
                 [csvParams]="csvParams" csvPath="resources">
23
    </advanced-search-page>
24

    
25
    `
26
 })
27

    
28
export class AdvancedSearchPeopleComponent {
29
  public results =[];
30
  public filters =[];
31

    
32
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
33

    
34
  public searchFields:SearchFields = new SearchFields();
35

    
36
  public fieldIds: string[] = this.searchFields.PERSON_ADVANCED_FIELDS;
37
  public fieldIdsMap  = this.searchFields.PERSON_FIELDS;
38
  public selectedFields:AdvancedField[] = [];
39

    
40
  @ViewChild (AdvancedSearchPageComponent) searchPage : AdvancedSearchPageComponent ;
41
  public resourcesQuery = "(oaftype exact person)";
42
  public csvParams: string;
43

    
44
  constructor (private route: ActivatedRoute, private _searchPeopleService: SearchPeopleService ) {
45

    
46
    this.results =[];
47
    var errorCodes:ErrorCodes = new ErrorCodes();
48
    this.searchUtils.status =errorCodes.LOADING;
49
    this.searchUtils.baseUrl = OpenaireProperties.searchLinkToAdvancedPeople;
50

    
51

    
52

    
53
  }
54
  ngOnInit() {
55
    var errorCodes:ErrorCodes = new ErrorCodes();
56
    this.searchUtils.status =errorCodes.LOADING;
57
   this.sub =  this.route.queryParams.subscribe(params => {
58
      let page = (params['page']=== undefined)?1:+params['page'];
59
      this.searchUtils.page = ( page <= 0 ) ? 1 : page;
60
      this.searchPage.fieldIds = this.fieldIds;
61
      this.searchPage.selectedFields = this.selectedFields;
62
      this.searchPage.fieldIdsMap = this.fieldIdsMap;
63
      this.searchPage.getSelectedFiltersFromUrl(params);
64
      this.getResults(this.searchPage.createQueryParameters(),  this.searchUtils.page, this.searchUtils.size);
65

    
66
    });
67
  }
68
  ngOnDestroy() {
69
    this.sub.unsubscribe();
70
  }
71
  sub: any;
72
  public getResults(parameters:string, page: number, size: number){
73
      if(parameters!= null && parameters != ''  ) {
74
        this.csvParams ="&type=people&query=( "+this.resourcesQuery + "and (" + parameters + "))";
75
      }else{
76
        this.csvParams ="&type=people&query="+this.resourcesQuery;
77
      }
78

    
79
    var errorCodes:ErrorCodes = new ErrorCodes();
80
    this.searchUtils.status = errorCodes.LOADING;
81
    this.searchPage.openLoading();
82

    
83
    console.info("Advanced Search People: Execute search query "+parameters);
84
     this._searchPeopleService.advancedSearchPeople(parameters, page, size).subscribe(
85
        data => {
86
            this.searchUtils.totalResults = data[0];
87
            console.info("search People total="+this.searchUtils.totalResults);
88
            this.results = data[1];
89
            this.searchPage.updateBaseUrlWithParameters();
90
            var errorCodes:ErrorCodes = new ErrorCodes();
91
             this.searchUtils.status = errorCodes.DONE;
92
            if(this.searchUtils.totalResults == 0 ){
93
              this.searchUtils.status = errorCodes.NONE;
94
            }
95
            this.searchPage.closeLoading();
96

    
97
        },
98
        err => {
99
            console.log(err);
100
            console.info("error");
101
            //TODO check erros (service not available, bad request)
102
            // if( ){
103
            //   this.searchUtils.status = errorCodes.ERROR;
104
            // }
105
            var errorCodes:ErrorCodes = new ErrorCodes();
106
            this.searchUtils.status = errorCodes.NOT_AVAILABLE;
107
            this.searchPage.closeLoading();
108

    
109
        }
110
    );
111
  }
112
  private setFilters(){
113
    //TODO set filters from
114
  }
115

    
116
  public queryChanged($event) {
117
    var parameters = $event.value;
118
    this.getResults(parameters, this.searchUtils.page,this.searchUtils.size);
119
    console.info("queryChanged: Execute search query "+parameters);
120

    
121
  }
122

    
123

    
124
}
(11-11/18)