Project

General

Profile

1
import {SearchDatasetsService} from '../../services/searchDatasets.service';
2
import { ErrorCodes} from '../../utils/properties/errorCodes';
3
import {SearchUtilsClass } from '../../searchPages/searchUtils/searchUtils.class';
4
import {DOI} from '../../utils/string-utils.class';
5
import {Subject} from 'rxjs/Subject';
6
import{EnvProperties} from '../../utils/properties/env-properties';
7

    
8
export class FetchDatasets{
9
  private errorCodes: ErrorCodes;
10

    
11
  public results =[];
12
  public requestComplete: Subject<void>;
13

    
14
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
15
  private sub: any;
16
  private subResults: any;
17

    
18
  public csvParams: string;
19

    
20
  constructor ( private _searchDatasetsService: SearchDatasetsService ) {
21

    
22
    this.errorCodes = new ErrorCodes();
23
    this.searchUtils.status = this.errorCodes.LOADING;
24

    
25
    this.requestComplete = new Subject<void>();
26
  }
27

    
28

    
29

    
30
  public ngOnDestroy() {
31
    if(this.sub){
32
      this.sub.unsubscribe();
33
    }
34
    if(this.subResults){
35
      this.subResults.unsubscribe();
36
    }
37
  }
38

    
39

    
40
  public getResultsByKeyword(keyword:string,  page: number, size: number, properties:EnvProperties){
41
    var parameters = "";
42
    if(keyword.length > 0){
43
      var DOIs:string[] = DOI.getDOIsFromString(keyword);
44
      var doisParams = "";
45

    
46
      for(var i =0 ;i < DOIs.length; i++){
47
        doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
48
      }
49
      if(doisParams.length > 0){
50
        parameters += "&"+doisParams;
51
      }else{
52
        parameters = "q=" + keyword;
53
      }
54
    }
55

    
56
    //var errorCodes:ErrorCodes = new ErrorCodes();
57
    this.searchUtils.status = this.errorCodes.LOADING;
58

    
59
    this.subResults = this._searchDatasetsService.searchDatasets(parameters,null, page, size, [], properties).subscribe(
60
        data => {
61
            this.searchUtils.totalResults = data[0];
62
            console.info("search Research Data: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
63
            this.results = data[1];
64

    
65
            //var errorCodes:ErrorCodes = new ErrorCodes();
66
            this.searchUtils.status = this.errorCodes.DONE;
67
            if(this.searchUtils.totalResults == 0 ){
68
              this.searchUtils.status = this.errorCodes.NONE;
69
            }
70
         },
71
        err => {
72
            console.log(err);
73
             //TODO check erros (service not available, bad request)
74
            // if( ){
75
            //   this.searchUtils.status = ErrorCodes.ERROR;
76
            // }
77
            //var errorCodes:ErrorCodes = new ErrorCodes();
78
            //this.searchUtils.status = errorCodes.ERROR;
79
            if(err.status == '404') {
80
              this.searchUtils.status = this.errorCodes.NOT_FOUND;
81
            } else if(err.status == '500') {
82
              this.searchUtils.status = this.errorCodes.ERROR;
83
            } else {
84
              this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
85
            }
86
         }
87
    );
88
  }
89

    
90
public getNumForEntity(entity:string, id:string, properties:EnvProperties){
91
    //var errorCodes:ErrorCodes = new ErrorCodes();
92
    this.searchUtils.status = this.errorCodes.LOADING;
93

    
94

    
95
    if(id != "" && entity != "") {
96
        this._searchDatasetsService.numOfEntityDatasets(id, entity, properties).subscribe(
97
            data => {
98
                this.searchUtils.totalResults = data;
99

    
100
                //var errorCodes:ErrorCodes = new ErrorCodes();
101
                this.searchUtils.status = this.errorCodes.DONE;
102
                if(this.searchUtils.totalResults == 0 ){
103
                  this.searchUtils.status = this.errorCodes.NONE;
104
                }
105
            },
106
            err => {
107
                console.log(err);
108
                 //TODO check erros (service not available, bad request)
109
                // if( ){
110
                //   this.searchUtils.status = ErrorCodes.ERROR;
111
                // }
112
                //var errorCodes:ErrorCodes = new ErrorCodes();
113
                //this.searchUtils.status = errorCodes.ERROR;
114
                if(err.status == '404') {
115
                  this.searchUtils.status = this.errorCodes.NOT_FOUND;
116
                } else if(err.status == '500') {
117
                  this.searchUtils.status = this.errorCodes.ERROR;
118
                } else {
119
                  this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
120
                }
121
            }
122
        );
123
    }
124
}
125

    
126
public getResultsForEntity(entity:string, id:string, page: number, size: number, properties:EnvProperties){
127
    //var errorCodes:ErrorCodes = new ErrorCodes();
128
    this.searchUtils.status = this.errorCodes.LOADING;
129

    
130
  var parameters = "";
131

    
132
  if(entity == "project") {
133
    parameters = "projects/"+id;
134
  } else if(entity == "organization") {
135
    parameters = "organizations/"+id;
136
  }
137

    
138
  if(parameters != "") {
139

    
140
      this._searchDatasetsService.searchDatasetsForEntity(parameters, page, size, properties).subscribe(
141
          data => {
142
              this.searchUtils.totalResults = data[0];
143
              console.info("search Research Data for "+entity+": [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
144
              this.results = data[1];
145

    
146
              //var errorCodes:ErrorCodes = new ErrorCodes();
147
              this.searchUtils.status = this.errorCodes.DONE;
148
              if(this.searchUtils.totalResults == 0 ){
149
                this.searchUtils.status = this.errorCodes.NONE;
150
              }
151
          },
152
          err => {
153
              console.log(err);
154
               //TODO check erros (service not available, bad request)
155
              // if( ){
156
              //   this.searchUtils.status = ErrorCodes.ERROR;
157
              // }
158
              //var errorCodes:ErrorCodes = new ErrorCodes();
159
              //this.searchUtils.status = errorCodes.ERROR;
160
              if(err.status == '404') {
161
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
162
              } else if(err.status == '500') {
163
                this.searchUtils.status = this.errorCodes.ERROR;
164
              } else {
165
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
166
              }
167
          }
168
      );
169
  }
170
}
171

    
172
public getResultsForDataproviders(id:string, resultsFrom:string, page: number, size: number, properties:EnvProperties){
173
    //var errorCodes:ErrorCodes = new ErrorCodes();
174
    this.searchUtils.status = this.errorCodes.LOADING;
175

    
176
  var parameters;
177
  if(resultsFrom == "collectedFrom") {
178
      parameters = "datasets?fq=collectedfromdatasourceid exact "+'"'+id+'"';
179
  } else if(resultsFrom == "hostedBy") {
180
      parameters = "datasets?fq=resulthostingdatasourceid exact "+'"'+id+'"';
181
  }
182

    
183
  if(parameters != "") {
184

    
185
      this._searchDatasetsService.searchDatasetsForDataproviders(parameters, page, size, properties).subscribe(
186
          data => {
187
              this.searchUtils.totalResults = data[0];
188
              console.info("search Research Data for Dataproviders: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
189
              this.results = data[1];
190

    
191
              //var errorCodes:ErrorCodes = new ErrorCodes();
192
              this.searchUtils.status = this.errorCodes.DONE;
193
              if(this.searchUtils.totalResults == 0 ){
194
                this.searchUtils.status = this.errorCodes.NONE;
195
              }
196
          },
197
          err => {
198
              console.log(err);
199
               //TODO check erros (service not available, bad request)
200
              // if( ){
201
              //   this.searchUtils.status = ErrorCodes.ERROR;
202
              // }
203
              //var errorCodes:ErrorCodes = new ErrorCodes();
204
              //this.searchUtils.status = errorCodes.ERROR;
205
              if(err.status == '404') {
206
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
207
              } else if(err.status == '500') {
208
                this.searchUtils.status = this.errorCodes.ERROR;
209
              } else {
210
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
211
              }
212
          }
213
      );
214
  }
215
}
216

    
217
public getAggregatorResults(id:string, page: number, size: number, properties:EnvProperties){
218
  //var errorCodes:ErrorCodes = new ErrorCodes();
219
  this.searchUtils.status = this.errorCodes.LOADING;
220

    
221
  this.subResults = this._searchDatasetsService.searchAggregators(id, '&fq=collectedfromdatasourceid exact "'+id+'"',"&refine=true&fields=resulthostingdatasource" , page, size, properties).subscribe(
222
      data => {
223
          this.results = data;
224
          this.searchUtils.totalResults = this.results.length;
225

    
226
          //var errorCodes:ErrorCodes = new ErrorCodes();
227
          this.searchUtils.status = this.errorCodes.DONE;
228
          if(this.searchUtils.totalResults == 0 ){
229
            this.searchUtils.status = this.errorCodes.NONE;
230
          }
231

    
232
          this.requestComplete.complete();
233
      },
234
      err => {
235
          console.log(err);
236
          console.info("status: "+err.status);
237
           //TODO check erros (service not available, bad request)
238
          // if( ){
239
          //   this.searchUtils.status = ErrorCodes.ERROR;
240
          // }
241
          //var errorCodes:ErrorCodes = new ErrorCodes();
242
          //this.searchUtils.status = errorCodes.ERROR;
243
          if(err.status == '404') {
244
            this.searchUtils.status = this.errorCodes.NOT_FOUND;
245
          } else if(err.status == '500') {
246
            this.searchUtils.status = this.errorCodes.ERROR;
247
          } else {
248
            this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
249
          }
250

    
251
          this.requestComplete.complete();
252
      }
253
  );
254
}
255

    
256
}
(2-2/7)