Project

General

Profile

1
import {Component, EventEmitter, Input, Output, ViewChild} from '@angular/core';
2
import {ActivatedRoute} from '@angular/router';
3
import {AdvancedField, Filter} from './searchUtils/searchHelperClasses.class';
4
import {SearchDataprovidersService} from '../services/searchDataproviders.service';
5
import {ErrorCodes} from '../utils/properties/errorCodes';
6
import {ErrorMessagesComponent} from '../utils/errorMessages.component';
7
import {SearchFields} from '../utils/properties/searchFields';
8
import {SearchCustomFilter, SearchUtilsClass} from './searchUtils/searchUtils.class';
9

    
10
import {EnvProperties} from '../utils/properties/env-properties';
11
import {NewSearchPageComponent} from "./searchUtils/newSearchPage.component";
12
import {DatasourcesHelperClass} from "./searchUtils/datasourcesHelper.class";
13
import {properties} from "../../../environments/environment";
14

    
15

    
16
@Component({
17
    selector: 'search-dataproviders',
18
    template: `
19
 
20
    <new-search-page
21
      pageTitle="{{(simpleView?'':'Advanced ')}} Find research {{ pageTitle }}"
22
      entityType="dataprovider"
23
      type="content providers"
24
      [results]="results"
25
      [searchUtils]="searchUtils"
26
      [sortedByChanged]="searchUtils.sortBy" [resultsPerPageChanged]="searchUtils.size"
27
      [fieldIds]="fieldIds" [fieldIdsMap]="fieldIdsMap" [selectedFields]="selectedFields"
28
      [csvParams]="csvParams" csvPath="datasources"
29
      [simpleSearchLink]="simpleSearchLink" [advancedSearchLink]="advancedSearchLink"
30
      [disableForms]="disableForms"
31
      [disableRefineForms]="disableRefineForms"
32
      [loadPaging]="loadPaging"
33
      [oldTotalResults]="oldTotalResults"
34
      [openaireLink]=openaireLink
35
      [includeOnlyResultsAndFilter]="includeOnlyResultsAndFilter"
36
      [piwikSiteId]=piwikSiteId [hasPrefix]="hasPrefix"
37
      [searchFormClass]="type!='deposit'?'datasourcesSearchForm':''"
38
      [entitiesSelection]="type=='all'"  [showSwitchSearchLink]="showSwitchSearchLink"  
39
      [filters]="filters"
40
      [simpleView]="simpleView" formPlaceholderText="Search by name, description, subject..." 
41
      [showResultCount]="(type=='all' || type == 'deposit')" [showIndexInfo]="type!='deposit'"
42
      [tableViewLink]="tableViewLink"
43
      [sort]="false" [showBreadcrumb]="showBreadcrumb" [basicMetaDescription]="metaDescription">
44

    
45
    </new-search-page>
46
<!--    [filters]="filters"-->
47

    
48
    `
49
 })
