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, doi, abstract content..."
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
    console.log(this.quickFilter);
96
    //TODO add checks about which result types are enabled!
97
    this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
98
      this.properties = data.envSpecific;
99
      this.pagingLimit = data.envSpecific.pagingLimit;
100
      this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
101

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

    
108
    });
109

    
110
    this.searchUtils.status = this.errorCodes.LOADING;
111
    var firstLoad = true;
112
    this.sub = this.route.queryParams.subscribe(params => {
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
console.log(params);
134
      this.selectedFields = [];
135
      this.searchPage.prepareSearchPage(this.fieldIds, this.selectedFields, this.refineFields, this.rangeFields, this.fieldIdsMap,this.customFilter,params, this.resultType, this.quickFilter);
136
      this._getResults(this.searchPage.getSearchAPIQueryForAdvancedSearhFields(), this.searchUtils.page, this.searchUtils.size, this.searchUtils.sortBy, refine, this.searchPage.getSearchAPIQueryForRangeFields(params)+this.searchPage.getSearchAPIQueryForRefineFields(params, firstLoad));
137
      firstLoad = false;
138
    });
139
  }
140

    
141
  ngOnDestroy() {
142
    this.sub.unsubscribe();
143
  }
144

    
145
  sub: any;
146

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

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

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

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

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

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

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

    
218
        }
219
      );
220
    }
221
  }
222

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

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