Project

General

Profile

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

    
5
/*
6
*  !!! USING TEMPORARY API ADDRESS AND USER
7
*/
8

    
9
import { HttpHeaders } from '@angular/common/http';
10
import { Injectable } from '@angular/core';
11
import { Http } from '@angular/http';
12
import { Observable } from 'rxjs/Observable';
13
import { InterfaceInformation, JobsOfUser, RuleSet, StoredJob } from '../domain/typeScriptClasses';
14

    
15
const httpOptions = {
16
  headers: new HttpHeaders().set('Content-Type', 'application/json')
17
};
18

    
19
@Injectable ()
20
export class ValidatorService {
21

    
22
  /*  private apiUrl = 'http://195.134.66.230:8380/uoa-repository-manager-service';*/
23
  private apiUrl = 'http://194.177.192.121:8380/uoa-repository-manager-service';
24

    
25
  constructor(private http: Http) { }
26

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

    
36

    
37
  getSetsOfRepository(baseUrl: string): Observable<string[]> {
38
    let url = `${this.apiUrl}/validator/getSetsOfRepository?url=${baseUrl}`;
39
    console.log(`knocking on: ${url}`);
40
    return this.http.get(url)
41
      .map(res => <string[]>res.json())
42
      .catch(this.handleError);
43
  }
44

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

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

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

    
78

    
79
/* from omtd project */
80
  private handleError(error: Response | any) {
81
    // In a real world app, we might use a remote logging infrastructure
82
    // We'd also dig deeper into the error to get a better message
83
    let errMsg = "";
84
    console.log('E R R O R !!!');
85
    console.log(error);
86
    if (error instanceof Response) {
87
      const body = error.text() || '';
88
      //const err = body.error || JSON.stringify(body);
89
      errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
90
    } else {
91
      errMsg = (error.message) ? error.message :
92
        error.status ? `${error.status} - ${error.statusText}` : 'Server error';
93
      console.error(errMsg); // log to console instead
94
    }
95
    return Observable.throw(errMsg);
96
  }
97
}
(8-8/8)