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 {SearchOrganizationsService} from '../../services/searchOrganizations.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-organizations',
|
14
|
template: `
|
15
|
|
16
|
<search-page pageTitle="Search Organizations" type="organizations" [(filters)] = "filters"
|
17
|
[(results)] = "results" [(searchUtils)] = "searchUtils"
|
18
|
[baseUrl] = "baseUrl" (queryChange)="queryChanged($event)" >
|
19
|
</search-page>
|
20
|
|
21
|
`
|
22
|
|
23
|
})
|
24
|
export class SearchOrganizationsComponent {
|
25
|
public results =[];
|
26
|
public filters =[];
|
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.ORGANIZATION_REFINE_FIELDS;
|
33
|
public fieldIdsMap = this.searchFields.ORGANIZATION_FIELDS;
|
34
|
public urlParams : Map<string, string>;
|
35
|
public _location:Location;
|
36
|
|
37
|
@ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
|
38
|
|
39
|
constructor (private route: ActivatedRoute, private _searchOrganizationsService: SearchOrganizationsService ) {
|
40
|
|
41
|
var errorCodes:ErrorCodes = new ErrorCodes();
|
42
|
this.searchUtils.status =errorCodes.LOADING;
|
43
|
this.baseUrl = OpenaireProperties.getLinkToSearchOrganizations();
|
44
|
|
45
|
}
|
46
|
|
47
|
public ngOnInit() {
|
48
|
this.searchPage.refineFields = this.refineFields;
|
49
|
this.searchPage.fieldIdsMap = this.fieldIdsMap;
|
50
|
|
51
|
this.sub = this.route.queryParams.subscribe(params => {
|
52
|
this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
|
53
|
this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
|
54
|
|
55
|
var queryParameters = this.searchPage.getQueryParametersFromUrl(params);
|
56
|
this._getResults(queryParameters, true, this.searchUtils.page, this.searchUtils.size);
|
57
|
});
|
58
|
}
|
59
|
|
60
|
public ngOnDestroy() {
|
61
|
if(this.sub){
|
62
|
this.sub.unsubscribe();
|
63
|
}
|
64
|
if(this.subResults){
|
65
|
this.subResults.unsubscribe();
|
66
|
}
|
67
|
}
|
68
|
|
69
|
|
70
|
public getResults(keyword:string,refine:boolean, page: number, size: number){
|
71
|
var parameters = "";
|
72
|
if(keyword.length > 0){
|
73
|
parameters = "q="+'"' + keyword + '"';
|
74
|
}
|
75
|
this._getResults(parameters,refine,page,this.searchUtils.size);
|
76
|
}
|
77
|
private _getResults(parameters:string,refine:boolean, page: number, size: number){
|
78
|
|
79
|
if(!refine && !this.searchPage){
|
80
|
this.searchPage = new SearchPageComponent(this._location);
|
81
|
}
|
82
|
var errorCodes:ErrorCodes = new ErrorCodes();
|
83
|
this.searchUtils.status = errorCodes.LOADING;
|
84
|
this.subResults = this._searchOrganizationsService.searchOrganizations(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, this.searchPage.getFields()).subscribe(
|
85
|
data => {
|
86
|
this.searchUtils.totalResults = data[0];
|
87
|
console.info("search Organizations: [Parameters:"+parameters+" ] [total results:"+this.searchUtils.totalResults+"]");
|
88
|
this.results = data[1];
|
89
|
this.filters = data[2];
|
90
|
this.searchPage.checkSelectedFilters(this.filters);
|
91
|
this.searchPage.updateBaseUrlWithParameters(this.filters);
|
92
|
var errorCodes:ErrorCodes = new ErrorCodes();
|
93
|
this.searchUtils.status = errorCodes.DONE;
|
94
|
if(this.searchUtils.totalResults == 0 ){
|
95
|
this.searchUtils.status = errorCodes.NONE;
|
96
|
}
|
97
|
},
|
98
|
err => {
|
99
|
console.log(err);
|
100
|
//TODO check erros (service not available, bad request)
|
101
|
// if( ){
|
102
|
// this.searchUtils.status = ErrorCodes.ERROR;
|
103
|
// }
|
104
|
var errorCodes:ErrorCodes = new ErrorCodes();
|
105
|
this.searchUtils.status = errorCodes.ERROR;
|
106
|
}
|
107
|
);
|
108
|
}
|
109
|
|
110
|
|
111
|
public queryChanged($event) {
|
112
|
var parameters = $event.value;
|
113
|
console.info("queryChanged: Execute search query "+parameters);
|
114
|
this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size);
|
115
|
}
|
116
|
}
|