Project

General

Profile

1
import {SearchDataprovidersService} from '../../services/searchDataproviders.service';
2
import { ErrorCodes} from '../../utils/properties/openaireProperties';
3
 import {ExportCSVComponent} from '../../utils/exportCSV.component';
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 parameters="datasources/count";
95
      if(keyword != "") {
96
          parameters += "?q="+'"'+keyword+'"';
97
      }
98
      this._searchDataprovidersService.numOfDataproviders(parameters).subscribe(
99
          data => {
100
              this.searchUtils.totalResults = data;
101
          },
102
          err => {
103
              console.log(err);
104
               //TODO check erros (service not available, bad request)
105
              // if( ){
106
              //   this.searchUtils.status = ErrorCodes.ERROR;
107
              // }
108
              var errorCodes:ErrorCodes = new ErrorCodes();
109
              this.searchUtils.status = errorCodes.ERROR;
110
          }
111
      );
112
  }
113

    
114
public getResultsForDeposit(id:string, type:string, page: number,  size: number){
115
    //var errorCodes:ErrorCodes = new ErrorCodes();
116
    //this.status = errorCodes.LOADING;
117
  if(id != "") {
118

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

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

    
146
    if(entity == "organization") {
147
      parameters = "organizations/"+id;
148
    }
149

    
150
    if(parameters != "") {
151

    
152
        this._searchDataprovidersService.searchDataprovidersForEntity(parameters, page, size).subscribe(
153
            data => {
154
                this.searchUtils.totalResults = data[0];
155
                console.info("search Dataproviders for "+entity+": [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
156
                this.results = data[1];
157

    
158
                var errorCodes:ErrorCodes = new ErrorCodes();
159
                this.searchUtils.status = errorCodes.DONE;
160
                if(this.searchUtils.totalResults == 0 ){
161
                  this.searchUtils.status = errorCodes.NONE;
162
                }
163
            },
164
            err => {
165
                console.log(err);
166
                 //TODO check erros (service not available, bad request)
167
                // if( ){
168
                //   this.searchUtils.status = ErrorCodes.ERROR;
169
                // }
170
                var errorCodes:ErrorCodes = new ErrorCodes();
171
                this.searchUtils.status = errorCodes.ERROR;
172
            }
173
        );
174
    }
175
  }
176

    
177
  public getResultsForDataproviders(id:string, page: number, size: number){
178

    
179
    this._searchDataprovidersService.getDataProvidersforEntityRegistry(id, page, size).subscribe(
180
        data => {
181
            this.searchUtils.totalResults = data[0];
182
            console.info("search Dataproviders for Entity Registry: [Id:"+id+" ]  [total results:"+this.searchUtils.totalResults+"]");
183
            this.results = data[1];
184

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

    
203

    
204

    
205
  public downloadClicked($event) {
206
      if(!this.CSVDownloaded) {
207
          this.CSVDownloaded = false;
208

    
209
          var parameters = $event.value;
210

    
211
          //this.getResultsCSV(parameters, false, 1, 1000);
212

    
213
          this._searchDataprovidersService.searchDataprovidersCSV(parameters, "", 1, 1000).subscribe(
214
              data => {
215
                    this.CSV.export = data;
216
                    ExportCSVComponent.downloadCSV(this.CSV, "dataproviders.csv");
217

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

    
252
}
(1-1/6)