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 {SearchOrpsService} from '../../services/searchOrps.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 {SearchCustomFilter, 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-orps',
18
    template: `
19

    
20
    <search-page pageTitle="Search Other Research Products"
21
                  formPlaceholderText = "Search for Other Research Products"
22
                 type="other research products" entityType="other" [(filters)] = "filters"
23
                 [(results)] = "results"   [(searchUtils)] = "searchUtils"
24
                 [baseUrl] = "baseUrl" (queryChange)="queryChanged($event)"
25
                 [csvParams]="csvParams" csvPath="other" advancedSearchLink="/search/advanced/other"
26
                 [disableForms]="disableForms"
27
                 [loadPaging]="loadPaging"
28
                 [oldTotalResults]="oldTotalResults"
29
                 [searchFormClass]="customFilter && customFilter.queryFieldName == 'communityId' ? 
30
                      'communityPanelBackground' : 'orpsSearchForm'"
31
                 [(openaireLink)]=openaireLink
32
                 [(advancedSearchParameters)]=advancedSearchParameters
33
                 [piwikSiteId]=piwikSiteId [hasPrefix]="hasPrefix"
34
                 [(sort)]=sort >
35
    </search-page>
36
    `
37
})
38

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

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

    
73
  }
74

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

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

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

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

    
113
    });
114
  }
115

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

    
125

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

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

    
133
  if(parameters != "") {
134

    
135
      this._searchOrpsService.searchOrpsForEntity(parameters, page, size, this.properties).subscribe(
136
          data => {
137
              this.searchUtils.totalResults = data[0];
138
              //console.info("search Other Research Products for "+entity+": [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
139
              this.results = data[1];
140

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

    
151
               //TODO check erros (service not available, bad request)
152
              /*if(err.status == '404') {
153
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
154
              } else if(err.status == '500') {
155
                this.searchUtils.status = this.errorCodes.ERROR;
156
              } else {
157
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
158
              }*/
159
          }
160
      );
161
  }
162
}
163

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

    
172
  if(parameters != "") {
173

    
174
      this._searchOrpsService.searchOrpsForDataproviders(parameters, page, size, this.properties).subscribe(
175
          data => {
176
              this.searchUtils.totalResults = data[0];
177
              //console.info("search Other Research Products for Dataproviders: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
178
              this.results = data[1];
179

    
180
              this.searchUtils.status = this.errorCodes.DONE;
181
              if(this.searchUtils.totalResults == 0 ){
182
                this.searchUtils.status = this.errorCodes.NONE;
183
              }
184
          },
185
          err => {
186
              //console.log(err);
187
              this.handleError("Error getting other research products for content provider ("+resultsFrom+") with id: "+id, err);
188
              this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
189

    
190
               //TODO check erros (service not available, bad request)
191
              /*if(err.status == '404') {
192
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
193
              } else if(err.status == '500') {
194
                this.searchUtils.status = this.errorCodes.ERROR;
195
              } else {
196
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
197
              }*/
198
          }
199
      );
200
  }
201
}
202

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

    
226
    this.searchUtils.status = this.errorCodes.LOADING;
227
    this.disableForms = true;
228
    this.results = [];
229
    this.searchUtils.totalResults = 0;
230

    
231
    this.subResults = this._searchOrpsService.searchOrps(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, sortBy, this.searchPage.getFields(), this.properties).subscribe(
232
      data => {
233
          this.searchUtils.totalResults = data[0];
234
          //console.info("search Other Research Products: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
235
          this.results = data[1];
236
          if(refine){
237
            this.filters = data[2];
238
          }
239
          this.searchPage.checkSelectedFilters(this.filters);
240
          this.searchPage.updateBaseUrlWithParameters(this.filters);
241

    
242
          this.searchUtils.status = this.errorCodes.DONE;
243
          if(this.searchUtils.totalResults == 0 ){
244
            this.searchUtils.status = this.errorCodes.NONE;
245
          }
246
          this.disableForms = false;
247

    
248
          if(this.searchUtils.status == this.errorCodes.DONE) {
249
            // Page out of limit!!!
250
            let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
251
            if(!(Number.isInteger(totalPages))) {
252
                totalPages = (parseInt(totalPages, 10) + 1);
253
            }
254
            if(totalPages < page) {
255
              this.searchUtils.totalResults = 0;
256
              this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
257
            }
258
          }
259
      },
260
      err => {
261
          //console.log(err);
262
          this.handleError("Error getting other research products", err);
263
          this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
264

    
265
           //TODO check erros (service not available, bad request)
266
          /*if(err.status == '404') {
267
            this.searchUtils.status = this.errorCodes.NOT_FOUND;
268
          } else if(err.status == '500') {
269
            this.searchUtils.status = this.errorCodes.ERROR;
270
          } else {
271
            this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
272
          }*/
273

    
274
          this.disableForms = false;
275
      }
276
    );
277
  }
278
}
279

    
280

    
281

    
282
  private setFilters(){
283
    //TODO set filters from
284
  }
285

    
286
  public queryChanged($event) {
287
    this.loadPaging = true;
288

    
289
    var parameters = $event.value;
290
    //this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size, this.searchUtils.sortBy);
291
  }
292

    
293
  private handleError(message: string, error) {
294
    console.error("Other Research Products simple Search Page: "+message, error);
295
  }
296
}
(7-7/14)