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, commentDesc } from '../../../domain/oa-description';
4
import {ApiParamDetails, 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
  comments: string;
16
}
17

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

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

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

    
38
  repoInterfaceForm: FormGroup;
39
  readonly repoInterfaceFormDef = {
40
    baseurl: ['', Validators.required],
41
    selectValidationSet: [''],
42
    compatibilityLevel: null,
43
    desiredCompatibilityLevel: null,
44
    compatibilityLevelOverride: null,
45
    comment: ['']
46
  };
47
  baseUrlDesc: Description = baseUrlDesc;
48
  existingValSetDesc: Description = existingValSetDesc;
49
  customValSetDesc: Description = customValSetDesc;
50
  compatibilityLevelDesc: Description = compatibilityLevelDesc;
51
  commentDesc: Description = commentDesc;
52

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

    
62
  constructor(private fb: FormBuilder,
63
              private valService: ValidatorService,
64
              private repoService: RepositoryService) {}
65

    
66
  ngOnInit() {
67
    if (this.data && (this.data.length >= 3)) {
68
      this.inRegister = this.data[0];
69
      this.interfaceID = this.data[1];
70
      this.currentRepo = this.data[2];
71
      this.repoInterfaceForm = this.fb.group(this.repoInterfaceFormDef);
72
      // this.chooseValSet(true);
73
      if (this.data[3]) {
74
        this.currentInterface = this.data[3];
75
        this.repoInterfaceForm.get('baseurl').setValue(this.currentInterface.baseurl);
76
        this.repoInterfaceForm.get('compatibilityLevel').setValue(this.currentInterface.compatibility);
77
        this.repoInterfaceForm.get('compatibilityLevelOverride').setValue(this.currentInterface.compatibilityOverride);
78
      }
79
      this.getInterfaceInfo();
80
      this.getCompatibilityClasses();
81
    }
82
  }
83

    
84

    
85
  getInterfaceInfo() {
86
    this.successMessage = '';
87
    this.errorMessage = '';
88

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

    
130
  getCompatibilityClasses() {
131
    // FIXME: Use eoscDatasourceType when we support the new model
132
    if (this.mode === null) {
133
      this.mode = this.currentRepo.datasourceType;
134
    }
135
    this.repoService.getCompatibilityClasses(this.mode).subscribe(
136
      classes => {
137
        this.compClasses = classes;
138
        this.classCodes = Object.keys(this.compClasses);
139
      },
140
      error => {
141
        this.errorMessage = noServiceMessage;
142
        console.log(error);
143
      },
144
      () => {
145
        this.getExistingCompatibilityLevel();
146
        this.repoInterfaceForm.updateValueAndValidity();
147
      }
148
    );
149
  }
150

    
151
  getExistingCompatibilityLevel() {
152
    if (this.currentInterface) {
153
      if (this.currentInterface.compatibility
154
        && this.classCodes.some( x => x === this.currentInterface.compatibilityOverride)) {
155
        this.existingCompLevel = this.compClasses[this.currentInterface.compatibility];
156
      } else {
157
        // this.repoInterfaceForm.get('compatibilityLevel').setValue('');
158
        this.existingCompLevel = this.currentInterface.compatibility;
159
      }
160
    }
161
  }
162

    
163
  checkIfCompatibilityLevelWasChosen() {
164
    return ( (this.repoInterfaceForm.get('compatibilityLevel').value !== '') ||
165
             (this.existingCompLevel && (this.existingCompLevel !== '')) );
166
  }
167

    
168
  formIsValid() {
169
    return (this.repoInterfaceForm.valid && this.identifiedBaseUrl && this.checkIfCompatibilityLevelWasChosen());
170
  }
171

    
172
  checkIfValid() {
173
    if (this.formIsValid()) {
174
      if (this.inRegister) {
175
        // this.successMessage = 'The interface will be stored when the registration procedure is completed.';
176
        this.successMessage = 'The harvesting settings are valid!';
177
        this.saveInterface();
178
      }
179
    } else {
180
      this.successMessage = '';
181
      this.interfaceToExport = null;
182
    }
183

    
184
  }
185

    
186
  saveInterface() {
187
    this.errorMessage = '';
188
    this.successMessage = '';
189
    if (this.formIsValid()) {
190
      const baseurl = this.repoInterfaceForm.get('baseurl').value;
191
      let valset = '';
192
      if (this.existingValSet) {
193
        valset = this.repoInterfaceForm.get('selectValidationSet').value;
194
      }
195
      let desiredCompLvl = '';
196
      if (this.repoInterfaceForm.get('compatibilityLevel').value) {
197
        // this.existingCompLevel = this.compClasses[this.repoInterfaceForm.get('compatibilityLevel').value];
198
        // console.log('this.existingCompLevel is', this.existingCompLevel);
199
        desiredCompLvl = this.repoInterfaceForm.get('compatibilityLevel').value;
200
      }
201
      const compLvl = this.existingCompLevel;
202
      let comment = '';
203
      if (this.repoInterfaceForm.get('comment').value) {
204
        comment = this.repoInterfaceForm.get('comment').value;
205
      }
206

    
207
      if (this.currentInterface) {
208
        this.updateCurrent(baseurl, valset, desiredCompLvl, compLvl, comment);
209
      } else {
210
        this.addCurrent(baseurl, valset, compLvl, comment);
211
      }
212
    } else {
213
      this.interfaceToExport = null;
214
      this.errorMessage = 'Please make sure all required fields are filled with acceptable values.';
215
    }
216
  }
217

    
218
  getCurrentValues() {
219
    console.log('getcurrentvalues');
220
    let intrf = this.currentInterface;
221
    if (intrf == null) {
222
      intrf = new RepositoryInterface();
223
    }
224
    intrf.baseurl = this.repoInterfaceForm.get('baseurl').value;
225
    this.updateValidationSet(intrf, this.repoInterfaceForm.get(this.existingValSet ? 'selectValidationSet' : 'customValidationSet').value);
226
    console.log(intrf);
227
    if (this.repoInterfaceForm.get('compatibilityLevel').value) {
228
      intrf.compatibilityOverride = this.repoInterfaceForm.get('compatibilityLevel').value;
229
      intrf.compatibility = this.repoInterfaceForm.get('compatibilityLevel').value;
230
    } else {
231
      intrf.compatibilityOverride = this.existingCompLevel;
232
      intrf.compatibility = this.existingCompLevel;
233
    }
234
    intrf.typology = this.currentRepo.datasourceClass;
235

    
236
    return intrf;
237
  }
238

    
239
  updateValidationSet(intrf: RepositoryInterface, value: string) {
240
    let validationSet = intrf.apiParams.find(element => element.param === 'set');
241
    if (!validationSet) {
242
      validationSet = new ApiParamDetails('set', value);
243
      intrf.apiParams.push(validationSet);
244
    } else {
245
      validationSet.value = this.repoInterfaceForm.get('selectValidationSet').value;
246
    }
247
  }
248

    
249
  addCurrent (baseurl: string, valset: string, compLvl: string, comment: string) {
250
    console.log('add current');
251
    const currentInterface = new RepositoryInterface();
252
    this.updateValidationSet(currentInterface, valset);
253
    currentInterface.baseurl = baseurl;
254
    currentInterface.compatibilityOverride = compLvl;
255
    currentInterface.compatibility = compLvl;
256
    currentInterface.typology = this.currentRepo.datasourceClass;
257
    currentInterface.comments = comment;
258

    
259
    if (!this.inRegister) {
260
      this.addInterface(currentInterface);
261
    } else {
262
      this.successMessage = 'The harvesting settings are valid!';
263
      console.log('SAVED !');
264
      this.interfaceToExport = currentInterface;
265
    }
266
  }
267

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

    
293
  }
