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 {OpenaireProperties, ErrorCodes} from '../../utils/properties/openaireProperties';
8
import {AdvancedSearchPageComponent} from '../searchUtils/advancedSearchPage.component';
9
import {SearchFields, FieldDetails} from '../../utils/properties/searchFields';
10
import {SearchUtilsClass } from '../searchUtils/searchUtils.class';
11

    
12

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

    
30
export class AdvancedSearchPublicationsComponent {
31
  private errorCodes: ErrorCodes;
32

    
33
  public results =[];
34
  public filters =[];
35

    
36
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
37
  public searchFields:SearchFields = new SearchFields();
38

    
39
  public fieldIds:  string[] = this.searchFields.RESULT_ADVANCED_FIELDS;
40
  public fieldIdsMap= this.searchFields.RESULT_FIELDS;
41
  public selectedFields:AdvancedField[] =  [];
42
  public resourcesQuery = "((oaftype exact result) and (resulttypeid exact publication))";
43
  public csvParams: string;
44
  public disableForms: boolean = false;
45
  public loadPaging: boolean = true;
46
  public oldTotalResults: number = 0;
47

    
48
  @ViewChild (AdvancedSearchPageComponent) searchPage : AdvancedSearchPageComponent ;
49

    
50

    
51
  constructor (private route: ActivatedRoute, private _searchPublicationsService: SearchPublicationsService ) {
52

    
53
    this.results =[];
54
    this.errorCodes = new ErrorCodes();
55
    this.searchUtils.status = this.errorCodes.LOADING;
56
    this.searchUtils.baseUrl = OpenaireProperties.searchLinkToAdvancedPublications;
57

    
58

    
59

    
60
  }
61
  ngOnInit() {
62
    //var errorCodes:ErrorCodes = new ErrorCodes();
63
    this.searchUtils.status = this.errorCodes.LOADING;
64
    this.sub =  this.route.queryParams.subscribe(params => {
65
      if(params['page'] && this.searchUtils.page != params['page']) {
66
        this.loadPaging = false;
67
        this.oldTotalResults = this.searchUtils.totalResults;
68
      }
69

    
70
      let page = (params['page']=== undefined)?1:+params['page'];
71
      this.searchUtils.page = ( page <= 0 ) ? 1 : page;
72
      this.searchPage.fieldIds = this.fieldIds;
73
      this.selectedFields =[];
74
      this.searchPage.selectedFields = this.selectedFields;
75
      this.searchPage.fieldIdsMap = this.fieldIdsMap;
76

    
77
      this.searchPage.getSelectedFiltersFromUrl(params);
78
      this.getResults(this.searchPage.createQueryParameters(),  this.searchUtils.page, this.searchUtils.size);
79
    });
80
  }
81
  ngOnDestroy() {
82
    this.sub.unsubscribe();
83
  }
84
  sub: any;
85
  public getResults(parameters:string, page: number, size: number){
86
      if(parameters!= null && parameters != ''  ) {
87
        this.csvParams ="&type=publications&query=("+this.resourcesQuery +" and (" + parameters + "))";
88
      }else{
89
        this.csvParams ="&type=publications&query="+this.resourcesQuery;
90
      }
91

    
92
    //var errorCodes:ErrorCodes = new ErrorCodes();
93
    this.searchUtils.status = this.errorCodes.LOADING;
94
    //this.searchPage.openLoading();
95
    this.disableForms = true;
96
    this.results = [];
97
    this.searchUtils.totalResults = 0;
98

    
99
    console.info("Advanced Search for Publications: Execute search query "+parameters);
100
     this._searchPublicationsService.advancedSearchPublications(parameters, page, size).subscribe(
101
        data => {
102
            this.searchUtils.totalResults = data[0];
103
            console.info("searchPubl total="+this.searchUtils.totalResults);
104
            this.results = data[1];
105
            this.searchPage.updateBaseUrlWithParameters();
106
            //var errorCodes:ErrorCodes = new ErrorCodes();
107
            this.searchUtils.status = this.errorCodes.DONE;
108
            if(this.searchUtils.totalResults == 0 ){
109
              this.searchUtils.status = this.errorCodes.NONE;
110
            }
111
            //this.searchPage.closeLoading();
112
            this.disableForms = false;
113

    
114
            if(this.searchUtils.status == this.errorCodes.DONE) {
115
              // Page out of limit!!!
116
              let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
117
              if(!(Number.isInteger(totalPages))) {
118
                  totalPages = (parseInt(totalPages, 10) + 1);
119
              }
120
              if(totalPages < page) {
121
                this.searchUtils.totalResults = 0;
122
                this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
123
              }
124
            }
125
        },
126
        err => {
127
            console.log(err);
128
            console.info("error");
129
            //TODO check erros (service not available, bad request)
130
            // if( ){
131
            //   this.searchUtils.status = ErrorCodes.ERROR;
132
            // }
133
            //var errorCodes:ErrorCodes = new ErrorCodes();
134
            //this.searchUtils.status = errorCodes.NOT_AVAILABLE;
135
            if(err.status == '404') {
136
              this.searchUtils.status = this.errorCodes.NOT_FOUND;
137
            } else if(err.status == '500') {
138
              this.searchUtils.status = this.errorCodes.ERROR;
139
            } else {
140
              this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
141
            }
142

    
143
            //this.searchPage.closeLoading();
144
            this.disableForms = false;
145

    
146
        }
147
    );
148
  }
149

    
150
  public queryChanged($event) {
151
    this.loadPaging = true;
152

    
153
    var parameters = $event.value;
154
    this.getResults(parameters, this.searchUtils.page,this.searchUtils.size);
155
    console.info("queryChanged: Execute search query "+parameters);
156

    
157
  }
158

    
159

    
160
}
(9-9/12)