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
                formPlaceholderText = "Search for Projects"
18
                 type="projects" entityType="project" [(filters)] = "filters"
19
                 [(results)] = "results" [(searchUtils)] = "searchUtils"
20
                 [baseUrl] = "baseUrl" (queryChange)="queryChanged($event)"
21
                 [csvParams]="csvParams" csvPath="projects" advancedSearchLink="/search/advanced/projects"
22
                 [disableForms]="disableForms"
23
                 [loadPaging]="loadPaging"
24
                 [oldTotalResults]="oldTotalResults"
25
                 searchFormClass="projectsSearchForm">
26
    </search-page>
27
     `
28

    
29
})
30
export class SearchProjectsComponent {
31
  private errorCodes: ErrorCodes;
32

    
33
  public results =[];
34
  public filters: Filter[] =[];
35
  public baseUrl:string;
36
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
37
  public sub: any;
38
  public subResults: any;
39
  public searchFields:SearchFields = new SearchFields();
40
  public refineFields: string[] =  this.searchFields.PROJECT_REFINE_FIELDS;
41
  public fieldIdsMap = this.searchFields.PROJECT_FIELDS;
42
  public urlParams : Map<string, string>;
43
  public _location:Location;
44
  public csvParams: string;
45
  public disableForms: boolean = false;
46
  public loadPaging: boolean = true;
47
  public oldTotalResults: number = 0;
48

    
49
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
50

    
51
  constructor (private route: ActivatedRoute, private _searchProjectsService: SearchProjectsService) {
52
    console.info(" constructor SearchProjectsComponent "+this.refineFields.length);
53

    
54
    this.errorCodes = new ErrorCodes();
55
    this.searchUtils.status = this.errorCodes.LOADING;
56
    this.searchUtils.page =1;
57
    this.baseUrl = OpenaireProperties.getLinkToSearchProjects();
58

    
59
  }
60

    
61
  public ngOnInit() {
62
    this.searchPage.refineFields = this.refineFields;
63
    this.searchPage.fieldIdsMap = this.fieldIdsMap;
64
    console.info(" ngOnInit SearchProjectsComponent "+this.refineFields.length);
65
    //get refine field filters  from url parameters
66
    var firstLoad = true;
67
    this.sub =  this.route.queryParams.subscribe(params => {
68
      if(params['page'] && this.searchUtils.page != params['page']) {
69
        this.loadPaging = false;
70
        this.oldTotalResults = this.searchUtils.totalResults;
71
      }
72

    
73
      //get keyword from url parameters
74
      this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
75
      var refine = true;
76
      if(this.searchUtils.page != ((params['page']=== undefined)?1:+params['page']) && this.filters && !firstLoad){
77
        refine = false;
78

    
79
      }
80
      firstLoad = false;
81
      //get page from url parameters
82
      this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
83
      var queryParameters = this.searchPage.getQueryParametersFromUrl(params);
84
      this._getResults(queryParameters, refine, this.searchUtils.page, this.searchUtils.size);
85
    });
86
  }
87

    
88
  public ngOnDestroy() {
89
    if(this.sub){
90
      this.sub.unsubscribe();
91
    }
92
    if(this.subResults){
93
      this.subResults.unsubscribe();
94
    }
95
  }
96

    
97
  public getResults(keyword:string,refine:boolean, page: number, size: number){
98
    var parameters = "";
99
    if(keyword.length > 0){
100
      parameters = "q="+keyword;
101
    }
102
    this._getResults(parameters,refine,page,this.searchUtils.size);
103
  }
104
  private _getResults(parameters:string,refine:boolean, page: number, size: number){
105
      this.csvParams = parameters;
106

    
107
    // if(!refine && !this.searchPage){
108
    //     this.searchPage = new SearchPageComponent(this._location);
109
    // }
110

    
111
    //var errorCodes:ErrorCodes = new ErrorCodes();
112
    this.searchUtils.status = this.errorCodes.LOADING;
113
    //this.searchPage.openLoading();
114
    this.disableForms = true;
115
    this.results = [];
116
    this.searchUtils.totalResults = 0;
117

    
118
  this.subResults = this._searchProjectsService.searchProjects(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, this.searchPage.getFields()).subscribe(
119
        data => {
120
            this.searchUtils.totalResults = data[0];
121
            console.info("search Projects: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
122
            this.results = data[1];
123
            if(refine){
124
              this.filters = data[2];
125
            }
126
            this.searchPage.checkSelectedFilters(this.filters);
127
            // this.filters = this.searchPage.checkSelectedFilters(data[2]);
128
            this.searchPage.updateBaseUrlWithParameters(this.filters);
129
            //var errorCodes:ErrorCodes = new ErrorCodes();
130
            this.searchUtils.status = this.errorCodes.DONE;
131
            if(this.searchUtils.totalResults == 0 ){
132
              this.searchUtils.status = this.errorCodes.NONE;
133
            }
134
            //this.searchPage.closeLoading();
135
            this.disableForms = false;
136

    
137
            if(this.searchUtils.status == this.errorCodes.DONE) {
138
              // Page out of limit!!!
139
              let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
140
              if(!(Number.isInteger(totalPages))) {
141
                  totalPages = (parseInt(totalPages, 10) + 1);
142
              }
143
              if(totalPages < page) {
144
                this.searchUtils.totalResults = 0;
145
                this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
146
              }
147
            }
148
        },
149
        err => {
150
            console.log(err);
151
             //TODO check erros (service not available, bad request)
152
            // if( ){
153
            //   this.searchUtils.status = ErrorCodes.ERROR;
154
            // }
155
            //var errorCodes:ErrorCodes = new ErrorCodes();
156
            //this.searchUtils.status = errorCodes.ERROR;
157
            if(err.status == '404') {
158
              this.searchUtils.status = this.errorCodes.NOT_FOUND;
159
            } else if(err.status == '500') {
160
              this.searchUtils.status = this.errorCodes.ERROR;
161
            } else {
162
              this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
163
            }
164

    
165
            //this.searchPage.closeLoading();
166
            this.disableForms = false;
167
        }
168
    );
169
  }
170

    
171
  public getResultsForDataproviders(id:string, page: number, size: number){
172

    
173
    this._searchProjectsService.getProjectsforDataProvider(id, page, size).subscribe(
174
        data => {
175
            this.searchUtils.totalResults = data[0];
176
            console.info("search Projects for Dataproviders: [Id:"+id+" ]  [total results:"+this.searchUtils.totalResults+"]");
177
            this.results = data[1];
178

    
179
            //var errorCodes:ErrorCodes = new ErrorCodes();
180
            this.searchUtils.status = this.errorCodes.DONE;
181
            if(this.searchUtils.totalResults == 0 ){
182
              this.searchUtils.status = this.errorCodes.NONE;
183
            }
184
        },
185
        err => {
186
            console.log(err);
187
             //TODO check erros (service not available, bad request)
188
            // if( ){
189
            //   this.searchUtils.status = ErrorCodes.ERROR;
190
            // }
191
            //var errorCodes:ErrorCodes = new ErrorCodes();
192
            //this.searchUtils.status = errorCodes.ERROR;
193
            if(err.status == '404') {
194
              this.searchUtils.status = this.errorCodes.NOT_FOUND;
195
            } else if(err.status == '500') {
196
              this.searchUtils.status = this.errorCodes.ERROR;
197
            } else {
198
              this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
199
            }
200
        }
201
    );
202
  }
203

    
204

    
205
  public queryChanged($event) {
206
    this.loadPaging = true;
207
    
208
    this.urlParams = undefined;
209
    var parameters = $event.value;
210
    this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size);
211
  }
212

    
213
}
(11-11/18)