Project

General

Profile

1
import {Component, Input, ViewChild} from '@angular/core';
2
import { ActivatedRoute} from '@angular/router';
3
import {Location} from '@angular/common';
4
import { Filter, Value} 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 {SearchFields} from '../../utils/properties/searchFields';
9
import {SearchPageComponent } from '../searchUtils/searchPage.component';
10
import {SearchUtilsClass } from '../searchUtils/searchUtils.class';
11

    
12
@Component({
13
    selector: 'search-projects',
14
    template: `
15

    
16
    <search-page pageTitle="Search Projects" type="projects" [(filters)] = "filters"
17
                 [(results)] = "results" [(searchUtils)] = "searchUtils"
18
                 [baseUrl] = "baseUrl" (queryChange)="queryChanged($event)"
19
                 [csvParams]="csvParams" csvPath="projects">
20
    </search-page>
21
     `
22

    
23
})
24
export class SearchProjectsComponent {
25
  public results =[];
26
  public filters: Filter[] =[];
27
  public baseUrl:string;
28
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
29
  public sub: any;
30
  public subResults: any;
31
  public searchFields:SearchFields = new SearchFields();
32
  public refineFields: string[] =  this.searchFields.PROJECT_REFINE_FIELDS;
33
  public fieldIdsMap = this.searchFields.PROJECT_FIELDS;
34
  public urlParams : Map<string, string>;
35
  public _location:Location;
36
  public csvParams: string;
37

    
38
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
39

    
40
  constructor (private route: ActivatedRoute, private _searchProjectsService: SearchProjectsService) {
41
    console.info(" constructor SearchProjectsComponent "+this.refineFields.length);
42

    
43
    var errorCodes:ErrorCodes = new ErrorCodes();
44
    this.searchUtils.status =errorCodes.LOADING;
45
    this.baseUrl = OpenaireProperties.getLinkToSearchProjects();
46

    
47
  }
48

    
49
  public ngOnInit() {
50
    this.searchPage.refineFields = this.refineFields;
51
    this.searchPage.fieldIdsMap = this.fieldIdsMap;
52
    console.info(" ngOnInit SearchProjectsComponent "+this.refineFields.length);
53
    //get refine field filters  from url parameters
54

    
55
    this.sub =  this.route.queryParams.subscribe(params => {
56

    
57
        //get keyword from url parameters
58
        this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
59

    
60
        //get page from url parameters
61
        this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
62

    
63
       var queryParameters = this.searchPage.getQueryParametersFromUrl(params);
64
        this._getResults(queryParameters, true, this.searchUtils.page, this.searchUtils.size);
65
    });
66
  }
67

    
68
  public ngOnDestroy() {
69
    if(this.sub){
70
      this.sub.unsubscribe();
71
    }
72
    if(this.subResults){
73
      this.subResults.unsubscribe();
74
    }
75
  }
76

    
77
  public getResults(keyword:string,refine:boolean, page: number, size: number){
78
    var parameters = "";
79
    if(keyword.length > 0){
80
      parameters = "q="+'"' + keyword + '"';
81
    }
82
    this._getResults(parameters,refine,page,this.searchUtils.size);
83
  }
84
  private _getResults(parameters:string,refine:boolean, page: number, size: number){
85
      this.csvParams = parameters;
86

    
87
    if(!refine && !this.searchPage){
88
        this.searchPage = new SearchPageComponent(this._location);
89
    }
90

    
91
    var errorCodes:ErrorCodes = new ErrorCodes();
92
    this.searchUtils.status = errorCodes.LOADING;
93
    this.searchPage.openLoading();
94

    
95
  this.subResults = this._searchProjectsService.searchProjects(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, this.searchPage.getFields()).subscribe(
96
        data => {
97
            this.searchUtils.totalResults = data[0];
98
            console.info("search Projects: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
99
            this.results = data[1];
100
            this.filters = data[2];
101
            this.searchPage.checkSelectedFilters(this.filters);
102
            // this.filters = this.searchPage.checkSelectedFilters(data[2]);
103
            this.searchPage.updateBaseUrlWithParameters(this.filters);
104
            var errorCodes:ErrorCodes = new ErrorCodes();
105
            this.searchUtils.status = errorCodes.DONE;
106
            if(this.searchUtils.totalResults == 0 ){
107
              this.searchUtils.status = errorCodes.NONE;
108
            }
109
            this.searchPage.closeLoading();
110
        },
111
        err => {
112
            console.log(err);
113
             //TODO check erros (service not available, bad request)
114
            // if( ){
115
            //   this.searchUtils.status = ErrorCodes.ERROR;
116
            // }
117
            var errorCodes:ErrorCodes = new ErrorCodes();
118
            this.searchUtils.status = errorCodes.ERROR;
119
            this.searchPage.closeLoading();
120
        }
121
    );
122
  }
123

    
124
  public getResultsForDataproviders(id:string, page: number, size: number){
125

    
126
    this._searchProjectsService.getProjectsforDataProvider(id, page, size).subscribe(
127
        data => {
128
            this.searchUtils.totalResults = data[0];
129
            console.info("search Projects for Dataproviders: [Id:"+id+" ]  [total results:"+this.searchUtils.totalResults+"]");
130
            this.results = data[1];
131

    
132
            var errorCodes:ErrorCodes = new ErrorCodes();
133
            this.searchUtils.status = errorCodes.DONE;
134
            if(this.searchUtils.totalResults == 0 ){
135
              this.searchUtils.status = errorCodes.NONE;
136
            }
137
        },
138
        err => {
139
            console.log(err);
140
             //TODO check erros (service not available, bad request)
141
            // if( ){
142
            //   this.searchUtils.status = ErrorCodes.ERROR;
143
            // }
144
            var errorCodes:ErrorCodes = new ErrorCodes();
145
            this.searchUtils.status = errorCodes.ERROR;
146
        }
147
    );
148
  }
149

    
150

    
151
  public queryChanged($event) {
152
    this.urlParams = undefined;
153
    var parameters = $event.value;
154
    this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size);
155
  }
156

    
157
}
(15-15/19)