50

    
51
export class SearchDataProvidersComponent {
52
  private errorCodes: ErrorCodes;
53
  private errorMessages: ErrorMessagesComponent;
54
  @Input() piwikSiteId = null;
55
  @Input() customFilter:SearchCustomFilter= null;
56
  @Input() tableViewLink;
57
  public results =[];
58
  public filters =[];
59
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
60
  public searchFields:SearchFields = new SearchFields();
61

    
62
  public fieldIds:  string[] = this.searchFields.DATASOURCE_ADVANCED_FIELDS;
63
  public fieldIdsMap= this.searchFields.DATASOURCE_FIELDS;
64
  public selectedFields:AdvancedField[] =  [];
65

    
66
  public resourcesQuery = "(oaftype exact datasource)";
67
  public csvParams: string;
68
  public disableForms: boolean = false;
69
  public disableRefineForms: boolean = false;
70
  public loadPaging: boolean = true;
71
  public oldTotalResults: number = 0;
72
  public pagingLimit: number = 0;
73
  public isPiwikEnabled;
74
  properties:EnvProperties;
75
  @Input() type: "all" | "registries" | "journals" | "compatible" | "deposit" = "all";
76
  public refineFields: string[];
77
  pageTitle;
78
  @ViewChild(NewSearchPageComponent) searchPage: NewSearchPageComponent;
79
  @Input() simpleView: boolean = true;
80
  @Input() simpleSearchLink: string = "";
81
  advancedSearchLink: string = "";
82
  @Input() hasPrefix: boolean = true;
83
  @Input() openaireLink: string;
84
  @Input() includeOnlyResultsAndFilter: boolean = false;
85
  @Output() searchPageUpdates = new EventEmitter();
86
  @Input() showSwitchSearchLink:boolean;
87
  @Input() showBreadcrumb: boolean = false;
88
  subs: any[] = [];
89
  searchResultsSub: any;
90
  searchFiltersSub: any;
91
  metaDescription = [];
92

    
93
  constructor (private route: ActivatedRoute, private _searchDataProvidersService: SearchDataprovidersService ) {
94
    this.results =[];
95
    this.errorCodes = new ErrorCodes();
96
    this.errorMessages = new ErrorMessagesComponent();
97
    this.searchUtils.status = this.errorCodes.LOADING;
98
  }
99
  ngOnInit() {
100
    this.refineFields = DatasourcesHelperClass.getrefineFields(this.type);
101
    this.pageTitle = DatasourcesHelperClass.getTitle(this.type);
102
    this.metaDescription = DatasourcesHelperClass.getDescription(this.type)
103
    if(this.showSwitchSearchLink == null){
104
      this.showSwitchSearchLink = (this.type == "all");
105
    }
106
    this.properties= properties;
107
    if (!this.simpleSearchLink) {
108
      this.simpleSearchLink = this.properties.searchLinkToDataProviders;
109
    }        this.advancedSearchLink = this.properties.searchLinkToAdvancedDataProviders;
110
    this.searchUtils.baseUrl = (this.simpleView)?this.simpleSearchLink:this.advancedSearchLink;
111
     this.pagingLimit = properties.pagingLimit;
112
     this.isPiwikEnabled = properties.enablePiwikTrack;
113

    
114
    let firstLoad = true;
115
    this.subs.push(this.route.queryParams.subscribe(params => {
116
     this.loadPaging = true;
117
     if(params['page'] && this.searchUtils.page != params['page']) {
118
       this.loadPaging = false;
119
       this.oldTotalResults = this.searchUtils.totalResults;
120
     }
121
     var refine = true;
122
      if(this.searchPage.searchUtils.refineStatus == this.errorCodes.DONE) {
123

    
124
        if (params['page'] != undefined && this.filters && !firstLoad && this.searchUtils.page != +params['page']) {
125
          refine = false;
126
        }
127

    
128
        if (params['size'] != undefined && this.filters && !firstLoad && this.searchUtils.size != params['size']) {
129
          refine = false;
130
        }
131
      }
132

    
133
      let page = (params['page']=== undefined)?0:+params['page'];
134
      this.searchUtils.page = ( page < 1 ) ? 1 : page;
135

    
136
      this.searchUtils.size = (params['size']=== undefined)?10:+params['size'];
137
      if(this.searchUtils.size != 5 && this.searchUtils.size != 10 && this.searchUtils.size != 20 && this.searchUtils.size != 50) {
138
        this.searchUtils.size = 10;
139
      }
140

    
141
      this.searchPage.fieldIds = this.fieldIds;
142
      this.selectedFields =[];
143
      if(this.type == "deposit"){
144
        this.searchPage.keywordFields = this.searchFields.DEPOSIT_DATASOURCE_KEYWORD_FIELDS;
145
        this.searchPage.usedBy = "deposit";
146
      }
147
      // console.log(this.refineFields)
148
      this.searchPage.prepareSearchPage(this.fieldIds, this.selectedFields, this.refineFields, [], this.fieldIdsMap,this.customFilter,params, "dataprovider");
149
      if(refine && (this.type == "all" || this.type == "deposit")) {
150
        this._getFilters(this.searchPage.getSearchAPIQueryForAdvancedSearhFields(), this.searchUtils.page, 0, true, this.searchPage.getSearchAPIQueryForRefineFields(params, firstLoad));
151
      } else if(refine) { // static filters
152
        this.searchUtils.refineStatus = this.errorCodes.LOADING;
153
        this.filters = DatasourcesHelperClass.createFilters(this.type);
154
      } else {
155
        this.searchUtils.refineStatus = this.errorCodes.DONE;
156
      }
157
      this.getResults(this.searchPage.getSearchAPIQueryForAdvancedSearhFields(),  this.searchUtils.page, this.searchUtils.size, refine, this.searchPage.getSearchAPIQueryForRefineFields(params, firstLoad));
158
      firstLoad = false;
159
    }));
160
  }
161
  ngOnDestroy() {
162
    if(this.searchResultsSub) {
163
      this.searchResultsSub.unsubscribe();
164
    }
165
    if(this.searchFiltersSub) {
166
      this.searchFiltersSub.unsubscribe();
167
    }
168
    for (let sub of this.subs) {
169
      sub.unsubscribe();
170
    }
171
  }
172

    
173
  public _getFilters(parameters:string, page: number, size: number, refine: boolean, refineFieldsFilterQuery = null){
174
    if (page <= this.pagingLimit || this.searchUtils.refineStatus == this.errorCodes.LOADING) {
175
      this.searchUtils.refineStatus = this.errorCodes.LOADING;
176
      this.disableRefineForms = true;
177
      this.searchPageUpdates.emit({disableForms: this.disableForms, disableRefineForms: this.disableRefineForms, searchUtils: this.searchUtils});
178

    
179
      let datasourceQueryPrefix = DatasourcesHelperClass.getQueryPrefix(this.type);
180
      this.searchFiltersSub = this._searchDataProvidersService.advancedSearchDataproviders( datasourceQueryPrefix +(datasourceQueryPrefix.length > 0 && parameters.length > 0 ?' and (':'') + parameters +(datasourceQueryPrefix.length > 0 && parameters.length > 0 ?' ) ':''),  page, size, this.properties, (refine && (this.type=="all" || this.type == "deposit")) ? this.searchPage.getRefineFieldsQuery() : null, this.searchPage.getFields(), refineFieldsFilterQuery, (this.type == "deposit"))
181
      //.switchMap(
182
        .subscribe(
183
          data => {
184
            let totalResults = data[0];
185
            let filters = data[2];
186
            this.filtersReturned(refine, filters, totalResults, page);
187
          },
188
          err => {
189
            this.filters = this.searchPage.prepareFiltersToShow([], 0);
190

    
191
            this.handleError("Error getting refine filters for content providers: ", err);
192
            this.searchUtils.refineStatus = this.errorMessages.getErrorCode(err.status);
193

    
194
            this.disableRefineForms = false;
195
            this.searchPageUpdates.emit({disableForms: this.disableForms, disableRefineForms: this.disableRefineForms, searchUtils: this.searchUtils})
196
          }
197
        );
198
    }
199
  }
200

    
201
  public filtersReturned(refine: boolean, filters: Filter[], totalResults, page: number) {
202
    if (refine) {
203
      this.filters = this.searchPage.prepareFiltersToShow(filters, totalResults);
204
    }
205

    
206
    this.searchUtils.refineStatus = this.errorCodes.DONE;
207
    if(totalResults == 0) {
208
      this.searchUtils.refineStatus = this.errorCodes.NONE;
209
    }
210

    
211
    if (this.searchUtils.refineStatus == this.errorCodes.DONE) {
212
      // Page out of limit!!!
213
      let totalPages: any = totalResults / (this.searchUtils.size);
214
      if (!(Number.isInteger(totalPages))) {
215
        totalPages = (parseInt(totalPages, 10) + 1);
216
      }
217
      if (totalPages < page) {
218
        this.searchUtils.refineStatus = this.errorCodes.OUT_OF_BOUND;
219
      }
220
    }
221

    
222
    if(this.searchUtils.refineStatus != this.errorCodes.DONE && this.searchUtils.status != this.searchUtils.refineStatus) {
223
      if (this.searchResultsSub) {
224
        this.searchResultsSub.unsubscribe();
225
      }
226
      this.resultsReturned(refine, [], totalResults, page);
227
    }
228

    
229
    this.disableRefineForms = false;
230
    this.searchPageUpdates.emit({disableForms: this.disableForms, disableRefineForms: this.disableRefineForms, searchUtils: this.searchUtils})
231

    
232
  }
233

    
234
  public getResults(parameters:string, page: number, size: number, refine: boolean, refineFieldsFilterQuery = null){
235
    if(page > this.pagingLimit ) {
236
      size=0;
237
    }
238
    if(page <= this.pagingLimit  || this.searchUtils.status == this.errorCodes.LOADING) {
239
      // if(parameters!= null && parameters != ''  ) {
240
      //   this.csvParams ="&fq=( "+this.resourcesQuery + "and (" + parameters + "))";
241
      // }else{
242
      //   this.csvParams ="&fq="+this.resourcesQuery;
243
      // }
244
      // this.csvParams += (refineFieldsFilterQuery?refineFieldsFilterQuery:'');
245

    
246
      this.csvParams = (parameters ? ("&fq=("+parameters) : "") + (parameters ? ")" : "");
247
      this.csvParams += (refineFieldsFilterQuery ? refineFieldsFilterQuery : "");
248

    
249
      //var errorCodes:ErrorCodes = new ErrorCodes();
250
      this.searchUtils.status = this.errorCodes.LOADING;
251
      //this.searchPage.openLoading();
252
      this.disableForms = true;
253
      this.searchPageUpdates.emit({disableForms: this.disableForms, disableRefineForms: this.disableRefineForms, searchUtils: this.searchUtils});
254

    
255
      this.results = [];
256
      this.searchUtils.totalResults = 0;
257

    
258
      //console.info("Advanced Search for Content Providers: Execute search query "+parameters);
259
      let datasourceQueryPrefix = DatasourcesHelperClass.getQueryPrefix(this.type);
260
      //this._searchDataProvidersService.advancedSearchDataproviders( datasourceQueryPrefix +(datasourceQueryPrefix.length > 0 && parameters.length > 0 ?' and (':'') + parameters +(datasourceQueryPrefix.length > 0 && parameters.length > 0 ?' ) ':''),  page, size, this.properties, (refine && (this.type=="all" || this.type == "deposit")) ? this.searchPage.getRefineFieldsQuery() : null, this.searchPage.getFields(), refineFieldsFilterQuery, (this.type == "deposit")).subscribe(
261
      this.searchResultsSub = this._searchDataProvidersService.advancedSearchDataproviders( datasourceQueryPrefix +(datasourceQueryPrefix.length > 0 && parameters.length > 0 ?' and (':'') + parameters +(datasourceQueryPrefix.length > 0 && parameters.length > 0 ?' ) ':''),  page, size, this.properties, null, this.searchPage.getFields(), refineFieldsFilterQuery, (this.type == "deposit")).subscribe(
262
        data => {
263
          let totalResults = data[0];
264
          let results = data[1];
265
          if(refine && !(this.type == "all" || this.type == "deposit")){
266
            this.filtersReturned(refine, this.filters, totalResults, page);
267
          }
268
          this.resultsReturned(refine, results, totalResults, page);
269
          },
270
          err => {
271
              //console.log(err);
272
              this.handleError("Error getting content providers", err);
273
              this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
274

    
275
              //TODO check erros (service not available, bad request)
276
              // if( ){
277
              //   this.searchUtils.status = errorCodes.ERROR;
278
              // }
279
              //var errorCodes:ErrorCodes = new ErrorCodes();
280
              //this.searchUtils.status = errorCodes.NOT_AVAILABLE;
281
              /*if(err.status == '404') {
282
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
283
              } else if(err.status == '500') {
284
                this.searchUtils.status = this.errorCodes.ERROR;
285
              } else {
286
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
287
              }*/
288
              //this.searchPage.closeLoading();
289
              this.disableForms = false;
290
            this.searchPageUpdates.emit({disableForms: this.disableForms, disableRefineForms: this.disableRefineForms, searchUtils: this.searchUtils});
291
            this.searchPage.hideFilters = false;
292
          }
293
        );
294
      }
295
  }
296

    
297
  public resultsReturned(refine: boolean, results: any, totalResults, page: number) {
298
    this.searchUtils.totalResults = totalResults;
299
    this.results = results;
300
    if(!refine) {
301
      this.searchPage.buildPageURLParameters(this.filters, [], false);
302
    }
303

    
304
    this.searchPage.hideFilters = false;
305

    
306
    this.searchUtils.status = this.errorCodes.DONE;
307
    if (this.searchUtils.totalResults == 0) {
308
      this.searchUtils.status = this.errorCodes.NONE;
309
    }
310

    
311
    if (this.searchUtils.status == this.errorCodes.DONE) {
312
      // Page out of limit!!!
313
      let totalPages: any = this.searchUtils.totalResults / (this.searchUtils.size);
314
      if (!(Number.isInteger(totalPages))) {
315
        totalPages = (parseInt(totalPages, 10) + 1);
316
      }
317
      if (totalPages < page) {
318
        this.searchUtils.totalResults = 0;
319
        this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
320
      }
321
    }
322

    
323
    if(this.searchUtils.status != this.errorCodes.DONE && this.searchUtils.refineStatus != this.searchUtils.status) {
324
      if(this.searchFiltersSub) {
325
        this.searchFiltersSub.unsubscribe();
326
      }
327
      this.filtersReturned(refine, [], totalResults, page);
328
    }
329

    
330
    this.disableForms = false;
331
    this.searchPageUpdates.emit({disableForms: this.disableForms, disableRefineForms: this.disableRefineForms, searchUtils: this.searchUtils});
332

    
333
  }
334

    
335
  private handleError(message: string, error) {
336
    console.error("Content Providers advanced Search Page: "+message, error);
337
  }
338
}
(1-1/8)