Project

General

Profile

1
import {Component, Input, ViewChild} from '@angular/core';
2
import { ActivatedRoute} from '@angular/router';
3
import {Location} from '@angular/common';
4
import { Filter, Value} from '../searchUtils/searchHelperClasses.class';
5

    
6
import {SearchDatasetsService} from '../../services/searchDatasets.service';
7
import {SearchResult}     from '../../utils/entities/searchResult';
8
import {OpenaireProperties, ErrorCodes} from '../../utils/properties/openaireProperties';
9
import {SearchFields} from '../../utils/properties/searchFields';
10
import {SearchPageComponent } from '../searchUtils/searchPage.component';
11
import {SearchUtilsClass } from '../searchUtils/searchUtils.class';
12
import {DOI} from '../../utils/string-utils.class';
13

    
14
@Component({
15
    selector: 'search-datasets',
16
    template: `
17

    
18
    <search-page pageTitle="Search Research Data"
19
                  formPlaceholderText = "Search for Research Data"
20
                 type="research data" entityType="dataset" [(filters)] = "filters"
21
                 [(results)] = "results"   [(searchUtils)] = "searchUtils"
22
                 [baseUrl] = "baseUrl" (queryChange)="queryChanged($event)"
23
                 [csvParams]="csvParams" csvPath="datasets" advancedSearchLink="/search/advanced/datasets"
24
                 [disableForms]="disableForms"
25
                 searchFormClass="datasetsSearchForm">
26
    </search-page>
27
    `
28
})
29

    
30
export class SearchDatasetsComponent {
31
  public results =[];
32
  public filters: Filter[] =[];
33
  // public totalResults:number  = 0 ;
34
  public baseUrl:string;
35

    
36
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
37
  private sub: any;
38
  private subResults: any;
39
  private searchFields:SearchFields = new SearchFields();
40
  public refineFields: string[] =  this.searchFields.RESULT_REFINE_FIELDS;
41
  public fieldIdsMap=this.searchFields.RESULT_FIELDS;
42
  private urlParams : Map<string, string>;
43
  private _location:Location;
44
  public csvParams: string;
45
  public disableForms: boolean = false;
46

    
47
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
48
  constructor (private route: ActivatedRoute, private _searchDatasetsService: SearchDatasetsService ) {
49

    
50
    var errorCodes:ErrorCodes = new ErrorCodes();
51
    this.searchUtils.status =errorCodes.LOADING;
52
    this.searchUtils.page =1;
53
    this.baseUrl = OpenaireProperties.getLinkToSearchDatasets();
54

    
55
  }
56

    
57
  public ngOnInit() {
58
    this.searchPage.refineFields = this.refineFields;
59
     this.searchPage.fieldIdsMap = this.fieldIdsMap;
60
     this.searchPage.type = "research data";
61
     var firstLoad =true;
62
    this.sub =  this.route.queryParams.subscribe(params => {
63
      this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
64
      var refine = true;
65
      if(this.searchUtils.page != ((params['page']=== undefined)?1:+params['page']) && this.filters && !firstLoad){
66
        refine = false;
67
      }
68
      firstLoad = false;
69
      this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
70

    
71
      var queryParameters = this.searchPage.getQueryParametersFromUrl(params);
72
       this._getResults(queryParameters, refine, this.searchUtils.page, this.searchUtils.size);
73

    
74
    });
75
  }
76

    
77
  public ngOnDestroy() {
78
    if(this.sub){
79
      this.sub.unsubscribe();
80
    }
81
    if(this.subResults){
82
      this.subResults.unsubscribe();
83
    }
84
  }
85

    
86

    
87
public getResultsForEntity(entity:string, id:string, page: number, size: number){
88
  var parameters = "";
89

    
90
  if(entity == "project") {
91
    parameters = "projects/"+id;
92
  }
93

    
94
  if(parameters != "") {
95

    
96
      this._searchDatasetsService.searchDatasetsForEntity(parameters, page, size).subscribe(
97
          data => {
98
              this.searchUtils.totalResults = data[0];
99
              console.info("search Research Data for "+entity+": [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
100
              this.results = data[1];
101

    
102
              var errorCodes:ErrorCodes = new ErrorCodes();
103
              this.searchUtils.status = errorCodes.DONE;
104
              if(this.searchUtils.totalResults == 0 ){
105
                this.searchUtils.status = errorCodes.NONE;
106
              }
107
          },
108
          err => {
109
              console.log(err);
110
               //TODO check erros (service not available, bad request)
111
              // if( ){
112
              //   this.searchUtils.status = ErrorCodes.ERROR;
113
              // }
114
              var errorCodes:ErrorCodes = new ErrorCodes();
115
              this.searchUtils.status = errorCodes.ERROR;
116
          }
117
      );
118
  }
119
}
120

    
121
public getResultsForDataproviders(id:string, resultsFrom:string, page: number, size: number){
122
  var parameters;
123
  if(resultsFrom == "collectedFrom") {
124
      parameters = "datasets?fq=collectedfromdatasourceid exact "+'"'+id+'"';
125
  } else if(resultsFrom == "hostedBy") {
126
      parameters = "datasets?fq=resulthostingdatasourceid exact "+'"'+id+'"';
127
  }
128

    
129
  if(parameters != "") {
130

    
131
      this._searchDatasetsService.searchDatasetsForDataproviders(parameters, page, size).subscribe(
132
          data => {
133
              this.searchUtils.totalResults = data[0];
134
              console.info("search Research Data for Dataproviders: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
135
              this.results = data[1];
136

    
137
              var errorCodes:ErrorCodes = new ErrorCodes();
138
              this.searchUtils.status = errorCodes.DONE;
139
              if(this.searchUtils.totalResults == 0 ){
140
                this.searchUtils.status = errorCodes.NONE;
141
              }
142
          },
143
          err => {
144
              console.log(err);
145
               //TODO check erros (service not available, bad request)
146
              // if( ){
147
              //   this.searchUtils.status = ErrorCodes.ERROR;
148
              // }
149
              var errorCodes:ErrorCodes = new ErrorCodes();
150
              this.searchUtils.status = errorCodes.ERROR;
151
          }
152
      );
153
  }
154
}
155

    
156
public getResults(keyword:string,refine:boolean, page: number, size: number){
157
  var parameters = "";
158
  if(keyword.length > 0){
159
    var DOIs:string[] = DOI.getDOIsFromString(keyword);
160
    var doisParams = "";
161
    for(var i =0 ;i < DOIs.length; i++){
162
      doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
163
    }
164
    if(doisParams.length > 0){
165
        parameters +=  "&"+doisParams;
166
    }else{
167
      parameters = "q=" + keyword;
168
    }
169
  }
170
  this._getResults(parameters,refine,page,size);
171
}
172
private _getResults(parameters:string,refine:boolean, page: number, size: number){
173
    this.csvParams = parameters;
174

    
175
  // if(!refine && !this.searchPage){
176
  //     this.searchPage = new SearchPageComponent(this._location);
177
  // }
178
  var errorCodes:ErrorCodes = new ErrorCodes();
179
  this.searchUtils.status = errorCodes.LOADING;
180
  //this.searchPage.openLoading();
181
  this.disableForms = true;
182
  this.results = [];
183
  this.searchUtils.totalResults = 0;
184

    
185
  this.subResults = this._searchDatasetsService.searchDatasets(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, this.searchPage.getFields()).subscribe(
186
      data => {
187
          this.searchUtils.totalResults = data[0];
188
          console.info("search Research Data: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
189
          this.results = data[1];
190
          if(refine){
191
            this.filters = data[2];
192
          }
193
          this.searchPage.checkSelectedFilters(this.filters);
194
          this.searchPage.updateBaseUrlWithParameters(this.filters);
195
          var errorCodes:ErrorCodes = new ErrorCodes();
196
          this.searchUtils.status = errorCodes.DONE;
197
          if(this.searchUtils.totalResults == 0 ){
198
            this.searchUtils.status = errorCodes.NONE;
199
          }
200
          //this.searchPage.closeLoading();
201
          this.disableForms = false;
202

    
203
          if(this.searchUtils.status == errorCodes.DONE) {
204
            // Page out of limit!!!
205
            let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
206
            if(!(Number.isInteger(totalPages))) {
207
                totalPages = (parseInt(totalPages, 10) + 1);
208
            }
209
            if(totalPages < page) {
210
              this.searchUtils.totalResults = 0;
211
              this.searchUtils.status = errorCodes.OUT_OF_BOUND;
212
            }
213
          }
214
      },
215
      err => {
216
          console.log(err);
217
           //TODO check erros (service not available, bad request)
218
          // if( ){
219
          //   this.searchUtils.status = ErrorCodes.ERROR;
220
          // }
221
          var errorCodes:ErrorCodes = new ErrorCodes();
222
          this.searchUtils.status = errorCodes.ERROR;
223
          //this.searchPage.closeLoading();
224
          this.disableForms = false;
225
      }
226
  );
227
}
228

    
229

    
230

    
231
  private setFilters(){
232
    //TODO set filters from
233
  }
234

    
235
  public queryChanged($event) {
236
    var parameters = $event.value;
237
    //this.getResults(parameters, this.searchUtils.page, this.searchUtils.size, "searchPage");
238
    this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size);
239
  }
240
}
(5-5/15)