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-dataproviders',
16
    template: `
17

    
18
    <search-page pageTitle="Compatible Dataproviders" type="datasource" [(filters)] = "filters"
19
                 [(results)] = "results"  [(searchUtils)] = "searchUtils"
20
                  [baseUrl] = "baseUrl" [showResultCount]=false (queryChange)="queryChanged($event)"
21
                  (downloadClick)="downloadClicked($event)">
22
    </search-page>
23
    <!--table-view  [(datasources)] = results></table-view-->
24

    
25
    `
26

    
27
})
28
export class SearchCompatibleDataprovidersComponent {
29
  public results =[];
30
  public filters =[];
31
  public baseUrl:string;
32
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
33
  public sub: any; public subResults: any;
34
  public _location:Location;
35
  public searchFields:SearchFields = new SearchFields();
36
  public refineFields: string[] =  this.searchFields.COMPATIBLE_DATAPROVIDER_FIELDS;
37
  public fieldIdsMap= this.searchFields.DATASOURCE_FIELDS;
38
  public _prefixQueryFields: {field:string,opName:string,opValue:string,values:string[]}[] =[{field:"compatibility",opName:"cm",opValue:"not", values:["UNKNOWN","hostedBy","notCompatible"]},{field:"type",opName:"tp",opValue:"not",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.searchLinkToCompatibleDataProviders;
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
      var queryParameters = this.searchPage.getIndexQueryParametersFromUrl(params);
71
      this._getResults(queryParameters, false, this.searchUtils.page, this.searchUtils.size);
72
    });
73
  }
74

    
75
  public ngOnDestroy() {
76
    if(this.sub){
77
      this.sub.unsubscribe();
78
    }
79
    if(this.subResults){
80
      this.subResults.unsubscribe();
81
    }  }
82
    private _getResults(parameters:string,refine:boolean, page: number, size: number){
83

    
84
      this.subResults = this._searchDataprovidersService.searchCompatibleDataproviders(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, []).subscribe(
85
          data => {
86
              this.searchUtils.totalResults = data[0];
87
              console.info("search Data Providers: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
88
              this.results = data[1];
89
              this.searchPage.checkSelectedFilters(this.filters);
90
              this.searchPage.updateBaseUrlWithParameters(this.filters);
91
              var errorCodes:ErrorCodes = new ErrorCodes();
92
              this.searchUtils.status = errorCodes.DONE;
93
              if(this.searchUtils.totalResults == 0 ){
94
                this.searchUtils.status = errorCodes.NONE;
95
              }
96
          },
97
          err => {
98
              console.error(err);
99
               //TODO check erros (service not available, bad request)
100
              // if( ){
101
              //   this.searchUtils.status = ErrorCodes.ERROR;
102
              // }
103
              var errorCodes:ErrorCodes = new ErrorCodes();
104
              this.searchUtils.status = errorCodes.ERROR;
105
          }
106
      );
107
    }
108
  private setFilters(){
109
    //TODO set filters from
110
  }
111

    
112
  public queryChanged($event) {
113
    var parameters = $event.index;
114
    console.info("queryChanged: Execute search query "+parameters);
115

    
116
    this._getResults(parameters, false, this.searchUtils.page, this.searchUtils.size);
117
  }
118
  private createFilters():Filter[] {
119
    var filter_names=["Type","Compatibility Level"];
120
    var filter_ids=["datasourcetypeuiid","datasourcecompatibilityid"];
121
    var searchFields = new SearchFields();
122
    var filter_original_ids = searchFields.COMPATIBLE_DATAPROVIDER_FIELDS;
123
    var value_names=[
124
      [
125
      "Institutional Publication Repository","Thematic Publication Repository", "Other Publication Repository",
126
     "Institutional Repositories Aggregators",
127
     "Thematic Repositories Aggregators", "Other Repositories Aggregators",
128
      "Data Repositories", "Data Repositories Aggregators", "Journals", "Journals Aggregators", "CRIS Systems", "Publication Catalogues"],
129
      ["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)"]];
130

    
131
    var value_original_ids=[
132
      ["pubsrepository::institutional","pubsrepository::thematic", "pubsrepository::unknown", "aggregator::pubsrepository::thematic","aggregator::pubsrepository::institutional","aggregator::pubsrepository::unknown",
133
      "datarepository::unknown", "aggregator::datarepository", "pubsrepository::journal", "aggregator::pubsrepository::journals", "cris", "pubscatalogue::unknown"],
134
      ["driver","openaire2.0", "driver-openaire2.0", "openaire3.0","openaire2.0_data"]];
135
    var filters: Filter[] =[];
136
    for(var i =0 ; i < filter_names.length;i++){
137
      var values:Value[] = [];
138
      for(var j =0 ; j < value_names[i].length;j++){
139
        var value:Value = {name: value_names[i][j], id: value_original_ids[i][j], number:j, selected:false}
140
        values.push(value);
141
      }
142
       var filter:Filter = {title: filter_names[i], filterId: filter_ids[i], originalFilterId:  filter_original_ids[i], values : values, countSelectedValues:0, "filterOperator": 'or'}
143
       filters.push(filter);
144
    }
145
    return filters;
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.searchCompatibleDataprovidersCSV(parameters,this.searchPage.getRefineFieldsQuery(), 1, 1000).subscribe(
157
                data => {
158
                      this.CSV.export = data;
159
                      ExportCSVComponent.downloadCSV(this.CSV, "compatibleDataproviders.csv");
160

    
161
                      var errorCodes:ErrorCodes = new ErrorCodes();
162
                      this.searchUtils.status = errorCodes.DONE;
163
                },
164
                err => {
165
                    console.error(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

    
177
}
(1-1/3)