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 { ErrorCodes} from '../../utils/properties/errorCodes';
9
import {SearchFields, FieldDetails} 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
import{EnvProperties} from '../../utils/properties/env-properties';
14

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

    
19
    <search-page pageTitle="Search Research Data"
20
                  formPlaceholderText = "Search for Research Data"
21
                 type="research data" entityType="dataset" [(filters)] = "filters"
22
                 [(results)] = "results"   [(searchUtils)] = "searchUtils"
23
                 [baseUrl] = "baseUrl" (queryChange)="queryChanged($event)"
24
                 [csvParams]="csvParams" csvPath="datasets" advancedSearchLink="/search/advanced/datasets"
25
                 [disableForms]="disableForms"
26
                 [loadPaging]="loadPaging"
27
                 [oldTotalResults]="oldTotalResults"
28
                 searchFormClass="datasetsSearchForm"
29
                 [(openaireLink)]=openaireLink
30
                 [(advancedSearchParameters)]=advancedSearchParameters
31
                 [piwikSiteId]=piwikSiteId
32
                 [(connectCommunityId)]=connectCommunityId >
33
    </search-page>
34
    `
35
})
36

    
37
export class SearchDatasetsComponent {
38
  private errorCodes: ErrorCodes;
39
@Input() piwikSiteId = null;
40
  public results =[];
41
  public filters: Filter[] =[];
42
  // public totalResults:number  = 0 ;
43
  public baseUrl:string;
44
  @Input() openaireLink: string ;
45
  @Input() connectCommunityId: string;
46
  @Input() advancedSearchParameters ;
47
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
48
  private sub: any;
49
  private subResults: any;
50
  private searchFields:SearchFields = new SearchFields();
51
  public refineFields: string[] =  this.searchFields.RESULT_REFINE_FIELDS;
52
  public fieldIdsMap=this.searchFields.RESULT_FIELDS;
53
  private urlParams : Map<string, string>;
54
  private _location:Location;
55
  public csvParams: string;
56
  public disableForms: boolean = false;
57
  public loadPaging: boolean = true;
58
  public oldTotalResults: number = 0;
59
  pagingLimit = 0;
60
properties: EnvProperties;
61
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
62
  constructor (private route: ActivatedRoute, private _searchDatasetsService: SearchDatasetsService ) {
63

    
64
    this.errorCodes = new ErrorCodes();
65
    this.searchUtils.status = this.errorCodes.LOADING;
66
    this.searchUtils.page =1;
67

    
68
  }
69

    
70
  public ngOnInit() {
71
    this.route.data
72
      .subscribe((data: { envSpecific: EnvProperties }) => {
73
        this.properties = data.envSpecific;
74
        this.baseUrl = data.envSpecific.searchLinkToDatasets;
75
        this.pagingLimit = data.envSpecific.pagingLimit;
76

    
77
      });
78
    this.searchPage.refineFields = this.refineFields;
79
     this.searchPage.fieldIdsMap = this.fieldIdsMap;
80
     this.searchPage.type = "research data";
81
     var firstLoad =true;
82
    this.sub =  this.route.queryParams.subscribe(params => {
83
      if(params['page'] && this.searchUtils.page != params['page']) {
84
        this.loadPaging = false;
85
        this.oldTotalResults = this.searchUtils.totalResults;
86
      }
87

    
88
      this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
89
      var refine = true;
90
      if(this.searchUtils.page != ((params['page']=== undefined)?1:+params['page']) && this.filters && !firstLoad){
91
        refine = false;
92
      }
93
      firstLoad = false;
94
      this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
95
      this.searchPage.connectCommunityId = this.connectCommunityId;
96
      var queryParameters = this.searchPage.getQueryParametersFromUrl(params);
97
       this._getResults(queryParameters, refine, this.searchUtils.page, this.searchUtils.size);
98

    
99
    });
100
  }
101

    
102
  public ngOnDestroy() {
103
    if(this.sub){
104
      this.sub.unsubscribe();
105
    }
106
    if(this.subResults){
107
      this.subResults.unsubscribe();
108
    }
109
  }
110

    
111

    
112
public getResultsForEntity(entity:string, id:string, page: number, size: number){
113
  var parameters = "";
114

    
115
  if(entity == "project") {
116
    parameters = "projects/"+id;
117
  }
118

    
119
  if(parameters != "") {
120

    
121
      this._searchDatasetsService.searchDatasetsForEntity(parameters, page, size, this.properties).subscribe(
122
          data => {
123
              this.searchUtils.totalResults = data[0];
124
              console.info("search Research Data for "+entity+": [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
125
              this.results = data[1];
126

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

    
153
public getResultsForDataproviders(id:string, resultsFrom:string, page: number, size: number){
154
  var parameters;
155
  if(resultsFrom == "collectedFrom") {
156
      parameters = "datasets?fq=collectedfromdatasourceid exact "+'"'+id+'"';
157
  } else if(resultsFrom == "hostedBy") {
158
      parameters = "datasets?fq=resulthostingdatasourceid exact "+'"'+id+'"';
159
  }
160

    
161
  if(parameters != "") {
162

    
163
      this._searchDatasetsService.searchDatasetsForDataproviders(parameters, page, size, this.properties).subscribe(
164
          data => {
165
              this.searchUtils.totalResults = data[0];
166
              console.info("search Research Data for Dataproviders: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
167
              this.results = data[1];
168

    
169
              //var errorCodes:ErrorCodes = new ErrorCodes();
170
              this.searchUtils.status = this.errorCodes.DONE;
171
              if(this.searchUtils.totalResults == 0 ){
172
                this.searchUtils.status = this.errorCodes.NONE;
173
              }
174
          },
175
          err => {
176
              console.log(err);
177
               //TODO check erros (service not available, bad request)
178
              // if( ){
179
              //   this.searchUtils.status = ErrorCodes.ERROR;
180
              // }
181
              //var errorCodes:ErrorCodes = new ErrorCodes();
182
              //this.searchUtils.status = errorCodes.ERROR;
183
              if(err.status == '404') {
184
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
185
              } else if(err.status == '500') {
186
                this.searchUtils.status = this.errorCodes.ERROR;
187
              } else {
188
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
189
              }
190
          }
191
      );
192
  }
193
}
194

    
195
public getResults(keyword:string,refine:boolean, page: number, size: number){
196
  var parameters = "";
197
  if(keyword.length > 0){
198
    var DOIs:string[] = DOI.getDOIsFromString(keyword);
199
    var doisParams = "";
200
    for(var i =0 ;i < DOIs.length; i++){
201
      doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
202
    }
203
    if(doisParams.length > 0){
204
        parameters +=  "&"+doisParams;
205
    }else{
206
      parameters = "q=" + keyword;
207
    }
208
  }
209
  this._getResults(parameters,refine,page,size);
210
}
211
private _getResults(parameters:string,refine:boolean, page: number, size: number){
212
  if(page > this.pagingLimit) {
213
    size=0;
214
  }
215
  if(page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
216
    this.csvParams = parameters;
217

    
218
    this.searchUtils.status = this.errorCodes.LOADING;
219
    //this.searchPage.openLoading();
220
    this.disableForms = true;
221
    this.results = [];
222
    this.searchUtils.totalResults = 0;
223

    
224
    this.subResults = this._searchDatasetsService.searchDatasets(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, this.searchPage.getFields(), this.properties).subscribe(
225
      data => {
226
          this.searchUtils.totalResults = data[0];
227
          console.info("search Research Data: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
228
          this.results = data[1];
229
          if(refine){
230
            this.filters = data[2];
231
          }
232
          this.searchPage.checkSelectedFilters(this.filters);
233
          this.searchPage.updateBaseUrlWithParameters(this.filters);
234
          //var errorCodes:ErrorCodes = new ErrorCodes();
235
          this.searchUtils.status = this.errorCodes.DONE;
236
          if(this.searchUtils.totalResults == 0 ){
237
            this.searchUtils.status = this.errorCodes.NONE;
238
          }
239
          //this.searchPage.closeLoading();
240
          this.disableForms = false;
241

    
242
          if(this.searchUtils.status == this.errorCodes.DONE) {
243
            // Page out of limit!!!
244
            let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
245
            if(!(Number.isInteger(totalPages))) {
246
                totalPages = (parseInt(totalPages, 10) + 1);
247
            }
248
            if(totalPages < page) {
249
              this.searchUtils.totalResults = 0;
250
              this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
251
            }
252
          }
253
      },
254
      err => {
255
          console.log(err);
256
           //TODO check erros (service not available, bad request)
257
          // if( ){
258
          //   this.searchUtils.status = ErrorCodes.ERROR;
259
          // }
260
          //var errorCodes:ErrorCodes = new ErrorCodes();
261
          //this.searchUtils.status = errorCodes.ERROR;
262
          if(err.status == '404') {
263
            this.searchUtils.status = this.errorCodes.NOT_FOUND;
264
          } else if(err.status == '500') {
265
            this.searchUtils.status = this.errorCodes.ERROR;
266
          } else {
267
            this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
268
          }
269

    
270
          //this.searchPage.closeLoading();
271
          this.disableForms = false;
272
      }
273
    );
274
  }
275
}
276

    
277

    
278

    
279
  private setFilters(){
280
    //TODO set filters from
281
  }
282

    
283
  public queryChanged($event) {
284
    this.loadPaging = true;
285

    
286
    var parameters = $event.value;
287
    //this.getResults(parameters, this.searchUtils.page, this.searchUtils.size, "searchPage");
288
    this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size);
289
  }
290
}
(3-3/12)