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.class';
13

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

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

    
26
    `
27

    
28
})
29
export class SearchCompatibleDataprovidersComponent {
30
  public results =[];
31
  public filters =[];
32
  public baseUrl:string;
33
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
34
  public sub: any; public subResults: any;
35
  public _location:Location;
36
  public searchFields:SearchFields = new SearchFields();
37
  public refineFields: string[] =  this.searchFields.COMPATIBLE_DATAPROVIDER_FIELDS;
38
  public fieldIdsMap= this.searchFields.DATASOURCE_FIELDS;
39
  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"]}];
40
  // ["entityregistry","entityregistry::projects","entityregistry::repositories"]}];
41
  public _prefixQuery: string = "";
42

    
43
  public CSV: any = {  "columnNames":  [ "Title", "Type", "Coutries", "Compatibility" ],
44
                        "export":[]
45
                     };
46
  public CSVDownloaded = false;
47
  public resourcesQuery = "&query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = hostedBy) not(datasourcecompatibilityid = notCompatible) not(datasourcetypeuiid = other))";
48
  public csvParams: string;
49

    
50
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
51

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

    
66
  public ngOnInit() {
67
    this.searchPage.refineFields = this.refineFields;
68
    this.searchPage.fieldIdsMap = this.fieldIdsMap;
69
    this.sub =  this.route.queryParams.subscribe(params => {
70
      this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
71
      this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
72
      this.filters = this.createFilters();
73
      var queryParameters = this.searchPage.getIndexQueryParametersFromUrl(params);
74
      this._getResults(queryParameters, false, this.searchUtils.page, this.searchUtils.size);
75
    });
76
  }
77

    
78
  public ngOnDestroy() {
79
    if(this.sub){
80
      this.sub.unsubscribe();
81
    }
82
    if(this.subResults){
83
      this.subResults.unsubscribe();
84
    }  }
85
    private _getResults(parameters:string,refine:boolean, page: number, size: number){
86
        this.csvParams = parameters+this.resourcesQuery+"&type=datasources";
87

    
88
      var errorCodes:ErrorCodes = new ErrorCodes();
89
      this.searchUtils.status = errorCodes.LOADING;
90
      this.searchPage.openLoading();
91

    
92
      this.subResults = this._searchDataprovidersService.searchCompatibleDataproviders(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, []).subscribe(
93
          data => {
94
              this.searchUtils.totalResults = data[0];
95
              console.info("search Data Providers: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
96
              this.results = data[1];
97
              this.searchPage.checkSelectedFilters(this.filters);
98
              this.searchPage.updateBaseUrlWithParameters(this.filters);
99
              var errorCodes:ErrorCodes = new ErrorCodes();
100
              this.searchUtils.status = errorCodes.DONE;
101
              if(this.searchUtils.totalResults == 0 ){
102
                this.searchUtils.status = errorCodes.NONE;
103
              }
104
              this.searchPage.closeLoading();
105

    
106
          },
107
          err => {
108
              console.log(err);
109
               //TODO check erros (service not available, bad request)
110
              // if( ){
111
              //   this.searchUtils.status = ErrorCodes.ERROR;
112
              // }
113
              var errorCodes:ErrorCodes = new ErrorCodes();
114
              this.searchUtils.status = errorCodes.ERROR;
115
              this.searchPage.closeLoading();
116

    
117
          }
118
      );
119
    }
120
  private setFilters(){
121
    //TODO set filters from
122
  }
123

    
124
  public queryChanged($event) {
125
    var parameters = $event.index;
126
    console.info("queryChanged: Execute search query "+parameters);
127

    
128
    this._getResults(parameters, false, this.searchUtils.page, this.searchUtils.size);
129
  }
130
  private createFilters():Filter[] {
131
    var filter_names=["Type","Compatibility Level"];
132
    var filter_ids=["datasourcetypeuiid","datasourcecompatibilityid"];
133
    var searchFields = new SearchFields();
134
    var filter_original_ids = searchFields.COMPATIBLE_DATAPROVIDER_FIELDS;
135
    var value_names=[
136
      [
137
      "Institutional Publication Repository","Thematic Publication Repository", "Other Publication Repository",
138
     "Institutional Repositories Aggregators",
139
     "Thematic Repositories Aggregators", "Other Repositories Aggregators",
140
      "Data Repositories", "Data Repositories Aggregators", "Journals", "Journals Aggregators", "CRIS Systems", "Publication Catalogues"],
141
      ["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)"]];
142

    
143
    var value_original_ids=[
144
      ["pubsrepository::institutional","pubsrepository::thematic", "pubsrepository::unknown", "aggregator::pubsrepository::thematic","aggregator::pubsrepository::institutional","aggregator::pubsrepository::unknown",
145
      "datarepository::unknown", "aggregator::datarepository", "pubsrepository::journal", "aggregator::pubsrepository::journals", "cris", "pubscatalogue::unknown"],
146
      ["driver","openaire2.0", "driver-openaire2.0", "openaire3.0","openaire2.0_data"]];
147
    var filters: Filter[] =[];
148
    for(var i =0 ; i < filter_names.length;i++){
149
      var values:Value[] = [];
150
      for(var j =0 ; j < value_names[i].length;j++){
151
        var value:Value = {name: value_names[i][j], id: value_original_ids[i][j], number:j, selected:false}
152
        values.push(value);
153
      }
154
       var filter:Filter = {title: filter_names[i], filterId: filter_ids[i], originalFilterId:  filter_original_ids[i], values : values, countSelectedValues:0, "filterOperator": 'or'}
155
       filters.push(filter);
156
    }
157
    return filters;
158
    }
159

    
160
    public downloadClicked($event) {
161
        if(!this.CSVDownloaded) {
162
            this.CSVDownloaded = false;
163

    
164
            var parameters = $event.value;
165

    
166
            //this.getResultsCSV(parameters, false, 1, 1000);
167

    
168
            this._searchDataprovidersService.searchCompatibleDataprovidersCSV(parameters,this.searchPage.getRefineFieldsQuery(), 1, 1000).subscribe(
169
                data => {
170
                      this.CSV.export = data;
171
                      ExportCSVComponent.downloadCSV(this.CSV, "compatibleDataproviders.csv");
172

    
173
                      var errorCodes:ErrorCodes = new ErrorCodes();
174
                      this.searchUtils.status = errorCodes.DONE;
175
                },
176
                err => {
177
                    console.log(err);
178
                     //TODO check erros (service not available, bad request)
179
                    // if( ){
180
                    //   this.searchUtils.status = ErrorCodes.ERROR;
181
                    // }
182
                    var errorCodes:ErrorCodes = new ErrorCodes();
183
                    this.searchUtils.status = errorCodes.ERROR;
184
                }
185
            );
186
        }
187
    }
188

    
189
}
(2-2/7)