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 Datasets" type="dataset" [(filters)] = "filters"
19
                 [(results)] = "results"   [(searchUtils)] = "searchUtils"
20
                 [baseUrl] = "baseUrl" (queryChange)="queryChanged($event)"  >
21
    </search-page>
22

    
23
    `
24

    
25
})
26
export class SearchDatasetsComponent {
27
  public results =[];
28
  public filters: Filter[] =[];
29
  // public totalResults:number  = 0 ;
30
  public baseUrl:string;
31

    
32
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
33
  private sub: any;
34
  private subResults: any;
35
  private searchFields:SearchFields = new SearchFields();
36
  public refineFields: string[] =  this.searchFields.RESULT_REFINE_FIELDS;
37
  public fieldIdsMap=this.searchFields.RESULT_FIELDS;
38
  private urlParams : Map<string, string>;
39
  private _location:Location;
40

    
41

    
42
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
43
  constructor (private route: ActivatedRoute, private _searchDatasetsService: SearchDatasetsService ) {
44

    
45
    var errorCodes:ErrorCodes = new ErrorCodes();
46
    this.searchUtils.status =errorCodes.LOADING;
47
    this.baseUrl = OpenaireProperties.getLinkToSearchDatasets();
48

    
49
  }
50

    
51
  public ngOnInit() {
52
    this.searchPage.refineFields = this.refineFields;
53
     this.searchPage.fieldIdsMap = this.fieldIdsMap;
54

    
55
    this.sub =  this.route.queryParams.subscribe(params => {
56
      this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
57
      this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
58
      // this.getRefineResults();
59
      //this.getResults(this.searchUtils.keyword, this.searchUtils.page, this.searchUtils.size, "searchPage");
60
      var queryParameters = this.searchPage.getQueryParametersFromUrl(params);
61
       this._getResults(queryParameters, true, this.searchUtils.page, this.searchUtils.size);
62

    
63
    });
64
  }
65

    
66
  public ngOnDestroy() {
67
    if(this.sub){
68
      this.sub.unsubscribe();
69
    }
70
    if(this.subResults){
71
      this.subResults.unsubscribe();
72
    }
73
  }
74

    
75

    
76
public getResultsForEntity(entity:string, id:string, page: number, size: number){
77
  var parameters = "";
78

    
79
  if(entity == "project") {
80
    parameters = "projects/"+id;
81
  } else if(entity == "person") {
82
    parameters = "people/"+id;
83
  }
84

    
85
  if(parameters != "") {
86

    
87
      this._searchDatasetsService.searchDatasetsForEntity(parameters, page, size).subscribe(
88
          data => {
89
              this.searchUtils.totalResults = data[0];
90
              console.info("search Datasets for "+entity+": [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
91
              this.results = data[1];
92

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

    
112
public getResultsForDataproviders(id:string, resultsFrom:string, page: number, size: number){
113
  var parameters;
114
  if(resultsFrom == "collectedFrom") {
115
      parameters = "datasets?fq=collectedfromdatasourceid exact "+'"'+id+'"';
116
  } else if(resultsFrom == "hostedBy") {
117
      parameters = "datasets?fq=resulthostingdatasourceid exact "+'"'+id+'"';
118
  }
119

    
120
  if(parameters != "") {
121

    
122
      this._searchDatasetsService.searchDatasetsForDataproviders(parameters, page, size).subscribe(
123
          data => {
124
              this.searchUtils.totalResults = data[0];
125
              console.info("search Datasets for Dataproviders: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
126
              this.results = data[1];
127

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

    
147
public getResults(keyword:string,refine:boolean, page: number, size: number){
148
  var parameters = "";
149
  if(keyword.length > 0){
150
    var DOIs:string[] = DOI.getDOIsFromString(keyword);
151
    var doisParams = "";
152

    
153
    for(var i =0 ;i < DOIs.length; i++){
154
      doisParams+=(doisParams.length > 0?" or ":"")+'((pidclassid exact doi) and (pid exact "'+ DOIs[i]+'"))';
155
    }
156
    if(doisParams.length > 0){
157
      parameters += "q=("+doisParams+")"
158
    }else{
159
      parameters = "q="+'"' + keyword + '"';
160
    }
161
  }
162
  this._getResults(parameters,refine,page,size);
163
}
164
private _getResults(parameters:string,refine:boolean, page: number, size: number){
165
  if(!refine && !this.searchPage){
166
      this.searchPage = new SearchPageComponent(this._location);
167
  }
168
  this.subResults = this._searchDatasetsService.searchDatasets(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, this.searchPage.getFields()).subscribe(
169
      data => {
170
          this.searchUtils.totalResults = data[0];
171
          console.info("search Datasets: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
172
          this.results = data[1];
173
          this.filters = data[2];
174
          this.searchPage.checkSelectedFilters(this.filters);
175
          this.searchPage.updateBaseUrlWithParameters(this.filters);
176
          var errorCodes:ErrorCodes = new ErrorCodes();
177
          this.searchUtils.status = errorCodes.DONE;
178
          if(this.searchUtils.totalResults == 0 ){
179
            this.searchUtils.status = errorCodes.NONE;
180
          }
181
      },
182
      err => {
183
          console.log(err);
184
           //TODO check erros (service not available, bad request)
185
          // if( ){
186
          //   this.searchUtils.status = ErrorCodes.ERROR;
187
          // }
188
          var errorCodes:ErrorCodes = new ErrorCodes();
189
          this.searchUtils.status = errorCodes.ERROR;
190
      }
191
  );
192
}
193

    
194

    
195

    
196
  private setFilters(){
197
    //TODO set filters from
198
  }
199

    
200
  public queryChanged($event) {
201
    var parameters = $event.value;
202
    //this.getResults(parameters, this.searchUtils.page, this.searchUtils.size, "searchPage");
203
    this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size);
204
  }
205
}
(2-2/6)