Project

General

Profile

1
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
2
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
3
import { baseUrlDesc, compatibilityLevelDesc, customValSetDesc, Description, existingValSetDesc, commentsDesc } from '../../../domain/oa-description';
4
import { InterfaceInformation, RepositoryInterface } from '../../../domain/typeScriptClasses';
5
import { ValidatorService } from '../../../services/validator.service';
6
import { RepositoryService } from '../../../services/repository.service';
7
import { formErrorWasntSaved, formInfoLoading, formSubmitting, formSuccessAddedInterface, formSuccessUpdatedInterface, invalidCustomBaseUrl,
8
         nonRemovableInterface, noServiceMessage } from '../../../domain/shared-messages';
9

    
10
export class RepoFields {
11
  id: string;
12
  datasourceType: string;
13
  datasourceClass: string;
14
  registeredBy: string;
15
}
16

    
17
@Component({
18
  selector: 'app-repository-interface-form',
19
  templateUrl: './datasource-new-interface-form.component.html'
20
})
21
export class DatasourceNewInterfaceFormComponent implements OnInit {
22
  loadingMessage: string;
23
  successMessage: string;
24
  errorMessage: string;
25
  invalidCustomBaseUrl = invalidCustomBaseUrl;
26

    
27
  @Input() data: any[] = []; // expects an array containing at least 3 of the 4 below fields in this order
28
  inRegister: boolean;
29
  interfaceID: number;      // holds the interface index in the interfaces array as displayed
30
  currentRepo: RepoFields;  // a fraction of the Repository class
31
  currentInterface: RepositoryInterface;
32

    
33
  @Output() emitDeleteInterface: EventEmitter<number> = new EventEmitter<number>();
34
  interfaceToExport: RepositoryInterface;
35

    
36
  repoInterfaceForm: FormGroup;
37
  readonly repoInterfaceFormDef = {
38
    baseUrl: ['', Validators.required],
39
    selectValidationSet: [''],
40
    customValidationSet: [''],
41
    compatibilityLevel: [''],
42
    comments: ['']
43
  };
44
  baseUrlDesc: Description = baseUrlDesc;
45
  existingValSetDesc: Description = existingValSetDesc;
46
  customValSetDesc: Description = customValSetDesc;
47
  compatibilityLevelDesc: Description = compatibilityLevelDesc;
48
  commentsDesc: Description = commentsDesc;
49

    
50
  identifiedBaseUrl: boolean;
51
  showIdentifiedBaseUrl: boolean;
52
  valsetList: string[] = [];
53
  existingCompLevel: string;
54
  classCodes: string[] = [];
55
  compClasses: Map<string, string> = new Map<string, string>();
56
  existingValSet: boolean;
57
  interfaceInfo: InterfaceInformation;
58

    
59
  constructor(private fb: FormBuilder,
60
              private valService: ValidatorService,
61
              private repoService: RepositoryService) {}
62

    
63
  ngOnInit() {
64
    if (this.data && (this.data.length >= 3)) {
65
      this.inRegister = this.data[0];
66
      this.interfaceID = this.data[1];
67
      this.currentRepo = this.data[2];
68
      this.repoInterfaceForm = this.fb.group(this.repoInterfaceFormDef);
69
      this.chooseValSet(true);
70
      if (this.data[3]) {
71
        this.currentInterface = this.data[3];
72
        this.repoInterfaceForm.get('baseUrl').setValue(this.currentInterface.baseUrl);
73
        this.repoInterfaceForm.get('compatibilityLevel').setValue(this.currentInterface.desiredCompatibilityLevel);
74
        this.repoInterfaceForm.get('comments').setValue(this.currentInterface.comments);
75
      }
76
      this.getInterfaceInfo();
77
      this.getCompatibilityClasses();
78
    }
79
  }
80

    
81

    
82
  getInterfaceInfo() {
83
    this.successMessage = '';
84
    this.errorMessage = '';
85

    
86
    const  baseUrl = this.repoInterfaceForm.get('baseUrl').value;
87
    if (baseUrl) {
88
      this.loadingMessage = formInfoLoading;
89
      this.valService.getInterfaceInformation(baseUrl).subscribe(
90
        info => {
91
          this.interfaceInfo = info;
92
          if (this.interfaceInfo.identified) {
93
            this.identifiedBaseUrl = true;
94
            this.showIdentifiedBaseUrl = true;
95
          } else {
96
            this.errorMessage = invalidCustomBaseUrl;
97
            this.identifiedBaseUrl = true;  // pass interface without baseUrl identification
98
            this.showIdentifiedBaseUrl = false;
99
          }
100
          if (this.interfaceInfo.sets) {
101
            this.valsetList = this.interfaceInfo.sets;
102
            console.log(this.valsetList);
103
          }
104
        },
105
        error => {
106
          console.log(error);
107
          this.loadingMessage = '';
108
          // this.identifiedBaseUrl = false;
109
          this.errorMessage = noServiceMessage;
110
        },
111
        () => {
112
          if ( this.currentInterface && this.currentInterface.accessParams && this.currentInterface.accessParams['set'] ) {
113
            if ( this.valsetList.some( x => x === this.currentInterface.accessParams['set']) ) {
114
              this.repoInterfaceForm.get('selectValidationSet').setValue(this.currentInterface.accessParams['set']);
115
            } else {
116
              this.repoInterfaceForm.get('customValidationSet').setValue(this.currentInterface.accessParams['set']);
117
            }
118
          }
119
          this.loadingMessage = '';
120
          this.repoInterfaceForm.updateValueAndValidity();
121
          this.checkIfValid();
122
        }
123
      );
124
    }
125
  }
126

    
127
  getCompatibilityClasses() {
128
    this.repoService.getCompatibilityClasses(this.currentRepo.datasourceType).subscribe(
129
      classes => {
130
        this.compClasses = classes;
131
        this.classCodes = Object.keys(this.compClasses);
132
      },
133
      error => {
134
        this.errorMessage = noServiceMessage;
135
        console.log(error);
136
      },
137
      () => {
138
        this.getExistingCompatibilityLevel();
139
        this.repoInterfaceForm.updateValueAndValidity();
140
      }
141
    );
142
  }
143

    
144
  getExistingCompatibilityLevel() {
145
    if (this.currentInterface) {
146
      if (this.currentInterface.desiredCompatibilityLevel &&
147
        this.classCodes.some( x => x === this.currentInterface.desiredCompatibilityLevel ) ) {
148
        this.existingCompLevel = this.compClasses[this.currentInterface.desiredCompatibilityLevel];
149
      } else {
150
        this.repoInterfaceForm.get('compatibilityLevel').setValue('');
151
        this.existingCompLevel = this.currentInterface.desiredCompatibilityLevel;
152
      }
153
    }
154
  }
155

    
156
  chooseValSet(fromList: boolean) {
157
    this.existingValSet = fromList;
158
    if (this.existingValSet) {
159
      this.repoInterfaceForm.get('selectValidationSet').enable();
160
      this.repoInterfaceForm.get('customValidationSet').disable();
161
    }  else {
162
      this.repoInterfaceForm.get('selectValidationSet').disable();
163
      this.repoInterfaceForm.get('customValidationSet').enable();
164
    }
165
    this.checkIfValid();
166
  }
167

    
168
  checkIfCompatibilityLevelWasChosen() {
169
    return ( (this.repoInterfaceForm.get('compatibilityLevel').value !== '') ||
170
             (this.existingCompLevel && (this.existingCompLevel !== '')) );
171
  }
172

    
173
  formIsValid() {
174
    return (this.repoInterfaceForm.valid &&
175
            this.identifiedBaseUrl &&
176
            this.checkIfCompatibilityLevelWasChosen());
177
  }
178

    
179
  checkIfValid() {
180
    if (this.formIsValid()) {
181
      if (this.inRegister) {
182
        this.successMessage = 'The interface will be stored when the registration procedure is completed.';
183
        this.saveInterface();
184
      }
185
    } else {
186
      this.successMessage = '';
187
      this.interfaceToExport = null;
188
    }
189

    
190
  }
191

    
192
  saveInterface() {
193
    this.errorMessage = '';
194
    this.successMessage = '';
195
    if (this.formIsValid()) {
196
      const baseUrl = this.repoInterfaceForm.get('baseUrl').value;
197
      let valset = '';
198
      if (this.existingValSet) {
199
        valset = this.repoInterfaceForm.get('selectValidationSet').value;
200
      } else {
201
        valset = this.repoInterfaceForm.get('customValidationSet').value;
202
      }
203
      let compLvl = '';
204
      if (this.repoInterfaceForm.get('compatibilityLevel').value) {
205
        this.existingCompLevel = this.compClasses[this.repoInterfaceForm.get('compatibilityLevel').value];
206
        console.log('this.existingCompLevel is', this.existingCompLevel);
207
        compLvl = this.repoInterfaceForm.get('compatibilityLevel').value;
208
      } else {
209
        compLvl = this.existingCompLevel;
210
      }
211
      let comment = '';
212
      if (this.repoInterfaceForm.get('comments').value) {
213
        comment = this.repoInterfaceForm.get('comments').value;
214
      }
215

    
216
      if (this.currentInterface) {
217
        this.updateCurrent(baseUrl, valset, compLvl, comment);
218
      } else {
219
        this.addCurrent(baseUrl, valset, compLvl, comment);
220
      }
221
    } else {
222
      this.interfaceToExport = null;
223
      this.errorMessage = 'Please make sure all required fields are filled with acceptable values.';
224
    }
225
  }
226

    
227
  getCurrentValues() {
228
    let intrf = this.currentInterface;
229
    if (intrf == null) {
230
      intrf = new RepositoryInterface();
231
    }
232
    intrf.baseUrl = this.repoInterfaceForm.get('baseUrl').value;
233
    if (this.existingValSet) {
234
      intrf.accessSet = this.repoInterfaceForm.get('selectValidationSet').value;
235
      intrf.accessParams = {'set': this.repoInterfaceForm.get('selectValidationSet').value};
236
    } else {
237
      intrf.accessSet = this.repoInterfaceForm.get('customValidationSet').value;
238
      intrf.accessParams = {'set': this.repoInterfaceForm.get('customValidationSet').value};
239
    }
240
    if (this.repoInterfaceForm.get('compatibilityLevel').value) {
241
      intrf.desiredCompatibilityLevel = this.repoInterfaceForm.get('compatibilityLevel').value;
242
      intrf.compliance = this.repoInterfaceForm.get('compatibilityLevel').value;
243
    } else {
244
      intrf.desiredCompatibilityLevel = this.existingCompLevel;
245
      intrf.compliance = this.existingCompLevel;
246
    }
247
    intrf.typology = this.currentRepo.datasourceClass;
248

    
249
    return intrf;
250
  }
251

    
252
  addCurrent (baseUrl: string, valset: string, compLvl: string, comment: string) {
253
    const currentInterface = new RepositoryInterface();
254
    currentInterface.baseUrl = baseUrl;
255
    currentInterface.accessSet = valset;
256
    currentInterface.accessParams = {'set': valset};
257
    currentInterface.desiredCompatibilityLevel = compLvl;
258
    currentInterface.compliance = compLvl;
259
    currentInterface.typology = this.currentRepo.datasourceClass;
260
    currentInterface.comments = comment;
261

    
262
    if (!this.inRegister) {
263
      this.addInterface(currentInterface);
264
    } else {
265
      this.successMessage = 'The interface will be stored when the registration procedure is completed';
266
      console.log('SAVED !');
267
      this.interfaceToExport = currentInterface;
268
    }
269
  }
270

    
271
  addInterface(newInterface: RepositoryInterface) {
272
    this.loadingMessage = formSubmitting;
273
    this.repoService.addInterface(this.currentRepo.datasourceType,
274
                                  this.currentRepo.id,
275
                                  this.currentRepo.registeredBy,
276
                                  newInterface).subscribe(
277
      addedInterface => {
278
        console.log(`addInterface responded ${JSON.stringify(addedInterface)}`);
279
        this.currentInterface = addedInterface;
280
      },
281
      error => {
282
        console.log(error);
283
        this.loadingMessage = '';
284
        this.errorMessage = formErrorWasntSaved;
285
        this.currentInterface = null;
286
      },
287
      () => {
288
        this.loadingMessage = '';
289
        if (this.currentInterface.id) {
290
          this.successMessage = formSuccessAddedInterface;
291
          this.getExistingCompatibilityLevel();
292
        } else {
293
          this.errorMessage = formErrorWasntSaved;
294
        }
295
      }
296
    );
297

    
298
  }
299

    
300

    
301
  updateCurrent (baseUrl: string, valset: string, compLvl: string, comment: string) {
302
    this.currentInterface.baseUrl = baseUrl;
303
    this.currentInterface.accessSet = valset;
304
    this.currentInterface.accessParams['set'] = valset;
305
    this.currentInterface.desiredCompatibilityLevel = compLvl;
306
    this.currentInterface.compliance = compLvl;
307
    this.currentInterface.typology = this.currentRepo.datasourceClass;
308
    this.currentInterface.comments = comment;
309

    
310
    if (!this.inRegister) {
311
      this.updateInterface();
312
    } else {
313
      this.successMessage = 'The interface will be stored when the registration procedure is completed';
314
      console.log('SAVED !');
315
      this.interfaceToExport = this.currentInterface;
316
    }
317
  }
318

    
319
  updateInterface() {
320
    this.loadingMessage = formSubmitting;
321
    this.repoService.updateInterface(this.currentRepo.id,
322
                                     this.currentRepo.registeredBy,
323
                                     this.currentInterface).subscribe(
324
      response => {
325
        console.log(`updateRepository responded ${JSON.stringify(response)}`);
326
        if (response) {
327
          this.currentInterface = response;
328
          this.successMessage = formSuccessUpdatedInterface;
329
        } else {
330
          this.errorMessage = formErrorWasntSaved;
331
        }
332
      },
333
      error => {
334
        console.log(error);
335
        this.loadingMessage = '';
336
        this.errorMessage = formErrorWasntSaved;
337
      },
338
      () => {
339
        this.loadingMessage = '';
340
        this.getExistingCompatibilityLevel();
341
      }
342
    );
343
  }
344

    
345
  removeInterface() {
346
    this.errorMessage = '';
347
    this.successMessage = '';
348
    if (this.interfaceID > 0) {
349
      if (this.currentInterface && (this.currentInterface.id !== null) && !this.inRegister) {
350
        this.repoService.deleteInterface(this.currentInterface.id, this.currentRepo.registeredBy).subscribe(
351
          res => console.log(`deleteInterface responded: ${JSON.stringify(res)}`),
352
          er => console.log(er),
353
          () => this.emitDeleteInterface.emit(this.interfaceID)
354
        );
355
      } else {
356
        this.emitDeleteInterface.emit(this.interfaceID);
357
      }
358
    } else {
359
      this.errorMessage = nonRemovableInterface;
360
    }
361
  }
362

    
363
  getInterface() {
364
    return this.interfaceToExport;
365
  }
366

    
367
}
(4-4/8)