Project

General

Profile

1
/*
2
* Created by myrto on 1/24/2018
3
*/
4

    
5
import { Injectable } from '@angular/core';
6
import { Http, Headers, RequestOptions } from '@angular/http';
7
import { Observable } from 'rxjs/Observable';
8
import { InterfaceInformation, JobForValidation, RuleSet, StoredJob } from '../domain/typeScriptClasses';
9
import { apiUrl } from '../domain/tempAPI';
10

    
11

    
12
let headers = new Headers({ 'Content-Type': 'application/json' });
13
let httpOptions = new RequestOptions({ headers: headers });
14

    
15
@Injectable ()
16
export class ValidatorService {
17

    
18
  /*private apiUrl = apiUrl + '/validator/';*/
19
  private apiUrl = process.env.API_ENDPOINT + '/validator/';
20

    
21
    constructor(private http: Http) { }
22

    
23
  /* returns array of sets of rules according to mode (literature, data, cris) */
24
  getRuleSets(mode: string): Observable<RuleSet[]> {
25
    let url = `${this.apiUrl}getRuleSets/${mode}`;
26
    console.log(`knocking on: ${url}`);
27
    httpOptions.withCredentials = true;
28
    return this.http.get(url,httpOptions)
29
      .map(res => <RuleSet[]>res.json())
30
      .catch(this.handleError);
31
  }
32

    
33

    
34
  getSetsOfRepository(baseUrl: string): Observable<string[]> {
35
    let url = `${this.apiUrl}getSetsOfRepository?url=${baseUrl}`;
36
    console.log(`knocking on: ${url}`);
37
    httpOptions.withCredentials = true;
38
    return this.http.get(url,httpOptions)
39
      .map(res => <string[]>res.json())
40
      .catch(this.handleError);
41
  }
42

    
43
  getStoredJobsNew(userEmail: string,
44
                   jobType:string,
45
                   offset: string,
46
                   limit: string,
47
                   dateFrom: string,
48
                   dateTo: string,
49
                   validationStatus: string): Observable<StoredJob[]> {
50
    let url = `${this.apiUrl}getStoredJobsNew?user=${userEmail}&jobType=${encodeURI(jobType)}&offset=${offset}&limit=${limit}&dateFrom=${dateFrom}&dateTo=${dateTo}&validationStatus=${validationStatus}`;
51
    console.log(`knocking on: ${url}`);
52
    httpOptions.withCredentials = true;
53
    return this.http.get(url,httpOptions)
54
      .map(res => <StoredJob[]>res.json())
55
      .catch(this.handleError);
56
  }
57

    
58
  /* returns true if there is a repository containing the baseUrl */
59
  identifyRepository(baseUrl: string): Observable<boolean> {
60
    let url = `${this.apiUrl}identifyRepository?url=${baseUrl}`;
61
    console.log(`knocking on: ${url}`);
62
    httpOptions.withCredentials = true;
63
    return this.http.get(url,httpOptions)
64
      .map(res => <boolean>res.json())
65
      .catch(this.handleError);
66
  }
67

    
68
  getInterfaceInformation(baseUrl: string): Observable<InterfaceInformation> {
69
    let url = `${this.apiUrl}getInterfaceInformation?baseUrl=${encodeURIComponent(baseUrl)}`;
70
    console.log(`knocking on: ${url}`);
71
    httpOptions.withCredentials = true;
72
    return this.http.get(url,httpOptions)
73
      .map(res => <InterfaceInformation>res.json())
74
      .catch(this.handleError);
75
  }
76

    
77
  reSubmitJobForValidation(id: string): Observable<string> {
78
    let url = `${this.apiUrl}reSubmitJobForValidation/${id}`;
79
    console.log(`knocking on: ${url}`);
80

    
81
    httpOptions.withCredentials = true;
82
    return this.http.post(url,httpOptions)
83
      .map(res => {
84
        console.log(`responded ${res.status}`);
85
        return res.status.toString();
86
      })
87
      .catch(this.handleError);
88
  }
89

    
90
  submitJobForValidation(job: JobForValidation): Observable<string> {
91
    let url = `${this.apiUrl}submitJobForValidation`;
92
    console.log(`knocking on: ${url}`);
93
    let body = JSON.stringify(job);
94
    httpOptions.withCredentials = true;
95
    return this.http.post(url,body,httpOptions)
96
      .map(res => {
97
        console.log(`responded ${res.status}`);
98
        return res.status.toString();
99
      })
100
      .catch(this.handleError);
101
  }
102

    
103

    
104
/* from omtd project */
105
  private handleError(error: Response | any) {
106
    // In a real world app, we might use a remote logging infrastructure
107
    // We'd also dig deeper into the error to get a better message
108
    let errMsg = "";
109
    console.log('E R R O R !!!');
110
    console.log(error);
111
    if (error instanceof Response) {
112
      const body = error.text() || '';
113
      //const err = body.error || JSON.stringify(body);
114
      errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
115
    } else {
116
      errMsg = (error.message) ? error.message :
117
        error.status ? `${error.status} - ${error.statusText}` : 'Server error';
118
      console.error(errMsg); // log to console instead
119
    }
120
    return Observable.throw(errMsg);
121
  }
122
}
(8-8/8)