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
    parameters = this.getEntityName(resultType,true)+"?fq=collectedfromdatasourceid exact "+'"'+id+'"' + "or resulthostingdatasourceid exact "+'"'+id+'"';
193

    
194
    if(parameters != "") {
195

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

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

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

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

    
237
        this.searchUtils.status = this.errorCodes.DONE;
238
        if(this.searchUtils.totalResults == 0 ){
239
          this.searchUtils.status = this.errorCodes.NONE;
240
        }
241

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

    
262
        this.requestComplete.complete();
263
      }
264
    );
265
  }
266

    
267
  private handleError(message: string, error) {
268
    console.error("Fetch Research Results (class): "+message, error);
269
  }
270

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