Project

General

Profile

1 54479 myrto.kouk
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
2
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
3 59207 andreas.ma
import { baseUrlDesc, compatibilityLevelDesc, customValSetDesc, Description, existingValSetDesc, commentDesc } from '../../../domain/oa-description';
4 62389 andreas.ma
import {ApiParamDetails, InterfaceInformation, RepositoryInterface} from '../../../domain/typeScriptClasses';
5 54479 myrto.kouk
import { ValidatorService } from '../../../services/validator.service';
6
import { RepositoryService } from '../../../services/repository.service';
7 55618 myrto.kouk
import { formErrorWasntSaved, formInfoLoading, formSubmitting, formSuccessAddedInterface, formSuccessUpdatedInterface, invalidCustomBaseUrl,
8
         nonRemovableInterface, noServiceMessage } from '../../../domain/shared-messages';
9 54479 myrto.kouk
10
export class RepoFields {
11
  id: string;
12
  datasourceType: string;
13
  datasourceClass: string;
14
  registeredBy: string;
15 60937 andreas.ma
  comments: string;
16 54479 myrto.kouk
}
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 59151 andreas.ma
  invalidCustomBaseUrl = invalidCustomBaseUrl;
27 54479 myrto.kouk
28
  @Input() data: any[] = []; // expects an array containing at least 3 of the 4 below fields in this order
29 62103 antonis.le
  @Input() mode: string = null;
30 54479 myrto.kouk
  inRegister: boolean;
31 55618 myrto.kouk
  interfaceID: number;      // holds the interface index in the interfaces array as displayed
32
  currentRepo: RepoFields;  // a fraction of the Repository class
33 54479 myrto.kouk
  currentInterface: RepositoryInterface;
34
35
  @Output() emitDeleteInterface: EventEmitter<number> = new EventEmitter<number>();
36
  interfaceToExport: RepositoryInterface;
37
38
  repoInterfaceForm: FormGroup;
39
  readonly repoInterfaceFormDef = {
40 62389 andreas.ma
    baseurl: ['', Validators.required],
41 54479 myrto.kouk
    selectValidationSet: [''],
42
    customValidationSet: [''],
43 59151 andreas.ma
    compatibilityLevel: [''],
44 59207 andreas.ma
    comment: ['']
45 54479 myrto.kouk
  };
46
  baseUrlDesc: Description = baseUrlDesc;
47
  existingValSetDesc: Description = existingValSetDesc;
48
  customValSetDesc: Description = customValSetDesc;
49
  compatibilityLevelDesc: Description = compatibilityLevelDesc;
50 59207 andreas.ma
  commentDesc: Description = commentDesc;
51 54479 myrto.kouk
52
  identifiedBaseUrl: boolean;
53 59151 andreas.ma
  showIdentifiedBaseUrl: boolean;
54 54479 myrto.kouk
  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 62389 andreas.ma
        this.repoInterfaceForm.get('baseurl').setValue(this.currentInterface.baseurl);
75
        this.repoInterfaceForm.get('compatibilityLevel').setValue(this.currentInterface.compatibilityOverride);
76 54479 myrto.kouk
      }
77
      this.getInterfaceInfo();
78
      this.getCompatibilityClasses();
79
    }
80
  }
81
82
83
  getInterfaceInfo() {
84
    this.successMessage = '';
85
    this.errorMessage = '';
86
87 62389 andreas.ma
    const  baseurl = this.repoInterfaceForm.get('baseurl').value;
88
    if (baseurl) {
89 54479 myrto.kouk
      this.loadingMessage = formInfoLoading;
90 62389 andreas.ma
      this.valService.getInterfaceInformation(baseurl).subscribe(
91 54479 myrto.kouk
        info => {
92
          this.interfaceInfo = info;
93
          if (this.interfaceInfo.identified) {
94
            this.identifiedBaseUrl = true;
95 59151 andreas.ma
            this.showIdentifiedBaseUrl = true;
96 54479 myrto.kouk
          } else {
97
            this.errorMessage = invalidCustomBaseUrl;
98 62389 andreas.ma
            this.identifiedBaseUrl = true;  // pass interface without baseurl identification
99 59151 andreas.ma
            this.showIdentifiedBaseUrl = false;
100 54479 myrto.kouk
          }
101
          if (this.interfaceInfo.sets) {
102
            this.valsetList = this.interfaceInfo.sets;
103 62389 andreas.ma
            // console.log(this.valsetList);
104 54479 myrto.kouk
          }
105
        },
106
        error => {
107
          console.log(error);
108
          this.loadingMessage = '';
109 59151 andreas.ma
          // this.identifiedBaseUrl = false;
110 54479 myrto.kouk
          this.errorMessage = noServiceMessage;
111
        },
112
        () => {
113 62389 andreas.ma
          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 54479 myrto.kouk
            } else {
117 62389 andreas.ma
              this.repoInterfaceForm.get('customValidationSet').setValue(this.currentInterface.apiParams.find(entry => entry.param === 'set').value);
118 54479 myrto.kouk
            }
119
          }
120
          this.loadingMessage = '';
121
          this.repoInterfaceForm.updateValueAndValidity();
122
          this.checkIfValid();
123
        }
124
      );
