Project

General

Profile

1
import {SearchDatasetsService} from '../../services/searchDatasets.service';
2
import { ErrorCodes} from '../../utils/properties/openaireProperties';
3
import {SearchUtilsClass } from '../../searchPages/searchUtils/searchUtils.class';
4
import {DOI} from '../../utils/string-utils.class';
5

    
6
export class FetchDatasets{
7
  public results =[];
8

    
9
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
10
  private sub: any;
11
  private subResults: any;
12

    
13
  public csvParams: string;
14

    
15
  constructor ( private _searchDatasetsService: SearchDatasetsService ) {
16

    
17
    var errorCodes:ErrorCodes = new ErrorCodes();
18
    this.searchUtils.status =errorCodes.LOADING;
19

    
20
  }
21

    
22

    
23

    
24
  public ngOnDestroy() {
25
    if(this.sub){
26
      this.sub.unsubscribe();
27
    }
28
    if(this.subResults){
29
      this.subResults.unsubscribe();
30
    }
31
  }
32

    
33

    
34
  public getResultsByKeyword(keyword:string,  page: number, size: number){
35
    var parameters = "";
36
    if(keyword.length > 0){
37
      var DOIs:string[] = DOI.getDOIsFromString(keyword);
38
      var doisParams = "";
39

    
40
      for(var i =0 ;i < DOIs.length; i++){
41
        doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
42
      }
43
      if(doisParams.length > 0){
44
        parameters += "&"+doisParams;
45
      }else{
46
        parameters = "q=" + keyword;
47
      }
48
    }
49

    
50
    var errorCodes:ErrorCodes = new ErrorCodes();
51
    this.searchUtils.status = errorCodes.LOADING;
52

    
53
    this.subResults = this._searchDatasetsService.searchDatasets(parameters,null, page, size, []).subscribe(
54
        data => {
55
            this.searchUtils.totalResults = data[0];
56
            console.info("search Datasets: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
57
            this.results = data[1];
58

    
59
            var errorCodes:ErrorCodes = new ErrorCodes();
60
            this.searchUtils.status = errorCodes.DONE;
61
            if(this.searchUtils.totalResults == 0 ){
62
              this.searchUtils.status = errorCodes.NONE;
63
            }
64
         },
65
        err => {
66
            console.log(err);
67
             //TODO check erros (service not available, bad request)
68
            // if( ){
69
            //   this.searchUtils.status = ErrorCodes.ERROR;
70
            // }
71
            var errorCodes:ErrorCodes = new ErrorCodes();
72
            this.searchUtils.status = errorCodes.ERROR;
73
         }
74
    );
75
  }
76

    
77
public getNumForEntity(entity:string, id:string){
78
    var errorCodes:ErrorCodes = new ErrorCodes();
79
    this.searchUtils.status = errorCodes.LOADING;
80

    
81

    
82
    if(id != "" && entity != "") {
83
        this._searchDatasetsService.numOfEntityDatasets(id, entity).subscribe(
84
            data => {
85
                this.searchUtils.totalResults = data;
86

    
87
                var errorCodes:ErrorCodes = new ErrorCodes();
88
                this.searchUtils.status = errorCodes.DONE;
89
                if(this.searchUtils.totalResults == 0 ){
90
                  this.searchUtils.status = errorCodes.NONE;
91
                }
92
            },
93
            err => {
94
                console.log(err);
95
                 //TODO check erros (service not available, bad request)
96
                // if( ){
97
                //   this.searchUtils.status = ErrorCodes.ERROR;
98
                // }
99
                var errorCodes:ErrorCodes = new ErrorCodes();
100
                this.searchUtils.status = errorCodes.ERROR;
101
            }
102
        );
103
    }
104
}
105

    
106
public getResultsForEntity(entity:string, id:string, page: number, size: number){
107
    var errorCodes:ErrorCodes = new ErrorCodes();
108
    this.searchUtils.status = errorCodes.LOADING;
109

    
110
  var parameters = "";
111

    
112
  if(entity == "project") {
113
    parameters = "projects/"+id;
114
  }
115

    
116
  if(parameters != "") {
117

    
118
      this._searchDatasetsService.searchDatasetsForEntity(parameters, page, size).subscribe(
119
          data => {
120
              this.searchUtils.totalResults = data[0];
121
              console.info("search Datasets for "+entity+": [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
122
              this.results = data[1];
123

    
124
              var errorCodes:ErrorCodes = new ErrorCodes();
125
              this.searchUtils.status = errorCodes.DONE;
126
              if(this.searchUtils.totalResults == 0 ){
127
                this.searchUtils.status = errorCodes.NONE;
128
              }
129
          },
130
          err => {
131
              console.log(err);
132
               //TODO check erros (service not available, bad request)
133
              // if( ){
134
              //   this.searchUtils.status = ErrorCodes.ERROR;
135
              // }
136
              var errorCodes:ErrorCodes = new ErrorCodes();
137
              this.searchUtils.status = errorCodes.ERROR;
138
          }
139
      );
140
  }
141
}
142

    
143
public getResultsForDataproviders(id:string, resultsFrom:string, page: number, size: number){
144
    var errorCodes:ErrorCodes = new ErrorCodes();
145
    this.searchUtils.status = errorCodes.LOADING;
146

    
147
  var parameters;
148
  if(resultsFrom == "collectedFrom") {
149
      parameters = "datasets?fq=collectedfromdatasourceid exact "+'"'+id+'"';
150
  } else if(resultsFrom == "hostedBy") {
151
      parameters = "datasets?fq=resulthostingdatasourceid exact "+'"'+id+'"';
152
  }
153

    
154
  if(parameters != "") {
155

    
156
      this._searchDatasetsService.searchDatasetsForDataproviders(parameters, page, size).subscribe(
157
          data => {
158
              this.searchUtils.totalResults = data[0];
159
              console.info("search Datasets for Dataproviders: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
160
              this.results = data[1];
161

    
162
              var errorCodes:ErrorCodes = new ErrorCodes();
163
              this.searchUtils.status = errorCodes.DONE;
164
              if(this.searchUtils.totalResults == 0 ){
165
                this.searchUtils.status = errorCodes.NONE;
166
              }
167
          },
168
          err => {
169
              console.log(err);
170
               //TODO check erros (service not available, bad request)
171
              // if( ){
172
              //   this.searchUtils.status = ErrorCodes.ERROR;
173
              // }
174
              var errorCodes:ErrorCodes = new ErrorCodes();
175
              this.searchUtils.status = errorCodes.ERROR;
176
          }
177
      );
178
  }
179
}
180

    
181
public getAggregatorResults(id:string, page: number, size: number){
182
    var errorCodes:ErrorCodes = new ErrorCodes();
183
    this.searchUtils.status = errorCodes.LOADING;
184

    
185
  this.subResults = this._searchDatasetsService.searchAggregators(id, '&fq=collectedfromdatasourceid exact "'+id+'"',"&refine=true&fields=resulthostingdatasource" , page, size).subscribe(
186
      data => {
187
          this.results = data;
188

    
189
          var errorCodes:ErrorCodes = new ErrorCodes();
190
          this.searchUtils.status = errorCodes.DONE;
191
          if(this.searchUtils.totalResults == 0 ){
192
            this.searchUtils.status = errorCodes.NONE;
193
          }
194
      },
195
      err => {
196
          console.log(err);
197
           //TODO check erros (service not available, bad request)
198
          // if( ){
199
          //   this.searchUtils.status = ErrorCodes.ERROR;
200
          // }
201
          var errorCodes:ErrorCodes = new ErrorCodes();
202
          this.searchUtils.status = errorCodes.ERROR;
203
      }
204
  );
205
}
206

    
207
}
(2-2/5)