Project

General

Profile

1 44035 konstantin
import {Component, Input, ViewChild} from '@angular/core';
2
import { ActivatedRoute} from '@angular/router';
3 44262 argiro.kok
import {Location} from '@angular/common';
4 44035 konstantin
5 44619 argiro.kok
import { Filter, Value} from '../searchUtils/searchHelperClasses.class';
6 44035 konstantin
7 44619 argiro.kok
import {SearchPeopleService} from '../../services/searchPeople.service';
8
import {SearchResult}     from '../../utils/entities/searchResult';
9
import {OpenaireProperties, ErrorCodes} from '../../utils/properties/openaireProperties';
10
import {SearchFields} from '../../utils/properties/searchFields';
11
import {SearchPageComponent } from '../searchUtils/searchPage.component';
12
import {SearchUtilsClass } from '../searchUtils/searchUtils.class';
13 44587 argiro.kok
14 44035 konstantin
@Component({
15
    selector: 'search-people',
16
    template: `
17
18 44739 argiro.kok
    <search-page pageTitle="Search People" type="person" [(filters)] = "filters"
19 44726 argiro.kok
                 [(results)] = "results"
20 44587 argiro.kok
                 [(searchUtils)] = "searchUtils" [baseUrl] = "baseUrl" (queryChange)="queryChanged($event)" [showRefine]=false >
21 44035 konstantin
    </search-page>
22
23
    `
24
25
})
26
export class SearchPeopleComponent {
27
  public results =[];
28 44826 argiro.kok
  public filters =[];
29
   public baseUrl:string;
30 44726 argiro.kok
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
31 44826 argiro.kok
  public sub: any;
32
  public _location:Location;
33
  public refineFields = [];
34
  public searchFields:SearchFields = new SearchFields();
35 44035 konstantin
36 44262 argiro.kok
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
37
38 44035 konstantin
  constructor (private route: ActivatedRoute, private _searchPeopleService: SearchPeopleService ) {
39
    //this.results =[];
40
    //this.filters =[];
41
    var errorCodes:ErrorCodes = new ErrorCodes();
42 44587 argiro.kok
    this.searchUtils.status =errorCodes.LOADING;
43 44035 konstantin
    this.baseUrl = OpenaireProperties.getLinkToSearchPeople();
44 44262 argiro.kok
    //get refine field filters  from url parameters
45
    if(!this.searchPage){
46
      this.searchPage = new SearchPageComponent(this._location);
47
    }
48 44035 konstantin
  }
49
50 44826 argiro.kok
  public ngOnInit() {
51 44262 argiro.kok
    this.searchPage.refineFields = this.refineFields; //TODO make it work as a directive
52 44035 konstantin
    this.sub =  this.route.queryParams.subscribe(params => {
53 44587 argiro.kok
    this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
54
    this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
55 44035 konstantin
    for(var i=0; i<5 ; i++){
56
           var  values = [];
57
           for(var j=0; j<10 ; j++){
58
             var value:Value = {name: "name"+j, id: "filter_"+i+ "_id_"+j, number:j, selected:false}
59
             values.push(value);
60
           }
61
           values.sort((n2,n1) => {
62
              if (n1.number > n2.number) {
63
                  return 1;
64
              }
65
66
              if (n1.number < n2.number) {
67
                  return -1;
68
              }
69
70
              return 0;
71
          });
72
          var filter:Filter = {title: "title"+i, filterId: "filter_"+i, originalFilterId: "filter_"+i, values : values, countSelectedValues:0, "filterOperator": 'and'}
73
          if(i==0) {
74
              var  values = [];
75
              for(var j=0; j<10 ; j++){
76
                var value:Value = {name: "MYname"+j, id: "MYfilter_"+i+ "_id_"+j, number:j, selected:false}
77
                values.push(value);
78
              }
79
              values.sort((n2,n1) => {
80
                 if (n1.number > n2.number) {
81
                     return 1;
82
                 }
83
84
                 if (n1.number < n2.number) {
85
                     return -1;
86
                 }
87
88
                 return 0;
89
              });
90
              var filter1:Filter = {title: "MYtitle"+i, filterId: "MYfilter_"+i, originalFilterId: "MYfilter_"+i, values : values, countSelectedValues:0, "filterOperator": 'or'}
91
              this.filters.push(filter1);
92 44587 argiro.kok
              this.getResults(this.searchUtils.keyword, true, this.searchUtils.page, this.searchUtils.size);
93 44035 konstantin
94
          }
95
96
97
98 44406 konstantin
          console.info(params);
99 44035 konstantin
          if(params[filter.filterId] != undefined) {
100
              let values = params[filter.filterId].split(",");
101
              for(let value of values) {
102
                  for(let filterValue of filter.values) {
103
                      if(filterValue.id == value) {
104
                          filterValue.selected = true;
105
                          filter.countSelectedValues++;
106
                      }
107
                  }
108
              }
109
          }
110
      }
111
112
    });
113
  }
114
115 44826 argiro.kok
  public ngOnDestroy() {
116 44035 konstantin
    this.sub.unsubscribe();
117
  }
118 44338 konstantin
/*
119 44587 argiro.kok
  public getResults(parameters:string, page: number, searchUtils.size: number){
120 44035 konstantin
    console.info("getResults: Execute search query "+parameters);
121
122 44587 argiro.kok
    this._searchPeopleService.searchPeople(parameters, page, searchUtils.size).subscribe(
123 44035 konstantin
        data => {
124 44726 argiro.kok
            this.searchUtils.totalResults = data[0];
125
            console.info("search People total="+this.searchUtils.totalResults);
126 44035 konstantin
            this.results = data[1];
127
            var errorCodes:ErrorCodes = new ErrorCodes();
128 44587 argiro.kok
             this.searchUtils.status = errorCodes.DONE;
129 44726 argiro.kok
            if(this.searchUtils.totalResults == 0 ){
130 44587 argiro.kok
              this.searchUtils.status = errorCodes.NONE;
131 44035 konstantin
            }
132
        },
133
        err => {
134
            console.error(err);
135
            console.info("error");
136
            //TODO check erros (service not available, bad request)
137
            // if( ){
138 44587 argiro.kok
            //   this.searchUtils.status = ErrorCodes.ERROR;
139 44035 konstantin
            // }
140
            var errorCodes:ErrorCodes = new ErrorCodes();
141 44587 argiro.kok
            this.searchUtils.status = errorCodes.NOT_AVAILABLE;
142 44035 konstantin
        }
143
    );
144
  }
145 44338 konstantin
*/
146 44035 konstantin
147 44338 konstantin
public getResults(keyword:string,refine:boolean, page: number, size: number){
148
  var parameters = "";
149
  if(keyword.length > 0){
150 44797 argiro.kok
    parameters = "q="+'"' + keyword + '"';
151 44338 konstantin
  }
152 44587 argiro.kok
  this._getResults(parameters,refine,page,this.searchUtils.size);
153 44338 konstantin
}
154
private _getResults(parameters:string,refine:boolean, page: number, size: number){
155
156
  this._searchPeopleService.searchPeople(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, this.searchPage.getFields()).subscribe(
157
      data => {
158 44726 argiro.kok
          this.searchUtils.totalResults = data[0];
159
          console.info("search People: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
160 44338 konstantin
          this.results = data[1];
161
            this.filters = this.searchPage.checkSelectedFilters(data[2]);
162
            this.searchPage.updateBaseUrlWithParameters(this.filters);
163
          var errorCodes:ErrorCodes = new ErrorCodes();
164 44587 argiro.kok
          this.searchUtils.status = errorCodes.DONE;
165 44726 argiro.kok
          if(this.searchUtils.totalResults == 0 ){
166 44587 argiro.kok
            this.searchUtils.status = errorCodes.NONE;
167 44338 konstantin
          }
168
      },
169
      err => {
170
          console.error(err);
171
           //TODO check erros (service not available, bad request)
172
          // if( ){
173 44587 argiro.kok
          //   this.searchUtils.status = ErrorCodes.ERROR;
174 44338 konstantin
          // }
175
          var errorCodes:ErrorCodes = new ErrorCodes();
176 44587 argiro.kok
          this.searchUtils.status = errorCodes.ERROR;
177 44338 konstantin
      }
178
  );
179
}
180
181 44035 konstantin
  private setFilters(){
182
    //TODO set filters from
183
  }
184
185 44826 argiro.kok
  public queryChanged($event) {
186 44035 konstantin
    var parameters = $event.value;
187
    console.info("queryChanged: Execute search query "+parameters);
188 44587 argiro.kok
    this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size);
189 44035 konstantin
  }
190
191
192
}