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 { AggregationDetails, Country, MetricsInfo, Repository, RepositoryInterface, Timezone, Typology } from '../domain/typeScriptClasses';
12
import { timezones } from '../domain/timezones';
13
import { typologies } from '../domain/typologies';
14
import {HttpClient, HttpHeaders, HttpResponse} from "@angular/common/http";
15
import {tap} from "rxjs/operators";
16

    
17
let headers = new Headers({ 'Content-Type': 'application/json' });
18
let httpOptions = new RequestOptions({ headers: headers });
19
const headerOptions = {
20
  headers : new HttpHeaders().set('Content-Type', 'application/json')
21
                             .set('Accept', 'application/json'),
22
  withCredentials: true
23
};
24

    
25

    
26
@Injectable ()
27
export class RepositoryService {
28
  private apiUrl = process.env.API_ENDPOINT + '/repository/';
29

    
30
  constructor(private http: Http,
31
              private httpClient: HttpClient) { }
32

    
33
  addInterface(datatype: string, repoId: string, registeredBy: string, newInterface: RepositoryInterface): Observable<RepositoryInterface> {
34
    let url = `${this.apiUrl}addInterface?datatype=${datatype}&repoId=${repoId}&registeredBy=${registeredBy}`;
35
    console.log(`knocking on: ${url}`);
36
    console.log(`sending ${JSON.stringify(newInterface)}`);
37
    httpOptions.withCredentials = true;
38
    return this.httpClient.post<RepositoryInterface>(url,newInterface,headerOptions);
39
  }
40

    
41
  updateInterface(repoId: string, registeredBy: string, interfaceInfo: RepositoryInterface): Observable<RepositoryInterface> {
42
    let url = `${this.apiUrl}updateRepositoryInterface?repoId=${repoId}&registeredBy=${registeredBy}`;
43
    console.log(`knocking on: ${url}`);
44
    console.log(`sending ${JSON.stringify(interfaceInfo)}`);
45
    httpOptions.withCredentials = true;
46
    return this.httpClient.post<RepositoryInterface>(url,interfaceInfo,headerOptions);
47
  }
48

    
49
  deleteInterface(id: string, registeredBy: string) {
50
    let url = `${this.apiUrl}deleteInterface/?id=${id}&registeredBy=${registeredBy}`;
51
    console.log(`knocking on: ${url}`);
52

    
53
    return this.httpClient.delete(url, {withCredentials: true, responseType:'text'});
54
  }
55

    
56
  addRepository(datatype: string, newRepository: Repository): Observable<Repository> {
57
    let url = `${this.apiUrl}addRepository?datatype=${datatype}`;
58
    console.log(`knocking on: ${url}`);
59
    console.log(`sending ${JSON.stringify(newRepository)}`);
60
    return this.httpClient.post<Repository>(url,newRepository,headerOptions);
61
  }
62

    
63
  updateRepository(repoInfo: Repository): Observable<Repository> {
64
    let url = `${this.apiUrl}updateRepository`;
65
    console.log(`knocking on: ${url}`);
66
    console.log(`sending ${JSON.stringify(repoInfo)}`);
67
    return this.httpClient.post<Repository>(url,repoInfo,headerOptions);
68
  }
69

    
70
  getRepositoriesOfCountry(country: string, mode: string): Observable<Repository[]> {
71
    let url = `${this.apiUrl}getRepositoriesByCountry/${country}/${mode}`;
72
    console.log(`knocking on: ${url}`);
73
    return this.httpClient.get(url, headerOptions);
74
  }
75

    
76
  getRepositoriesOfUser(userEmail: string): Observable<Repository[]> {
77
    let url = `${this.apiUrl}getRepositoriesOfUser/${userEmail}/0/100`;
78
    console.log(`knocking on: ${url}`);
79
    return this.httpClient.get(url, headerOptions);
80
  }
81

    
82

    
83
  getRepositoryById(id: string): Observable<Repository> {
84
    let url = `${this.apiUrl}getRepositoryById/${id}`;
85
    console.log(`knocking on: ${url}`);
86
    return this.httpClient.get(url, headerOptions);
87
  }
88

    
89
  getRepositoryInterface(id: string): Observable<RepositoryInterface[]>{
90
    let url = `${this.apiUrl}getRepositoryInterface/${id}`;
91
    console.log(`knocking on: ${url}`);
92
    return this.httpClient.get(url, headerOptions);
93
  }
94

    
95

    
96
  getUrlsOfUserRepos(userEmail: string): Observable<string[]>{
97
    let url = `${this.apiUrl}getUrlsOfUserRepos/${userEmail}/0/100/`;
98
    console.log(`knocking on: ${url}`);
99
    return this.httpClient.get(url, headerOptions);
100
  }
101

    
102
  getRepositoryAggregations(id: string): Observable<AggregationDetails[]>{
103
    let url = `${this.apiUrl}getRepositoryAggregations/${id}`;
104
    console.log(`knocking on: ${url}`);
105
    return this.httpClient.get(url, headerOptions);
106
  }
107

    
108
  getRepositoryAggregationsByYear(id: string): Observable<Map<string,AggregationDetails[]>>{
109
    let url = `${this.apiUrl}getRepositoryAggregationsByYear/${id}`;
110
    console.log(`knocking on: ${url}`);
111
    return this.httpClient.get<Map<string,AggregationDetails[]>>(url, headerOptions);
112
  }
113

    
114
  getTimezones(): Observable<Timezone[]>{
115
/*    let url = `${this.apiUrl}getTimezones`;
116
    console.log(`knocking on: ${url}`);
117
    return this.httpClient.get(url, headerOptions);*/
118
    return Observable.of(<Timezone[]>timezones);
119
  }
120

    
121
  getTypologies(): Observable<Typology[]>{
122
/*    let url = `${this.apiUrl}getTypologies`;
123
    console.log(`knocking on: ${url}`);
124
    return this.httpClient.get(url, headerOptions);*/
125
    return Observable.of(<Typology[]>typologies);
126
  }
127

    
128
  getCountries(): Observable<Country[]> {
129
    let url = `${this.apiUrl}getCountries`;
130
    console.log(`knocking on: ${url}`);
131
    return this.httpClient.get(url, headerOptions);
132
  }
133

    
134

    
135
  getCompatibilityClasses (mode: string): Observable<Map<string,string>> {
136
    let url = `${this.apiUrl}getCompatibilityClasses/${mode}`;
137
    console.log(`knocking on: ${url}`);
138
    return this.httpClient.get(url, headerOptions);
139
  }
140

    
141
  getDatasourceClasses(mode: string): Observable<Map<string,string>>{
142
    let url = `${this.apiUrl}getDatasourceClasses/${mode}`;
143
    console.log(`knocking on: ${url}`);
144
    return this.httpClient.get(url, headerOptions);
145
  }
146

    
147

    
148
  getMetricsInfoForRepository (repoId: string): Observable<MetricsInfo> {
149
    let url = `${this.apiUrl}getMetricsInfoForRepository/${repoId}`;
150
    console.log(`knocking on: ${url}`);
151
    return this.httpClient.get(url, headerOptions);
152
  }
153

    
154
  updateEnglishName(id: string, englishname: string) {
155
    let url = `${this.apiUrl}updateEnglishName?id=${id}&officialName=DSpace&englishname=${englishname}`;
156
    console.log(`knocking on: ${url}`);
157
    const body = {};
158

    
159
    return this.httpClient.post(url, body, {withCredentials: true, responseType: 'text'});
160
  }
161

    
162
  updateLongtitude(id: string, longtitude: string){
163
    let url = `${this.apiUrl}updateLongtitude`;
164
    console.log(`knocking on: ${url}`);
165
    let body = JSON.stringify({
166
      id : id,
167
      logntitude: longtitude
168
    });
169
    console.log(`sending ${body}`);
170

    
171
    return this.httpClient.post(url,body, {withCredentials: true, responseType: 'text'});
172
  }
173

    
174
  updateLatitude(id: string, latitude: string) {
175
    let url = `${this.apiUrl}updateLatitude`;
176
    console.log(`knocking on: ${url}`);
177
    let body = JSON.stringify({
178
      id : id,
179
      latitude: latitude
180
    });
181
    console.log(`sending ${body}`);
182

    
183
    return this.httpClient.post(url,body,{withCredentials: true, responseType: 'text'});
184
  }
185

    
186
  updateLogoUrl(id: string, logoUrl: string) {
187
    let url = `${this.apiUrl}updateLogoUrl`;
188
    console.log(`knocking on: ${url}`);
189
    let body = JSON.stringify({
190
      id : id,
191
      logoUrl: logoUrl
192
    });
193
    console.log(`sending ${body}`);
194

    
195
    return this.httpClient.post(url,body,{withCredentials: true, responseType: 'text'});
196
  }
197

    
198
  updateTimezone(id: string, timezone: string) {
199
    let url = `${this.apiUrl}updateTimezone`;
200
    console.log(`knocking on: ${url}`);
201
    let body = JSON.stringify({
202
      id : id,
203
      timezone: timezone
204
    });
205
    console.log(`sending ${body}`);
206

    
207
    return this.httpClient.post(url,body,{withCredentials: true, responseType: 'text'});
208
  }
209

    
210
  getListLatestUpdate(mode: string): Observable<any> {
211
    let url = `${this.apiUrl}getListLatestUpdate/${mode}`;
212
    console.log(`knocking on: ${url}`);
213
    return this.httpClient.get(url, headerOptions);
214
  }
215

    
216

    
217
  private handleError(error: Response | any) {
218
    // In a real world app, we might use a remote logging infrastructure
219
    // We'd also dig deeper into the error to get a better message
220
    let errMsg = "";
221
    console.log('E R R O R !!');
222
    console.log(error);
223
    if (error instanceof Response) {
224
      const body = error.text() || '';
225
      //const err = body.error || JSON.stringify(body);
226
      errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
227
      console.log(errMsg);
228
    } else {
229
      errMsg = (error.message) ? error.message : error.status ? `${error.status} - ${error.statusText}` : 'Server error';
230
      console.error(errMsg); // log to console instead
231
    }
232
    return Observable.throw(errMsg);
233
  }
234

    
235
}
(8-8/10)