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 {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
  public pagingLimit: number = 0;
57
  public isPiwikEnabled;
58
  public sort: boolean = true;
59
  properties:EnvProperties;
60

    
61
  @ViewChild (AdvancedSearchPageComponent) searchPage : AdvancedSearchPageComponent ;
62

    
63

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

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

    
71

    
72

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

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

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

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

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

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

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

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

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

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

    
182
          }
183
      );
184
    }
185
  }
186

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

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

    
194
  }
195

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