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 {SearchProjectsService} from '../../services/searchProjects.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
@Component({
13
    selector: 'advanced-search-projects',
14
    template: `
15
    <advanced-search-page pageTitle="Advanced Search for Projects" entityType="project"
16
                 type = "projects"
17
                 [(results)] = "results"
18
                 [(searchUtils)] = "searchUtils"
19
                 [(fieldIds)]="fieldIds" [(fieldIdsMap)]="fieldIdsMap"   [(selectedFields)]="selectedFields"
20
                 (queryChange)="queryChanged($event)"
21
                 [csvParams]="csvParams" csvPath="resources" simpleSearchLink="/search/find/projects"
22
                 [disableForms]="disableForms"
23
                 [loadPaging]="loadPaging"
24
                 [oldTotalResults]="oldTotalResults">
25
    </advanced-search-page>
26

    
27
    `
28
 })
29

    
30
export class AdvancedSearchProjectsComponent {
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.PROJECT_ADVANCED_FIELDS;
40
  public fieldIdsMap = this.searchFields.PROJECT_FIELDS;
41
  public selectedFields:AdvancedField[] =  [];
42

    
43
  @ViewChild (AdvancedSearchPageComponent) searchPage : AdvancedSearchPageComponent ;
44
  public resourcesQuery = "(oaftype exact project)";
45
  public csvParams: string;
46
  public disableForms: boolean = false;
47
  public loadPaging: boolean = true;
48
  public oldTotalResults: number = 0;
49

    
50
  constructor (private route: ActivatedRoute, private _searchProjectsService: SearchProjectsService ) {
51

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

    
57

    
58

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

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

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

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

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

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

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

    
145
        }
146
    );
147
  }
148
  private setFilters(){
149
    //TODO set filters from
150
  }
151

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

    
159
  }
160

    
161

    
162
}
(7-7/12)