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
    customValidationSet: [''],
43
    compatibilityLevel: [''],
44
    comment: ['']
45
  };
46
  baseUrlDesc: Description = baseUrlDesc;
47
  existingValSetDesc: Description = existingValSetDesc;
48
  customValSetDesc: Description = customValSetDesc;
49
  compatibilityLevelDesc: Description = compatibilityLevelDesc;
50
  commentDesc: Description = commentDesc;
51

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

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

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

    
82

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

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

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

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

    
161
  chooseValSet(fromList: boolean) {
162
    this.existingValSet = fromList;
163
    if (this.existingValSet) {
164
      this.repoInterfaceForm.get('selectValidationSet').enable();
165
      this.repoInterfaceForm.get('customValidationSet').disable();
166
    }  else {
167
      this.repoInterfaceForm.get('selectValidationSet').disable();
168
      this.repoInterfaceForm.get('customValidationSet').enable();
169
    }
170
    this.checkIfValid();
171
  }
172

    
173
  checkIfCompatibilityLevelWasChosen() {
174
    return ( (this.repoInterfaceForm.get('compatibilityLevel').value !== '') ||
175
             (this.existingCompLevel && (this.existingCompLevel !== '')) );
176
  }
177

    
178
  formIsValid() {
179
    return (this.repoInterfaceForm.valid &&
180
            this.identifiedBaseUrl &&
181
            this.checkIfCompatibilityLevelWasChosen());
182
  }
183

    
184
  checkIfValid() {
185
    if (this.formIsValid()) {
186
      if (this.inRegister) {
187
        // this.successMessage = 'The interface will be stored when the registration procedure is completed.';
188
        this.successMessage = 'The harvesting settings are valid!';
189
        this.saveInterface();
190
      }
191
    } else {
192
      this.successMessage = '';
193
      this.interfaceToExport = null;
194
    }
195

    
196
  }
197

    
198
  saveInterface() {
199
    this.errorMessage = '';
200
    this.successMessage = '';
201
    if (this.formIsValid()) {
202
      const baseurl = this.repoInterfaceForm.get('baseurl').value;
203
      let valset = '';
204
      if (this.existingValSet) {
205
        valset = this.repoInterfaceForm.get('selectValidationSet').value;
206
      } else {
207
        valset = this.repoInterfaceForm.get('customValidationSet').value;
208
      }
209
      let compLvl = '';
210
      if (this.repoInterfaceForm.get('compatibilityLevel').value) {
211
        this.existingCompLevel = this.compClasses[this.repoInterfaceForm.get('compatibilityLevel').value];
212
        console.log('this.existingCompLevel is', this.existingCompLevel);
213
        compLvl = this.repoInterfaceForm.get('compatibilityLevel').value;
214
      } else {
215
        compLvl = this.existingCompLevel;
216
      }
217
      let comment = '';
218
      if (this.repoInterfaceForm.get('comment').value) {
219
        comment = this.repoInterfaceForm.get('comment').value;
220
      }
221

    
222
      if (this.currentInterface) {
223
        this.updateCurrent(baseurl, valset, compLvl, comment);
224
      } else {
225
        this.addCurrent(baseurl, valset, compLvl, comment);
226
      }
227
    } else {
228
      this.interfaceToExport = null;
229
      this.errorMessage = 'Please make sure all required fields are filled with acceptable values.';
230
    }
231
  }
232

    
233
  getCurrentValues() {
234
    console.log('getcurrentvalues');
235
    let intrf = this.currentInterface;
236
    if (intrf == null) {
237
      intrf = new RepositoryInterface();
238
    }
239
    intrf.baseurl = this.repoInterfaceForm.get('baseurl').value;
240
    this.updateValidationSet(intrf, this.repoInterfaceForm.get(this.existingValSet ? 'selectValidationSet' : 'customValidationSet').value);
241
    console.log(intrf);
242
    if (this.repoInterfaceForm.get('compatibilityLevel').value) {
243
      intrf.compatibilityOverride = this.repoInterfaceForm.get('compatibilityLevel').value;
244
      intrf.compatibility = this.repoInterfaceForm.get('compatibilityLevel').value;
245
    } else {
246
      intrf.compatibilityOverride = this.existingCompLevel;
247
      intrf.compatibility = this.existingCompLevel;
248
    }
249
    intrf.typology = this.currentRepo.datasourceClass;
250

    
251
    return intrf;
252
  }
253

    
254
  updateValidationSet(intrf: RepositoryInterface, value: string) {
255
    let validationSet = intrf.apiParams.find(element => element.param === 'set');
256
    if (!validationSet) {
257
      validationSet = new ApiParamDetails('set', value);
258
      intrf.apiParams.push(validationSet);
259
    } else {
260
      validationSet.value = this.repoInterfaceForm.get(this.existingValSet ? 'selectValidationSet' : 'customValidationSet').value;
261
    }
262
  }
