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"
19
                    type="datasources" entityType="dataprovider" [(filters)] = "filters"
20
                    [(results)] = "results"  [(searchUtils)] = "searchUtils"
21
                    [baseUrl] = "baseUrl" [showResultCount]=false
22
                    (queryChange)="queryChanged($event)"
23
                    (downloadClick)="downloadClicked($event)"
24
                    [csvParams]="csvParams" csvPath="resources">
25
    </search-page>
26
    <!--table-view  [(datasources)] = results></table-view-->
27

    
28
    `
29

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

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

    
52
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
53

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

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

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

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

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

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

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

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

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

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

    
163
    public downloadClicked($event) {
164
        if(!this.CSVDownloaded) {
165
            this.CSVDownloaded = false;
166

    
167
            var parameters = $event.value;
168

    
169
            //this.getResultsCSV(parameters, false, 1, 1000);
170

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

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

    
192
}
(2-2/7)