1
|
import {SearchProjectsService} from '../../services/searchProjects.service';
|
2
|
import {ErrorCodes} from '../../utils/properties/errorCodes';
|
3
|
import {ErrorMessagesComponent} from '../../utils/errorMessages.component';
|
4
|
import {SearchCustomFilter, SearchUtilsClass} from '../../searchPages/searchUtils/searchUtils.class';
|
5
|
import {EnvProperties} from '../../utils/properties/env-properties';
|
6
|
import {StringUtils} from '../../utils/string-utils.class';
|
7
|
import {SearchResult} from "../entities/searchResult";
|
8
|
import {Subscriber} from "rxjs";
|
9
|
|
10
|
export class FetchProjects {
|
11
|
private errorCodes: ErrorCodes;
|
12
|
private errorMessages: ErrorMessagesComponent;
|
13
|
|
14
|
public results: SearchResult[] = [];
|
15
|
|
16
|
public filters; // for getResultsForOrganizations
|
17
|
public funders: any = []; // for getResultsForOrganizations // this is filled with the initial query - before filtering
|
18
|
|
19
|
subscriptions = [];
|
20
|
public searchUtils: SearchUtilsClass = new SearchUtilsClass();
|
21
|
|
22
|
|
23
|
constructor(private _searchProjectsService: SearchProjectsService) {
|
24
|
this.errorCodes = new ErrorCodes();
|
25
|
this.errorMessages = new ErrorMessagesComponent();
|
26
|
this.searchUtils.status = this.errorCodes.LOADING;
|
27
|
|
28
|
}
|
29
|
|
30
|
public clearSubscriptions() {
|
31
|
|
32
|
this.subscriptions.forEach(subscription => {
|
33
|
if (subscription instanceof Subscriber) {
|
34
|
subscription.unsubscribe();
|
35
|
}
|
36
|
});
|
37
|
}
|
38
|
public getResultsByKeyword(keyword: string, page: number, size: number, properties: EnvProperties, customFilter: SearchCustomFilter = null) {
|
39
|
var parameters = "";
|
40
|
if (keyword.length > 0) {
|
41
|
parameters = "q=" + StringUtils.URIEncode(keyword);
|
42
|
}
|
43
|
|
44
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
45
|
this.searchUtils.status = this.errorCodes.LOADING;
|
46
|
var refineParams = null;
|
47
|
if (customFilter) {
|
48
|
refineParams = (refineParams ? (refineParams + '&') : '') + "&fq=" + StringUtils.URIEncode(customFilter.queryFieldName + " exact " + StringUtils.quote((customFilter.valueId)));
|
49
|
}
|
50
|
this.subscriptions.push(this._searchProjectsService.searchProjects(parameters, refineParams, page, size, [], properties).subscribe(
|
51
|
data => {
|
52
|
this.searchUtils.totalResults = data[0];
|
53
|
//console.info("search Projects: [Parameters:"+parameters+" ] [total results:"+this.searchUtils.totalResults+"]");
|
54
|
this.results = data[1];
|
55
|
|
56
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
57
|
this.searchUtils.status = this.errorCodes.DONE;
|
58
|
if (this.searchUtils.totalResults == 0) {
|
59
|
this.searchUtils.status = this.errorCodes.NONE;
|
60
|
}
|
61
|
},
|
62
|
err => {
|
63
|
/*console.log(err);
|
64
|
//TODO check erros (service not available, bad request)
|
65
|
// if( ){
|
66
|
// this.searchUtils.status = ErrorCodes.ERROR;
|
67
|
// }
|
68
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
69
|
//this.searchUtils.status = errorCodes.ERROR;
|
70
|
if(err.status == '404') {
|
71
|
this.searchUtils.status = this.errorCodes.NOT_FOUND;
|
72
|
} else if(err.status == '500') {
|
73
|
this.searchUtils.status = this.errorCodes.ERROR;
|
74
|
} else {
|
75
|
this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
|
76
|
}*/
|
77
|
this.handleError("Error getting projects for keyword: " + keyword, err);
|
78
|
this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
|
79
|
}
|
80
|
));
|
81
|
}
|
82
|
|
83
|
public getResultsForDataproviders(id: string, page: number, size: number, properties: EnvProperties) {
|
84
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
85
|
this.searchUtils.status = this.errorCodes.LOADING;
|
86
|
|
87
|
this.subscriptions.push(this._searchProjectsService.getProjectsforDataProvider(id, page, size, properties).subscribe(
|
88
|
data => {
|
89
|
this.searchUtils.totalResults = data[0];
|
90
|
//console.info("search Projects for Dataproviders: [Id:"+id+" ] [total results:"+this.searchUtils.totalResults+"]");
|
91
|
this.results = data[1];
|
92
|
|
93
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
94
|
this.searchUtils.status = this.errorCodes.DONE;
|
95
|
if (this.searchUtils.totalResults == 0) {
|
96
|
this.searchUtils.status = this.errorCodes.NONE;
|
97
|
}
|
98
|
},
|
99
|
err => {
|
100
|
/*console.log(err);
|
101
|
//TODO check erros (service not available, bad request)
|
102
|
// if( ){
|
103
|
// this.searchUtils.status = ErrorCodes.ERROR;
|
104
|
// }
|
105
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
106
|
//this.searchUtils.status = errorCodes.ERROR;
|
107
|
if(err.status == '404') {
|
108
|
this.searchUtils.status = this.errorCodes.NOT_FOUND;
|
109
|
} else if(err.status == '500') {
|
110
|
this.searchUtils.status = this.errorCodes.ERROR;
|
111
|
} else {
|
112
|
this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
|
113
|
}*/
|
114
|
this.handleError("Error getting projects for content provider with id: " + id, err);
|
115
|
this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
|
116
|
}
|
117
|
));
|
118
|
}
|
119
|
|
120
|
public getNumForEntity(entity: string, id: string, properties: EnvProperties) {
|
121
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
122
|
this.searchUtils.status = this.errorCodes.LOADING;
|
123
|
|
124
|
if (id != "" && entity != "") {
|
125
|
|
126
|
this.subscriptions.push(this._searchProjectsService.numOfEntityProjects(id, entity, properties).subscribe(
|
127
|
data => {
|
128
|
this.searchUtils.totalResults = data;
|
129
|
|
130
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
131
|
this.searchUtils.status = this.errorCodes.DONE;
|
132
|
if (this.searchUtils.totalResults == 0) {
|
133
|
this.searchUtils.status = this.errorCodes.NONE;
|
134
|
}
|
135
|
},
|
136
|
err => {
|
137
|
/*console.log(err);
|
138
|
//TODO check erros (service not available, bad request)
|
139
|
// if( ){
|
140
|
// this.searchUtils.status = ErrorCodes.ERROR;
|
141
|
// }
|
142
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
143
|
//this.searchUtils.status = errorCodes.ERROR;
|
144
|
|
145
|
if(err.status == '404') {
|
146
|
this.searchUtils.status = this.errorCodes.NOT_FOUND;
|
147
|
} else if(err.status == '500') {
|
148
|
this.searchUtils.status = this.errorCodes.ERROR;
|
149
|
} else {
|
150
|
this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
|
151
|
}*/
|
152
|
this.handleError("Error getting number of projects for " + entity + " with id: " + id, err);
|
153
|
this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
|
154
|
}
|
155
|
));
|
156
|
}
|
157
|
}
|
158
|
|
159
|
public getResultsForOrganizations(organizationId: string, filterquery: string, page: number, size: number, refineFields: string[], properties: EnvProperties) {
|
160
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
161
|
this.searchUtils.status = this.errorCodes.LOADING;
|
162
|
this.subscriptions.push(this._searchProjectsService.getProjectsForOrganizations(organizationId, filterquery, page, size, refineFields, properties).subscribe(
|
163
|
data => {
|
164
|
this.searchUtils.totalResults = data[0]; // the results can be filtered so this number can be no total results
|
165
|
//console.info("search Projects for Organization: [Id:"+organizationId+" ] [total results:"+this.searchUtils.totalResults+"]");
|
166
|
this.results = data[1];
|
167
|
if (refineFields && refineFields.length > 0) {
|
168
|
this.filters = data[2];
|
169
|
filterquery = decodeURIComponent(filterquery);
|
170
|
for (var i = 0; i < this.filters.length; i++) {
|
171
|
if (filterquery.indexOf(this.filters[i].filterId) !== -1) {
|
172
|
//console.log("this.filters[i].filterId:"+this.filters[i].filterId);
|
173
|
for (var j = 0; j < this.filters[i].values.length; j++) {
|
174
|
//console.log("this.filters[i].values[j].id:"+this.filters[i].values[j].id);
|
175
|
if (filterquery.indexOf(this.filters[i].values[j].id) !== -1) {
|
176
|
this.filters[i].values[j].selected = true;
|
177
|
}
|
178
|
}
|
179
|
}
|
180
|
}
|
181
|
}
|
182
|
if (filterquery == "") {
|
183
|
this.searchUtils.totalResultsNoFilters = this.searchUtils.totalResults;
|
184
|
this.funders = [];
|
185
|
for (var i = 0; i < this.filters.length; i++) {
|
186
|
//console.log("this.filters[i].filterId:"+this.filters[i].filterId);
|
187
|
if (this.filters[i].filterId == "funder") {
|
188
|
this.funders = (this.filters[i].values);
|
189
|
|
190
|
}
|
191
|
}
|
192
|
//console.log(" this.funders:"+ this.funders);
|
193
|
|
194
|
}
|
195
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
196
|
this.searchUtils.status = this.errorCodes.DONE;
|
197
|
if (this.searchUtils.totalResults == 0) {
|
198
|
this.searchUtils.status = this.errorCodes.NONE;
|
199
|
}
|
200
|
},
|
201
|
err => {
|
202
|
this.handleError("Error getting projects for organization with id: " + organizationId, err);
|
203
|
this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
|
204
|
}
|
205
|
));
|
206
|
}
|
207
|
|
208
|
private handleError(message: string, error) {
|
209
|
console.error("Fetch Projects (class): " + message, error);
|
210
|
}
|
211
|
}
|