Project

General

Profile

1
import {Component, EventEmitter, Input, Output, ViewChild} from '@angular/core';
2
import {ActivatedRoute} from '@angular/router';
3
import {AdvancedField} from './searchUtils/searchHelperClasses.class';
4
import {SearchProjectsService} from '../services/searchProjects.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
@Component({
14
    selector: 'search-projects',
15
    template: `
16
      <new-search-page
17
        pageTitle="{{(simpleView?'':'Advanced ')}} Search for {{ 'projects' | titlecase }}"
18
        entityType="project"
19
        type="projects"
20
        [results]="results"
21
        [searchUtils]="searchUtils"
22
        [fieldIds]="fieldIds" [fieldIdsMap]="fieldIdsMap" [selectedFields]="selectedFields"
23
        [csvParams]="csvParams" csvPath="projects"
24
        [simpleSearchLink]="simpleSearchLink" [advancedSearchLink]="advancedSearchLink"
25
        [disableForms]="disableForms"
26
        [loadPaging]="loadPaging"
27
        [oldTotalResults]="oldTotalResults"
28
        [openaireLink]=openaireLink
29
        [piwikSiteId]=piwikSiteId [hasPrefix]="hasPrefix"
30
        searchFormClass="projectsSearchForm"
31
        [includeOnlyResultsAndFilter]="includeOnlyResultsAndFilter"
32
        [filters]="filters"
33
        [rangeFilters]="rangeFilters" [rangeFields]="rangeFields"
34
        [simpleView]="simpleView" formPlaceholderText="Search by title, acronym, project code..."
35
      >
36
      </new-search-page>
37

    
38
    `
39
 })
