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 {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-software',
17
    template: `
18

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

    
38
export class SearchSoftwareComponent {
39
  private errorCodes: ErrorCodes;
40
  @Input() openaireLink: string ;
41
  @Input() connectCommunityId: string;
42
  @Input() advancedSearchParameters ;
43
  @Input() piwikSiteId = null;
44
  public results =[];
45
  public filters: Filter[] =[];
46
  // public totalResults:number  = 0 ;
47
  public baseUrl:string;
48

    
49
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
50
  private sub: any;
51
  private subResults: any;
52
  private searchFields:SearchFields = new SearchFields();
53
  public refineFields: string[] =  this.searchFields.RESULT_REFINE_FIELDS;
54
  public fieldIdsMap=this.searchFields.RESULT_FIELDS;
55
  private urlParams : Map<string, string>;
56
  private _location:Location;
57
  public csvParams: string;
58
  public disableForms: boolean = false;
59
  public loadPaging: boolean = true;
60
  public oldTotalResults: number = 0;
61
  pagingLimit = 0;
62
  public sort: boolean = true;
63
properties: EnvProperties;
64
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
65
  constructor (private route: ActivatedRoute, private _searchSoftwareService: SearchSoftwareService ) {
66

    
67
    this.errorCodes = new ErrorCodes();
68
    this.searchUtils.status = this.errorCodes.LOADING;
69
    this.searchUtils.page =1;
70

    
71
  }
72

    
73
  public ngOnInit() {
74
    this.route.data
75
      .subscribe((data: { envSpecific: EnvProperties }) => {
76
        this.properties = data.envSpecific;
77
        this.baseUrl = data.envSpecific.searchLinkToSoftware;
78
        this.pagingLimit = data.envSpecific.pagingLimit;
79

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

    
91
      this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
92
      var refine = true;
93
      if(this.searchUtils.page != ((params['page']=== undefined)?1:+params['page']) && this.filters && !firstLoad){
94
        refine = false;
95
      }
96
      firstLoad = false;
97
      this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
98
      this.searchUtils.size = (params['size']=== undefined)?10:+params['size'];
99
      if(this.searchUtils.size != 5 && this.searchUtils.size != 10 && this.searchUtils.size != 20 && this.searchUtils.size != 50) {
100
        this.searchUtils.size = 10;
101
      }
102
      this.searchUtils.sortBy = (params['sortBy'])?params['sortBy']:'';
103
      if(this.searchUtils.sortBy && this.searchUtils.sortBy != "resultdateofacceptance,descending" && this.searchUtils.sortBy != "resultdateofacceptance,ascending") {
104
        this.searchUtils.sortBy = "";
105
      }
106

    
107
      this.searchPage.connectCommunityId = this.connectCommunityId;
108
      var queryParameters = this.searchPage.getQueryParametersFromUrl(params);
109
       this._getResults(queryParameters, refine, this.searchUtils.page, this.searchUtils.size, this.searchUtils.sortBy);
110

    
111
    });
112
  }
113

    
114
  public ngOnDestroy() {
115
    if(this.sub){
116
      this.sub.unsubscribe();
117
    }
118
    if(this.subResults){
119
      this.subResults.unsubscribe();
120
    }
121
  }
122

    
123

    
124
public getResultsForEntity(entity:string, id:string, page: number, size: number){
125
  var parameters = "";
126

    
127
  if(entity == "project") {
128
    parameters = "projects/"+id;
129
  }
130

    
131
  if(parameters != "") {
132

    
133
      this._searchSoftwareService.searchSoftwareForEntity(parameters, page, size, this.properties).subscribe(
134
          data => {
135
              this.searchUtils.totalResults = data[0];
136
              console.info("search Software for "+entity+": [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
137
              this.results = data[1];
138

    
139
              //var errorCodes:ErrorCodes = new ErrorCodes();
140
              this.searchUtils.status = this.errorCodes.DONE;
141
              if(this.searchUtils.totalResults == 0 ){
142
                this.searchUtils.status = this.errorCodes.NONE;
143
              }
144
          },
145
          err => {
146
              console.log(err);
147
               //TODO check erros (service not available, bad request)
148
              // if( ){
149
              //   this.searchUtils.status = ErrorCodes.ERROR;
150
              // }
151
              //var errorCodes:ErrorCodes = new ErrorCodes();
152
              //this.searchUtils.status = errorCodes.ERROR;
153
              if(err.status == '404') {
154
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
155
              } else if(err.status == '500') {
156
                this.searchUtils.status = this.errorCodes.ERROR;
157
              } else {
158
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
159
              }
160
          }
161
      );
162
  }
163
}
164

    
165
public getResultsForDataproviders(id:string, resultsFrom:string, page: number, size: number){
166
  var parameters;
167
  if(resultsFrom == "collectedFrom") {
168
      parameters = "software?fq=collectedfromdatasourceid exact "+'"'+id+'"';
169
  } else if(resultsFrom == "hostedBy") {
170
      parameters = "software?fq=resulthostingdatasourceid exact "+'"'+id+'"';
171
  }
172

    
173
  if(parameters != "") {
174

    
175
      this._searchSoftwareService.searchSoftwareForDataproviders(parameters, page, size, this.properties).subscribe(
176
          data => {
177
              this.searchUtils.totalResults = data[0];
178
              console.info("search Software for Dataproviders: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
179
              this.results = data[1];
180

    
181
              //var errorCodes:ErrorCodes = new ErrorCodes();
182
              this.searchUtils.status = this.errorCodes.DONE;
183
              if(this.searchUtils.totalResults == 0 ){
184
                this.searchUtils.status = this.errorCodes.NONE;
185
              }
186
          },
187
          err => {
188
              console.log(err);
189
               //TODO check erros (service not available, bad request)
190
              // if( ){
191
              //   this.searchUtils.status = ErrorCodes.ERROR;
192
              // }
193
              //var errorCodes:ErrorCodes = new ErrorCodes();
194
              //this.searchUtils.status = errorCodes.ERROR;
195
              if(err.status == '404') {
196
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
197
              } else if(err.status == '500') {
198
                this.searchUtils.status = this.errorCodes.ERROR;
199
              } else {
200
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
201
              }
202
          }
203
      );
204
  }
205
}
206

    
207
public getResults(keyword:string,refine:boolean, page: number, size: number, sortBy: string){
208
  var parameters = "";
209
  if(keyword.length > 0){
210
    var DOIs:string[] = DOI.getDOIsFromString(keyword);
211
    var doisParams = "";
212
    for(var i =0 ;i < DOIs.length; i++){
213
      doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
214
    }
215
    if(doisParams.length > 0){
216
        parameters +=  "&"+doisParams;
217
    }else{
218
      parameters = "q=" + keyword;
219
    }
220
  }
221
  this._getResults(parameters,refine,page,size,sortBy);
222
}
223
private _getResults(parameters:string,refine:boolean, page: number, size: number, sortBy: string){
224
  if(page > this.pagingLimit) {
225
    size=0;
226
  }
227
  if(page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
228
    this.csvParams = parameters;
229

    
230
    this.searchUtils.status = this.errorCodes.LOADING;
231
    //this.searchPage.openLoading();
232
    this.disableForms = true;
233
    this.results = [];
234
    this.searchUtils.totalResults = 0;
235

    
236
    this.subResults = this._searchSoftwareService.searchSoftware(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, sortBy, this.searchPage.getFields(), this.properties).subscribe(
237
        data => {
238
            this.searchUtils.totalResults = data[0];
239
            console.info("search Software: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
240
            this.results = data[1];
241
            if(refine){
242
              this.filters = data[2];
243
            }
244
            this.searchPage.checkSelectedFilters(this.filters);
245
            this.searchPage.updateBaseUrlWithParameters(this.filters);
246
            //var errorCodes:ErrorCodes = new ErrorCodes();
247
            this.searchUtils.status = this.errorCodes.DONE;
248
            if(this.searchUtils.totalResults == 0 ){
249
              this.searchUtils.status = this.errorCodes.NONE;
250
            }
251
            //this.searchPage.closeLoading();
252
            this.disableForms = false;
253

    
254
            if(this.searchUtils.status == this.errorCodes.DONE) {
255
              // Page out of limit!!!
256
              let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
257
              if(!(Number.isInteger(totalPages))) {
258
                  totalPages = (parseInt(totalPages, 10) + 1);
259
              }
260
              if(totalPages < page) {
261
                this.searchUtils.totalResults = 0;
262
                this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
263
              }
264
            }
265
        },
266
        err => {
267
            console.log(err);
268
             //TODO check erros (service not available, bad request)
269
            // if( ){
270
            //   this.searchUtils.status = ErrorCodes.ERROR;
271
            // }
272
            //var errorCodes:ErrorCodes = new ErrorCodes();
273
            //this.searchUtils.status = errorCodes.ERROR;
274
            if(err.status == '404') {
275
              this.searchUtils.status = this.errorCodes.NOT_FOUND;
276
            } else if(err.status == '500') {
277
              this.searchUtils.status = this.errorCodes.ERROR;
278
            } else {
279
              this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
280
            }
281

    
282
            //this.searchPage.closeLoading();
283
            this.disableForms = false;
284
        }
285
    );
286
  }
287
}
288

    
289

    
290

    
291
  private setFilters(){
292
    //TODO set filters from
293
  }
294

    
295
  public queryChanged($event) {
296
    this.loadPaging = true;
297

    
298
    var parameters = $event.value;
299
    //this.getResults(parameters, this.searchUtils.page, this.searchUtils.size, "searchPage");
300
    this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size, this.searchUtils.sortBy);
301
  }
302
}
(13-13/14)