Project

General

Profile

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
    httpOptions.withCredentials = true;
90
    return this.http.get(url, httpOptions)
91
      .map( res => <Repository[]>res.json())
92
      .catch(this.handleError);
93
  }
94

    
95

    
96
  getRepositoryById(id: string): Observable<Repository> {
97
    let url = `${this.apiUrl}getRepositoryById/${id}`;
98
    console.log(`knocking on: ${url}`);
99
    httpOptions.withCredentials = true;
100
    return this.http.get(url, httpOptions)
101
      .map( res => <Repository>res.json())
102
      .do(res => console.log(`got repository with name: ${res.officialName}`))
103
      .catch(this.handleError);
104
  }
105

    
106
  getRepositoryInterface(id: string): Observable<RepositoryInterface[]>{
107
    let url = `${this.apiUrl}getRepositoryInterface/${id}`;
108
    console.log(`knocking on: ${url}`);
109
    httpOptions.withCredentials = true;
110
    return this.http.get(url, httpOptions)
111
      .map( res => <RepositoryInterface[]>res.json())
112
      .catch(this.handleError);
113
  }
114

    
115

    
116
  getUrlsOfUserRepos(userEmail: string): Observable<string[]>{
117
    let url = `${this.apiUrl}getUrlsOfUserRepos/${userEmail}/0/100/`;
118
    console.log(`knocking on: ${url}`);
119
    httpOptions.withCredentials = true;
120
    return this.http.get(url, httpOptions)
121
      .map( res => <string[]>res.json())
122
      .catch(this.handleError);
123
  }
124

    
125
  getRepositoryAggregations(id: string): Observable<Aggregations>{
126
    let url = `${this.apiUrl}getRepositoryAggregations/${id}`;
127
    console.log(`knocking on: ${url}`);
128
    httpOptions.withCredentials = true;
129
    return this.http.get(url,httpOptions)
130
      .map(res => <Aggregations>res.json())
131
      .catch(this.handleError);
132
  }
133

    
134
  getTimezones(): Observable<Timezone[]>{
135
/*    let url = `${this.apiUrl}getTimezones`;
136
    console.log(`knocking on: ${url}`);
137
    return this.http.get(url)
138
      .map( res => <Timezone[]>res.json())
139
      .catch(this.handleError);*/
140
    return Observable.of(<Timezone[]>timezones);
141
  }
142

    
143
  getTypologies(): Observable<Typology[]>{
144
/*    let url = `${this.apiUrl}getTypologies`;
145
    console.log(`knocking on: ${url}`);
146
    return this.http.get(url)
147
      .map( res => <string[]>res.json())
148
      .catch(this.handleError);*/
149
    return Observable.of(<Typology[]>typologies);
150
  }
151

    
152
  getCountries(): Observable<Country[]> {
153
    let url = `${this.apiUrl}getCountries`;
154
    console.log(`knocking on: ${url}`);
155
    return this.http.get(url)
156
      .map( res => <Country[]>res.json())
157
      .catch(this.handleError);
158
  }
159

    
160

    
161
  getCompatibilityClasses (mode: string): Observable<Map<string,string>> {
162
    let url = `${this.apiUrl}getCompatibilityClasses/${mode}`;
163
    console.log(`knocking on: ${url}`);
164
    return this.http.get(url)
165
      .map( res => <Map<string,string>>res.json())
166
      .catch(this.handleError);
167
  }
168

    
169
  getDatasourceClasses(mode: string): Observable<Map<string,string>>{
170
    let url = `${this.apiUrl}getDatasourceClasses/${mode}`;
171
    console.log(`knocking on: ${url}`);
172
    return this.http.get(url)
173
      .map( res => <Map<string,string>>res.json())
174
      .catch(this.handleError);
175
  }
176

    
177

    
178
  getMetricsInfoForRepository (repoId: string): Observable<MetricsInfo> {
179
    let url = `${this.apiUrl}getMetricsInfoForRepository/${repoId}`;
180
    console.log(`knocking on: ${url}`);
181
    httpOptions.withCredentials = true;
182
    return this.http.get(url, httpOptions)
183
      .map( res => <MetricsInfo>res.json())
184
      .catch(this.handleError);
185
  }
