methods.service.ts 5.28 KB
Newer Older
Vitali Stupin's avatar
Vitali Stupin committed
1
import { Injectable, Output, EventEmitter } from '@angular/core';
Vitali Stupin's avatar
Initial  
Vitali Stupin committed
2
3
4
5
import { HttpClient } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { Subsystem } from './subsystem';
Vitali Stupin's avatar
Vitali Stupin committed
6
import { Method } from './method';
Vitali Stupin's avatar
Initial  
Vitali Stupin committed
7

8
9
const MAX_LIMIT: number = 1000000;

Vitali Stupin's avatar
Initial  
Vitali Stupin committed
10
11
12
13
14
@Injectable({
  providedIn: 'root'
})
export class MethodsService {

Vitali Stupin's avatar
Vitali Stupin committed
15
16
  private apiUrlBase = 'https://www.x-tee.ee/catalogue/EE/wsdls/';
  //public apiUrlBase = 'http://localhost/';
17
  private limit: number = 10;
Vitali Stupin's avatar
Vitali Stupin committed
18
  private offset: number = 0;
19
  private nonEmpty: boolean = false;
Vitali Stupin's avatar
Vitali Stupin committed
20
  private filter: string = "";
Vitali Stupin's avatar
Initial  
Vitali Stupin committed
21
22
  private apiService = 'index.json';
  private apiUrl = this.apiUrlBase + this.apiService;
23
24
  private subsystems: Subsystem[] = [];
  private loadingDone: boolean = false;
25
  private lastMessage: string = '';
Vitali Stupin's avatar
Vitali Stupin committed
26
27

  @Output() subsystemsUpdated: EventEmitter<any> = new EventEmitter();
Vitali Stupin's avatar
Vitali Stupin committed
28
  @Output() hideDetails: EventEmitter<boolean> = new EventEmitter();
29
  @Output() newMessage: EventEmitter<string> = new EventEmitter();
Vitali Stupin's avatar
Initial  
Vitali Stupin committed
30
31

  constructor(private http: HttpClient) {
Vitali Stupin's avatar
Vitali Stupin committed
32
    this.http.get<Subsystem[]>(this.apiUrl)
Vitali Stupin's avatar
Initial  
Vitali Stupin committed
33
34
    .pipe(
      catchError(this.handleError('getMethods', []))
Vitali Stupin's avatar
Vitali Stupin committed
35
36
    ).subscribe(subsystems => {
      this.subsystems = subsystems;
Vitali Stupin's avatar
Vitali Stupin committed
37
      this.setFullNames();
38
      this.loadingDone = true;
Vitali Stupin's avatar
Vitali Stupin committed
39
40
41
42
43
44
45
46
      this.signalRefresh();
    })
  }

  private signalRefresh() {
    this.subsystemsUpdated.emit(null);
  }

Vitali Stupin's avatar
Vitali Stupin committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  private signalHideDetails(signal: boolean) {
    this.hideDetails.emit(signal);
  }

  private filteredSubsystems(): Subsystem[] {
    let filtered: Subsystem[] = []
    let limit: number = this.limit
    for (let subsystem of this.subsystems) {
      if (this.nonEmpty && !subsystem.methods.length) {
        // Filtering out empty subsystems
        continue
      }
      if (
        this.filter != ''
        && !subsystem.methods.length
      ) {
        // Subsystem without methods
        if (subsystem.fullSubsystemName.toLowerCase().indexOf(this.filter.toLowerCase()) < 0) {
          // Subsystem name does not match the filter
          continue
        }
      } else if (this.filter != '') {
        // Subsystem with methods
        let filteredMethods: Method[] = []
        for (let method of subsystem.methods) {
          if(method.fullMethodName.toLowerCase().indexOf(this.filter.toLowerCase()) >= 0) {
            filteredMethods.push(method)
          }
        }
        if (!filteredMethods.length) {
          // No matching method names found
          continue
        }
        // Leaving only matcing methods
        subsystem.methods = filteredMethods
      }

      filtered.push(subsystem)
      limit -= 1
      if (limit == 0) {
        break
      }
    }

    return filtered.slice(this.offset, this.limit);
  }

