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
        this.repoService.getInterfaceDesiredCompatibilityLevel(this.currentInterface.datasource, this.currentInterface.id).subscribe(
79
          res => {
80
            if (res !== null) {
81
              this.repoInterfaceForm.get('desiredCompatibilityLevel').setValue(res['desiredCompatibilityLevel']);
82
            }
83
          }
84
        );
85
      }
86
      this.getInterfaceInfo();
87
      this.getCompatibilityClasses();
88
    }
89
  }
90

    
91

    
92
  getInterfaceInfo() {
93
    this.successMessage = '';
94
    this.errorMessage = '';
95

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

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

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

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

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

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

    
188
  }
189

    
190
  saveInterface() {
191
    this.errorMessage = '';
192
    this.successMessage = '';
193
    console.log('saving interface: ' + this.currentInterface?.id);
194
    if (this.formIsValid()) {
195
      const baseurl = this.repoInterfaceForm.get('baseurl').value;
196
      let valset = '';
197
      if (this.existingValSet) {
198
        valset = this.repoInterfaceForm.get('selectValidationSet').value;
199
      }
200
      let desiredCompLvl = '';
201
      if (this.repoInterfaceForm.get('compatibilityLevel').value) {
202
        // this.existingCompLevel = this.compClasses[this.repoInterfaceForm.get('compatibilityLevel').value];
203
        // console.log('this.existingCompLevel is', this.existingCompLevel);
204
        desiredCompLvl = this.repoInterfaceForm.get('compatibilityLevel').value;
205
      }
206
      const compLvl = this.existingCompLevel;
207
      let comment = '';
208
      if (this.repoInterfaceForm.get('comment').value) {
209
        comment = this.repoInterfaceForm.get('comment').value;
210
      }
211

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

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

    
241
    return intrf;
242
  }
243

    
244
  updateValidationSet(intrf: RepositoryInterface, value: string) {
245
    let validationSet = intrf.apiParams.find(element => element.param === 'set');
246
    if (!validationSet) {
247
      validationSet = new ApiParamDetails('set', value);
248
      intrf.apiParams.push(validationSet);
249
    } else {
250
      validationSet.value = this.repoInterfaceForm.get('selectValidationSet').value;
251
    }
252
  }
253

    
254
  addCurrent (baseurl: string, valset: string, compLvl: string, comment: string) {
255
    console.log('add current');
256
    const currentInterface = new RepositoryInterface();
257
    this.updateValidationSet(currentInterface, valset);
258
    currentInterface.baseurl = baseurl;
259
    currentInterface.compatibilityOverride = compLvl;
260
    currentInterface.compatibility = compLvl;
261
    currentInterface.typology = this.currentRepo.datasourceClass;
262
    currentInterface.comments = comment;
263

    
264
    if (!this.inRegister) {
265
      this.addInterface(currentInterface);
266
    } else {
267
      this.successMessage = 'The harvesting settings are valid!';
268
      console.log('SAVED !');
269
      this.interfaceToExport = currentInterface;
270
    }
271
  }
272

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

    
299
  }
300

    
301
  updateCurrent (baseurl: string, valset: string, desiredCompLvl: string, compLvl: string, comment: string) {
302
    console.log('update current');
303
    this.updateValidationSet(this.currentInterface, valset);
304
    this.currentInterface.baseurl = baseurl;
305
    this.currentInterface.compatibilityOverride = compLvl;
306
    this.currentInterface.compatibility = 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 harvesting settings are valid!';
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, this.currentRepo.registeredBy, this.currentRepo.comments,
322
                                     this.currentInterface, this.repoInterfaceForm.get('desiredCompatibilityLevel').value).subscribe(
323
      response => {
324
        console.log(`updateRepository responded ${JSON.stringify(response)}`);
325
        if (response) {
326
          this.currentInterface = response;
327
          this.successMessage = formSuccessUpdatedInterface;
328
        } else {
329
          this.errorMessage = formErrorWasntSaved;
330
        }
331
      },
332
      error => {
333
        console.log(error);
334
        this.loadingMessage = '';
335
        this.errorMessage = formErrorWasntSaved;
336
      },
337
      () => {
338
        this.loadingMessage = '';
339
        this.getExistingCompatibilityLevel();
340
      }
341
    );
342
  }
343

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

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

    
366
}
(4-4/8)