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')" [showLastIndex]="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.filters = DatasourcesHelperClass.createFilters(this.type);
116
    this.subs.push(this.route.queryParams.subscribe(params => {
117
     this.loadPaging = true;
118
     if(params['page'] && this.searchUtils.page != params['page']) {
119
       this.loadPaging = false;
120
       this.oldTotalResults = this.searchUtils.totalResults;
121
     }
122
     var refine = true;
123
      if (params['page'] != undefined && this.filters && !firstLoad && this.searchUtils.page != +params['page']) {
124
        refine = false;
125
      }
126

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

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

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

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

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

    
176
      let datasourceQueryPrefix = DatasourcesHelperClass.getQueryPrefix(this.type);
177
      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"))
178
      //.switchMap(
179
        .subscribe(
180
          data => {
181
            let totalResults = data[0];
182
            let filters = data[2];
183
            this.filtersReturned(refine, filters, totalResults, page);
184
            this.searchPageUpdates.emit({disableForms: this.disableForms, disableRefineForms: this.disableRefineForms, searchUtils: this.searchUtils})
185
          },
186
          err => {
187
            this.handleError("Error getting organizations: ", err);
188
            this.searchUtils.refineStatus = this.errorMessages.getErrorCode(err.status);
189

    
190
            this.disableRefineForms = false;
191
            this.searchPageUpdates.emit({disableForms: this.disableForms, disableRefineForms: this.disableRefineForms, searchUtils: this.searchUtils})
192
          }
193
        );
194
    }
195
  }
196

    
197
  public filtersReturned(refine: boolean, filters: Filter[], totalResults, page: number) {
198
    if (refine) {
199
      this.filters = this.searchPage.prepareFiltersToShow(filters, totalResults);
200
    }
201

    
202
    this.searchUtils.refineStatus = this.errorCodes.DONE;
203
    if(totalResults == 0) {
204
      this.searchUtils.refineStatus = this.errorCodes.NONE;
205
    }
206

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

    
218
    if(this.searchUtils.refineStatus != this.errorCodes.DONE && this.searchUtils.status != this.searchUtils.refineStatus) {
219
      if (this.searchResultsSub) {
220
        this.searchResultsSub.unsubscribe();
221
      }
222
      this.resultsReturned(refine, [], totalResults, page);
223
    }
224

    
225
    this.disableRefineForms = false;
226
  }
227

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

    
240
      this.csvParams = (parameters ? ("&fq=("+parameters) : "") + (parameters ? ")" : "");
241
      this.csvParams += (refineFieldsFilterQuery ? refineFieldsFilterQuery : "");
242

    
243
      //var errorCodes:ErrorCodes = new ErrorCodes();
244
      this.searchUtils.status = this.errorCodes.LOADING;
245
      //this.searchPage.openLoading();
246
      this.disableForms = true;
247
      this.searchPageUpdates.emit({disableForms: this.disableForms, disableRefineForms: this.disableRefineForms, searchUtils: this.searchUtils});
248

    
249
      this.results = [];
250
      this.searchUtils.totalResults = 0;
251

    
252
      //console.info("Advanced Search for Content Providers: Execute search query "+parameters);
253
      let datasourceQueryPrefix = DatasourcesHelperClass.getQueryPrefix(this.type);
254
      //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(
255
      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(
256
        data => {
257
          let totalResults = data[0];
258
          let results = data[1];
259
          this.resultsReturned(refine, results, totalResults, page);
260
          this.searchPageUpdates.emit({disableForms: this.disableForms, disableRefineForms: this.disableRefineForms, searchUtils: this.searchUtils});
261
          },
262
          err => {
263
              //console.log(err);
264
              this.handleError("Error getting content providers", err);
265
              this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
266

    
267
              //TODO check erros (service not available, bad request)
268
              // if( ){
269
              //   this.searchUtils.status = errorCodes.ERROR;
270
              // }
271
              //var errorCodes:ErrorCodes = new ErrorCodes();
272
              //this.searchUtils.status = errorCodes.NOT_AVAILABLE;
273
              /*if(err.status == '404') {
274
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
275
              } else if(err.status == '500') {
276
                this.searchUtils.status = this.errorCodes.ERROR;
277
              } else {
278
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
279
              }*/
280
              //this.searchPage.closeLoading();
281
              this.disableForms = false;
282
            this.searchPageUpdates.emit({disableForms: this.disableForms, disableRefineForms: this.disableRefineForms, searchUtils: this.searchUtils});
283
            this.searchPage.hideFilters = false;
284
          }
285
        );
286
      }
287
  }
288

    
289
  public resultsReturned(refine: boolean, results: any, totalResults, page: number) {
290
    this.searchUtils.totalResults = totalResults;
291
    this.results = results;
292
    if(!refine) {
293
      this.searchPage.buildPageURLParameters(this.filters, [], false);
294
    }
295

    
296
    this.searchPage.hideFilters = false;
297

    
298
    this.searchUtils.status = this.errorCodes.DONE;
299
    if (this.searchUtils.totalResults == 0) {
300
      this.searchUtils.status = this.errorCodes.NONE;
301
    }
302

    
303
    if (this.searchUtils.status == this.errorCodes.DONE) {
304
      // Page out of limit!!!
305
      let totalPages: any = this.searchUtils.totalResults / (this.searchUtils.size);
306
      if (!(Number.isInteger(totalPages))) {
307
        totalPages = (parseInt(totalPages, 10) + 1);
308
      }
309
      if (totalPages < page) {
310
        this.searchUtils.totalResults = 0;
311
        this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
312
      }
313
    }
314

    
315
    if(this.searchUtils.status != this.errorCodes.DONE && this.searchUtils.refineStatus != this.searchUtils.status) {
316
      if(this.searchFiltersSub) {
317
        this.searchFiltersSub.unsubscribe();
318
      }
319
      this.filtersReturned(refine, [], totalResults, page);
320
    }
321

    
322
    this.disableForms = false;
323
  }
324

    
325
  private handleError(message: string, error) {
326
    console.error("Content Providers advanced Search Page: "+message, error);
327
  }
328
}
(1-1/8)