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 {SearchSoftwareService} from '../../services/searchSoftware.service';
7
import {SearchResult}     from '../../utils/entities/searchResult';
8
import {OpenaireProperties, ErrorCodes} from '../../utils/properties/openaireProperties';
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

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

    
18
    <search-page pageTitle="Search Software"
19
                  formPlaceholderText = "Search for Software"
20
                 type="software" entityType="software" [(filters)] = "filters"
21
                 [(results)] = "results"   [(searchUtils)] = "searchUtils"
22
                 [baseUrl] = "baseUrl" (queryChange)="queryChanged($event)"
23
                 [csvParams]="csvParams" csvPath="software" advancedSearchLink="/search/advanced/software"
24
                 [disableForms]="disableForms"
25
                 [loadPaging]="loadPaging"
26
                 [oldTotalResults]="oldTotalResults"
27
                 searchFormClass="softwareSearchForm">
28
    </search-page>
29
    `
30
})
31

    
32
export class SearchSoftwareComponent {
33
  private errorCodes: ErrorCodes;
34

    
35
  public results =[];
36
  public filters: Filter[] =[];
37
  // public totalResults:number  = 0 ;
38
  public baseUrl:string;
39

    
40
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
41
  private sub: any;
42
  private subResults: any;
43
  private searchFields:SearchFields = new SearchFields();
44
  public refineFields: string[] =  this.searchFields.RESULT_REFINE_FIELDS;
45
  public fieldIdsMap=this.searchFields.RESULT_FIELDS;
46
  private urlParams : Map<string, string>;
47
  private _location:Location;
48
  public csvParams: string;
49
  public disableForms: boolean = false;
50
  public loadPaging: boolean = true;
51
  public oldTotalResults: number = 0;
52

    
53
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
54
  constructor (private route: ActivatedRoute, private _searchSoftwareService: SearchSoftwareService ) {
55

    
56
    this.errorCodes = new ErrorCodes();
57
    this.searchUtils.status = this.errorCodes.LOADING;
58
    this.searchUtils.page =1;
59
    this.baseUrl = OpenaireProperties.getLinkToSearchSoftware();
60

    
61
  }
62

    
63
  public ngOnInit() {
64
    this.searchPage.refineFields = this.refineFields;
65
     this.searchPage.fieldIdsMap = this.fieldIdsMap;
66
     this.searchPage.type = "software";
67
     var firstLoad =true;
68
    this.sub =  this.route.queryParams.subscribe(params => {
69
      if(params['page'] && this.searchUtils.page != params['page']) {
70
        this.loadPaging = false;
71
        this.oldTotalResults = this.searchUtils.totalResults;
72
      }
73

    
74
      this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
75
      var refine = true;
76
      if(this.searchUtils.page != ((params['page']=== undefined)?1:+params['page']) && this.filters && !firstLoad){
77
        refine = false;
78
      }
79
      firstLoad = false;
80
      this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
81

    
82
      var queryParameters = this.searchPage.getQueryParametersFromUrl(params);
83
       this._getResults(queryParameters, refine, this.searchUtils.page, this.searchUtils.size);
84

    
85
    });
86
  }
87

    
88
  public ngOnDestroy() {
89
    if(this.sub){
90
      this.sub.unsubscribe();
91
    }
92
    if(this.subResults){
93
      this.subResults.unsubscribe();
94
    }
95
  }
96

    
97

    
98
public getResultsForEntity(entity:string, id:string, page: number, size: number){
99
  var parameters = "";
100

    
101
  if(entity == "project") {
102
    parameters = "projects/"+id;
103
  }
104

    
105
  if(parameters != "") {
106

    
107
      this._searchSoftwareService.searchSoftwareForEntity(parameters, page, size).subscribe(
108
          data => {
109
              this.searchUtils.totalResults = data[0];
110
              console.info("search Software for "+entity+": [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
111
              this.results = data[1];
112

    
113
              //var errorCodes:ErrorCodes = new ErrorCodes();
114
              this.searchUtils.status = this.errorCodes.DONE;
115
              if(this.searchUtils.totalResults == 0 ){
116
                this.searchUtils.status = this.errorCodes.NONE;
117
              }
118
          },
119
          err => {
120
              console.log(err);
121
               //TODO check erros (service not available, bad request)
122
              // if( ){
123
              //   this.searchUtils.status = ErrorCodes.ERROR;
124
              // }
125
              //var errorCodes:ErrorCodes = new ErrorCodes();
126
              //this.searchUtils.status = errorCodes.ERROR;
127
              if(err.status == '404') {
128
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
129
              } else if(err.status == '500') {
130
                this.searchUtils.status = this.errorCodes.ERROR;
131
              } else {
132
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
133
              }
134
          }
135
      );
136
  }
137
}
138

    
139
public getResultsForDataproviders(id:string, resultsFrom:string, page: number, size: number){
140
  var parameters;
141
  if(resultsFrom == "collectedFrom") {
142
      parameters = "software?fq=collectedfromdatasourceid exact "+'"'+id+'"';
143
  } else if(resultsFrom == "hostedBy") {
144
      parameters = "software?fq=resulthostingdatasourceid exact "+'"'+id+'"';
145
  }
146

    
147
  if(parameters != "") {
148

    
149
      this._searchSoftwareService.searchSoftwareForDataproviders(parameters, page, size).subscribe(
150
          data => {
151
              this.searchUtils.totalResults = data[0];
152
              console.info("search Software for Dataproviders: [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
          }
177
      );
178
  }
179
}
180

    
181
public getResults(keyword:string,refine:boolean, page: number, size: number){
182
  var parameters = "";
183
  if(keyword.length > 0){
184
    var DOIs:string[] = DOI.getDOIsFromString(keyword);
185
    var doisParams = "";
186
    for(var i =0 ;i < DOIs.length; i++){
187
      doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
188
    }
189
    if(doisParams.length > 0){
190
        parameters +=  "&"+doisParams;
191
    }else{
192
      parameters = "q=" + keyword;
193
    }
194
  }
195
  this._getResults(parameters,refine,page,size);
196
}
197
private _getResults(parameters:string,refine:boolean, page: number, size: number){
198
    this.csvParams = parameters;
199

    
200
  // if(!refine && !this.searchPage){
201
  //     this.searchPage = new SearchPageComponent(this._location);
202
  // }
203
  //var errorCodes:ErrorCodes = new ErrorCodes();
204
  this.searchUtils.status = this.errorCodes.LOADING;
205
  //this.searchPage.openLoading();
206
  this.disableForms = true;
207
  this.results = [];
208
  this.searchUtils.totalResults = 0;
209

    
210
  this.subResults = this._searchSoftwareService.searchSoftware(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, this.searchPage.getFields()).subscribe(
211
      data => {
212
          this.searchUtils.totalResults = data[0];
213
          console.info("search Software: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
214
          this.results = data[1];
215
          if(refine){
216
            this.filters = data[2];
217
          }
218
          this.searchPage.checkSelectedFilters(this.filters);
219
          this.searchPage.updateBaseUrlWithParameters(this.filters);
220
          //var errorCodes:ErrorCodes = new ErrorCodes();
221
          this.searchUtils.status = this.errorCodes.DONE;
222
          if(this.searchUtils.totalResults == 0 ){
223
            this.searchUtils.status = this.errorCodes.NONE;
224
          }
225
          //this.searchPage.closeLoading();
226
          this.disableForms = false;
227

    
228
          if(this.searchUtils.status == this.errorCodes.DONE) {
229
            // Page out of limit!!!
230
            let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
231
            if(!(Number.isInteger(totalPages))) {
232
                totalPages = (parseInt(totalPages, 10) + 1);
233
            }
234
            if(totalPages < page) {
235
              this.searchUtils.totalResults = 0;
236
              this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
237
            }
238
          }
239
      },
240
      err => {
241
          console.log(err);
242
           //TODO check erros (service not available, bad request)
243
          // if( ){
244
          //   this.searchUtils.status = ErrorCodes.ERROR;
245
          // }
246
          //var errorCodes:ErrorCodes = new ErrorCodes();
247
          //this.searchUtils.status = errorCodes.ERROR;
248
          if(err.status == '404') {
249
            this.searchUtils.status = this.errorCodes.NOT_FOUND;
250
          } else if(err.status == '500') {
251
            this.searchUtils.status = this.errorCodes.ERROR;
252
          } else {
253
            this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
254
          }
255

    
256
          //this.searchPage.closeLoading();
257
          this.disableForms = false;
258
      }
259
  );
260
}
261

    
262

    
263

    
264
  private setFilters(){
265
    //TODO set filters from
266
  }
267

    
268
  public queryChanged($event) {
269
    this.loadPaging = true;
270

    
271
    var parameters = $event.value;
272
    //this.getResults(parameters, this.searchUtils.page, this.searchUtils.size, "searchPage");
273
    this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size);
274
  }
275
}
(11-11/12)