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"
17
                 type="projects" entityType="project" [(filters)] = "filters"
18
                 [(results)] = "results" [(searchUtils)] = "searchUtils"
19
                 [baseUrl] = "baseUrl" (queryChange)="queryChanged($event)"
20
                 [csvParams]="csvParams" csvPath="projects" advancedSearchLink="/search/advanced/projects">
21
    </search-page>
22
     `
23

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

    
39
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
40

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

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

    
49
  }
50

    
51
  public ngOnInit() {
52
    this.searchPage.refineFields = this.refineFields;
53
    this.searchPage.fieldIdsMap = this.fieldIdsMap;
54
    console.info(" ngOnInit SearchProjectsComponent "+this.refineFields.length);
55
    //get refine field filters  from url parameters
56
    var firstLoad = true;
57
    this.sub =  this.route.queryParams.subscribe(params => {
58

    
59
        //get keyword from url parameters
60
        this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
61
        var refine = true;
62
        if(this.searchUtils.page != ((params['page']=== undefined)?1:+params['page']) && this.filters && !firstLoad){
63
          refine = false;
64

    
65
        }
66
        firstLoad = false;
67
        //get page from url parameters
68
        this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
69
       var queryParameters = this.searchPage.getQueryParametersFromUrl(params);
70
        this._getResults(queryParameters, refine, this.searchUtils.page, this.searchUtils.size);
71
    });
72
  }
73

    
74
  public ngOnDestroy() {
75
    if(this.sub){
76
      this.sub.unsubscribe();
77
    }
78
    if(this.subResults){
79
      this.subResults.unsubscribe();
80
    }
81
  }
82

    
83
  public getResults(keyword:string,refine:boolean, page: number, size: number){
84
    var parameters = "";
85
    if(keyword.length > 0){
86
      parameters = "q="+keyword;
87
    }
88
    this._getResults(parameters,refine,page,this.searchUtils.size);
89
  }
90
  private _getResults(parameters:string,refine:boolean, page: number, size: number){
91
      this.csvParams = parameters;
92

    
93
    // if(!refine && !this.searchPage){
94
    //     this.searchPage = new SearchPageComponent(this._location);
95
    // }
96

    
97
    var errorCodes:ErrorCodes = new ErrorCodes();
98
    this.searchUtils.status = errorCodes.LOADING;
99
    this.searchPage.openLoading();
100

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

    
132
  public getResultsForDataproviders(id:string, page: number, size: number){
133

    
134
    this._searchProjectsService.getProjectsforDataProvider(id, page, size).subscribe(
135
        data => {
136
            this.searchUtils.totalResults = data[0];
137
            console.info("search Projects for Dataproviders: [Id:"+id+" ]  [total results:"+this.searchUtils.totalResults+"]");
138
            this.results = data[1];
139

    
140
            var errorCodes:ErrorCodes = new ErrorCodes();
141
            this.searchUtils.status = errorCodes.DONE;
142
            if(this.searchUtils.totalResults == 0 ){
143
              this.searchUtils.status = errorCodes.NONE;
144
            }
145
        },
146
        err => {
147
            console.log(err);
148
             //TODO check erros (service not available, bad request)
149
            // if( ){
150
            //   this.searchUtils.status = ErrorCodes.ERROR;
151
            // }
152
            var errorCodes:ErrorCodes = new ErrorCodes();
153
            this.searchUtils.status = errorCodes.ERROR;
154
        }
155
    );
156
  }
157

    
158

    
159
  public queryChanged($event) {
160
    this.urlParams = undefined;
161
    var parameters = $event.value;
162
    this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size);
163
  }
164

    
165
}
(14-14/18)