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 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">
23
    </advanced-search-page>
24

    
25
    `
26
 })
27

    
28
export class AdvancedSearchOrganizationsComponent {
29
  public results =[];
30
  public filters =[];
31
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
32
  public searchFields:SearchFields = new SearchFields();
33

    
34
  public fieldIds:  string[] = this.searchFields.ORGANIZATION_ADVANCED_FIELDS;
35
  public fieldIdsMap  = this.searchFields.ORGANIZATION_FIELDS;
36
  public selectedFields:AdvancedField[] =  [];
37
  public csvParams: string;
38

    
39
  @ViewChild (AdvancedSearchPageComponent) searchPage : AdvancedSearchPageComponent ;
40

    
41
public resourcesQuery = "(oaftype exact organization)";
42
  constructor (private route: ActivatedRoute, private _searchOrganizationsService: SearchOrganizationsService ) {
43

    
44
    this.results =[];
45
    var errorCodes:ErrorCodes = new ErrorCodes();
46
    this.searchUtils.status =errorCodes.LOADING;
47
    this.searchUtils.baseUrl = OpenaireProperties.searchLinkToAdvancedOrganizations;
48

    
49

    
50

    
51
  }
52
  ngOnInit() {
53
    var errorCodes:ErrorCodes = new ErrorCodes();
54
    this.searchUtils.status =errorCodes.LOADING;
55

    
56
   this.sub =  this.route.queryParams.subscribe(params => {
57
      let page = (params['page']=== undefined)?1:+params['page'];
58
      this.searchUtils.page = ( page <= 0 ) ? 1 : page;
59
      this.searchPage.fieldIds = this.fieldIds;
60
      this.searchPage.selectedFields = this.selectedFields;
61
      this.searchPage.fieldIdsMap = this.fieldIdsMap;
62
      this.searchPage.getSelectedFiltersFromUrl(params);
63
      this.getResults(this.searchPage.createQueryParameters(),  this.searchUtils.page, this.searchUtils.size);
64

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

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

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

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

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

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

    
120
  }
121

    
122

    
123
}
(8-8/18)