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="project" [(filters)] = "filters"
17
                 [(results)] = "results"
18
                 [(searchUtils)] = "searchUtils"
19
                  [baseUrl] = "baseUrl" (queryChange)="queryChanged($event)"  >
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

    
37
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
38

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

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

    
46
  }
47

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

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

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

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

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

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

    
76
  public getResults(keyword:string,refine:boolean, page: number, size: number){
77
    var parameters = "";
78
    if(keyword.length > 0){
79
      parameters = "q="+'"' + keyword + '"';
80
    }
81
    this._getResults(parameters,refine,page,this.searchUtils.size);
82
  }
83
  private _getResults(parameters:string,refine:boolean, page: number, size: number){
84
    if(!refine && !this.searchPage){
85
        this.searchPage = new SearchPageComponent(this._location);
86
    }
87
  this.subResults = this._searchProjectsService.searchProjects(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, this.searchPage.getFields()).subscribe(
88
        data => {
89
            this.searchUtils.totalResults = data[0];
90
            console.info("search Projects: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
91
            this.results = data[1];
92
            this.filters = data[2];
93
            this.searchPage.checkSelectedFilters(this.filters);
94
            // this.filters = this.searchPage.checkSelectedFilters(data[2]);
95
            this.searchPage.updateBaseUrlWithParameters(this.filters);
96
            var errorCodes:ErrorCodes = new ErrorCodes();
97
            this.searchUtils.status = errorCodes.DONE;
98
            if(this.searchUtils.totalResults == 0 ){
99
              this.searchUtils.status = errorCodes.NONE;
100
            }
101
        },
102
        err => {
103
            console.error(err);
104
             //TODO check erros (service not available, bad request)
105
            // if( ){
106
            //   this.searchUtils.status = ErrorCodes.ERROR;
107
            // }
108
            var errorCodes:ErrorCodes = new ErrorCodes();
109
            this.searchUtils.status = errorCodes.ERROR;
110
        }
111
    );
112
  }
113

    
114
  public getResultsForDataproviders(id:string, page: number, size: number){
115

    
116
    this._searchProjectsService.getProjectsforDataProvider(id, page, size).subscribe(
117
        data => {
118
            this.searchUtils.totalResults = data[0];
119
            console.info("search Projects for Dataproviders: [Id:"+id+" ]  [total results:"+this.searchUtils.totalResults+"]");
120
            this.results = data[1];
121

    
122
            var errorCodes:ErrorCodes = new ErrorCodes();
123
            this.searchUtils.status = errorCodes.DONE;
124
            if(this.searchUtils.totalResults == 0 ){
125
              this.searchUtils.status = errorCodes.NONE;
126
            }
127
        },
128
        err => {
129
            console.error(err);
130
             //TODO check erros (service not available, bad request)
131
            // if( ){
132
            //   this.searchUtils.status = ErrorCodes.ERROR;
133
            // }
134
            var errorCodes:ErrorCodes = new ErrorCodes();
135
            this.searchUtils.status = errorCodes.ERROR;
136
        }
137
    );
138
  }
139

    
140

    
141
  public queryChanged($event) {
142
    this.urlParams = undefined;
143
    var parameters = $event.value;
144
    this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size);
145
  }
146

    
147
}
(5-5/6)