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