Project

General

Profile

1
import {Component, EventEmitter, Input, OnChanges, Output, SimpleChanges, ViewChild} from '@angular/core';
2
import {ActivatedRoute} from '@angular/router';
3
import {AdvancedField, Filter} from './searchUtils/searchHelperClasses.class';
4
import {SearchResearchResultsService} from '../services/searchResearchResults.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
import {EnvProperties} from '../utils/properties/env-properties';
10
import {NewSearchPageComponent} from "./searchUtils/newSearchPage.component";
11
import {RangeFilter} from "../utils/rangeFilter/rangeFilterHelperClasses.class";
12

    
13

    
14
@Component({
15
  selector: 'search-research-results',
16
  template: `    
17
    <new-search-page
18
      pageTitle="{{(simpleView?'':'Advanced ')}} Search for {{ getEntityName(resultType, true, true) | titlecase }}"
19
      [entityType]="resultType"
20
      [type]="getEntityName(resultType, true, true)"
21
      [results]="results"
22
      [searchUtils]="searchUtils"
23
      [fieldIds]="fieldIds" [fieldIdsMap]="fieldIdsMap" [selectedFields]="selectedFields"
24
      [csvParams]="csvParams" [csvPath]="getEntityName(resultType, true, false)"
25
      [simpleSearchLink]="simpleSearchLink" [advancedSearchLink]="advancedSearchLink"
26
      [disableForms]="disableForms"
27
      [loadPaging]="loadPaging"
28
      [oldTotalResults]="oldTotalResults"
29
      [openaireLink]=openaireLink
30
      [piwikSiteId]=piwikSiteId [hasPrefix]="hasPrefix"
31
      searchFormClass="publicationsSearchForm"
32
      [sort]=sort
33
      [filters]="filters" [quickFilter]="quickFilter"
34
      [rangeFilters]="rangeFilters" [rangeFields]="rangeFields"
35
      [simpleView]="simpleView" formPlaceholderText="Search by title, author, abstract, DOI, orcid..."
36
      [includeOnlyResultsAndFilter]="includeOnlyResultsAndFilter"
37
    >
38
    </new-search-page>
39

    
40
  `
41
})
42

    
43
export class SearchResearchResultsComponent {
44
  @Input() resultType: string = "result";
45

    
46
  @Input() simpleSearchLink: string = "";
47
  advancedSearchLink: string = "";
48

    
49
  private errorCodes: ErrorCodes;
50
  private errorMessages: ErrorMessagesComponent;
51
  @Input() piwikSiteId = null;
52
  @Input() hasPrefix: boolean = true;
53
  public results = [];
54
  public filters = [];
55
  public rangeFilters: RangeFilter[] = [];
56

    
57
  public searchUtils: SearchUtilsClass = new SearchUtilsClass();
58
  public searchFields: SearchFields = new SearchFields();
59

    
60
  public fieldIds: string[] = this.searchFields.RESULT_ADVANCED_FIELDS;
61
  public fieldIdsMap = this.searchFields.RESULT_FIELDS;
62
  public rangeFields:string[][] = this.searchFields.RESULT_RANGE_FIELDS;
63
  public selectedFields: AdvancedField[] = [];
64
  public resourcesQuery = "((oaftype exact result) and (resulttypeid exact " + this.resultType + "))";
65
  public csvParams: string;
66
  public disableForms: boolean = false;
67
  public loadPaging: boolean = true;
68
  public oldTotalResults: number = 0;
69
  @Input() openaireLink: string;
70
  @Input() customFilter: SearchCustomFilter = null;
71
  public pagingLimit: number = 0;
72
  public isPiwikEnabled;
73
  public sort: boolean = true;
74
  properties: EnvProperties;
75
  public refineFields: string[] = this.searchFields.RESULT_REFINE_FIELDS;
76
  @ViewChild(NewSearchPageComponent) searchPage: NewSearchPageComponent;
77
  @Input() simpleView: boolean = true;
78
  quickFilter: { filter: Filter, selected: boolean, filterId: string, value: string } = {
79
    filter: null,
80
    selected: true,
81
    filterId: "resultbestaccessright",
82
    value: "Open Access"
83
  };
84
  @Input() includeOnlyResultsAndFilter: boolean = false;
85
  @Output() searchPageUpdates = new EventEmitter();
86
  subs: any[]=[];
87
  constructor(private route: ActivatedRoute, private _searchResearchResultsService: SearchResearchResultsService) {
88
    this.results = [];
89
    this.errorCodes = new ErrorCodes();
90
    this.errorMessages = new ErrorMessagesComponent();
91
    this.searchUtils.status = this.errorCodes.LOADING;
92
  }
93

    
94
  ngOnInit() {
95
    //TODO add checks about which result types are enabled!
96
    this.subs.push(this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
97
      this.properties = data.envSpecific;
98
      this.pagingLimit = data.envSpecific.pagingLimit;
99
      this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
100

    
101
      if (!this.simpleSearchLink) {
102
        this.simpleSearchLink = this.properties.searchLinkToResults;
103
      }
104
      this.advancedSearchLink = this.properties.searchLinkToAdvancedResults;
105
      this.searchUtils.baseUrl = (this.simpleView) ? this.simpleSearchLink : this.advancedSearchLink;
106

    
107
    }));
108

    
109
    this.searchUtils.status = this.errorCodes.LOADING;
110
    var firstLoad = true;
