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} from '../../utils/string-utils.class';
7
import {Subject} from 'rxjs';
8
import{EnvProperties} from '../../utils/properties/env-properties';
9
import {StringUtils} from '../../utils/string-utils.class';
10

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

    
15
  public results =[];
16

    
17
  public requestComplete: Subject<void>;
18

    
19
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
20
  public sub: any;
21
  public subResults: any;
22
  public searchFields:SearchFields = new SearchFields();
23

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

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

    
39
    this.requestComplete = new Subject<void>();
40
  }
41

    
42
  public ngOnDestroy() {
43
    if(this.sub){
44
      this.sub.unsubscribe();
45
    }
46
    if(this.subResults){
47
      this.subResults.unsubscribe();
48
    }
49
  }
50

    
51
  public getResultsByKeyword(resultType:string, keyword:string,  page: number, size: number, properties:EnvProperties,  customFilter:SearchCustomFilter=null){
52
    var parameters = "";
53
    if(keyword.length > 0){
54
      var DOIs:string[] = DOI.getDOIsFromString(keyword);
55
      var doisParams = "";
56

    
57
      for(var i =0 ;i < DOIs.length; i++){
58
        doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
59
      }
60
      if(doisParams.length > 0){
61
        parameters +=  "&"+doisParams;
62
      }else{
63
        parameters = "q=" + StringUtils.URIEncode(keyword);
64
      }
65
    }
66

    
67
    this.searchUtils.status = this.errorCodes.LOADING;
68
    var refineParams = null;
69
    if(customFilter){
70
      refineParams = (refineParams?(refineParams+'&'):'')+"&fq="+StringUtils.URIEncode(customFilter.queryFieldName + " exact " + StringUtils.quote((customFilter.valueId )));
71
    }
72
    this.subResults = this._searchResearchResultsService.search(this.getEntityName(resultType,false), parameters,refineParams, page, size, "", [], properties).subscribe(
73
      data => {
74
        this.searchUtils.totalResults = data[0];
75
        this.results = data[1];
76

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

    
103
  public getNumForEntity(resultType: string, entity:string, id:string, properties:EnvProperties){
104
    this.searchUtils.status = this.errorCodes.LOADING;
105

    
106
    if(id != "" && entity != "") {
107
      this._searchResearchResultsService.numOfEntityResults(this.getEntityName(resultType,false), id, entity, properties).subscribe(
108
        data => {
109
          this.searchUtils.totalResults = data;
110

    
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
          /*console.log(err);
118
           //TODO check erros (service not available, bad request)
119
          // if( ){
120
          //   this.searchUtils.status = ErrorCodes.ERROR;
121
          // }
122
          //var errorCodes:ErrorCodes = new ErrorCodes();
123
          //this.searchUtils.status = errorCodes.ERROR;
124
          if(err.status == '404') {
125
            this.searchUtils.status = this.errorCodes.NOT_FOUND;
126
          } else if(err.status == '500') {
127
            this.searchUtils.status = this.errorCodes.ERROR;
128
          } else {
129
            this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
130
          }*/
131
          this.handleError("Error getting "+this.getEntityName(resultType,true)+" for "+entity+" with id: "+id, err);
132
          this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
133
        }
134
      );
135
    }
136
  }
137

    
138
  public getResultsForEntity(resultType: string, entity:string, id:string, page: number, size: number, properties:EnvProperties){
139
    this.searchUtils.status = this.errorCodes.LOADING;
140

    
141
    var parameters = "";
142
    if(entity == "project") {
143
      parameters = "projects/"+id;
144
    } else if(entity == "organization") {
145
      parameters = "organizations/"+id;
146
    }
147

    
148
    if(parameters != "") {
149
      this._searchResearchResultsService.searchResultForEntity(this.getEntityName(resultType,false), parameters, page, size, properties).subscribe(
150
        data => {
151
          this.searchUtils.totalResults = data[0];
152
          this.results = data[1];
153

    
154
          this.searchUtils.status = this.errorCodes.DONE;
155
          if(this.searchUtils.totalResults == 0 ){
156
            this.searchUtils.status = this.errorCodes.NONE;
157
          }
158
        },
159
        err => {
160
          /*console.log(err);
161
           //TODO check erros (service not available, bad request)
162
          // if( ){
163
          //   this.searchUtils.status = ErrorCodes.ERROR;
164
          // }
165
          //var errorCodes:ErrorCodes = new ErrorCodes();
166
          //this.searchUtils.status = errorCodes.ERROR;
167

    
168
          if(err.status == '404') {
169
            this.searchUtils.status = this.errorCodes.NOT_FOUND;
170
          } else if(err.status == '500') {
171
            this.searchUtils.status = this.errorCodes.ERROR;
172
          } else {
173
            this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
174
          }*/
175
          this.handleError("Error getting "+this.getEntityName(resultType,true)+" for "+entity+" with id: "+id, err);
176
          this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
177
        }
178
      );
179
    }
180
  }
181

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

    
185
    var parameters;
186
    if(resultsFrom == "collectedFrom") {
187
      parameters = this.getEntityName(resultType,true)+"?fq=collectedfromdatasourceid exact "+'"'+id+'"';
188
    } else if(resultsFrom == "hostedBy") {
189
      parameters = this.getEntityName(resultType,true)+"?fq=resulthostingdatasourceid exact "+'"'+id+'"';
190
    }
191

    
192
    if(parameters != "") {
193

    
194
      this._searchResearchResultsService.searchForDataproviders(this.getEntityName(resultType,false), parameters, page, size, properties).subscribe(
195
        data => {
196
          this.searchUtils.totalResults = data[0];
197
          this.results = data[1];
198

    
199
          this.searchUtils.status = this.errorCodes.DONE;
200
          if(this.searchUtils.totalResults == 0 ){
201
            this.searchUtils.status = this.errorCodes.NONE;
202
          }
203
        },
204
        err => {
205
          /*console.log(err);
206
           //TODO check erros (service not available, bad request)
207
          // if( ){
208
          //   this.searchUtils.status = ErrorCodes.ERROR;
209
          // }
210
          //var errorCodes:ErrorCodes = new ErrorCodes();
211
          //this.searchUtils.status = errorCodes.ERROR;
212
          if(err.status == '404') {
213
            this.searchUtils.status = this.errorCodes.NOT_FOUND;
214
          } else if(err.status == '500') {
215
            this.searchUtils.status = this.errorCodes.ERROR;
216
          } else {
217
            this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
218
          }*/
219
          this.handleError("Error getting "+this.getEntityName(resultType,true)+" for content provider ("+resultsFrom+") with id: "+id, err);
220
          this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
221
        }
222
      );
223
    }
224
  }
225

    
226
  public getAggregatorResults(resultType: string, id:string, page: number, size: number, properties:EnvProperties){
227
    this.searchUtils.status = this.errorCodes.LOADING;
228

    
229
    this.subResults = this._searchResearchResultsService.searchAggregators(this.getEntityName(resultType,false), id, '&fq=collectedfromdatasourceid exact "'+id+'"',"&refine=true&fields=resulthostingdatasource" , page, size, properties).subscribe(
230
      data => {
231
        this.results = data;
232
        this.searchUtils.totalResults = this.results.length;
233

    
234
        this.searchUtils.status = this.errorCodes.DONE;
235
        if(this.searchUtils.totalResults == 0 ){
236
          this.searchUtils.status = this.errorCodes.NONE;
237
        }
238

    
239
        this.requestComplete.complete();
240
      },
241
      err => {
242
        /*console.log(err);
243
         //TODO check erros (service not available, bad request)
244
        // if( ){
245
        //   this.searchUtils.status = ErrorCodes.ERROR;
246
        // }
247
        //var errorCodes:ErrorCodes = new ErrorCodes();
248
        //this.searchUtils.status = errorCodes.ERROR;
249
        if(err.status == '404') {
250
          this.searchUtils.status = this.errorCodes.NOT_FOUND;
251
        } else if(err.status == '500') {
252
          this.searchUtils.status = this.errorCodes.ERROR;
253
        } else {
254
          this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
255
        }*/
256
        this.handleError("Error getting "+this.getEntityName(resultType,true)+" for aggregator with id: "+id, err);
257
        this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
258

    
259
        this.requestComplete.complete();
260
      }
261
    );
262
  }
263

    
264
  private handleError(message: string, error) {
265
    console.error("Fetch Research Results (class): "+message, error);
266
  }
267

    
268
  private getEntityName (entityType:string, plural:boolean){
269
    if(entityType == "publication" ||entityType == "dataset" || entityType == "organization" || entityType == "datasource" || entityType == "project" ){
270
      if(plural){
271
        return entityType+ "s";
272
      }else{
273
        return entityType;
274
      }
275
    }else{
276
      return entityType;
277
    }
278
  }
279
}
(4-4/4)