1
|
import {Component, Input, ViewChild} from '@angular/core';
|
2
|
import {Observable} from 'rxjs';
|
3
|
import { Router, ActivatedRoute} from '@angular/router';
|
4
|
import {Filter, Value,AdvancedField} from '../searchUtils/searchHelperClasses.class';
|
5
|
import {SearchProjectsService} from '../../services/searchProjects.service';
|
6
|
import {SearchResult} from '../../utils/entities/searchResult';
|
7
|
import {ErrorCodes} from '../../utils/properties/errorCodes';
|
8
|
import {ErrorMessagesComponent} from '../../utils/errorMessages.component';
|
9
|
import {AdvancedSearchPageComponent} from '../searchUtils/advancedSearchPage.component';
|
10
|
import {SearchFields, FieldDetails} from '../../utils/properties/searchFields';
|
11
|
import {SearchCustomFilter, SearchUtilsClass} from '../searchUtils/searchUtils.class';
|
12
|
import{EnvProperties} from '../../utils/properties/env-properties';
|
13
|
|
14
|
@Component({
|
15
|
selector: 'advanced-search-projects',
|
16
|
template: `
|
17
|
<advanced-search-page pageTitle="Advanced Search for Projects" entityType="project"
|
18
|
type = "projects"
|
19
|
[(results)] = "results"
|
20
|
[(searchUtils)] = "searchUtils"
|
21
|
[(fieldIds)]="fieldIds" [(fieldIdsMap)]="fieldIdsMap" [(selectedFields)]="selectedFields"
|
22
|
(queryChange)="queryChanged($event)"
|
23
|
[csvParams]="csvParams" csvPath="projects" simpleSearchLink="/search/find/projects"
|
24
|
[disableForms]="disableForms"
|
25
|
[loadPaging]="loadPaging"
|
26
|
[oldTotalResults]="oldTotalResults" [piwikSiteId]=piwikSiteId
|
27
|
searchFormClass="projectsSearchForm">
|
28
|
</advanced-search-page>
|
29
|
|
30
|
`
|
31
|
})
|
32
|
|
33
|
export class AdvancedSearchProjectsComponent {
|
34
|
private errorCodes: ErrorCodes;
|
35
|
private errorMessages: ErrorMessagesComponent;
|
36
|
@Input() piwikSiteId = null;
|
37
|
@Input() customFilter:SearchCustomFilter= null;
|
38
|
public results =[];
|
39
|
public filters =[];
|
40
|
|
41
|
public searchUtils:SearchUtilsClass = new SearchUtilsClass();
|
42
|
public searchFields:SearchFields = new SearchFields();
|
43
|
|
44
|
public fieldIds: string[] = this.searchFields.PROJECT_ADVANCED_FIELDS;
|
45
|
public fieldIdsMap = this.searchFields.PROJECT_FIELDS;
|
46
|
public selectedFields:AdvancedField[] = [];
|
47
|
properties:EnvProperties;
|
48
|
|
49
|
@ViewChild (AdvancedSearchPageComponent) searchPage : AdvancedSearchPageComponent ;
|
50
|
public resourcesQuery = "(oaftype exact project)";
|
51
|
public csvParams: string;
|
52
|
public disableForms: boolean = false;
|
53
|
public loadPaging: boolean = true;
|
54
|
public oldTotalResults: number = 0;
|
55
|
public pagingLimit: number = 0;
|
56
|
public isPiwikEnabled;
|
57
|
|
58
|
constructor (private route: ActivatedRoute, private _searchProjectsService: SearchProjectsService ) {
|
59
|
|
60
|
this.results =[];
|
61
|
this.errorCodes = new ErrorCodes();
|
62
|
this.errorMessages = new ErrorMessagesComponent();
|
63
|
this.searchUtils.status = this.errorCodes.LOADING;
|
64
|
|
65
|
|
66
|
|
67
|
|
68
|
}
|
69
|
ngOnInit() {
|
70
|
this.route.data
|
71
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
72
|
this.properties= data.envSpecific;
|
73
|
this.searchUtils.baseUrl = data.envSpecific.searchLinkToAdvancedProjects;
|
74
|
this.pagingLimit = data.envSpecific.pagingLimit;
|
75
|
this.isPiwikEnabled = data.envSpecific.enablePiwikTrack;
|
76
|
|
77
|
});
|
78
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
79
|
this.searchUtils.status = this.errorCodes.LOADING;
|
80
|
this.sub = this.route.queryParams.subscribe(params => {
|
81
|
if(params['page'] && this.searchUtils.page != params['page']) {
|
82
|
this.loadPaging = false;
|
83
|
this.oldTotalResults = this.searchUtils.totalResults;
|
84
|
}
|
85
|
|
86
|
let page = (params['page']=== undefined)?1:+params['page'];
|
87
|
this.searchUtils.page = ( page <= 0 ) ? 1 : page;
|
88
|
|
89
|
this.searchUtils.size = (params['size']=== undefined)?10:+params['size'];
|
90
|
if(this.searchUtils.size != 5 && this.searchUtils.size != 10 && this.searchUtils.size != 20 && this.searchUtils.size != 50) {
|
91
|
this.searchUtils.size = 10;
|
92
|
}
|
93
|
|
94
|
this.searchPage.fieldIds = this.fieldIds;
|
95
|
this.selectedFields =[];
|
96
|
this.searchPage.selectedFields = this.selectedFields;
|
97
|
this.searchPage.fieldIdsMap = this.fieldIdsMap;
|
98
|
this.searchPage.customFilter = this.customFilter;
|
99
|
this.searchPage.getSelectedFiltersFromUrl(params);
|
100
|
this.getResults(this.searchPage.createQueryParameters(), this.searchUtils.page, this.searchUtils.size);
|
101
|
|
102
|
});
|
103
|
}
|
104
|
ngOnDestroy() {
|
105
|
this.sub.unsubscribe();
|
106
|
}
|
107
|
sub: any;
|
108
|
public getResults(parameters:string, page: number, size: number){
|
109
|
if(page > this.pagingLimit) {
|
110
|
size=0;
|
111
|
}
|
112
|
if(page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
|
113
|
if(parameters!= null && parameters != '' ) {
|
114
|
this.csvParams ="&fq=( "+this.resourcesQuery + "and (" + parameters + "))";
|
115
|
}else{
|
116
|
this.csvParams ="&fq="+this.resourcesQuery;
|
117
|
}
|
118
|
|
119
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
120
|
this.searchUtils.status = this.errorCodes.LOADING;
|
121
|
//this.searchPage.openLoading();
|
122
|
this.disableForms = true;
|
123
|
this.results = [];
|
124
|
this.searchUtils.totalResults = 0;
|
125
|
|
126
|
//console.info("Advanced Search for Publications: Execute search query "+parameters);
|
127
|
this._searchProjectsService.advancedSearchProjects(parameters, page, size, this.properties).subscribe(
|
128
|
data => {
|
129
|
this.searchUtils.totalResults = data[0];
|
130
|
this.results = data[1];
|
131
|
this.searchPage.updateBaseUrlWithParameters();
|
132
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
133
|
this.searchUtils.status = this.errorCodes.DONE;
|
134
|
if(this.searchUtils.totalResults == 0 ){
|
135
|
this.searchUtils.status = this.errorCodes.NONE;
|
136
|
}
|
137
|
//this.searchPage.closeLoading();
|
138
|
this.disableForms = false;
|
139
|
|
140
|
if(this.searchUtils.status == this.errorCodes.DONE) {
|
141
|
// Page out of limit!!!
|
142
|
let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
|
143
|
if(!(Number.isInteger(totalPages))) {
|
144
|
totalPages = (parseInt(totalPages, 10) + 1);
|
145
|
}
|
146
|
if(totalPages < page) {
|
147
|
this.searchUtils.totalResults = 0;
|
148
|
this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
|
149
|
}
|
150
|
}
|
151
|
},
|
152
|
err => {
|
153
|
//console.log(err);
|
154
|
this.handleError("Error getting projects", err);
|
155
|
this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
|
156
|
|
157
|
//TODO check erros (service not available, bad request)
|
158
|
// if( ){
|
159
|
// this.searchUtils.status = errorCodes.ERROR;
|
160
|
// }
|
161
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
162
|
//this.searchUtils.status = errorCodes.NOT_AVAILABLE;
|
163
|
/*if(err.status == '404') {
|
164
|
this.searchUtils.status = this.errorCodes.NOT_FOUND;
|
165
|
} else if(err.status == '500') {
|
166
|
this.searchUtils.status = this.errorCodes.ERROR;
|
167
|
} else {
|
168
|
this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
|
169
|
}*/
|
170
|
|
171
|
//this.searchPage.closeLoading();
|
172
|
this.disableForms = false;
|
173
|
|
174
|
}
|
175
|
);
|
176
|
}
|
177
|
}
|
178
|
private setFilters(){
|
179
|
//TODO set filters from
|
180
|
}
|
181
|
|
182
|
public queryChanged($event) {
|
183
|
this.loadPaging = true;
|
184
|
|
185
|
var parameters = $event.value;
|
186
|
//this.getResults(parameters, this.searchUtils.page,this.searchUtils.size);
|
187
|
//console.info("queryChanged: Execute search query "+parameters);
|
188
|
|
189
|
}
|
190
|
|
191
|
private handleError(message: string, error) {
|
192
|
console.error("Projects advanced Search Page: "+message, error);
|
193
|
}
|
194
|
}
|