1
|
import {Component, EventEmitter, Input, Output, ViewChild} from '@angular/core';
|
2
|
import {ActivatedRoute} from '@angular/router';
|
3
|
import {AdvancedField} from './searchUtils/searchHelperClasses.class';
|
4
|
import {SearchOrganizationsService} from '../services/searchOrganizations.service';
|
5
|
import {ErrorCodes} from '../utils/properties/errorCodes';
|
6
|
import {ErrorMessagesComponent} from '../utils/errorMessages.component';
|
7
|
import {SearchFields} from '../utils/properties/searchFields';
|
8
|
import {SearchCustomFilter, SearchUtilsClass} from './searchUtils/searchUtils.class';
|
9
|
import {EnvProperties} from '../utils/properties/env-properties';
|
10
|
import {NewSearchPageComponent} from "./searchUtils/newSearchPage.component";
|
11
|
|
12
|
|
13
|
@Component({
|
14
|
selector: 'search-organizations',
|
15
|
template: `
|
16
|
<new-search-page
|
17
|
pageTitle="{{(simpleView?'':'Advanced ')}} Search for {{ 'organizations' | titlecase }}"
|
18
|
entityType="organization"
|
19
|
type="organizations"
|
20
|
[results]="results"
|
21
|
[searchUtils]="searchUtils"
|
22
|
[fieldIds]="fieldIds" [fieldIdsMap]="fieldIdsMap" [selectedFields]="selectedFields"
|
23
|
[csvParams]="csvParams" csvPath="organizations"
|
24
|
[simpleSearchLink]="simpleSearchLink" [advancedSearchLink]="advancedSearchLink"
|
25
|
[disableForms]="disableForms"
|
26
|
[loadPaging]="loadPaging"
|
27
|
[oldTotalResults]="oldTotalResults"
|
28
|
[openaireLink]=openaireLink
|
29
|
[piwikSiteId]=piwikSiteId [hasPrefix]="hasPrefix"
|
30
|
searchFormClass="organizationsSearchForm"
|
31
|
[includeOnlyResultsAndFilter]="includeOnlyResultsAndFilter"
|
32
|
|
33
|
[filters]="filters"
|
34
|
[simpleView]="simpleView" formPlaceholderText="Search by organization name..."
|
35
|
>
|
36
|
</new-search-page>
|
37
|
`
|
38
|
})
|
39
|
|
40
|
export class SearchOrganizationsComponent {
|
41
|
private errorCodes: ErrorCodes;
|
42
|
private errorMessages: ErrorMessagesComponent;
|
43
|
properties:EnvProperties;
|
44
|
@Input() piwikSiteId = null;
|
45
|
public results =[];
|
46
|
public filters =[];
|
47
|
public searchUtils:SearchUtilsClass = new SearchUtilsClass();
|
48
|
public searchFields:SearchFields = new SearchFields();
|
49
|
|
50
|
public fieldIds: string[] = this.searchFields.ORGANIZATION_ADVANCED_FIELDS;
|
51
|
public fieldIdsMap = this.searchFields.ORGANIZATION_FIELDS;
|
52
|
public selectedFields:AdvancedField[] = [];
|
53
|
public csvParams: string;
|
54
|
public disableForms: boolean = false;
|
55
|
public loadPaging: boolean = true;
|
56
|
public oldTotalResults: number = 0;
|
57
|
public pagingLimit: number = 0;
|
58
|
public isPiwikEnabled;
|
59
|
|
60
|
@Input() customFilter:SearchCustomFilter= null;
|
61
|
public refineFields: string[] = this.searchFields.ORGANIZATION_REFINE_FIELDS;
|
62
|
@ViewChild(NewSearchPageComponent) searchPage: NewSearchPageComponent;
|
63
|
@Input() simpleView: boolean = true;
|
64
|
@Input() simpleSearchLink: string = "";
|
65
|
advancedSearchLink: string = "";
|
66
|
@Input() hasPrefix: boolean = true;
|
67
|
@Input() openaireLink: string;
|
68
|
@Input() includeOnlyResultsAndFilter: boolean = false;
|
69
|
@Output() searchPageUpdates = new EventEmitter();
|
70
|
public resourcesQuery = "(oaftype exact organization)";
|
71
|
constructor (private route: ActivatedRoute, private _searchOrganizationsService: SearchOrganizationsService ) {
|
72
|
this.results =[];
|
73
|
this.errorCodes = new ErrorCodes();
|
74
|
this.errorMessages = new ErrorMessagesComponent();
|
75
|
this.searchUtils.status = this.errorCodes.LOADING;
|
76
|
|
77
|
|
78
|
|
79
|
}
|
80
|
ngOnInit() {
|
81
|
this.route.data
|
82
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
83
|
this.properties= data.envSpecific;
|
84
|
if (!this.simpleSearchLink) {
|
85
|
this.simpleSearchLink = this.properties.searchLinkToOrganizations;
|
86
|
} this.advancedSearchLink = this.properties.searchLinkToAdvancedOrganizations;
|
87
|
this.searchUtils.baseUrl = (this.simpleView)?this.simpleSearchLink:this.advancedSearchLink;
|
88
|
this.pagingLimit = data.envSpecific.pagingLimit;
|
89
|
this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
|
90
|
|
91
|
});
|
92
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
93
|
this.searchUtils.status = this.errorCodes.LOADING;
|
94
|
var firstLoad = true;
|
95
|
|
96
|
this.sub = this.route.queryParams.subscribe(params => {
|
97
|
this.loadPaging = true;
|
98
|
if(params['page'] && this.searchUtils.page != params['page']) {
|
99
|
this.loadPaging = false;
|
100
|
this.oldTotalResults = this.searchUtils.totalResults;
|
101
|
}
|
102
|
var refine = true;
|
103
|
if (this.searchUtils.page != ((params['page'] === undefined) ? 1 : +params['page']) && this.filters && !firstLoad) {
|
104
|
refine = false;
|
105
|
|
106
|
}
|
107
|
let page = (params['page']=== undefined)?1:+params['page'];
|
108
|
this.searchUtils.page = ( page <= 0 ) ? 1 : page;
|
109
|
|
110
|
this.searchUtils.size = (params['size']=== undefined)?10:+params['size'];
|
111
|
if(this.searchUtils.size != 5 && this.searchUtils.size != 10 && this.searchUtils.size != 20 && this.searchUtils.size != 50) {
|
112
|
this.searchUtils.size = 10;
|
113
|
}
|
114
|
|
115
|
this.searchPage.fieldIds = this.fieldIds;
|
116
|
this.selectedFields =[];
|
117
|
// this.searchPage.selectedFields = this.selectedFields;
|
118
|
// this.searchPage.fieldIdsMap = this.fieldIdsMap;
|
119
|
// this.searchPage.customFilter = this.customFilter;
|
120
|
// this.searchPage.getSelectedFiltersFromUrl(params);
|
121
|
this.searchPage.prepareSearchPage(this.fieldIds, this.selectedFields, this.refineFields, [], this.fieldIdsMap,this.customFilter,params, "organization");
|
122
|
|
123
|
this.getResults(this.searchPage.getSearchAPIQueryForAdvancedSearhFields(), this.searchUtils.page, this.searchUtils.size, refine, this.searchPage.getSearchAPIQueryForRefineFields(params, firstLoad));
|
124
|
|
125
|
});
|
126
|
}
|
127
|
ngOnDestroy() {
|
128
|
this.sub.unsubscribe();
|
129
|
}
|
130
|
sub: any;
|
131
|
public getResults(parameters:string, page: number, size: number, refine: boolean, refineFieldsFilterQuery = null){
|
132
|
if(page > this.pagingLimit) {
|
133
|
size=0;
|
134
|
}
|
135
|
if(page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
|
136
|
// if(parameters!= null && parameters != '' ) {
|
137
|
// this.csvParams ="&fq=( "+this.resourcesQuery + "and (" + parameters + "))";
|
138
|
// }else{
|
139
|
// this.csvParams ="&fq="+this.resourcesQuery;
|
140
|
// }
|
141
|
|
142
|
this.csvParams = (parameters ? ("&fq=("+parameters) : "") + (parameters ? ")" : "");
|
143
|
this.csvParams += (refineFieldsFilterQuery ? refineFieldsFilterQuery : "");
|
144
|
|
145
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
146
|
this.searchUtils.status = this.errorCodes.LOADING;
|
147
|
//this.searchPage.openLoading();
|
148
|
this.disableForms = true;
|
149
|
this.searchPageUpdates.emit({disableForms: this.disableForms,searchUtils: this.searchUtils});
|
150
|
|
151
|
this.results = [];
|
152
|
this.searchUtils.totalResults = 0;
|
153
|
|
154
|
//console.info("Advanced Search for Organizations: Execute search query "+parameters);
|
155
|
this._searchOrganizationsService.advancedSearchOrganizations(parameters, page, size, this.properties,(refine) ? this.searchPage.getRefineFieldsQuery() : null, this.searchPage.getFields(), refineFieldsFilterQuery).subscribe(
|
156
|
data => {
|
157
|
this.searchUtils.totalResults = data[0];
|
158
|
this.results = data[1];
|
159
|
if (refine) {
|
160
|
this.filters = this.searchPage.prepareFiltersToShow(data[2], this.searchUtils.totalResults);
|
161
|
}else{
|
162
|
this.searchPage.buildPageURLParameters(this.filters, [],false);
|
163
|
}
|
164
|
// this.searchPage.updateBaseUrlWithParameters();
|
165
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
166
|
this.searchUtils.status = this.errorCodes.DONE;
|
167
|
if(this.searchUtils.totalResults == 0 ){
|
168
|
this.searchUtils.status = this.errorCodes.NONE;
|
169
|
}
|
170
|
//this.searchPage.closeLoading();
|
171
|
this.disableForms = false;
|
172
|
this.searchPageUpdates.emit({disableForms: this.disableForms,searchUtils: this.searchUtils});
|
173
|
|
174
|
if(this.searchUtils.status == this.errorCodes.DONE) {
|
175
|
// Page out of limit!!!
|
176
|
let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
|
177
|
if(!(Number.isInteger(totalPages))) {
|
178
|
totalPages = (parseInt(totalPages, 10) + 1);
|
179
|
}
|
180
|
if(totalPages < page) {
|
181
|
this.searchUtils.totalResults = 0;
|
182
|
this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
|
183
|
}
|
184
|
}
|
185
|
},
|
186
|
err => {
|
187
|
//console.log(err);
|
188
|
this.handleError("Error getting organizations", err);
|
189
|
this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
|
190
|
|
191
|
//TODO check erros (service not available, bad request)
|
192
|
// if( ){
|
193
|
// this.searchUtils.status = errorCodes.ERROR;
|
194
|
// }
|
195
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
196
|
//this.searchUtils.status = errorCodes.NOT_AVAILABLE;
|
197
|
/*if(err.status == '404') {
|
198
|
this.searchUtils.status = this.errorCodes.NOT_FOUND;
|
199
|
} else if(err.status == '500') {
|
200
|
this.searchUtils.status = this.errorCodes.ERROR;
|
201
|
} else {
|
202
|
this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
|
203
|
}*/
|
204
|
|
205
|
//this.searchPage.closeLoading();
|
206
|
this.disableForms = false;
|
207
|
this.searchPageUpdates.emit({disableForms: this.disableForms,searchUtils: this.searchUtils});
|
208
|
|
209
|
}
|
210
|
);
|
211
|
}
|
212
|
}
|
213
|
|
214
|
private handleError(message: string, error) {
|
215
|
console.error("Organizations advanced Search Page: "+message, error);
|
216
|
}
|
217
|
}
|