1
|
/*
|
2
|
* Created by myrto on 12/05/2017
|
3
|
*/
|
4
|
|
5
|
|
6
|
import { Injectable } from '@angular/core';
|
7
|
import { Observable } from 'rxjs/Observable';
|
8
|
import 'rxjs/add/operator/map';
|
9
|
import { Http, Response, Headers, RequestOptions } from '@angular/http';
|
10
|
|
11
|
import {
|
12
|
Aggregations, Country, MetricsInfo, Repository, RepositoryInterface, Timezone,
|
13
|
Typology
|
14
|
} from '../domain/typeScriptClasses';
|
15
|
import { apiUrl } from '../domain/tempAPI';
|
16
|
import { timezones } from '../domain/timezones';
|
17
|
import { typologies } from '../domain/typologies';
|
18
|
|
19
|
let headers = new Headers({ 'Content-Type': 'application/json' });
|
20
|
let httpOptions = new RequestOptions({ headers: headers });
|
21
|
|
22
|
@Injectable ()
|
23
|
export class RepositoryService {
|
24
|
/*private apiUrl = apiUrl + '/repository/';*/
|
25
|
private apiUrl = process.env.API_ENDPOINT + '/repository/';
|
26
|
|
27
|
constructor(private http: Http) { }
|
28
|
|
29
|
addInterface(datatype: string, repoId: string, newInterface: RepositoryInterface): Observable<RepositoryInterface> {
|
30
|
let url = `${this.apiUrl}addInterface?datatype=${datatype}&repoId=${repoId}`;
|
31
|
console.log(`knocking on: ${url}`);
|
32
|
console.log(`sending ${JSON.stringify(newInterface)}`);
|
33
|
httpOptions.withCredentials = true;
|
34
|
return this.http.post(url,newInterface,httpOptions)
|
35
|
.map( res => <RepositoryInterface>res.json())
|
36
|
.catch(this.handleError);
|
37
|
}
|
38
|
|
39
|
updateInterface(repoId: string, interfaceInfo: RepositoryInterface): Observable<RepositoryInterface> {
|
40
|
let url = `${this.apiUrl}updateRepositoryInterface?repoId=${repoId}`;
|
41
|
console.log(`knocking on: ${url}`);
|
42
|
console.log(`sending ${JSON.stringify(interfaceInfo)}`);
|
43
|
httpOptions.withCredentials = true;
|
44
|
return this.http.post(url,interfaceInfo,httpOptions)
|
45
|
.map( res => <RepositoryInterface>res.json())
|
46
|
.catch(this.handleError);
|
47
|
}
|
48
|
|
49
|
deleteInterface(id: string): Observable<string> {
|
50
|
let url = `${this.apiUrl}deleteInterface/?id=${id}`;
|
51
|
console.log(`knocking on: ${url}`);
|
52
|
httpOptions.withCredentials = true;
|
53
|
return this.http.delete(url,httpOptions)
|
54
|
.map( res => res.status.toString() )
|
55
|
.catch(this.handleError);
|
56
|
}
|
57
|
|
58
|
addRepository(datatype: string, newRepository: Repository): Observable<Repository> {
|
59
|
let url = `${this.apiUrl}addRepository?datatype=${datatype}`;
|
60
|
console.log(`knocking on: ${url}`);
|
61
|
console.log(`sending ${JSON.stringify(newRepository)}`);
|
62
|
httpOptions.withCredentials = true;
|
63
|
return this.http.post(url,newRepository,httpOptions)
|
64
|
.map( res => <Repository>res.json())
|
65
|
.catch(this.handleError);
|
66
|
}
|
67
|
|
68
|
updateRepository(repoInfo: Repository): Observable<Repository> {
|
69
|
let url = `${this.apiUrl}updateRepository`;
|
70
|
console.log(`knocking on: ${url}`);
|
71
|
console.log(`sending ${JSON.stringify(repoInfo)}`);
|
72
|
httpOptions.withCredentials = true;
|
73
|
return this.http.post(url,repoInfo,httpOptions)
|
74
|
.map( res => <Repository>res.json())
|
75
|
.catch(this.handleError);
|
76
|
}
|
77
|
|
78
|
getRepositoriesOfCountry(country: string, mode: string): Observable<Repository[]> {
|
79
|
let url = `${this.apiUrl}getRepositoriesByCountry/${country}/${mode}`;
|
80
|
console.log(`knocking on: ${url}`);
|
81
|
return this.http.get(url)
|
82
|
.map( res => <Repository[]>res.json())
|
83
|
.catch(this.handleError);
|
84
|
}
|
85
|
|
86
|
getRepositoriesOfUser(userEmail: string): Observable<Repository[]> {
|
87
|
let url = `${this.apiUrl}getRepositoriesOfUser/${userEmail}/0/100`;
|
88
|
console.log(`knocking on: ${url}`);
|
89
|
return this.http.get(url)
|
90
|
.map( res => <Repository[]>res.json())
|
91
|
.catch(this.handleError);
|
92
|
}
|
93
|
|
94
|
|
95
|
getRepositoryById(id: string): Observable<Repository> {
|
96
|
let url = `${this.apiUrl}getRepositoryById/${id}`;
|
97
|
console.log(`knocking on: ${url}`);
|
98
|
return this.http.get(url)
|
99
|
.map( res => <Repository>res.json())
|
100
|
.do(res => console.log(`got repository with name: ${res.officialName}`))
|
101
|
.catch(this.handleError);
|
102
|
}
|
103
|
|
104
|
getRepositoryInterface(id: string): Observable<RepositoryInterface[]>{
|
105
|
let url = `${this.apiUrl}getRepositoryInterface/${id}`;
|
106
|
console.log(`knocking on: ${url}`);
|
107
|
return this.http.get(url)
|
108
|
.map( res => <RepositoryInterface[]>res.json())
|
109
|
.catch(this.handleError);
|
110
|
}
|
111
|
|
112
|
|
113
|
getUrlsOfUserRepos(userEmail: string): Observable<string[]>{
|
114
|
let url = `${this.apiUrl}getUrlsOfUserRepos/${userEmail}/0/100/`;
|
115
|
console.log(`knocking on: ${url}`);
|
116
|
return this.http.get(url)
|
117
|
.map( res => <string[]>res.json())
|
118
|
.catch(this.handleError);
|
119
|
}
|
120
|
|
121
|
getRepositoryAggregations(id: string): Observable<Aggregations>{
|
122
|
let url = `${this.apiUrl}getRepositoryAggregations/${id}`;
|
123
|
console.log(`knocking on: ${url}`);
|
124
|
return this.http.get(url)
|
125
|
.map(res => <Aggregations>res.json())
|
126
|
.catch(this.handleError);
|
127
|
}
|
128
|
|
129
|
getTimezones(): Observable<Timezone[]>{
|
130
|
/* let url = `${this.apiUrl}getTimezones`;
|
131
|
console.log(`knocking on: ${url}`);
|
132
|
return this.http.get(url)
|
133
|
.map( res => <Timezone[]>res.json())
|
134
|
.catch(this.handleError);*/
|
135
|
return Observable.of(<Timezone[]>timezones);
|
136
|
}
|
137
|
|
138
|
getTypologies(): Observable<Typology[]>{
|
139
|
/* let url = `${this.apiUrl}getTypologies`;
|
140
|
console.log(`knocking on: ${url}`);
|
141
|
return this.http.get(url)
|
142
|
.map( res => <string[]>res.json())
|
143
|
.catch(this.handleError);*/
|
144
|
return Observable.of(<Typology[]>typologies);
|
145
|
}
|
146
|
|
147
|
getCountries(): Observable<Country[]> {
|
148
|
let url = `${this.apiUrl}getCountries`;
|
149
|
console.log(`knocking on: ${url}`);
|
150
|
return this.http.get(url)
|
151
|
.map( res => <Country[]>res.json())
|
152
|
.catch(this.handleError);
|
153
|
}
|
154
|
|
155
|
|
156
|
getCompatibilityClasses (mode: string): Observable<Map<string,string>> {
|
157
|
let url = `${this.apiUrl}getCompatibilityClasses/${mode}`;
|
158
|
console.log(`knocking on: ${url}`);
|
159
|
return this.http.get(url)
|
160
|
.map( res => <Map<string,string>>res.json())
|
161
|
.catch(this.handleError);
|
162
|
}
|
163
|
|
164
|
getDatasourceClasses(mode: string): Observable<Map<string,string>>{
|
165
|
let url = `${this.apiUrl}getDatasourceClasses/${mode}`;
|
166
|
console.log(`knocking on: ${url}`);
|
167
|
return this.http.get(url)
|
168
|
.map( res => <Map<string,string>>res.json())
|
169
|
.catch(this.handleError);
|
170
|
}
|
171
|
|
172
|
|
173
|
getMetricsInfoForRepository (repoId: string): Observable<MetricsInfo> {
|
174
|
let url = `${this.apiUrl}getMetricsInfoForRepository/${repoId}`;
|
175
|
console.log(`knocking on: ${url}`);
|
176
|
return this.http.get(url)
|
177
|
.map( res => <MetricsInfo>res.json())
|
178
|
.catch(this.handleError);
|
179
|
}
|
180
|
|
181
|
updateEnglishName(id: string, englishname: string): Observable<string>{
|
182
|
let url = `${this.apiUrl}updateEnglishName?id=${id}&officialName=DSpace&englishname=${englishname}`;
|
183
|
console.log(`knocking on: ${url}`);
|
184
|
httpOptions.withCredentials = true;
|
185
|
return this.http.post(url,httpOptions)
|
186
|
.map( res => {
|
187
|
console.log(`responded ${res.statusText}`);
|
188
|
return res.status.toString();
|
189
|
})
|
190
|
.catch(this.handleError).share();
|
191
|
}
|
192
|
|
193
|
updateLongtitude(id: string, longtitude: string): Observable<string>{
|
194
|
let url = `${this.apiUrl}updateLongtitude`;
|
195
|
console.log(`knocking on: ${url}`);
|
196
|
let body = JSON.stringify({
|
197
|
id : id,
|
198
|
logntitude: longtitude
|
199
|
});
|
200
|
console.log(`sending ${body}`);
|
201
|
httpOptions.withCredentials = true;
|
202
|
|
203
|
return this.http.post(url,body,httpOptions)
|
204
|
.map( res => <string>res.json())
|
205
|
.catch(this.handleError).share();
|
206
|
}
|
207
|
|
208
|
updateLatitude(id: string, latitude: string): Observable<string>{
|
209
|
let url = `${this.apiUrl}updateLatitude`;
|
210
|
console.log(`knocking on: ${url}`);
|
211
|
let body = JSON.stringify({
|
212
|
id : id,
|
213
|
latitude: latitude
|
214
|
});
|
215
|
console.log(`sending ${body}`);
|
216
|
httpOptions.withCredentials = true;
|
217
|
|
218
|
return this.http.post(url,body,httpOptions)
|
219
|
.map( res => <string>res.json())
|
220
|
.catch(this.handleError).share();
|
221
|
}
|
222
|
|
223
|
updateLogoUrl(id: string, logoUrl: string): Observable<string>{
|
224
|
let url = `${this.apiUrl}updateLogoUrl`;
|
225
|
console.log(`knocking on: ${url}`);
|
226
|
let body = JSON.stringify({
|
227
|
id : id,
|
228
|
logoUrl: logoUrl
|
229
|
});
|
230
|
console.log(`sending ${body}`);
|
231
|
httpOptions.withCredentials = true;
|
232
|
|
233
|
return this.http.post(url, body, httpOptions)
|
234
|
.map( res => <string>res.json())
|
235
|
.catch(this.handleError).share();
|
236
|
}
|
237
|
|
238
|
updateTimezone(id: string, timezone: string): Observable<string>{
|
239
|
let url = `${this.apiUrl}updateTimezone`;
|
240
|
console.log(`knocking on: ${url}`);
|
241
|
let body = JSON.stringify({
|
242
|
id : id,
|
243
|
timezone: timezone
|
244
|
});
|
245
|
console.log(`sending ${body}`);
|
246
|
httpOptions.withCredentials = true;
|
247
|
|
248
|
return this.http.post(url, body, httpOptions)
|
249
|
.map( res => <string>res.json())
|
250
|
.catch(this.handleError).share();
|
251
|
}
|
252
|
|
253
|
getListLatestUpdate(mode: string): Observable<string> {
|
254
|
let url = `${this.apiUrl}getListLatestUpdate/${mode}`;
|
255
|
console.log(`knocking on: ${url}`);
|
256
|
return this.http.get(url)
|
257
|
.map( res => res.json()['lastCollectionDate'])
|
258
|
.catch(this.handleError);
|
259
|
}
|
260
|
|
261
|
|
262
|
private handleError(error: Response | any) {
|
263
|
// In a real world app, we might use a remote logging infrastructure
|
264
|
// We'd also dig deeper into the error to get a better message
|
265
|
let errMsg = "";
|
266
|
console.log('E R R O R !!');
|
267
|
console.log(error);
|
268
|
if (error instanceof Response) {
|
269
|
const body = error.text() || '';
|
270
|
//const err = body.error || JSON.stringify(body);
|
271
|
errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
|
272
|
console.log(errMsg);
|
273
|
} else {
|
274
|
errMsg = (error.message) ? error.message : error.status ? `${error.status} - ${error.statusText}` : 'Server error';
|
275
|
console.error(errMsg); // log to console instead
|
276
|
}
|
277
|
return Observable.throw(errMsg);
|
278
|
}
|
279
|
|
280
|
}
|