263

    
264
  addCurrent (baseurl: string, valset: string, compLvl: string, comment: string) {
265
    console.log('add current');
266
    const currentInterface = new RepositoryInterface();
267
    this.updateValidationSet(currentInterface, valset);
268
    currentInterface.baseurl = baseurl;
269
    currentInterface.compatibilityOverride = compLvl;
270
    currentInterface.compatibility = compLvl;
271
    currentInterface.typology = this.currentRepo.datasourceClass;
272
    currentInterface.comments = comment;
273

    
274
    if (!this.inRegister) {
275
      this.addInterface(currentInterface);
276
    } else {
277
      this.successMessage = 'The harvesting settings are valid!';
278
      console.log('SAVED !');
279
      this.interfaceToExport = currentInterface;
280
    }
281
  }
282

    
283
  addInterface(newInterface: RepositoryInterface) {
284
    this.loadingMessage = formSubmitting;
285
    this.repoService.addInterface(this.currentRepo.datasourceType,
286
                                  this.currentRepo.id,
287
                                  this.currentRepo.registeredBy,
288
                                  this.currentRepo.comments,
289
                                  newInterface).subscribe(
290
      addedInterface => {
291
        console.log(`addInterface responded ${JSON.stringify(addedInterface)}`);
292
        this.currentInterface = addedInterface;
293
      },
294
      error => {
295
        console.log(error);
296
        this.loadingMessage = '';
297
        this.errorMessage = formErrorWasntSaved;
298
        this.currentInterface = null;
299
      },
300
      () => {
301
        this.loadingMessage = '';
302
        if (this.currentInterface.id) {
303
          this.successMessage = formSuccessAddedInterface;
304
          this.getExistingCompatibilityLevel();
305
        } else {
306
          this.errorMessage = formErrorWasntSaved;
307
        }
308
      }
309
    );
310

    
311
  }
312

    
313
  updateCurrent (baseurl: string, valset: string, compLvl: string, comment: string) {
314
    console.log('update current');
315
    this.updateValidationSet(this.currentInterface, valset);
316
    this.currentInterface.baseurl = baseurl;
317
    this.currentInterface.compatibilityOverride = compLvl;
318
    this.currentInterface.compatibility = compLvl;
319
    this.currentInterface.typology = this.currentRepo.datasourceClass;
320
    this.currentInterface.comments = comment;
321

    
322
    if (!this.inRegister) {
323
      this.updateInterface();
324
    } else {
325
      this.successMessage = 'The harvesting settings are valid!';
326
      console.log('SAVED !');
327
      this.interfaceToExport = this.currentInterface;
328
    }
329
  }
330

    
331
  updateInterface() {
332
    this.loadingMessage = formSubmitting;
333
    this.repoService.updateInterface(this.currentRepo.id,
334
                                     this.currentRepo.registeredBy,
335
                                     this.currentRepo.comments,
336
                                     this.currentInterface).subscribe(
337
      response => {
338
        console.log(`updateRepository responded ${JSON.stringify(response)}`);
339
        if (response) {
340
          this.currentInterface = response;
341
          this.successMessage = formSuccessUpdatedInterface;
342
        } else {
343
          this.errorMessage = formErrorWasntSaved;
344
        }
345
      },
346
      error => {
347
        console.log(error);
348
        this.loadingMessage = '';
349
        this.errorMessage = formErrorWasntSaved;
350
      },
351
      () => {
352
        this.loadingMessage = '';
353
        this.getExistingCompatibilityLevel();
354
      }
355
    );
356
  }
357

    
358
  removeInterface() {
359
    this.errorMessage = '';
360
    this.successMessage = '';
361
    if (this.interfaceID > 0) {
362
      if (this.currentInterface && (this.currentInterface.id !== null) && !this.inRegister) {
363
        this.repoService.deleteInterface(this.currentInterface.id, this.currentRepo.registeredBy).subscribe(
364
          res => console.log(`deleteInterface responded: ${JSON.stringify(res)}`),
365
          er => console.log(er),
366
          () => this.emitDeleteInterface.emit(this.interfaceID)
367
        );
368
      } else {
369
        this.emitDeleteInterface.emit(this.interfaceID);
370
      }
371
    } else {
372
      this.errorMessage = nonRemovableInterface;
373
    }
374
  }
375

    
376
  getInterface() {
377
    return this.interfaceToExport;
378
  }
379

    
380
}
(4-4/8)