Project

General

Profile

1
import {Component, Input, ViewChild} from '@angular/core';
2
import { ActivatedRoute} from '@angular/router';
3
import {Location} from '@angular/common';
4
import { Filter, Value} from '../searchUtils/searchHelperClasses.class';
5
import {SearchDataprovidersService} from '../../services/searchDataproviders.service';
6
import {SearchResult}     from '../../utils/entities/searchResult';
7
import {OpenaireProperties, ErrorCodes} from '../../utils/properties/openaireProperties';
8
import {SearchFields} from '../../utils/properties/searchFields';
9
import {SearchPageComponent } from '../searchUtils/searchPage.component';
10
import {ExportCSVComponent} from '../../utils/exportCSV.component';
11
import {SearchUtilsClass } from '../searchUtils/searchUtils.class';
12

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

    
17
    <search-page pageTitle="Search Dataproviders" type="datasource" [(filters)] = "filters"
18
                 [(results)] = "results"   [(searchUtils)] = "searchUtils" [baseUrl] = "baseUrl"
19
				 (queryChange)="queryChanged($event)"  (downloadClick)="downloadClicked($event)">
20
    </search-page>
21

    
22
    `
23
})
24
export class SearchDataprovidersComponent {
25
  public results =[];
26
  private filters =[];
27
  public totalResults:number  = 0 ;
28
  private baseUrl:string;
29
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
30
  private sub: any; private subResults: any;
31
  private _location:Location;
32
  private searchFields:SearchFields = new SearchFields();
33
  private refineFields: string[] =  this.searchFields.DATAPROVIDER_INDEX;
34
  private indexIdsMap: { [key:string]:string } = this.searchFields.DATAPROVIDER_INDEX_PARAM_MAP;
35
  private fieldIdsMap:  { [key:string]:{ name:string, operator:string, type:string, indexField:string, equalityOperator:string  }} = this.searchFields.DATAPROVIDER_FIELDS_MAP;
36
  private CSV: any = {  "columnNames":  [ "Title", "Type", "Coutries", "Compatibility" ],
37
                        "export":[]
38
                     };
39
  private CSVDownloaded = false;
40

    
41
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
42

    
43
  constructor (private route: ActivatedRoute, private _searchDataprovidersService: SearchDataprovidersService ) {
44
    var errorCodes:ErrorCodes = new ErrorCodes();
45
    this.searchUtils.status =errorCodes.LOADING;
46
    this.baseUrl = OpenaireProperties.getLinkToSearchDataProviders();
47
  }
48

    
49
  private ngOnInit() {
50
    this.searchPage.refineFields = this.refineFields;
51
    this.searchPage.indexIdsMap = this.indexIdsMap;
52
    this.searchPage.fieldIdsMap = this.fieldIdsMap;
53

    
54
    this.sub =  this.route.queryParams.subscribe(params => {
55
        this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
56
        this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
57
console.info("PAGE init = "+this.searchUtils.page);
58

    
59
         var queryParameters = this.searchPage.getQueryParametersFromUrl(params);
60
        this._getResults(queryParameters, true, this.searchUtils.page, this.searchUtils.size);
61
    });
62
  }
63

    
64
  private ngOnDestroy() {
65
    if(this.sub){
66
      this.sub.unsubscribe();
67
    }
68
    if(this.subResults){
69
      this.subResults.unsubscribe();
70
    }
71
  }
72

    
73
  public getNumForEntity(entity: string, id:string) {
74
      console.info("getNumForEntity : Dataproviders Component");
75
      var parameters="";
76

    
77
      if(entity == "organization") {
78
        parameters = "organizations/"+id+"/datasources/count";
79
      }
80

    
81
      if(parameters != "") {
82

    
83
          this._searchDataprovidersService.numOfDataproviders(parameters).subscribe(
84
              data => {
85
                  this.searchUtils.totalResults = data;
86
              },
87
              err => {
88
                  console.error(err);
89
                   //TODO check erros (service not available, bad request)
90
                  // if( ){
91
                  //   this.searchUtils.status = ErrorCodes.ERROR;
92
                  // }
93
                  var errorCodes:ErrorCodes = new ErrorCodes();
94
                  this.searchUtils.status = errorCodes.ERROR;
95
              }
96
          );
97
      }
98
  }
99

    
100
  public getNumForSearch(keyword: string) {
101
      var parameters="datasources/count";
102
      if(keyword != "") {
103
          parameters += "?q="+keyword;
104
      }
105
      this._searchDataprovidersService.numOfDataproviders(parameters).subscribe(
106
          data => {
107
              this.searchUtils.totalResults = data;
108
          },
109
          err => {
110
              console.error(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
          }
118
      );
119
  }
120

    
121
public getResultsForDeposit(id:string, type:string, page: number,  size: number){
122
    //var errorCodes:ErrorCodes = new ErrorCodes();
123
    //this.status = errorCodes.LOADING;
124
  if(id != "") {
125

    
126
      this._searchDataprovidersService.searchDataprovidersForDeposit(id,type, page, size).subscribe(
127
          data => {
128
              this.searchUtils.totalResults = data[0];
129
              console.info("search Dataproviders forDeposit: [id:"+id+", type:"+type+" ]  [total results:"+this.totalResults+"]");
130
              this.results = data[1];
131

    
132
              var errorCodes:ErrorCodes = new ErrorCodes();
133
              this.searchUtils.status = errorCodes.DONE;
134
              if(this.searchUtils.totalResults == 0 ){
135
                this.searchUtils.status = errorCodes.NONE;
136
              }
137
          },
138
          err => {
139
              console.error(err);
140
               //TODO check erros (service not available, bad request)
141
              // if( ){
142
              //   this.searchUtils.status = ErrorCodes.ERROR;
143
              // }
144
              var errorCodes:ErrorCodes = new ErrorCodes();
145
              this.searchUtils.status = errorCodes.ERROR;
146
          }
147
      );
148
  }
149
}
150
  public getResultsForEntity(entity:string, id:string, page: number, size: number){
151
    var parameters = "";
152

    
153
    if(entity == "organization") {
154
      parameters = "organizations/"+id;
155
    }
156

    
157
    if(parameters != "") {
158

    
159
        this._searchDataprovidersService.searchDataprovidersForEntity(parameters, page, size).subscribe(
160
            data => {
161
                this.searchUtils.totalResults = data[0];
162
                console.info("search Dataproviders for "+entity+": [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
163
                this.results = data[1];
164

    
165
                var errorCodes:ErrorCodes = new ErrorCodes();
166
                this.searchUtils.status = errorCodes.DONE;
167
                if(this.searchUtils.totalResults == 0 ){
168
                  this.searchUtils.status = errorCodes.NONE;
169
                }
170
            },
171
            err => {
172
                console.error(err);
173
                 //TODO check erros (service not available, bad request)
174
                // if( ){
175
                //   this.searchUtils.status = ErrorCodes.ERROR;
176
                // }
177
                var errorCodes:ErrorCodes = new ErrorCodes();
178
                this.searchUtils.status = errorCodes.ERROR;
179
            }
180
        );
181
    }
182
  }
183

    
184
  public getResults(keyword:string,refine:boolean, page: number, size: number){
185
    var parameters = "";
186
    if(keyword.length > 0){
187
      parameters = "q=" + keyword + "&op=and";
188
    }
189
    this._getResults(parameters,refine,page,this.searchUtils.size);
190
  }
191
  private _getResults(parameters:string,refine:boolean, page: number, size: number){
192
    if(!refine && !this.searchPage){
193
        this.searchPage = new SearchPageComponent(this._location);
194
    }
195
    this.subResults = this._searchDataprovidersService.searchDataproviders(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, this.searchPage.getFields()).subscribe(
196
        data => {
197
            this.searchUtils.totalResults = data[0];
198
            console.info("search Data Providers: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
199
            this.results = data[1];
200
            this.filters = data[2];
201
            this.searchPage.checkSelectedFilters(this.filters);
202
            // this.filters = this.searchPage.checkSelectedFilters(data[2]);
203
            this.searchPage.updateBaseUrlWithParameters(this.filters);
204
            var errorCodes:ErrorCodes = new ErrorCodes();
205
            this.searchUtils.status = errorCodes.DONE;
206
            if(this.searchUtils.totalResults == 0 ){
207
              this.searchUtils.status = errorCodes.NONE;
208
            }
209
        },
210
        err => {
211
            console.error(err);
212
             //TODO check erros (service not available, bad request)
213
            // if( ){
214
            //   this.searchUtils.status = ErrorCodes.ERROR;
215
            // }
216
            var errorCodes:ErrorCodes = new ErrorCodes();
217
            this.searchUtils.status = errorCodes.ERROR;
218
        }
219
    );
220
  }
221

    
222
  private setFilters(){
223
    //TODO set filters from
224
  }
225

    
226
  private queryChanged($event) {
227
    var parameters = $event.value;
228
console.info("PAGE queryChanged = "+this.searchUtils.page);
229
    this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size);
230
  }
231

    
232
  private downloadClicked($event) {
233
      if(!this.CSVDownloaded) {
234
          this.CSVDownloaded = false;
235

    
236
          var parameters = $event.value;
237

    
238
          //this.getResultsCSV(parameters, false, 1, 1000);
239

    
240
          this._searchDataprovidersService.searchDataprovidersCSV(parameters, this.searchPage.getRefineFieldsQuery(), 1, 1000).subscribe(
241
              data => {
242
                    this.CSV.export = data;
243
                    ExportCSVComponent.downloadCSV(this.CSV, "dataproviders.csv");
244

    
245
                    var errorCodes:ErrorCodes = new ErrorCodes();
246
                    this.searchUtils.status = errorCodes.DONE;
247
                    if(this.searchUtils.totalResults == 0 ){
248
                      this.searchUtils.status = errorCodes.NONE;
249
                    }
250
              },
251
              err => {
252
                  console.error(err);
253
                   //TODO check erros (service not available, bad request)
254
                  // if( ){
255
                  //   this.searchUtils.status = ErrorCodes.ERROR;
256
                  // }
257
                  var errorCodes:ErrorCodes = new ErrorCodes();
258
                  this.searchUtils.status = errorCodes.ERROR;
259
              }
260
          );
261
          /*
262
          this.CSV.export.push(
263
              [
264
                  this.quote(project.name),
265
                  this.quote(project.acronym),
266
                  this.quote(project.code),
267
                  this.quote(project.funder),
268
                  this.quote(project.fundingStream),
269
                  this.quote(project.fundingLevel1),
270
                  this.quote(project.fundingLevel2),
271
                  this.quote(project.sc39),
272
                  this.quote(project.startDate),
273
                  this.quote(project.endDate)
274
              ]);
275
          }*/
276
      }
277
  }
278

    
279
}
(1-1/6)