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

    
18
    <search-page    pageTitle="OpenAIRE Journals"
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
                    [disableForms]="disableForms"
26
                    [tableViewLink]="'/search/journals-table'">
27
    </search-page>
28
    `
29

    
30
})
31
export class SearchJournalsComponent {
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.JOURNAL_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 = notCompatible) and (datasourcetypeuiid exact "pubsrepository::journal" or datasourcetypeuiid exact "aggregator::pubsrepository::journals" ))';
50
  //"&query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = notCompatible) not(datasourcetypeuiid = other))";
51
  public csvParams: string;
52
  public disableForms: boolean = false;
53

    
54
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
55

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

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

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

    
93
      var errorCodes:ErrorCodes = new ErrorCodes();
94
      this.searchUtils.status = errorCodes.LOADING;
95
      //this.searchPage.openLoading();
96
      this.disableForms = true;
97
      this.results = [];
98
      this.searchUtils.totalResults = 0;
99

    
100
      this.subResults = this._searchDataprovidersService.searchJournals(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, []).subscribe(
101
          data => {
102
              this.searchUtils.totalResults = data[0];
103
              console.info("search Journals: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
104
              this.results = data[1];
105
              this.searchPage.checkSelectedFilters(this.filters);
106
              this.searchPage.updateBaseUrlWithParameters(this.filters);
107
              var errorCodes:ErrorCodes = new ErrorCodes();
108
              this.searchUtils.status = errorCodes.DONE;
109
              if(this.searchUtils.totalResults == 0 ){
110
                this.searchUtils.status = errorCodes.NONE;
111
              }
112
              //this.searchPage.closeLoading();
113
              this.disableForms = false;
114

    
115
          },
116
          err => {
117
              console.log(err);
118
               //TODO check erros (service not available, bad request)
119
              // if( ){
120
              //   this.searchUtils.status = ErrorCodes.ERROR;
121
              // }
122
              var errorCodes:ErrorCodes = new ErrorCodes();
123
              this.searchUtils.status = errorCodes.ERROR;
124
              //this.searchPage.closeLoading();
125
              this.disableForms = false;
126

    
127
          }
128
      );
129
    }
130
  private setFilters(){
131
    //TODO set filters from
132
  }
133

    
134
  public queryChanged($event) {
135
    var parameters = $event.index;
136
    console.info("queryChanged: Execute search query "+parameters);
137

    
138
    this._getResults(parameters, false, this.searchUtils.page, this.searchUtils.size);
139
  }
140
  private createFilters():Filter[] {
141
    var filter_names=["Type","Compatibility Level"];
142
    var filter_ids=["datasourcetypeuiid","datasourcecompatibilityname"];
143
    var searchFields = new SearchFields();
144
    var filter_original_ids = searchFields.JOURNAL_FIELDS;
145
    var value_names=[
146
      /*[
147
      "Institutional Publication Repository","Thematic Publication Repository", "Other Publication Repository",
148
     "Institutional Repositories Aggregators",
149
     "Thematic Repositories Aggregators", "Other Repositories Aggregators",
150
      "Data Repositories", "Data Repositories Aggregators", "Journals", "Journals Aggregators", "CRIS Systems", "Publication Catalogues"],
151
      */
152
      ["Journal", "Journal Aggregator\/Publisher"],
153
      ["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)"]];
154

    
155
    var value_original_ids=[
156
      ["pubsrepository::journal", "aggregator::pubsrepository::journals"],
157
      //["driver","openaire2.0", "driver-openaire2.0", "openaire3.0","openaire2.0_data"]
158
    ["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)"]];
159
    var filters: Filter[] =[];
160
    for(var i =0 ; i < filter_names.length;i++){
161
      var values:Value[] = [];
162
      for(var j =0 ; j < value_names[i].length;j++){
163
        var value:Value = {name: value_names[i][j], id: value_original_ids[i][j], number:j, selected:false}
164
        values.push(value);
165
      }
166
       var filter:Filter = {title: filter_names[i], filterId: filter_ids[i], originalFilterId:  filter_original_ids[i], values : values, countSelectedValues:0, "filterOperator": 'or' };
167
       filters.push(filter);
168
    }
169
    return filters;
170
    }
171

    
172
    public downloadClicked($event) {
173
        if(!this.CSVDownloaded) {
174
            this.CSVDownloaded = false;
175

    
176
            var parameters = $event.value;
177

    
178
            //this.getResultsCSV(parameters, false, 1, 1000);
179

    
180
            this._searchDataprovidersService.searchCompatibleDataprovidersCSV(parameters,this.searchPage.getRefineFieldsQuery(), 1, 1000).subscribe(
181
                data => {
182
                      this.CSV.export = data;
183
                      ExportCSVComponent.downloadCSV(this.CSV, "compatibleDataproviders.csv");
184

    
185
                      var errorCodes:ErrorCodes = new ErrorCodes();
186
                      this.searchUtils.status = errorCodes.DONE;
187
                },
188
                err => {
189
                    console.log(err);
190
                     //TODO check erros (service not available, bad request)
191
                    // if( ){
192
                    //   this.searchUtils.status = ErrorCodes.ERROR;
193
                    // }
194
                    var errorCodes:ErrorCodes = new ErrorCodes();
195
                    this.searchUtils.status = errorCodes.ERROR;
196
                }
197
            );
198
        }
199
    }
200

    
201
}
(14-14/18)