Project

General

Profile

1
import {Component, Input, ViewChild} from '@angular/core';
2
import {Observable}       from 'rxjs';
3
import { Router, ActivatedRoute} from '@angular/router';
4
import {Filter, Value,AdvancedField} from '../searchUtils/searchHelperClasses.class';
5
import {SearchPublicationsService} from '../../services/searchPublications.service';
6
import {SearchResult}     from '../../utils/entities/searchResult';
7
import {ErrorCodes} from '../../utils/properties/errorCodes';
8
import {ErrorMessagesComponent}    from '../../utils/errorMessages.component';
9
import {AdvancedSearchPageComponent} from '../searchUtils/advancedSearchPage.component';
10
import {SearchFields, FieldDetails} from '../../utils/properties/searchFields';
11
import {SearchCustomFilter, SearchUtilsClass} from '../searchUtils/searchUtils.class';
12
import{EnvProperties} from '../../utils/properties/env-properties';
13

    
14

    
15
@Component({
16
    selector: 'advanced-search-publications',
17
    template: `
18
    <advanced-search-page pageTitle="Advanced Search for Publications" entityType="publication"
19
                 type="publications"
20
                 [(results)] = "results"
21
                 [(searchUtils)] = "searchUtils"
22
                 [(fieldIds)]="fieldIds" [(fieldIdsMap)]="fieldIdsMap"   [(selectedFields)]="selectedFields"
23
                 (queryChange)="queryChanged($event)"
24
                 [csvParams]="csvParams" csvPath="publications" simpleSearchLink="/search/find/publications"
25
                 [disableForms]="disableForms"
26
                 [loadPaging]="loadPaging"
27
                 [oldTotalResults]="oldTotalResults"
28
                 [(openaireLink)]=openaireLink
29
                 [piwikSiteId]=piwikSiteId [hasPrefix]="hasPrefix"
30
                 searchFormClass="publicationsSearchForm"
31
                 [(sort)]=sort >
32
    </advanced-search-page>
33
    `
34
 })
