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