186

    
187
  updateEnglishName(id: string, englishname: string): Observable<string>{
188
    let url = `${this.apiUrl}updateEnglishName?id=${id}&officialName=DSpace&englishname=${englishname}`;
189
    console.log(`knocking on: ${url}`);
190
    httpOptions.withCredentials = true;
191
    return this.http.post(url,httpOptions)
192
      .map( res => {
193
        console.log(`responded ${res.statusText}`);
194
        return res.status.toString();
195
      })
196
      .catch(this.handleError);
197
  }
198

    
199
  updateLongtitude(id: string, longtitude: string): Observable<string>{
200
    let url = `${this.apiUrl}updateLongtitude`;
201
    console.log(`knocking on: ${url}`);
202
    let body = JSON.stringify({
203
      id : id,
204
      logntitude: longtitude
205
    });
206
    console.log(`sending ${body}`);
207
    httpOptions.withCredentials = true;
208

    
209
    return this.http.post(url,body,httpOptions)
210
      .map( res => <string>res.json())
211
      .catch(this.handleError);
212
  }
213

    
214
  updateLatitude(id: string, latitude: string): Observable<string>{
215
    let url = `${this.apiUrl}updateLatitude`;
216
    console.log(`knocking on: ${url}`);
217
    let body = JSON.stringify({
218
      id : id,
219
      latitude: latitude
220
    });
221
    console.log(`sending ${body}`);
222
    httpOptions.withCredentials = true;
223

    
224
    return this.http.post(url,body,httpOptions)
225
      .map( res => <string>res.json())
226
      .catch(this.handleError);
227
  }
228

    
229
  updateLogoUrl(id: string, logoUrl: string): Observable<any>{
230
    let url = `${this.apiUrl}updateLogoUrl`;
231
    console.log(`knocking on: ${url}`);
232
    let body = JSON.stringify({
233
      id : id,
234
      logoUrl: logoUrl
235
    });
236
    console.log(`sending ${body}`);
237
    httpOptions.withCredentials = true;
238

    
239
    return this.http.post(url, body, httpOptions)
240
      .map( res => <any>res.json())
241
      .catch(this.handleError);
242
  }
243

    
244
  updateTimezone(id: string, timezone: string): Observable<string>{
245
    let url = `${this.apiUrl}updateTimezone`;
246
    console.log(`knocking on: ${url}`);
247
    let body = JSON.stringify({
248
      id : id,
249
      timezone: timezone
250
    });
251
    console.log(`sending ${body}`);
252
    httpOptions.withCredentials = true;
253

    
254
    return this.http.post(url, body, httpOptions)
255
      .map( res => <string>res.json())
256
      .catch(this.handleError);
257
  }
258

    
259
  getListLatestUpdate(mode: string): Observable<string> {
260
    let url = `${this.apiUrl}getListLatestUpdate/${mode}`;
261
    console.log(`knocking on: ${url}`);
262
    return this.http.get(url)
263
      .map( res => res.json()['lastCollectionDate'])
264
      .catch(this.handleError);
265
  }
266

    
267

    
268
  private handleError(error: Response | any) {
269
    // In a real world app, we might use a remote logging infrastructure
270
    // We'd also dig deeper into the error to get a better message
271
    let errMsg = "";
272
    console.log('E R R O R !!');
273
    console.log(error);
274
    if (error instanceof Response) {
275
      const body = error.text() || '';
276
      //const err = body.error || JSON.stringify(body);
277
      errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
278
      console.log(errMsg);
279
    } else {
280
      errMsg = (error.message) ? error.message : error.status ? `${error.status} - ${error.statusText}` : 'Server error';
281
      console.error(errMsg); // log to console instead
282
    }
283
    return Observable.throw(errMsg);
284
  }
285

    
286
}
(7-7/8)