35

    
36
export class AdvancedSearchPublicationsComponent {
37
  private errorCodes: ErrorCodes;
38
  private errorMessages: ErrorMessagesComponent;
39
  @Input() piwikSiteId = null;
40
  @Input() hasPrefix: boolean = true;
41
  public results =[];
42
  public filters =[];
43

    
44
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
45
  public searchFields:SearchFields = new SearchFields();
46

    
47
  public fieldIds:  string[] = this.searchFields.RESULT_ADVANCED_FIELDS;
48
  public fieldIdsMap= this.searchFields.RESULT_FIELDS;
49
  public selectedFields:AdvancedField[] =  [];
50
  public resourcesQuery = "((oaftype exact result) and (resulttypeid exact publication))";
51
  public csvParams: string;
52
  public disableForms: boolean = false;
53
  public loadPaging: boolean = true;
54
  public oldTotalResults: number = 0;
55
  @Input() openaireLink: string ;
56
  @Input() customFilter:SearchCustomFilter= null;
57
  public pagingLimit: number = 0;
58
  public isPiwikEnabled;
59
  public sort: boolean = true;
60
  properties:EnvProperties;
61

    
62
  @ViewChild (AdvancedSearchPageComponent) searchPage : AdvancedSearchPageComponent ;
63

    
64

    
65
  constructor (private route: ActivatedRoute, private _searchPublicationsService: SearchPublicationsService ) {
66

    
67
    this.results =[];
68
    this.errorCodes = new ErrorCodes();
69
    this.errorMessages = new ErrorMessagesComponent();
70
    this.searchUtils.status = this.errorCodes.LOADING;
71

    
72

    
73

    
74
  }
75
  ngOnInit() {
76
    this.route.data
77
      .subscribe((data: { envSpecific: EnvProperties }) => {
78
        this.properties= data.envSpecific;
79
        this.searchUtils.baseUrl = data.envSpecific.searchLinkToAdvancedPublications;
80
         this.pagingLimit = data.envSpecific.pagingLimit;
81
         this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
82

    
83
      });
84
    //var errorCodes:ErrorCodes = new ErrorCodes();
85
    this.searchUtils.status = this.errorCodes.LOADING;
86
    this.sub =  this.route.queryParams.subscribe(params => {
87
      if(params['page'] && this.searchUtils.page != params['page']) {
88
        this.loadPaging = false;
89
        this.oldTotalResults = this.searchUtils.totalResults;
90
      }
91

    
92
      let page = (params['page']=== undefined)?1:+params['page'];
93
      this.searchUtils.page = ( page <= 0 ) ? 1 : page;
94

    
95
      this.searchUtils.size = (params['size']=== undefined)?10:+params['size'];
96
      if(this.searchUtils.size != 5 && this.searchUtils.size != 10 && this.searchUtils.size != 20 && this.searchUtils.size != 50) {
97
        this.searchUtils.size = 10;
98
      }
99
      this.searchUtils.sortBy = (params['sortBy'])?params['sortBy']:'';
100
      if(this.searchUtils.sortBy && this.searchUtils.sortBy != "resultdateofacceptance,descending" && this.searchUtils.sortBy != "resultdateofacceptance,ascending") {
101
        this.searchUtils.sortBy = "";
102
      }
103

    
104
      this.searchPage.fieldIds = this.fieldIds;
105
      this.selectedFields =[];
106
      this.searchPage.selectedFields = this.selectedFields;
107
      this.searchPage.fieldIdsMap = this.fieldIdsMap;
108
      this.searchPage.customFilter = this.customFilter;
109
      this.searchPage.getSelectedFiltersFromUrl(params);
110
      this.getResults(this.searchPage.createQueryParameters(),  this.searchUtils.page, this.searchUtils.size, this.searchUtils.sortBy);
111
    });
112
  }
113
  ngOnDestroy() {
114
    this.sub.unsubscribe();
115
  }
116
  sub: any;
117
  public getResults(parameters:string, page: number, size: number, sortBy: string){
118
    if(page > this.pagingLimit) {
119
      size=0;
120
    }
121
    if(page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
122
      if(parameters!= null && parameters != ''  ) {
123
        this.csvParams ="&fq=("+this.resourcesQuery +" and (" + parameters + "))";
124
      }else{
125
        this.csvParams ="&fq="+this.resourcesQuery;
126
      }
127

    
128
      //var errorCodes:ErrorCodes = new ErrorCodes();
129
      this.searchUtils.status = this.errorCodes.LOADING;
130
      //this.searchPage.openLoading();
131
      this.disableForms = true;
132
      this.results = [];
133
      this.searchUtils.totalResults = 0;
134

    
135
      //console.info("Advanced Search for Publications: Execute search query "+parameters);
136
       this._searchPublicationsService.advancedSearchPublications(parameters, page, size, sortBy, this.properties).subscribe(
137
          data => {
138
              this.searchUtils.totalResults = data[0];
139
              this.results = data[1];
140
              this.searchPage.updateBaseUrlWithParameters();
141
              //var errorCodes:ErrorCodes = new ErrorCodes();
142
              this.searchUtils.status = this.errorCodes.DONE;
143
              if(this.searchUtils.totalResults == 0 ){
144
                this.searchUtils.status = this.errorCodes.NONE;
145
              }
146
              //this.searchPage.closeLoading();
147
              this.disableForms = false;
148

    
149
              if(this.searchUtils.status == this.errorCodes.DONE) {
150
                // Page out of limit!!!
151
                let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
152
                if(!(Number.isInteger(totalPages))) {
153
                    totalPages = (parseInt(totalPages, 10) + 1);
154
                }
155
                if(totalPages < page) {
156
                  this.searchUtils.totalResults = 0;
157
                  this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
158
                }
159
              }
160
          },
161
          err => {
162
              //console.log(err);
163
              this.handleError("Error getting publications", err);
164
              this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
165

    
166
              //TODO check erros (service not available, bad request)
167
              // if( ){
168
              //   this.searchUtils.status = ErrorCodes.ERROR;
169
              // }
170
              //var errorCodes:ErrorCodes = new ErrorCodes();
171
              //this.searchUtils.status = errorCodes.NOT_AVAILABLE;
172
              /*if(err.status == '404') {
173
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
174
              } else if(err.status == '500') {
175
                this.searchUtils.status = this.errorCodes.ERROR;
176
              } else {
177
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
178
              }*/
179

    
180
              //this.searchPage.closeLoading();
181
              this.disableForms = false;
182

    
183
          }
184
      );
185
    }
186
  }
187

    
188
  public queryChanged($event) {
189
    this.loadPaging = true;
190

    
191
    var parameters = $event.value;
192
    //this.getResults(parameters, this.searchUtils.page,this.searchUtils.size, this.searchUtils.sortBy);
193
    //console.info("queryChanged: Execute search query "+parameters);
194

    
195
  }
196

    
197
  private handleError(message: string, error) {
198
    console.error("Publications advanced Search Page: "+message, error);
199
  }
200
}
(11-11/14)