Project

General

Profile

1
import {SearchSoftwareService} from '../../services/searchSoftware.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

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

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

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

    
21
  public csvParams: string;
22

    
23
  constructor ( private _searchSoftwareService: SearchSoftwareService ) {
24

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

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

    
32

    
33

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

    
43

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

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

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

    
72
            //var errorCodes:ErrorCodes = new ErrorCodes();
73
            this.searchUtils.status = this.errorCodes.DONE;
74
            if(this.searchUtils.totalResults == 0 ){
75
              this.searchUtils.status = this.errorCodes.NONE;
76
            }
77
         },
78
        err => {
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
            this.handleError("Error getting software for keyword: "+keyword + (doisParams ? "(DOI)" : ""), err);
94
            this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
95
         }
96
    );
97
  }
98

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

    
103

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

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

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

    
141
  var parameters = "";
142

    
143
  if(entity == "project") {
144
    parameters = "projects/"+id;
145
  }
146

    
147
  if(parameters != "") {
148

    
149
      this._searchSoftwareService.searchSoftwareForEntity(parameters, page, size, properties).subscribe(
150
          data => {
151
              this.searchUtils.totalResults = data[0];
152
              //console.info("search Software for "+entity+": [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
153
              this.results = data[1];
154

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

    
183
public getResultsForDataproviders(id:string, resultsFrom:string, page: number, size: number, properties:EnvProperties){
184
    //var errorCodes:ErrorCodes = new ErrorCodes();
185
    this.searchUtils.status = this.errorCodes.LOADING;
186

    
187
  var parameters;
188
  if(resultsFrom == "collectedFrom") {
189
      parameters = "software?fq=collectedfromdatasourceid exact "+'"'+id+'"';
190
  } else if(resultsFrom == "hostedBy") {
191
      parameters = "software?fq=resulthostingdatasourceid exact "+'"'+id+'"';
192
  }
193

    
194
  if(parameters != "") {
195

    
196
      this._searchSoftwareService.searchSoftwareForDataproviders(parameters, page, size, properties).subscribe(
197
          data => {
198
              this.searchUtils.totalResults = data[0];
199
              //console.info("search Software for Dataproviders: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
200
              this.results = data[1];
201

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

    
230
public getAggregatorResults(id:string, page: number, size: number, properties:EnvProperties){
231
  //var errorCodes:ErrorCodes = new ErrorCodes();
232
  this.searchUtils.status = this.errorCodes.LOADING;
233

    
234
  this.subResults = this._searchSoftwareService.searchAggregators(id, '&fq=collectedfromdatasourceid exact "'+id+'"',"&refine=true&fields=resulthostingdatasource" , page, size, properties).subscribe(
235
      data => {
236
          this.results = data;
237
          this.searchUtils.totalResults = this.results.length;
238

    
239
          //var errorCodes:ErrorCodes = new ErrorCodes();
240
          this.searchUtils.status = this.errorCodes.DONE;
241
          if(this.searchUtils.totalResults == 0 ){
242
            this.searchUtils.status = this.errorCodes.NONE;
243
          }
244

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

    
266
          this.requestComplete.complete();
267
      }
268
  );
269
}
270

    
271
  private handleError(message: string, error) {
272
    console.error("Fetch Software (class): "+message, error);
273
  }
274
}
(7-7/7)