Project

General

Profile

1
import {Component, Input, ViewChild} from '@angular/core';
2
import {Observable}       from 'rxjs/Observable';
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 {AdvancedSearchPageComponent} from '../searchUtils/advancedSearchPage.component';
9
import {SearchFields, FieldDetails} from '../../utils/properties/searchFields';
10
import {SearchUtilsClass } from '../searchUtils/searchUtils.class';
11
import{EnvProperties} from '../../utils/properties/env-properties';
12

    
13

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

    
34
export class AdvancedSearchPublicationsComponent {
35
  private errorCodes: ErrorCodes;
36
@Input() piwikSiteId = null;
37
  public results =[];
38
  public filters =[];
39

    
40
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
41
  public searchFields:SearchFields = new SearchFields();
42

    
43
  public fieldIds:  string[] = this.searchFields.RESULT_ADVANCED_FIELDS;
44
  public fieldIdsMap= this.searchFields.RESULT_FIELDS;
45
  public selectedFields:AdvancedField[] =  [];
46
  public resourcesQuery = "((oaftype exact result) and (resulttypeid exact publication))";
47
  public csvParams: string;
48
  public disableForms: boolean = false;
49
  public loadPaging: boolean = true;
50
  public oldTotalResults: number = 0;
51
  @Input() openaireLink: string ;
52
  @Input() connectCommunityId: string;
53
  public pagingLimit: number = 0;
54
  public isPiwikEnabled;
55
  properties:EnvProperties;
56

    
57
  @ViewChild (AdvancedSearchPageComponent) searchPage : AdvancedSearchPageComponent ;
58

    
59

    
60
  constructor (private route: ActivatedRoute, private _searchPublicationsService: SearchPublicationsService ) {
61

    
62
    this.results =[];
63
    this.errorCodes = new ErrorCodes();
64
    this.searchUtils.status = this.errorCodes.LOADING;
65

    
66

    
67

    
68
  }
69
  ngOnInit() {
70
    this.route.data
71
      .subscribe((data: { envSpecific: EnvProperties }) => {
72
        this.properties= data.envSpecific;
73
        this.searchUtils.baseUrl = data.envSpecific.searchLinkToAdvancedPublications;
74
         this.pagingLimit = data.envSpecific.pagingLimit;
75
         this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
76

    
77
      });
78
    //var errorCodes:ErrorCodes = new ErrorCodes();
79
    this.searchUtils.status = this.errorCodes.LOADING;
80
    this.sub =  this.route.queryParams.subscribe(params => {
81
      if(params['page'] && this.searchUtils.page != params['page']) {
82
        this.loadPaging = false;
83
        this.oldTotalResults = this.searchUtils.totalResults;
84
      }
85

    
86
      let page = (params['page']=== undefined)?1:+params['page'];
87
      this.searchUtils.page = ( page <= 0 ) ? 1 : page;
88
      this.searchPage.fieldIds = this.fieldIds;
89
      this.selectedFields =[];
90
      this.searchPage.selectedFields = this.selectedFields;
91
      this.searchPage.fieldIdsMap = this.fieldIdsMap;
92
      this.searchPage.connectCommunityId = this.connectCommunityId;
93
      this.searchPage.getSelectedFiltersFromUrl(params);
94
      this.getResults(this.searchPage.createQueryParameters(),  this.searchUtils.page, this.searchUtils.size);
95
    });
96
  }
97
  ngOnDestroy() {
98
    this.sub.unsubscribe();
99
  }
100
  sub: any;
101
  public getResults(parameters:string, page: number, size: number){
102
    if(page > this.pagingLimit) {
103
      size=0;
104
    }
105
    if(page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
106
      if(parameters!= null && parameters != ''  ) {
107
        this.csvParams ="&fq=("+this.resourcesQuery +" and (" + parameters + "))";
108
      }else{
109
        this.csvParams ="&fq="+this.resourcesQuery;
110
      }
111

    
112
      //var errorCodes:ErrorCodes = new ErrorCodes();
113
      this.searchUtils.status = this.errorCodes.LOADING;
114
      //this.searchPage.openLoading();
115
      this.disableForms = true;
116
      this.results = [];
117
      this.searchUtils.totalResults = 0;
118

    
119
      console.info("Advanced Search for Publications: Execute search query "+parameters);
120
       this._searchPublicationsService.advancedSearchPublications(parameters, page, size, this.properties).subscribe(
121
          data => {
122
              this.searchUtils.totalResults = data[0];
123
              console.info("searchPubl total="+this.searchUtils.totalResults);
124
              this.results = data[1];
125
              this.searchPage.updateBaseUrlWithParameters();
126
              //var errorCodes:ErrorCodes = new ErrorCodes();
127
              this.searchUtils.status = this.errorCodes.DONE;
128
              if(this.searchUtils.totalResults == 0 ){
129
                this.searchUtils.status = this.errorCodes.NONE;
130
              }
131
              //this.searchPage.closeLoading();
132
              this.disableForms = false;
133

    
134
              if(this.searchUtils.status == this.errorCodes.DONE) {
135
                // Page out of limit!!!
136
                let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
137
                if(!(Number.isInteger(totalPages))) {
138
                    totalPages = (parseInt(totalPages, 10) + 1);
139
                }
140
                if(totalPages < page) {
141
                  this.searchUtils.totalResults = 0;
142
                  this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
143
                }
144
              }
145
          },
146
          err => {
147
              console.log(err);
148
              console.info("error");
149
              //TODO check erros (service not available, bad request)
150
              // if( ){
151
              //   this.searchUtils.status = ErrorCodes.ERROR;
152
              // }
153
              //var errorCodes:ErrorCodes = new ErrorCodes();
154
              //this.searchUtils.status = errorCodes.NOT_AVAILABLE;
155
              if(err.status == '404') {
156
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
157
              } else if(err.status == '500') {
158
                this.searchUtils.status = this.errorCodes.ERROR;
159
              } else {
160
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
161
              }
162

    
163
              //this.searchPage.closeLoading();
164
              this.disableForms = false;
165

    
166
          }
167
      );
168
    }
169
  }
170

    
171
  public queryChanged($event) {
172
    this.loadPaging = true;
173

    
174
    var parameters = $event.value;
175
    this.getResults(parameters, this.searchUtils.page,this.searchUtils.size);
176
    console.info("queryChanged: Execute search query "+parameters);
177

    
178
  }
179

    
180

    
181
}
(11-11/14)