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 {ErrorMessagesComponent}    from '../../utils/errorMessages.component';
10
import {SearchFields, FieldDetails} from '../../utils/properties/searchFields';
11
import {SearchPageComponent } from '../searchUtils/searchPage.component';
12
import {SearchUtilsClass } from '../searchUtils/searchUtils.class';
13
import {DOI} from '../../utils/string-utils.class';
14
import{EnvProperties} from '../../utils/properties/env-properties';
15

    
16
@Component({
17
    selector: 'search-software',
18
    template: `
19

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

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

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

    
69
    this.errorCodes = new ErrorCodes();
70
    this.errorMessages = new ErrorMessagesComponent();
71
    this.searchUtils.status = this.errorCodes.LOADING;
72
    this.searchUtils.page =1;
73

    
74
  }
75

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

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

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

    
110
      this.searchPage.connectCommunityId = this.connectCommunityId;
111
      var queryParameters = this.searchPage.getQueryParametersFromUrl(params);
112
       this._getResults(queryParameters, refine, this.searchUtils.page, this.searchUtils.size, this.searchUtils.sortBy);
113

    
114
    });
115
  }
116

    
117
  public ngOnDestroy() {
118
    if(this.sub){
119
      this.sub.unsubscribe();
120
    }
121
    if(this.subResults){
122
      this.subResults.unsubscribe();
123
    }
124
  }
125

    
126

    
127
public getResultsForEntity(entity:string, id:string, page: number, size: number){
128
  var parameters = "";
129

    
130
  if(entity == "project") {
131
    parameters = "projects/"+id;
132
  }
133

    
134
  if(parameters != "") {
135

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

    
142
              //var errorCodes:ErrorCodes = new ErrorCodes();
143
              this.searchUtils.status = this.errorCodes.DONE;
144
              if(this.searchUtils.totalResults == 0 ){
145
                this.searchUtils.status = this.errorCodes.NONE;
146
              }
147
          },
148
          err => {
149
              //console.log(err);
150
              this.handleError("Error getting software for "+entity+" with id: "+id, err);
151
              this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
152

    
153
               //TODO check erros (service not available, bad request)
154
              // if( ){
155
              //   this.searchUtils.status = ErrorCodes.ERROR;
156
              // }
157
              //var errorCodes:ErrorCodes = new ErrorCodes();
158
              //this.searchUtils.status = errorCodes.ERROR;
159
              /*if(err.status == '404') {
160
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
161
              } else if(err.status == '500') {
162
                this.searchUtils.status = this.errorCodes.ERROR;
163
              } else {
164
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
165
              }*/
166
          }
167
      );
168
  }
169
}
170

    
171
public getResultsForDataproviders(id:string, resultsFrom:string, page: number, size: number){
172
  var parameters;
173
  if(resultsFrom == "collectedFrom") {
174
      parameters = "software?fq=collectedfromdatasourceid exact "+'"'+id+'"';
175
  } else if(resultsFrom == "hostedBy") {
176
      parameters = "software?fq=resulthostingdatasourceid exact "+'"'+id+'"';
177
  }
178

    
179
  if(parameters != "") {
180

    
181
      this._searchSoftwareService.searchSoftwareForDataproviders(parameters, page, size, this.properties).subscribe(
182
          data => {
183
              this.searchUtils.totalResults = data[0];
184
              //console.info("search Software for Dataproviders: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
185
              this.results = data[1];
186

    
187
              //var errorCodes:ErrorCodes = new ErrorCodes();
188
              this.searchUtils.status = this.errorCodes.DONE;
189
              if(this.searchUtils.totalResults == 0 ){
190
                this.searchUtils.status = this.errorCodes.NONE;
191
              }
192
          },
193
          err => {
194
              //console.log(err);
195
              this.handleError("Error getting software for content provider ("+resultsFrom+") with id: "+id, err);
196
              this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
197

    
198
               //TODO check erros (service not available, bad request)
199
              // if( ){
200
              //   this.searchUtils.status = ErrorCodes.ERROR;
201
              // }
202
              //var errorCodes:ErrorCodes = new ErrorCodes();
203
              //this.searchUtils.status = errorCodes.ERROR;
204
              /*if(err.status == '404') {
205
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
206
              } else if(err.status == '500') {
207
                this.searchUtils.status = this.errorCodes.ERROR;
208
              } else {
209
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
210
              }*/
211
          }
212
      );
213
  }
214
}
215

    
216
public getResults(keyword:string,refine:boolean, page: number, size: number, sortBy: string){
217
  var parameters = "";
218
  if(keyword.length > 0){
219
    var DOIs:string[] = DOI.getDOIsFromString(keyword);
220
    var doisParams = "";
221
    for(var i =0 ;i < DOIs.length; i++){
222
      doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"';
223
    }
224
    if(doisParams.length > 0){
225
        parameters +=  "&"+doisParams;
226
    }else{
227
      parameters = "q=" + keyword;
228
    }
229
  }
230
  this._getResults(parameters,refine,page,size,sortBy);
231
}
232
private _getResults(parameters:string,refine:boolean, page: number, size: number, sortBy: string){
233
  if(page > this.pagingLimit) {
234
    size=0;
235
  }
236
  if(page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
237
    this.csvParams = parameters;
238

    
239
    this.searchUtils.status = this.errorCodes.LOADING;
240
    //this.searchPage.openLoading();
241
    this.disableForms = true;
242
    this.results = [];
243
    this.searchUtils.totalResults = 0;
244

    
245
    this.subResults = this._searchSoftwareService.searchSoftware(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, sortBy, this.searchPage.getFields(), this.properties).subscribe(
246
        data => {
247
            this.searchUtils.totalResults = data[0];
248
            //console.info("search Software: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
249
            this.results = data[1];
250
            if(refine){
251
              this.filters = data[2];
252
            }
253
            this.searchPage.checkSelectedFilters(this.filters);
254
            this.searchPage.updateBaseUrlWithParameters(this.filters);
255
            //var errorCodes:ErrorCodes = new ErrorCodes();
256
            this.searchUtils.status = this.errorCodes.DONE;
257
            if(this.searchUtils.totalResults == 0 ){
258
              this.searchUtils.status = this.errorCodes.NONE;
259
            }
260
            //this.searchPage.closeLoading();
261
            this.disableForms = false;
262

    
263
            if(this.searchUtils.status == this.errorCodes.DONE) {
264
              // Page out of limit!!!
265
              let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
266
              if(!(Number.isInteger(totalPages))) {
267
                  totalPages = (parseInt(totalPages, 10) + 1);
268
              }
269
              if(totalPages < page) {
270
                this.searchUtils.totalResults = 0;
271
                this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
272
              }
273
            }
274
        },
275
        err => {
276
            //console.log(err);
277
            this.handleError("Error getting software", err);
278
            this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
279

    
280
             //TODO check erros (service not available, bad request)
281
            // if( ){
282
            //   this.searchUtils.status = ErrorCodes.ERROR;
283
            // }
284
            //var errorCodes:ErrorCodes = new ErrorCodes();
285
            //this.searchUtils.status = errorCodes.ERROR;
286
            /*if(err.status == '404') {
287
              this.searchUtils.status = this.errorCodes.NOT_FOUND;
288
            } else if(err.status == '500') {
289
              this.searchUtils.status = this.errorCodes.ERROR;
290
            } else {
291
              this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
292
            }*/
293

    
294
            //this.searchPage.closeLoading();
295
            this.disableForms = false;
296
        }
297
    );
298
  }
299
}
300

    
301

    
302

    
303
  private setFilters(){
304
    //TODO set filters from
305
  }
306

    
307
  public queryChanged($event) {
308
    this.loadPaging = true;
309

    
310
    var parameters = $event.value;
311
    //this.getResults(parameters, this.searchUtils.page, this.searchUtils.size, "searchPage");
312
    this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size, this.searchUtils.sortBy);
313
  }
314

    
315
  private handleError(message: string, error) {
316
    console.error("Software simple Search Page: "+message, error);
317
  }
318
}
(13-13/14)