40

    
41
export class SearchProjectsComponent {
42
  private errorCodes: ErrorCodes;
43
  private errorMessages: ErrorMessagesComponent;
44
@Input() piwikSiteId = null;
45
@Input() customFilter:SearchCustomFilter= null;
46
  public results =[];
47
  public filters =[];
48
  public rangeFilters: RangeFilter[] = [];
49

    
50
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
51
  public searchFields:SearchFields = new SearchFields();
52

    
53
  public fieldIds:  string[] = this.searchFields.PROJECT_ADVANCED_FIELDS;
54
  public fieldIdsMap = this.searchFields.PROJECT_FIELDS;
55
  public rangeFields:string[][] = this.searchFields.PROJECT_RANGE_FIELDS;
56
  public selectedFields:AdvancedField[] =  [];
57
  properties:EnvProperties;
58

    
59
  public resourcesQuery = "(oaftype exact project)";
60
  public csvParams: string;
61
  public disableForms: boolean = false;
62
  public loadPaging: boolean = true;
63
  public oldTotalResults: number = 0;
64
  public pagingLimit: number = 0;
65
  public isPiwikEnabled;
66
  public refineFields: string[] = this.searchFields.PROJECT_REFINE_FIELDS;
67
  @ViewChild(NewSearchPageComponent) searchPage: NewSearchPageComponent;
68
  @Input() simpleView: boolean = true;
69
  @Input() simpleSearchLink: string = "";
70
  advancedSearchLink: string = "";
71
  @Input() hasPrefix: boolean = true;
72
  @Input() openaireLink: string;
73
  @Input() includeOnlyResultsAndFilter: boolean = false;
74
  @Output() searchPageUpdates = new EventEmitter();
75
  constructor (private route: ActivatedRoute, private _searchProjectsService: SearchProjectsService ) {
76

    
77
    this.results =[];
78
    this.errorCodes = new ErrorCodes();
79
    this.errorMessages = new ErrorMessagesComponent();
80
    this.searchUtils.status = this.errorCodes.LOADING;
81

    
82

    
83

    
84

    
85
  }
86
  ngOnInit() {
87
    this.route.data
88
      .subscribe((data: { envSpecific: EnvProperties }) => {
89
        this.properties= data.envSpecific;
90
        if (!this.simpleSearchLink) {
91
          this.simpleSearchLink = this.properties.searchLinkToProjects;
92
        }
93
        this.advancedSearchLink = this.properties.searchLinkToAdvancedProjects;
94
        this.searchUtils.baseUrl = (this.simpleView)?this.simpleSearchLink:this.advancedSearchLink;
95
         this.pagingLimit = data.envSpecific.pagingLimit;
96
         this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
97

    
98
      });
99
    //var errorCodes:ErrorCodes = new ErrorCodes();
100
    this.searchUtils.status = this.errorCodes.LOADING;
101
    var firstLoad = true;
102

    
103
    this.sub =  this.route.queryParams.subscribe(params => {
104
      this.loadPaging = true;
105
      if(params['page'] && this.searchUtils.page != params['page']) {
106
        this.loadPaging = false;
107
        this.oldTotalResults = this.searchUtils.totalResults;
108
      }
109
      var refine = true;
110
      if (this.searchUtils.page != ((params['page'] === undefined) ? 1 : +params['page']) && this.filters && !firstLoad) {
111
        refine = false;
112

    
113
      }
114
      let page = (params['page']=== undefined)?1:+params['page'];
115
      this.searchUtils.page = ( page <= 0 ) ? 1 : page;
116

    
117
      this.searchUtils.size = (params['size']=== undefined)?10:+params['size'];
118
      if(this.searchUtils.size != 5 && this.searchUtils.size != 10 && this.searchUtils.size != 20 && this.searchUtils.size != 50) {
119
        this.searchUtils.size = 10;
120
      }
121

    
122
      this.searchPage.fieldIds = this.fieldIds;
123
      this.selectedFields = [];
124
      this.searchPage.prepareSearchPage(this.fieldIds, this.selectedFields, this.refineFields, this.rangeFields, this.fieldIdsMap,this.customFilter,params, "project");
125
      this.getResults(this.searchPage.getSearchAPIQueryForAdvancedSearhFields(),  this.searchUtils.page, this.searchUtils.size, refine, this.searchPage.getSearchAPIQueryForRangeFields(params)+this.searchPage.getSearchAPIQueryForRefineFields(params, firstLoad));
126
      firstLoad = false;
127
    });
128
  }
129
  ngOnDestroy() {
130
    this.sub.unsubscribe();
131
  }
132
  sub: any;
133
  public getResults(parameters:string, page: number, size: number, refine: boolean, refineFieldsFilterQuery = null){
134
    if(page > this.pagingLimit) {
135
      size=0;
136
    }
137
    if(page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
138
      // if(parameters!= null && parameters != ''  ) {
139
      //   this.csvParams ="&fq=( "+this.resourcesQuery + "and (" + parameters + "))";
140
      // }else{
141
      //   this.csvParams ="&fq="+this.resourcesQuery;
142
      // }
143
      // this.csvParams += (refineFieldsFilterQuery?refineFieldsFilterQuery:'');
144
      //
145
      // //var errorCodes:ErrorCodes = new ErrorCodes();
146

    
147
      this.csvParams = (parameters ? ("&fq=("+parameters) : "") + (parameters ? ")" : "");
148
      this.csvParams += (refineFieldsFilterQuery ? refineFieldsFilterQuery : "");
149

    
150
      this.searchUtils.status = this.errorCodes.LOADING;
151
      //this.searchPage.openLoading();
152
      this.disableForms = true;
153
      this.searchPageUpdates.emit({disableForms: this.disableForms,searchUtils: this.searchUtils});
154
      this.results = [];
155
      this.searchUtils.totalResults = 0;
156

    
157
      //console.info("Advanced Search for Publications: Execute search query "+parameters);
158
       this._searchProjectsService.advancedSearchProjects(parameters,  page, size, this.properties,(refine) ? this.searchPage.getRefineFieldsQuery() : null, this.searchPage.getFields(), refineFieldsFilterQuery).subscribe(
159
          data => {
160
              this.searchUtils.totalResults = data[0];
161
              this.results = data[1];
162
              // this.searchPage.updateBaseUrlWithParameters();
163
              if (refine) {
164
                this.filters = this.searchPage.prepareFiltersToShow(data[2], this.searchUtils.totalResults);
165
                this.rangeFilters = this.searchPage.prepareRangeFiltersToShow();
166
              }else{
167
                this.searchPage.buildPageURLParameters(this.filters, this.rangeFilters, false);
168
              }
169

    
170
            //var errorCodes:ErrorCodes = new ErrorCodes();
171
              this.searchUtils.status = this.errorCodes.DONE;
172
              if(this.searchUtils.totalResults == 0 ){
173
                this.searchUtils.status = this.errorCodes.NONE;
174
              }
175
              //this.searchPage.closeLoading();
176
              this.disableForms = false;
177
            this.searchPageUpdates.emit({disableForms: this.disableForms, searchUtils: this.searchUtils})
178

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

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

    
210
              //this.searchPage.closeLoading();
211
              this.disableForms = false;
212
            this.searchPageUpdates.emit({disableForms: this.disableForms, searchUtils: this.searchUtils})
213

    
214
          }
215
      );
216
    }
217
  }
218

    
219
  private handleError(message: string, error) {
220
    console.error("Projects advanced Search Page: "+message, error);
221
  }
222
}
(5-5/8)