Project

General

Profile

1
import {SearchDataprovidersService} from '../../services/searchDataproviders.service';
2
import { ErrorCodes} from '../../utils/properties/openaireProperties';
3
 import {ExportCSVComponent} from '../../utils/exportCSV.class';
4
import {SearchUtilsClass } from '../../searchPages/searchUtils/searchUtils.class';
5

    
6
export class FetchDataproviders {
7
  public results =[];
8
   public searchUtils:SearchUtilsClass = new SearchUtilsClass();
9
  public sub: any; public subResults: any;
10
  public CSV: any = {  "columnNames":  [ "Title", "Type", "Coutries", "Compatibility" ],
11
                        "export":[]
12
                     };
13
  public CSVDownloaded = false;
14
  public csvParams: string;
15

    
16

    
17
  constructor ( private _searchDataprovidersService: SearchDataprovidersService ) {
18
    var errorCodes:ErrorCodes = new ErrorCodes();
19
    this.searchUtils.status =errorCodes.LOADING;
20
   }
21

    
22

    
23
  public ngOnDestroy() {
24
    if(this.sub){
25
      this.sub.unsubscribe();
26
    }
27
    if(this.subResults){
28
      this.subResults.unsubscribe();
29
    }
30
  }
31

    
32
  public getResultsByKeyword(keyword:string, page: number, size: number){
33
    var parameters = "";
34
    if(keyword.length > 0){
35
      parameters = "q=" + keyword;
36
    }
37

    
38
    var errorCodes:ErrorCodes = new ErrorCodes();
39
    this.searchUtils.status = errorCodes.LOADING;
40

    
41

    
42
    this.subResults = this._searchDataprovidersService.searchDataproviders(parameters,null, page, size,[]).subscribe(
43
        data => {
44
            this.searchUtils.totalResults = data[0];
45
            console.info("search Data Providers: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
46
            this.results = data[1];
47

    
48
            var errorCodes:ErrorCodes = new ErrorCodes();
49
            this.searchUtils.status = errorCodes.DONE;
50
            if(this.searchUtils.totalResults == 0 ){
51
              this.searchUtils.status = errorCodes.NONE;
52
            }
53
        },
54
        err => {
55
            console.log(err);
56
             //TODO check erros (service not available, bad request)
57
            // if( ){
58
            //   this.searchUtils.status = ErrorCodes.ERROR;
59
            // }
60
            var errorCodes:ErrorCodes = new ErrorCodes();
61
            this.searchUtils.status = errorCodes.ERROR;
62
        }
63
    );
64
  }
65

    
66
  public getNumForEntity(entity: string, id:string) {
67
      console.info("getNumForEntity : Dataproviders Component");
68
      var parameters="";
69

    
70
      if(entity == "organization") {
71
        parameters = "organizations/"+id+"/datasources/count";
72
      }
73

    
74
      if(parameters != "") {
75

    
76
          this._searchDataprovidersService.numOfDataproviders(parameters).subscribe(
77
              data => {
78
                  this.searchUtils.totalResults = data;
79
              },
80
              err => {
81
                  console.log(err);
82
                   //TODO check erros (service not available, bad request)
83
                  // if( ){
84
                  //   this.searchUtils.status = ErrorCodes.ERROR;
85
                  // }
86
                  var errorCodes:ErrorCodes = new ErrorCodes();
87
                  this.searchUtils.status = errorCodes.ERROR;
88
              }
89
          );
90
      }
91
  }
92

    
93
  public getNumForSearch(keyword: string) {
94
      var errorCodes:ErrorCodes = new ErrorCodes();
95
      this.searchUtils.status = errorCodes.LOADING;
96
      var parameters="datasources/count";
97
      if(keyword != "") {
98
          parameters += "?q=" +keyword ;
99
      }
100
      this._searchDataprovidersService.numOfDataproviders(parameters).subscribe(
101
          data => {
102
              this.searchUtils.totalResults = data;
103
              this.searchUtils.status = errorCodes.DONE;
104
          },
105
          err => {
106
              console.log(err);
107
               //TODO check erros (service not available, bad request)
108
              // if( ){
109
              //   this.searchUtils.status = ErrorCodes.ERROR;
110
              // }
111
              var errorCodes:ErrorCodes = new ErrorCodes();
112
              this.searchUtils.status = errorCodes.ERROR;
113
          }
114
      );
115
  }
116

    
117
public getResultsForDeposit(id:string, type:string, page: number,  size: number){
118
    var errorCodes:ErrorCodes = new ErrorCodes();
119
    this.searchUtils.status = errorCodes.LOADING;
120

    
121
  if(id != "") {
122

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

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

    
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.log(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 getResultsForDataproviders(id:string, page: number, size: number){
185
      var errorCodes:ErrorCodes = new ErrorCodes();
186
      this.searchUtils.status = errorCodes.LOADING;
187

    
188
    this._searchDataprovidersService.getDataProvidersforEntityRegistry(id, page, size).subscribe(
189
        data => {
190
            this.searchUtils.totalResults = data[0];
191
            console.info("search Dataproviders for Entity Registry: [Id:"+id+" ]  [total results:"+this.searchUtils.totalResults+"]");
192
            this.results = data[1];
193

    
194
            var errorCodes:ErrorCodes = new ErrorCodes();
195
            this.searchUtils.status = errorCodes.DONE;
196
            if(this.searchUtils.totalResults == 0 ){
197
              this.searchUtils.status = errorCodes.NONE;
198
            }
199
        },
200
        err => {
201
            console.log(err);
202
             //TODO check erros (service not available, bad request)
203
            // if( ){
204
            //   this.searchUtils.status = ErrorCodes.ERROR;
205
            // }
206
            var errorCodes:ErrorCodes = new ErrorCodes();
207
            this.searchUtils.status = errorCodes.ERROR;
208
        }
209
    );
210
  }
211

    
212

    
213

    
214
  public downloadClicked($event) {
215
      if(!this.CSVDownloaded) {
216
          this.CSVDownloaded = false;
217

    
218
          var parameters = $event.value;
219

    
220
          //this.getResultsCSV(parameters, false, 1, 1000);
221

    
222
          this._searchDataprovidersService.searchDataprovidersCSV(parameters, "", 1, 1000).subscribe(
223
              data => {
224
                    this.CSV.export = data;
225
                    ExportCSVComponent.downloadCSV(this.CSV, "dataproviders.csv");
226

    
227
                    var errorCodes:ErrorCodes = new ErrorCodes();
228
                    this.searchUtils.status = errorCodes.DONE;
229
                    if(this.searchUtils.totalResults == 0 ){
230
                      this.searchUtils.status = errorCodes.NONE;
231
                    }
232
              },
233
              err => {
234
                  console.log(err);
235
                   //TODO check erros (service not available, bad request)
236
                  // if( ){
237
                  //   this.searchUtils.status = ErrorCodes.ERROR;
238
                  // }
239
                  var errorCodes:ErrorCodes = new ErrorCodes();
240
                  this.searchUtils.status = errorCodes.ERROR;
241
              }
242
          );
243
          /*
244
          this.CSV.export.push(
245
              [
246
                  this.quote(project.name),
247
                  this.quote(project.acronym),
248
                  this.quote(project.code),
249
                  this.quote(project.funder),
250
                  this.quote(project.fundingStream),
251
                  this.quote(project.fundingLevel1),
252
                  this.quote(project.fundingLevel2),
253
                  this.quote(project.sc39),
254
                  this.quote(project.startDate),
255
                  this.quote(project.endDate)
256
              ]);
257
          }*/
258
      }
259
  }
260

    
261
}
(1-1/6)