111
    this.subs.push(this.route.queryParams.subscribe(params => {
112
      this.loadPaging = true;
113
      if (params['page'] && this.searchUtils.page != params['page']) {
114
        this.loadPaging = false;
115
        this.oldTotalResults = this.searchUtils.totalResults;
116
      }
117
      var refine = true;
118
      if (this.searchUtils.page != ((params['page'] === undefined) ? 1 : +params['page']) && this.filters && !firstLoad) {
119
        refine = false;
120

    
121
      }
122
      let page = (params['page'] === undefined) ? 1 : +params['page'];
123
      this.searchUtils.page = (page <= 0) ? 1 : page;
124

    
125
      this.searchUtils.size = (params['size'] === undefined) ? 10 : +params['size'];
126
      if (this.searchUtils.size != 5 && this.searchUtils.size != 10 && this.searchUtils.size != 20 && this.searchUtils.size != 50) {
127
        this.searchUtils.size = 10;
128
      }
129
      this.searchUtils.sortBy = (params['sortBy']) ? params['sortBy'] : '';
130
      if (this.searchUtils.sortBy && this.searchUtils.sortBy != "resultdateofacceptance,descending" && this.searchUtils.sortBy != "resultdateofacceptance,ascending") {
131
        this.searchUtils.sortBy = "";
132
      }
133
      this.selectedFields = [];
134
      this.searchPage.prepareSearchPage(this.fieldIds, this.selectedFields, this.refineFields, this.rangeFields, this.fieldIdsMap,this.customFilter,params, this.resultType, this.quickFilter);
135
      this._getResults(this.searchPage.getSearchAPIQueryForAdvancedSearhFields(), this.searchUtils.page, this.searchUtils.size, this.searchUtils.sortBy, refine, this.searchPage.getSearchAPIQueryForRangeFields(params)+this.searchPage.getSearchAPIQueryForRefineFields(params, firstLoad));
136
      firstLoad = false;
137
    }));
138
  }
139

    
140
  ngOnDestroy() {
141
    for(let sub of this.subs){
142
      sub.unsubscribe();
143
    }
144
  }
145

    
146
  public _getResults(parameters: string, page: number, size: number, sortBy: string, refine: boolean, refineFieldsFilterQuery = null) {
147
    if (page > this.pagingLimit) {
148
      size = 0;
149
    }
150
    if (page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
151
      // if (parameters != null && parameters != '') {
152
      //   this.csvParams = "&fq=(" + this.resourcesQuery + " and (" + parameters + "))";
153
      // } else {
154
      //   this.csvParams = "&fq=" + this.resourcesQuery;
155
      // }
156
      this.csvParams = (parameters ? ("&fq=("+parameters) : "") + (parameters ? ")" : "");
157
      this.csvParams += (refineFieldsFilterQuery ? refineFieldsFilterQuery : "");
158

    
159
      this.searchUtils.status = this.errorCodes.LOADING;
160
      this.disableForms = true;
161
      this.searchPageUpdates.emit({disableForms: this.disableForms,searchUtils: this.searchUtils});
162
      this.results = [];
163
      this.searchUtils.totalResults = 0;
164
      this.subs.push(this._searchResearchResultsService.advancedSearchResults(this.resultType, parameters, page, size, sortBy, this.properties, (refine) ? this.searchPage.getRefineFieldsQuery() : null, this.searchPage.getFields(), refineFieldsFilterQuery)
165
        .subscribe(
166
        data => {
167
          this.searchUtils.totalResults = data[0];
168
          this.results = data[1];
169
          if (refine) {
170
            this.filters = this.searchPage.prepareFiltersToShow(data[2],this.searchUtils.totalResults);
171
            this.rangeFilters = this.searchPage.prepareRangeFiltersToShow();
172
          } else {
173
            this.searchPage.buildPageURLParameters(this.filters, this.rangeFilters, false);
174
          }
175

    
176
          this.searchUtils.status = this.errorCodes.DONE;
177
          if (this.searchUtils.totalResults == 0) {
178
            this.searchUtils.status = this.errorCodes.NONE;
179
          }
180
          this.disableForms = false;
181
          this.searchPageUpdates.emit({disableForms: this.disableForms, searchUtils: this.searchUtils})
182

    
183
          if (this.searchUtils.status == this.errorCodes.DONE) {
184
            // Page out of limit!!!
185
            let totalPages: any = this.searchUtils.totalResults / (this.searchUtils.size);
186
            if (!(Number.isInteger(totalPages))) {
187
              totalPages = (parseInt(totalPages, 10) + 1);
188
            }
189
            if (totalPages < page) {
190
              this.searchUtils.totalResults = 0;
191
              this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
192
            }
193
          }
194
        },
195
        err => {
196
          this.handleError("Error getting " + this.getEntityName(this.resultType, true, true), err);
197
          this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
198

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

    
213
          //this.searchPage.closeLoading();
214
          this.disableForms = false;
215
          this.searchPageUpdates.emit({disableForms: this.disableForms, searchUtils: this.searchUtils})
216

    
217
        }
218
      ));
219
    }
220
  }
221

    
222
  private handleError(message: string, error) {
223
    console.error(this.getEntityName(this.resultType, true, true) + " advanced Search Page: " + message, error);
224
  }
225

    
226
  public getEntityName(entityType: string, plural: boolean, full: boolean): string {
227
    if (entityType == "publication") {
228
      return "publication" + (plural ? "s" : "");
229
    } else if (entityType == "dataset") {
230
      return (full ? "research data" : ("dataset" + (plural ? "s" : "")));
231
    } else if (entityType == "software") {
232
      return "software";
233
    } else if (entityType == "other") {
234
      return (full ? ("other research product" + (plural ? "s" : "")) : "other");
235
    } else if (entityType == "result") {
236
      return (full ? ("research outcome" + (plural ? "s" : "")) : "result");
237
    }
238
  }
239
}
(7-7/8)