  private setFullNames() {
    for (let i in this.subsystems) {
      this.subsystems[i].fullSubsystemName = this.subsystems[i].xRoadInstance
        + '/' + this.subsystems[i].memberClass
        + '/' + this.subsystems[i].memberCode
        + '/' + this.subsystems[i].subsystemCode
      for (let j in this.subsystems[i].methods) {
        this.subsystems[i].methods[j].fullMethodName = this.subsystems[i].fullSubsystemName
        + '/' + this.subsystems[i].methods[j].serviceCode
        + '/' + this.subsystems[i].methods[j].serviceVersion
      }
    }
Vitali Stupin's avatar
Vitali Stupin committed
106
107
  }

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  getLimit(): string {
    if (this.limit == MAX_LIMIT) {
      return 'All'
    }
    return this.limit.toString()
  }

  getNonEmpty(): boolean {
    return this.nonEmpty
  }

  getfilter(): string {
    return this.filter
  }

  isLoadingDone(): boolean {
    return this.loadingDone
  }

Vitali Stupin's avatar
Vitali Stupin committed
127
  getMethods(): Subsystem[] {
Vitali Stupin's avatar
Vitali Stupin committed
128
    return this.filteredSubsystems();
Vitali Stupin's avatar
Initial  
Vitali Stupin committed
129
130
  }

131
132
133
134
135
136
137
138
139
140
141
142
143
144
  setNonEmpty(nonEmpty: boolean) {
    this.nonEmpty = nonEmpty;
    this.signalRefresh();
  }

  setLimit (limit: string) {
    switch(limit) {
      case '20':
        this.limit = 20;
        break;
      case '50':
        this.limit = 50;
        break;
      case 'All':
145
        this.limit = MAX_LIMIT;
146
147
148
        break;
      default:
        this.limit = 10;
Vitali Stupin's avatar
Initial  
Vitali Stupin committed
149
    }
Vitali Stupin's avatar
Vitali Stupin committed
150
    this.signalRefresh();
Vitali Stupin's avatar
Initial  
Vitali Stupin committed
151
152
  }

Vitali Stupin's avatar
Vitali Stupin committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  setFilter(filter: string) {
    if (this.filter != filter.trim()) {
      if (filter.trim() == '') {
        // Always close methods when filter is deleted
        this.signalHideDetails(true)
      } else {
        // Always open methods when filter is inserted
        this.signalHideDetails(false)
      }
      this.filter = filter.trim();
      this.signalRefresh();
    }
  }

167
168
169
170
171
172
  getSubsystem(name: string): Subsystem {
    return this.subsystems.find(function(element) {
      return element.fullSubsystemName === name;
    })
  }

Vitali Stupin's avatar
Vitali Stupin committed
173
174
175
176
177
178
179
  getHideDetails(): boolean {
    if (this.filter == '') {
      return true
    } else {
      return false
    }
  }
Vitali Stupin's avatar
Initial  
Vitali Stupin committed
180

181
182
183
184
  getMessage(): string {
    return this.lastMessage
  }

Vitali Stupin's avatar
Initial  
Vitali Stupin committed
185
186
187
188
189
190
191
192
193
  /**
   * Handle Http operation that failed.
   * Let the app continue.
   * @param operation - name of the operation that failed
   * @param result - optional value to return as the observable result
   */
  private handleError<T> (operation = 'operation', result?: T) {
    return (error: any): Observable<T> => {

194
195
196
      //console.error(error); // log to console for debuging
      this.lastMessage = 'Error loading data!'
      this.newMessage.emit(this.lastMessage);
Vitali Stupin's avatar
Initial  
Vitali Stupin committed
197
198
199
200
201
202

      // Let the app keep running by returning an empty result.
      return of(result as T);
    };
  }
}