125
    }
126
  }
127
128
  getCompatibilityClasses() {
129 62103 antonis.le
    // 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 54479 myrto.kouk
      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 62389 andreas.ma
      if (this.currentInterface.compatibilityOverride &&
152
        this.classCodes.some( x => x === this.currentInterface.compatibilityOverride ) ) {
153
        this.existingCompLevel = this.compClasses[this.currentInterface.compatibilityOverride];
154 54479 myrto.kouk
      } else {
155
        this.repoInterfaceForm.get('compatibilityLevel').setValue('');
156 62389 andreas.ma
        this.existingCompLevel = this.currentInterface.compatibilityOverride;
157 54479 myrto.kouk
      }
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 59904 stefania.m
        // this.successMessage = 'The interface will be stored when the registration procedure is completed.';
188
        this.successMessage = 'The harvesting settings are valid!';
189 54479 myrto.kouk
        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 62389 andreas.ma
      const baseurl = this.repoInterfaceForm.get('baseurl').value;
203 54479 myrto.kouk
      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 59151 andreas.ma
      let comment = '';
218 59207 andreas.ma
      if (this.repoInterfaceForm.get('comment').value) {
219
        comment = this.repoInterfaceForm.get('comment').value;
220 59151 andreas.ma
      }
221 54479 myrto.kouk
222
      if (this.currentInterface) {
223 62389 andreas.ma
        this.updateCurrent(baseurl, valset, compLvl, comment);
224 54479 myrto.kouk
      } else {
225 62389 andreas.ma
        this.addCurrent(baseurl, valset, compLvl, comment);
226 54479 myrto.kouk
      }
227
    } else {
228
      this.interfaceToExport = null;
229 55618 myrto.kouk
      this.errorMessage = 'Please make sure all required fields are filled with acceptable values.';
230 54479 myrto.kouk
    }
231
  }
232
233 55618 myrto.kouk
  getCurrentValues() {
234 62389 andreas.ma
    console.log('getcurrentvalues');
235 55618 myrto.kouk
    let intrf = this.currentInterface;
236
    if (intrf == null) {
237
      intrf = new RepositoryInterface();
238
    }
239 62389 andreas.ma
    intrf.baseurl = this.repoInterfaceForm.get('baseurl').value;
240
    this.updateValidationSet(intrf, this.repoInterfaceForm.get(this.existingValSet ? 'selectValidationSet' : 'customValidationSet').value);
241
    console.log(intrf);
242 55618 myrto.kouk
    if (this.repoInterfaceForm.get('compatibilityLevel').value) {
243 62389 andreas.ma
      intrf.compatibilityOverride = this.repoInterfaceForm.get('compatibilityLevel').value;
244
      intrf.compatibility = this.repoInterfaceForm.get('compatibilityLevel').value;
245 55618 myrto.kouk
    } else {
246 62389 andreas.ma
      intrf.compatibilityOverride = this.existingCompLevel;
247
      intrf.compatibility = this.existingCompLevel;
248 55618 myrto.kouk
    }
249
    intrf.typology = this.currentRepo.datasourceClass;
250
251
    return intrf;
252
  }
253
254 62389 andreas.ma
  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 54479 myrto.kouk
    const currentInterface = new RepositoryInterface();
267 62389 andreas.ma
    this.updateValidationSet(currentInterface, valset);
268
    currentInterface.baseurl = baseurl;
269
    currentInterface.compatibilityOverride = compLvl;
270
    currentInterface.compatibility = compLvl;
271 54479 myrto.kouk
    currentInterface.typology = this.currentRepo.datasourceClass;
272 60937 andreas.ma
    currentInterface.comments = comment;
273 59151 andreas.ma
274 54479 myrto.kouk
    if (!this.inRegister) {
275
      this.addInterface(currentInterface);
276
    } else {
277 59904 stefania.m
      this.successMessage = 'The harvesting settings are valid!';
278 54479 myrto.kouk
      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 60937 andreas.ma
                                  this.currentRepo.comments,
289 54479 myrto.kouk
                                  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 62389 andreas.ma
  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 54479 myrto.kouk
    this.currentInterface.typology = this.currentRepo.datasourceClass;
320 60937 andreas.ma
    this.currentInterface.comments = comment;
321 54479 myrto.kouk
322
    if (!this.inRegister) {
323
      this.updateInterface();
324
    } else {
325 59904 stefania.m
      this.successMessage = 'The harvesting settings are valid!';
326 54479 myrto.kouk
      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 60937 andreas.ma
                                     this.currentRepo.comments,
336 54479 myrto.kouk
                                     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 55618 myrto.kouk
      if (this.currentInterface && (this.currentInterface.id !== null) && !this.inRegister) {
363 54479 myrto.kouk
        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
}