Project

General

Profile

1
import {SearchResearchResultsService} from '../../services/searchResearchResults.service';
2
import {ErrorCodes} from '../../utils/properties/errorCodes';
3
import {ErrorMessagesComponent} from '../../utils/errorMessages.component';
4
import {SearchFields} from '../../utils/properties/searchFields';
5
import {SearchCustomFilter, SearchUtilsClass} from '../../searchPages/searchUtils/searchUtils.class';
6
import {DOI, StringUtils} from '../../utils/string-utils.class';
7
import {Subject, Subscriber} from 'rxjs';
8
import {EnvProperties} from '../../utils/properties/env-properties';
9

    
10
export class FetchResearchResults {
11
  private errorCodes: ErrorCodes;
12
  private errorMessages: ErrorMessagesComponent;
13

    
14
  public results =[];
15

    
16
  public requestComplete: Subject<void>;
17

    
18
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
19
  subscriptions = [];
20
  public searchFields:SearchFields = new SearchFields();
21

    
22
  public CSV: any = {
23
    "columnNames":  [
24
      "Title", "Authors", "Publication Year", "DOI",
25
      "Funder", "Project Name (GA Number)", "Access"
26
    ],
27
    "export":[]
28
  };
29
  public CSVDownloaded = false;
30
  public csvParams: string;
31

    
32
  constructor ( private _searchResearchResultsService: SearchResearchResultsService ) {
33
    this.errorCodes = new ErrorCodes();
34
    this.errorMessages = new ErrorMessagesComponent();
35
    this.searchUtils.status = this.errorCodes.LOADING;
36

    
37
    this.requestComplete = new Subject<void>();
38
  }
39

    
40
  public clearSubscriptions() {
41
    this.subscriptions.forEach(subscription => {
42
      if (subscription instanceof Subscriber) {
43
        subscription.unsubscribe();
44
      }
45
    });
46
  }
47

    
48
  public getResultsForCommunity(resultType:string, communityId: string, page: number, size: number, properties:EnvProperties, contextId = null) {
49
    this.searchUtils.status = this.errorCodes.LOADING;
50
    this.subscriptions.push(this._searchResearchResultsService.search(resultType, "", "&fq=communityid=" + communityId + (contextId?'&fq=categoryid=' + encodeURIComponent(contextId):''), page, size, "resultdateofacceptance,descending", [], properties).subscribe(
51
      data => {
52
        this.searchUtils.totalResults = data[0];
53
        this.results = data[1];
54

    
55
        this.searchUtils.status = this.errorCodes.DONE;
56
        if(this.searchUtils.totalResults == 0 ){
57
          this.searchUtils.status = this.errorCodes.NONE;
58
        }
59
      },
60
      err => {
61
        this.handleError("Error getting "+this.getEntityName(resultType,true)+" for community: "+communityId, err);
62
        this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
63
      }
64
    ));
65
  }
66

    
67
  public getNumForCommunity(resultType:string, communityId: string, properties:EnvProperties, contextId = null) {
68
    this.searchUtils.status = this.errorCodes.LOADING;
69

    
70
    this.subscriptions.push(this._searchResearchResultsService.countTotalResults(resultType, properties, "&fq=communityid=" + communityId +(contextId?'&fq=categoryid=' + encodeURIComponent(contextId):'')).subscribe(
71
      data => {
72
        this.searchUtils.totalResults = data;
73

    
74
        this.searchUtils.status = this.errorCodes.DONE;
75
        if(this.searchUtils.totalResults == 0 ){
76
          this.searchUtils.status = this.errorCodes.NONE;
77
        }
78
      },
79
      err => {
80
        this.handleError("Error getting number of "+this.getEntityName(resultType,true)+" for community: "+communityId, err);
81
        this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
82
      }
83
    ));
84
  }
85
  public getAllResultsForCommunity(resultType:string, communityId: string, page: number, size: number, properties:EnvProperties, contextId = null) {
86
    this.searchUtils.status = this.errorCodes.LOADING;
87

    
88
    this.subscriptions.push(this._searchResearchResultsService.advancedSearchResults(resultType, "",  page, size, "resultdateofacceptance,descending",  properties, "&type=results&fq=communityid=" + communityId +(contextId?'&fq=categoryid=' + encodeURIComponent(contextId):'')).subscribe(
89
      data => {
90
        this.searchUtils.totalResults = data[0];
91
        this.results = data[1];
92

    
93
        this.searchUtils.status = this.errorCodes.DONE;
94
        if(this.searchUtils.totalResults == 0 ){
95
          this.searchUtils.status = this.errorCodes.NONE;
96
        }
97
      },
98
      err => {
99
        this.handleError("Error getting "+this.getEntityName(resultType,true)+" for community: "+communityId, err);
100
        this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
101
      }
102
    ));
103
  }
104
  public getResultsByKeyword(resultType:string, keyword:string,  page: number, size: number, properties:EnvProperties,  customFilter:SearchCustomFilter=null){
105
    var parameters = "";
106
    if(keyword.length > 0){
107
      var DOIs:string[] = DOI.getDOIsFromString(keyword);
108
      var doisParams = "";
109

    
110
      for(var i =0 ;i < DOIs.length; i++){
111
        doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
112
      }
113
      if(doisParams.length > 0){
114
        parameters +=  "&"+doisParams;
115
      }else{
116
        parameters = "q=" + StringUtils.URIEncode(keyword);
117
      }
118
    }
119

    
120
    this.searchUtils.status = this.errorCodes.LOADING;
121
    var refineParams = null;
122
    if(customFilter){
123
      refineParams = (refineParams?(refineParams+'&'):'')+"&fq="+StringUtils.URIEncode(customFilter.queryFieldName + " exact " + StringUtils.quote((customFilter.valueId )));
124
    }
125
    this.subscriptions.push(this._searchResearchResultsService.search(this.getEntityName(resultType,false), parameters,refineParams, page, size, "", [], properties).subscribe(
126
      data => {
127
        this.searchUtils.totalResults = data[0];
128
        this.results = data[1];
129

    
130
        this.searchUtils.status = this.errorCodes.DONE;
131
        if(this.searchUtils.totalResults == 0 ){
132
          this.searchUtils.status = this.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
        if(err.status == '404') {
144
          this.searchUtils.status = this.errorCodes.NOT_FOUND;
145
        } else if(err.status == '500') {
146
          this.searchUtils.status = this.errorCodes.ERROR;
147
        } else {
148
          this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
149
        }*/
150
        this.handleError("Error getting "+this.getEntityName(resultType,true)+" for keyword: "+keyword + (doisParams ? "(DOI)" : ""), err);
151
        this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
152
      }
153
    ));
154
  }
155

    
156
  public getNumForEntity(resultType: string, entity:string, id:string, properties:EnvProperties){
157
    this.searchUtils.status = this.errorCodes.LOADING;
158

    
159
    if(id != "" && entity != "") {
160
      this.subscriptions.push(this._searchResearchResultsService.numOfEntityResults(this.getEntityName(resultType,false), id, entity, properties).subscribe(
161
        data => {
162
          this.searchUtils.totalResults = data;
163

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

    
191
  public getDmps(entity:string, id:string, page: number, size: number, properties:EnvProperties) {
192
    this.searchUtils.status = this.errorCodes.LOADING;
193
    let parameters = "";
194
    if(entity == "project") {
195
      parameters = '(relprojectid exact "'+id+'") and (instancetypename exact "Data Management Plan")';
196
    } else if(entity == "organization") {
197
      parameters = '(relorganizationid exact "'+id+'") and (instancetypename exact "Data Management Plan")';
198
    }
199
    if(parameters != "") {
200
      this.subscriptions.push(this._searchResearchResultsService.searchResultForEntity('publication', parameters, page, size, properties).subscribe(
201
        data => {
202
          this.searchUtils.totalResults = data[0];
203
          this.results = data[1];
204
      
205
          this.searchUtils.status = this.errorCodes.DONE;
206
          if (this.searchUtils.totalResults == 0) {
207
            this.searchUtils.status = this.errorCodes.NONE;
208
          }
209
        },
210
        err => {
211
          /*console.log(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
          if(err.status == '404') {
220
            this.searchUtils.status = this.errorCodes.NOT_FOUND;
221
          } else if(err.status == '500') {
222
            this.searchUtils.status = this.errorCodes.ERROR;
223
          } else {
224
            this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
225
          }*/
226
          this.handleError("Error getting dmps for " + entity + " with id: " + id, err);
227
          this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
228
        }
229
      ));
230
    }
231
  }
232
  
233
  public getResultsForEntity(resultType: string, entity:string, id:string, page: number, size: number, properties:EnvProperties){
234
    this.searchUtils.status = this.errorCodes.LOADING;
235

    
236
    let parameters = "";
237
    if(entity == "project") {
238
      //parameters = "projects/"+id;
239
      parameters = '(relprojectid exact "'+id+'")';
240
    } else if(entity == "organization") {
241
      //parameters = "organizations/"+id;
242
      parameters = '(relorganizationid exact "'+id+'")';
243
    }
244

    
245
    if(parameters != "") {
246
      this.subscriptions.push(this._searchResearchResultsService.searchResultForEntity(this.getEntityName(resultType,false), parameters, page, size, properties).subscribe(
247
        data => {
248
          this.searchUtils.totalResults = data[0];
249
          this.results = data[1];
250

    
251
          this.searchUtils.status = this.errorCodes.DONE;
252
          if(this.searchUtils.totalResults == 0 ){
253
            this.searchUtils.status = this.errorCodes.NONE;
254
          }
255
        },
256
        err => {
257
          /*console.log(err);
258
           //TODO check erros (service not available, bad request)
259
          // if( ){
260
          //   this.searchUtils.status = ErrorCodes.ERROR;
261
          // }
262
          //var errorCodes:ErrorCodes = new ErrorCodes();
263
          //this.searchUtils.status = errorCodes.ERROR;
264

    
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 "+this.getEntityName(resultType,true)+" for "+entity+" with id: "+id, err);
273
          this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
274
        }
275
      ));
276
    }
277
  }
278

    
279
  public getResultsForDataproviders(resultType: string, id:string, resultsFrom:string, page: number, size: number, properties:EnvProperties){
280
    this.searchUtils.status = this.errorCodes.LOADING;
281

    
282
    var parameters;
283
    // if(resultsFrom == "collectedFrom") {
284
    //   parameters = this.getEntityName(resultType,true)+"?fq=collectedfromdatasourceid exact "+'"'+id+'"';
285
    // } else if(resultsFrom == "hostedBy") {
286
    //   parameters = this.getEntityName(resultType,true)+"?fq=resulthostingdatasourceid exact "+'"'+id+'"';
287
    // }
288

    
289
    parameters = this.getEntityName(resultType,true)+"?fq=collectedfromdatasourceid exact "+'"'+id+'"' + "or resulthostingdatasourceid exact "+'"'+id+'"';
290

    
291
    if(parameters != "") {
292

    
293
      this.subscriptions.push(this._searchResearchResultsService.searchForDataproviders(this.getEntityName(resultType,false), parameters, page, size, properties).subscribe(
294
        data => {
295
          this.searchUtils.totalResults = data[0];
296
          this.results = data[1];
297

    
298
          this.searchUtils.status = this.errorCodes.DONE;
299
          if(this.searchUtils.totalResults == 0 ){
300
            this.searchUtils.status = this.errorCodes.NONE;
301
          }
302
        },
303
        err => {
304
          /*console.log(err);
305
           //TODO check erros (service not available, bad request)
306
          // if( ){
307
          //   this.searchUtils.status = ErrorCodes.ERROR;
308
          // }
309
          //var errorCodes:ErrorCodes = new ErrorCodes();
310
          //this.searchUtils.status = errorCodes.ERROR;
311
          if(err.status == '404') {
312
            this.searchUtils.status = this.errorCodes.NOT_FOUND;
313
          } else if(err.status == '500') {
314
            this.searchUtils.status = this.errorCodes.ERROR;
315
          } else {
316
            this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
317
          }*/
318
          this.handleError("Error getting "+this.getEntityName(resultType,true)+" for content provider ("+resultsFrom+") with id: "+id, err);
319
          this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
320
        }
321
      ));
322
    }
323
  }
324

    
325
  public getAggregatorResults(resultType: string, id:string, page: number, size: number, properties:EnvProperties){
326
    this.searchUtils.status = this.errorCodes.LOADING;
327

    
328
    // this.getEntityName(resultType,false)
329
    this.subscriptions.push(this._searchResearchResultsService.searchAggregators(resultType, id, '&fq=(collectedfromdatasourceid exact "'+id+'" or resulthostingdatasourceid exact "'+id+'")',"&refine=true&fields=resulthostingdatasource&type="+resultType , page, size, properties).subscribe(
330
      data => {
331
        this.results = data;
332
        this.searchUtils.totalResults = this.results.length;
333

    
334
        this.searchUtils.status = this.errorCodes.DONE;
335
        if(this.searchUtils.totalResults == 0 ){
336
          this.searchUtils.status = this.errorCodes.NONE;
337
        }
338

    
339
        this.requestComplete.complete();
340
      },
341
      err => {
342
        /*console.log(err);
343
         //TODO check erros (service not available, bad request)
344
        // if( ){
345
        //   this.searchUtils.status = ErrorCodes.ERROR;
346
        // }
347
        //var errorCodes:ErrorCodes = new ErrorCodes();
348
        //this.searchUtils.status = errorCodes.ERROR;
349
        if(err.status == '404') {
350
          this.searchUtils.status = this.errorCodes.NOT_FOUND;
351
        } else if(err.status == '500') {
352
          this.searchUtils.status = this.errorCodes.ERROR;
353
        } else {
354
          this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
355
        }*/
356
        this.handleError("Error getting "+this.getEntityName(resultType,true)+" for aggregator with id: "+id, err);
357
        this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
358

    
359
        this.requestComplete.complete();
360
      }
361
    ));
362
  }
363

    
364
  private handleError(message: string, error) {
365
    console.error("Fetch Research Results (class): "+message, error);
366
  }
367

    
368
  private getEntityName (entityType:string, plural:boolean){
369
    if(entityType == "publication" ||entityType == "dataset" || entityType == "organization" || entityType == "datasource" || entityType == "project" ){
370
      if(plural){
371
        return entityType+ "s";
372
      }else{
373
        return entityType;
374
      }
375
    }else{
376
      return entityType;
377
    }
378
  }
379
}
(4-4/4)