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

    
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.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.sub = this.route.queryParams.subscribe(params => {
112
      if (params['page'] && this.searchUtils.page != params['page']) {
113
        this.loadPaging = false;
114
        this.oldTotalResults = this.searchUtils.totalResults;
115
      }
116
      var refine = true;
117
      if (this.searchUtils.page != ((params['page'] === undefined) ? 1 : +params['page']) && this.filters && !firstLoad) {
118
        refine = false;
119

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

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

    
139
  ngOnDestroy() {
140
    this.sub.unsubscribe();
141
  }
142

    
143
  sub: any;
144

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

    
158
      this.searchUtils.status = this.errorCodes.LOADING;
159
      this.disableForms = true;
160
      this.searchPageUpdates.emit({disableForms: this.disableForms,searchUtils: this.searchUtils});
161
      this.results = [];
162
      this.searchUtils.totalResults = 0;
163
      this._searchResearchResultsService.advancedSearchResults(this.resultType, parameters, page, size, sortBy, this.properties, (refine) ? this.searchPage.getRefineFieldsQuery() : null, this.searchPage.getFields(), refineFieldsFilterQuery).subscribe(
164
        data => {
165
          this.searchUtils.totalResults = data[0];
166
          this.results = data[1];
167

    
168
          if (refine) {
169
            this.filters = this.searchPage.prepareFiltersToShow(data[2]);
170
          } else {
171
            this.searchPage.buildPageURLParameters(this.filters, this.rangeFilters, false);
172
          }
173
          this.rangeFilters = this.searchPage.prepareRangeFiltersToShow();
174

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

    
182
          if (this.searchUtils.status == this.errorCodes.DONE) {
183
            // Page out of limit!!!
184
            let totalPages: any = this.searchUtils.totalResults / (this.searchUtils.size);
185
            if (!(Number.isInteger(totalPages))) {
186
              totalPages = (parseInt(totalPages, 10) + 1);
187
            }
188
            if (totalPages < page) {
189
              this.searchUtils.totalResults = 0;
190
              this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
191
            }
192
          }
193
        },
194
        err => {
195
          this.handleError("Error getting " + this.getEntityName(this.resultType, true, true), 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.NOT_AVAILABLE;
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
          //this.searchPage.closeLoading();
213
          this.disableForms = false;
214
          this.searchPageUpdates.emit({disableForms: this.disableForms, searchUtils: this.searchUtils})
215

    
216
        }
217
      );
218
    }
219
  }
220

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

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