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