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
                 [(connectCommunityId)]=connectCommunityId  [piwikSiteId]=piwikSiteId
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
  public results =[];
41
  public filters =[];
42

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

    
46
  public fieldIds:  string[] = this.searchFields.RESULT_ADVANCED_FIELDS;
47
  public fieldIdsMap= this.searchFields.RESULT_FIELDS;
48
  public selectedFields:AdvancedField[] =  [];
49
  public resourcesQuery = "((oaftype exact result) and (resulttypeid exact publication))";
50
  public csvParams: string;
51
  public disableForms: boolean = false;
52
  public loadPaging: boolean = true;
53
  public oldTotalResults: number = 0;
54
  @Input() openaireLink: string ;
55
  @Input() connectCommunityId: 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.connectCommunityId = this.connectCommunityId;
109
      this.searchPage.customFilter = this.customFilter;
110
      this.searchPage.getSelectedFiltersFromUrl(params);
111
      this.getResults(this.searchPage.createQueryParameters(),  this.searchUtils.page, this.searchUtils.size, this.searchUtils.sortBy);
112
    });
113
  }
114
  ngOnDestroy() {
115
    this.sub.unsubscribe();
116
  }
117
  sub: any;
118
  public getResults(parameters:string, page: number, size: number, sortBy: string){
119
    if(page > this.pagingLimit) {
120
      size=0;
121
    }
122
    if(page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
123
      if(parameters!= null && parameters != ''  ) {
124
        this.csvParams ="&fq=("+this.resourcesQuery +" and (" + parameters + "))";
125
      }else{
126
        this.csvParams ="&fq="+this.resourcesQuery;
127
      }
128

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

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

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

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

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

    
184
          }
185
      );
186
    }
187
  }
188

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

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

    
196
  }
197

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