294

    
295
  updateCurrent (baseurl: string, valset: string, desiredCompLvl: string, compLvl: string, comment: string) {
296
    console.log('update current');
297
    this.updateValidationSet(this.currentInterface, valset);
298
    this.currentInterface.baseurl = baseurl;
299
    this.currentInterface.compatibilityOverride = compLvl;
300
    this.currentInterface.compatibility = compLvl;
301
    this.currentInterface.typology = this.currentRepo.datasourceClass;
302
    this.currentInterface.comments = comment;
303

    
304
    if (!this.inRegister) {
305
      this.updateInterface(desiredCompLvl);
306
    } else {
307
      this.successMessage = 'The harvesting settings are valid!';
308
      console.log('SAVED !');
309
      this.interfaceToExport = this.currentInterface;
310
    }
311
  }
312

    
313
  updateInterface(desiredCompatibilityLevel: string) {
314
    this.loadingMessage = formSubmitting;
315
    this.repoService.updateInterface(this.currentRepo.id, this.currentRepo.registeredBy, this.currentRepo.comments,
316
                                     this.currentInterface, this.repoInterfaceForm.get('desiredCompatibilityLevel').value).subscribe(
317
      response => {
318
        console.log(`updateRepository responded ${JSON.stringify(response)}`);
319
        if (response) {
320
          this.currentInterface = response;
321
          this.successMessage = formSuccessUpdatedInterface;
322
        } else {
323
          this.errorMessage = formErrorWasntSaved;
324
        }
325
      },
326
      error => {
327
        console.log(error);
328
        this.loadingMessage = '';
329
        this.errorMessage = formErrorWasntSaved;
330
      },
331
      () => {
332
        this.loadingMessage = '';
333
        this.getExistingCompatibilityLevel();
334
      }
335
    );
336
  }
337

    
338
  removeInterface() {
339
    this.errorMessage = '';
340
    this.successMessage = '';
341
    if (this.interfaceID > 0) {
342
      if (this.currentInterface && (this.currentInterface.id !== null) && !this.inRegister) {
343
        this.repoService.deleteInterface(this.currentInterface.id, this.currentRepo.registeredBy).subscribe(
344
          res => console.log(`deleteInterface responded: ${JSON.stringify(res)}`),
345
          er => console.log(er),
346
          () => this.emitDeleteInterface.emit(this.interfaceID)
347
        );
348
      } else {
349
        this.emitDeleteInterface.emit(this.interfaceID);
350
      }
351
    } else {
352
      this.errorMessage = nonRemovableInterface;
353
    }
354
  }
355

    
356
  getInterface() {
357
    return this.interfaceToExport;
358
  }
359

    
360
}
(4-4/8)