Project

General

Profile

1
import {SearchDatasetsService} from '../../services/searchDatasets.service';
2
import { ErrorCodes} from '../../utils/properties/errorCodes';
3
import {ErrorMessagesComponent}    from '../../utils/errorMessages.component';
4
import {SearchCustomFilter, SearchUtilsClass} from '../../searchPages/searchUtils/searchUtils.class';
5
import {DOI} from '../../utils/string-utils.class';
6
import {Subject} from 'rxjs';
7
import{EnvProperties} from '../../utils/properties/env-properties';
8
import {StringUtils} from '../../utils/string-utils.class';
9
export class FetchDatasets{
10
  private errorCodes: ErrorCodes;
11
  private errorMessages: ErrorMessagesComponent;
12

    
13
  public results =[];
14
  public requestComplete: Subject<void>;
15

    
16
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
17
  private sub: any;
18
  private subResults: any;
19

    
20
  public csvParams: string;
21

    
22
  constructor ( private _searchDatasetsService: SearchDatasetsService ) {
23

    
24
    this.errorCodes = new ErrorCodes();
25
    this.errorMessages = new ErrorMessagesComponent();
26
    this.searchUtils.status = this.errorCodes.LOADING;
27

    
28
    this.requestComplete = new Subject<void>();
29
  }
30

    
31

    
32

    
33
  public ngOnDestroy() {
34
    if(this.sub){
35
      this.sub.unsubscribe();
36
    }
37
    if(this.subResults){
38
      this.subResults.unsubscribe();
39
    }
40
  }
41

    
42

    
43
  public getResultsByKeyword(keyword:string,  page: number, size: number, properties:EnvProperties, connectCommunityId=null, customFilter:SearchCustomFilter=null){
44
    var parameters = "";
45
    if(keyword.length > 0){
46
      var DOIs:string[] = DOI.getDOIsFromString(keyword);
47
      var doisParams = "";
48

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

    
59
    //var errorCodes:ErrorCodes = new ErrorCodes();
60
    this.searchUtils.status = this.errorCodes.LOADING;
61
    var refineParams = (connectCommunityId)?("&fq="+StringUtils.URIEncode("communityId exact " + StringUtils.quote((connectCommunityId )))):null;
62
    if(customFilter){
63
      refineParams = (refineParams?(refineParams+'&'):'')+"&fq="+StringUtils.URIEncode(customFilter.queryFieldName + " exact " + StringUtils.quote((customFilter.valueId )));
64
    }
65
    this.subResults = this._searchDatasetsService.searchDatasets(parameters,refineParams, page, size, "", [], properties).subscribe(
66
        data => {
67
            this.searchUtils.totalResults = data[0];
68
            //console.info("search Research Data: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
69
            this.results = data[1];
70

    
71
            //var errorCodes:ErrorCodes = new ErrorCodes();
72
            this.searchUtils.status = this.errorCodes.DONE;
73
            if(this.searchUtils.totalResults == 0 ){
74
              this.searchUtils.status = this.errorCodes.NONE;
75
            }
76
         },
77
        err => {
78
            /*
79
            console.log(err);
80
             //TODO check erros (service not available, bad request)
81
            // if( ){
82
            //   this.searchUtils.status = ErrorCodes.ERROR;
83
            // }
84
            //var errorCodes:ErrorCodes = new ErrorCodes();
85
            //this.searchUtils.status = errorCodes.ERROR;
86
            if(err.status == '404') {
87
              this.searchUtils.status = this.errorCodes.NOT_FOUND;
88
            } else if(err.status == '500') {
89
              this.searchUtils.status = this.errorCodes.ERROR;
90
            } else {
91
              this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
92
            }
93
            */
94
            this.handleError("Error getting research data for keyword: "+keyword + (doisParams ? "(DOI)" : ""), err);
95
            this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
96
         }
97
    );
98
  }
99

    
100
public getNumForEntity(entity:string, id:string, properties:EnvProperties){
101
    //var errorCodes:ErrorCodes = new ErrorCodes();
102
    this.searchUtils.status = this.errorCodes.LOADING;
103

    
104

    
105
    if(id != "" && entity != "") {
106
        this._searchDatasetsService.numOfEntityDatasets(id, entity, properties).subscribe(
107
            data => {
108
                this.searchUtils.totalResults = data;
109

    
110
                //var errorCodes:ErrorCodes = new ErrorCodes();
111
                this.searchUtils.status = this.errorCodes.DONE;
112
                if(this.searchUtils.totalResults == 0 ){
113
                  this.searchUtils.status = this.errorCodes.NONE;
114
                }
115
            },
116
            err => {
117
                /*
118
                console.log(err);
119
                 //TODO check erros (service not available, bad request)
120
                // if( ){
121
                //   this.searchUtils.status = ErrorCodes.ERROR;
122
                // }
123
                //var errorCodes:ErrorCodes = new ErrorCodes();
124
                //this.searchUtils.status = errorCodes.ERROR;
125
                if(err.status == '404') {
126
                  this.searchUtils.status = this.errorCodes.NOT_FOUND;
127
                } else if(err.status == '500') {
128
                  this.searchUtils.status = this.errorCodes.ERROR;
129
                } else {
130
                  this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
131
                }
132
                */
133
                this.handleError("Error getting research data for "+entity+" with id: "+id, err);
134
                this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
135
            }
136
        );
137
    }
138
}
139

    
140
public getResultsForEntity(entity:string, id:string, page: number, size: number, properties:EnvProperties){
141
    //var errorCodes:ErrorCodes = new ErrorCodes();
142
    this.searchUtils.status = this.errorCodes.LOADING;
143

    
144
  var parameters = "";
145

    
146
  if(entity == "project") {
147
    parameters = "projects/"+id;
148
  } else if(entity == "organization") {
149
    parameters = "organizations/"+id;
150
  }
151

    
152
  if(parameters != "") {
153

    
154
      this._searchDatasetsService.searchDatasetsForEntity(parameters, page, size, properties).subscribe(
155
          data => {
156
              this.searchUtils.totalResults = data[0];
157
              //console.info("search Research Data for "+entity+": [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
158
              this.results = data[1];
159

    
160
              //var errorCodes:ErrorCodes = new ErrorCodes();
161
              this.searchUtils.status = this.errorCodes.DONE;
162
              if(this.searchUtils.totalResults == 0 ){
163
                this.searchUtils.status = this.errorCodes.NONE;
164
              }
165
          },
166
          err => {
167
              /*
168
              console.log(err);
169
               //TODO check erros (service not available, bad request)
170
              // if( ){
171
              //   this.searchUtils.status = ErrorCodes.ERROR;
172
              // }
173
              //var errorCodes:ErrorCodes = new ErrorCodes();
174
              //this.searchUtils.status = errorCodes.ERROR;
175
              if(err.status == '404') {
176
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
177
              } else if(err.status == '500') {
178
                this.searchUtils.status = this.errorCodes.ERROR;
179
              } else {
180
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
181
              }
182
              */
183
              this.handleError("Error getting research data for "+entity+" with id: "+id, err);
184
              this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
185
          }
186
      );
187
  }
188
}
189

    
190
public getResultsForDataproviders(id:string, resultsFrom:string, page: number, size: number, properties:EnvProperties){
191
    //var errorCodes:ErrorCodes = new ErrorCodes();
192
    this.searchUtils.status = this.errorCodes.LOADING;
193

    
194
  var parameters;
195
  if(resultsFrom == "collectedFrom") {
196
      parameters = "datasets?fq=collectedfromdatasourceid exact "+'"'+id+'"';
197
  } else if(resultsFrom == "hostedBy") {
198
      parameters = "datasets?fq=resulthostingdatasourceid exact "+'"'+id+'"';
199
  }
200

    
201
  if(parameters != "") {
202

    
203
      this._searchDatasetsService.searchDatasetsForDataproviders(parameters, page, size, properties).subscribe(
204
          data => {
205
              this.searchUtils.totalResults = data[0];
206
              //console.info("search Research Data for Dataproviders: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
207
              this.results = data[1];
208

    
209
              //var errorCodes:ErrorCodes = new ErrorCodes();
210
              this.searchUtils.status = this.errorCodes.DONE;
211
              if(this.searchUtils.totalResults == 0 ){
212
                this.searchUtils.status = this.errorCodes.NONE;
213
              }
214
          },
215
          err => {
216
              /*
217
              console.log(err);
218
               //TODO check erros (service not available, bad request)
219
              // if( ){
220
              //   this.searchUtils.status = ErrorCodes.ERROR;
221
              // }
222
              //var errorCodes:ErrorCodes = new ErrorCodes();
223
              //this.searchUtils.status = errorCodes.ERROR;
224
              if(err.status == '404') {
225
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
226
              } else if(err.status == '500') {
227
                this.searchUtils.status = this.errorCodes.ERROR;
228
              } else {
229
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
230
              }*/
231
              this.handleError("Error getting research data for content provider ("+resultsFrom+") with id: "+id, err);
232
              this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
233
          }
234
      );
235
  }
236
}
237

    
238
public getAggregatorResults(id:string, page: number, size: number, properties:EnvProperties){
239
  //var errorCodes:ErrorCodes = new ErrorCodes();
240
  this.searchUtils.status = this.errorCodes.LOADING;
241

    
242
  this.subResults = this._searchDatasetsService.searchAggregators(id, '&fq=collectedfromdatasourceid exact "'+id+'"',"&refine=true&fields=resulthostingdatasource" , page, size, properties).subscribe(
243
      data => {
244
          this.results = data;
245
          this.searchUtils.totalResults = this.results.length;
246

    
247
          //var errorCodes:ErrorCodes = new ErrorCodes();
248
          this.searchUtils.status = this.errorCodes.DONE;
249
          if(this.searchUtils.totalResults == 0 ){
250
            this.searchUtils.status = this.errorCodes.NONE;
251
          }
252

    
253
          this.requestComplete.complete();
254
      },
255
      err => {
256
          /*
257
          console.log(err);
258
          //console.info("status: "+err.status);
259
           //TODO check erros (service not available, bad request)
260
          // if( ){
261
          //   this.searchUtils.status = ErrorCodes.ERROR;
262
          // }
263
          //var errorCodes:ErrorCodes = new ErrorCodes();
264
          //this.searchUtils.status = errorCodes.ERROR;
265
          if(err.status == '404') {
266
            this.searchUtils.status = this.errorCodes.NOT_FOUND;
267
          } else if(err.status == '500') {
268
            this.searchUtils.status = this.errorCodes.ERROR;
269
          } else {
270
            this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
271
          }*/
272
          this.handleError("Error getting research data for aggregator with id: "+id, err);
273
          this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
274

    
275
          this.requestComplete.complete();
276
      }
277
  );
278
}
279

    
280
  private handleError(message: string, error) {
281
    console.error("Fetch Research Data (class): "+message, error);
282
  }
283
}
(2-2/7)