Project

General

Profile

1
import {Component, Input, ViewChild} from '@angular/core';
2
import { ActivatedRoute} from '@angular/router';
3

    
4
import { Filter, Value} from '../searchUtils/searchHelperClasses.class';
5

    
6
import {SearchDataprovidersService} from '../../services/searchDataproviders.service';
7
import {SearchResult}     from '../../utils/entities/searchResult';
8
import {OpenaireProperties, ErrorCodes} from '../../utils/properties/openaireProperties';
9
import {SearchFields} from '../../utils/properties/searchFields';
10
import {SearchPageComponent } from '../searchUtils/searchPage.component';
11
import {SearchUtilsClass } from '../searchUtils/searchUtils.class';
12
import {ExportCSVComponent} from '../../utils/exportCSV.component';
13

    
14
@Component({
15
    selector: 'search-entity-registries',
16
    template: `
17

    
18
    <search-page pageTitle="Entity Registries" type="datasources" [(filters)] = "filters"
19
                 [(results)] = "results" [(searchUtils)] = "searchUtils"
20
                 [baseUrl] = "baseUrl" [showResultCount]=false (queryChange)="queryChanged($event)"
21
                 (downloadClick)="downloadClicked($event)">
22
    </search-page>
23

    
24
    `
25

    
26
})
27
export class SearchEntityRegistriesComponent {
28
  public results =[];
29
  public filters =[];
30
  public baseUrl:string;
31
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
32
  public sub: any; public subResults: any;
33
  public _location:Location;
34
  public searchFields:SearchFields = new SearchFields();
35
  public refineFields: string[] =  this.searchFields.ENTITY_REGISTRIES_FIELDS;
36
  public fieldIdsMap= this.searchFields.DATASOURCE_FIELDS;
37
  public _prefixQueryFields: {field:string,opName:string,opValue:string,values:string[]}[] =[
38
    {field:"type",opName:"tp",opValue:"and",values: ["other"]}];
39
    // ["entityregistry","entityregistry::projects","entityregistry::repositories"]}];
40
  public _prefixQuery: string = "";
41

    
42
  public CSV: any = {  "columnNames":  [ "Title", "Type", "Coutries", "Compatibility" ],
43
                        "export":[]
44
                     };
45
  public CSVDownloaded = false;
46

    
47
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
48

    
49
  constructor (private route: ActivatedRoute, private _searchDataprovidersService: SearchDataprovidersService ) {
50
    var errorCodes:ErrorCodes = new ErrorCodes();
51
    this.searchUtils.status =errorCodes.LOADING;
52
    this.baseUrl = OpenaireProperties.searchLinkToEntityRegistriesDataProviders;
53
    for(var i = 0; i < this._prefixQueryFields.length; i++ ){
54
      for(var j =0; j < this._prefixQueryFields[i].values.length; j++){
55
        this._prefixQuery+="&" + this._prefixQueryFields[i].field + "="
56
        + this._prefixQueryFields[i].values[j] + "&" +
57
        this._prefixQueryFields[i].opName + "=" + this._prefixQueryFields[i].opValue;
58
      }
59
    }
60
    this._prefixQuery+="&";
61
  }
62

    
63
  public ngOnInit() {
64
    this.searchPage.refineFields = this.refineFields;
65
    this.searchPage.fieldIdsMap = this.fieldIdsMap;
66
    this.sub =  this.route.queryParams.subscribe(params => {
67
      this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
68
      this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
69
      this.filters = this.createFilters();
70

    
71
      var queryParameters = this.searchPage.getIndexQueryParametersFromUrl(params);
72
      this._getResults(queryParameters, false, this.searchUtils.page, this.searchUtils.size);
73
    });
74
  }
75

    
76
  public ngOnDestroy() {
77
    if(this.sub){
78
      this.sub.unsubscribe();
79
    }
80
    if(this.subResults){
81
      this.subResults.unsubscribe();
82
    }  }
83
    private _getResults(parameters:string,refine:boolean, page: number, size: number){
84
      var errorCodes:ErrorCodes = new ErrorCodes();
85
      this.searchUtils.status = errorCodes.LOADING;
86
       this.subResults = this._searchDataprovidersService.searchEntityRegistries(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, []).subscribe(
87
          data => {
88
              this.searchUtils.totalResults = data[0];
89
              console.info("search Entity Registries: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
90
              this.results = data[1];
91
              this.searchPage.checkSelectedFilters(this.filters);
92
              this.searchPage.updateBaseUrlWithParameters(this.filters);
93
              var errorCodes:ErrorCodes = new ErrorCodes();
94
              this.searchUtils.status = errorCodes.DONE;
95
              if(this.searchUtils.totalResults == 0 ){
96
                this.searchUtils.status = errorCodes.NONE;
97
              }
98
          },
99
          err => {
100
              console.log(err);
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.ERROR;
107
          }
108
      );
109
    }
110
  private setFilters(){
111
    //TODO set filters from
112
  }
113

    
114
  public queryChanged($event) {
115
    var parameters = $event.index;
116
    console.info("queryChanged: Execute search query "+parameters);
117
    this._getResults(parameters, false, this.searchUtils.page, this.searchUtils.size);
118
  }
119
  private createFilters():Filter[] {
120
    var filter_names=["Type","Compatibility Level"];
121
    var filter_ids=["datasourcetypeid","datasourcecompatibilityid"];
122
    var searchFields = new SearchFields();
123
    var filter_original_ids = searchFields.ENTITY_REGISTRIES_FIELDS;
124
    var value_names=[
125
      ["Funder database","Registry of repositories","Scholarly Comm. Infrastructure","Registry","Information Space","Web Source"],
126

    
127
      ["OpenAIRE Basic (DRIVER OA)","OpenAIRE 2.0 (EC funding)", "OpenAIRE 2.0+ (DRIVER OA, EC funding)", "OpenAIRE 3.0 (OA, funding)","OpenAIRE Data (funded, referenced datasets)"]];
128

    
129
    var value_original_ids=[
130
      ["entityregistry::projects","entityregistry::repositories","scholarcomminfra","entityregistry","infospace","websource"],
131
      ["driver","openaire2.0", "driver-openaire2.0", "openaire3.0","openaire2.0_data"]];
132

    
133
    var filters: Filter[] =[];
134
    for(var i =0 ; i < filter_names.length;i++){
135
      var values:Value[] = [];
136
      for(var j =0 ; j < value_names[i].length;j++){
137
        var value:Value = {name: value_names[i][j], id: value_original_ids[i][j], number:j, selected:false}
138
        values.push(value);
139
      }
140
       var filter:Filter = {title: filter_names[i], filterId: filter_ids[i], originalFilterId:  filter_original_ids[i], values : values, countSelectedValues:0, "filterOperator": 'or'}
141
       filters.push(filter);
142
    }
143
    return filters;
144
    }
145

    
146

    
147

    
148
      public downloadClicked($event) {
149
          if(!this.CSVDownloaded) {
150
              this.CSVDownloaded = false;
151

    
152
              var parameters = $event.value;
153

    
154
              //this.getResultsCSV(parameters, false, 1, 1000);
155

    
156
              this._searchDataprovidersService.searchEntityRegistriesCSV(parameters, this.searchPage.getRefineFieldsQuery(), 1, 1000).subscribe(
157
                  data => {
158
                        this.CSV.export = data;
159
                        ExportCSVComponent.downloadCSV(this.CSV, "etityRegistries.csv");
160

    
161
                        var errorCodes:ErrorCodes = new ErrorCodes();
162
                        this.searchUtils.status = errorCodes.DONE;
163
                  },
164
                  err => {
165
                      console.log(err);
166
                       //TODO check erros (service not available, bad request)
167
                      // if( ){
168
                      //   this.searchUtils.status = ErrorCodes.ERROR;
169
                      // }
170
                      var errorCodes:ErrorCodes = new ErrorCodes();
171
                      this.searchUtils.status = errorCodes.ERROR;
172
                  }
173
              );
174
          }
175
      }
176
}
(3-3/3)