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 54479 myrto.kouk
import { InterfaceInformation, RepositoryInterface } from '../../../domain/typeScriptClasses';
5
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
    baseUrl: ['', Validators.required],
41
    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
        this.repoInterfaceForm.get('baseUrl').setValue(this.currentInterface.baseUrl);
75
        this.repoInterfaceForm.get('compatibilityLevel').setValue(this.currentInterface.desiredCompatibilityLevel);
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 59151 andreas.ma
            this.showIdentifiedBaseUrl = true;
96 54479 myrto.kouk
          } else {
97
            this.errorMessage = invalidCustomBaseUrl;
98 59151 andreas.ma
            this.identifiedBaseUrl = true;  // pass interface without baseUrl identification
99
            this.showIdentifiedBaseUrl = false;
100 54479 myrto.kouk
          }
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 59151 andreas.ma
          // this.identifiedBaseUrl = false;
110 54479 myrto.kouk
          this.errorMessage = noServiceMessage;
111
        },
112
        () => {
113
          if ( this.currentInterface && this.currentInterface.accessParams && this.currentInterface.accessParams['set'] ) {
114
            if ( this.valsetList.some( x => x === this.currentInterface.accessParams['set']) ) {
115
              this.repoInterfaceForm.get('selectValidationSet').setValue(this.currentInterface.accessParams['set']);
116
            } else {
117
              this.repoInterfaceForm.get('customValidationSet').setValue(this.currentInterface.accessParams['set']);
118
            }
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
      if (this.currentInterface.desiredCompatibilityLevel &&
152
        this.classCodes.some( x => x === this.currentInterface.desiredCompatibilityLevel ) ) {
153
        this.existingCompLevel = this.compClasses[this.currentInterface.desiredCompatibilityLevel];
154
      } else {
155
        this.repoInterfaceForm.get('compatibilityLevel').setValue('');
156
        this.existingCompLevel = this.currentInterface.desiredCompatibilityLevel;
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 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
      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 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 59151 andreas.ma
        this.updateCurrent(baseUrl, valset, compLvl, comment);
224 54479 myrto.kouk
      } else {
225 59151 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
    let intrf = this.currentInterface;
235
    if (intrf == null) {
236
      intrf = new RepositoryInterface();
237
    }
238
    intrf.baseUrl = this.repoInterfaceForm.get('baseUrl').value;
239
    if (this.existingValSet) {
240
      intrf.accessSet = this.repoInterfaceForm.get('selectValidationSet').value;
241
      intrf.accessParams = {'set': this.repoInterfaceForm.get('selectValidationSet').value};
242
    } else {
243
      intrf.accessSet = this.repoInterfaceForm.get('customValidationSet').value;
244
      intrf.accessParams = {'set': this.repoInterfaceForm.get('customValidationSet').value};
245
    }
246
    if (this.repoInterfaceForm.get('compatibilityLevel').value) {
247
      intrf.desiredCompatibilityLevel = this.repoInterfaceForm.get('compatibilityLevel').value;
248
      intrf.compliance = this.repoInterfaceForm.get('compatibilityLevel').value;
249
    } else {
250
      intrf.desiredCompatibilityLevel = this.existingCompLevel;
251
      intrf.compliance = this.existingCompLevel;
252
    }
253
    intrf.typology = this.currentRepo.datasourceClass;
254
255
    return intrf;
256
  }
257
258 59151 andreas.ma
  addCurrent (baseUrl: string, valset: string, compLvl: string, comment: string) {
259 54479 myrto.kouk
    const currentInterface = new RepositoryInterface();
260
    currentInterface.baseUrl = baseUrl;
261
    currentInterface.accessSet = valset;
262
    currentInterface.accessParams = {'set': valset};
263
    currentInterface.desiredCompatibilityLevel = compLvl;
264
    currentInterface.compliance = compLvl;
265
    currentInterface.typology = this.currentRepo.datasourceClass;
266 60937 andreas.ma
    currentInterface.comments = comment;
267 59151 andreas.ma
268 54479 myrto.kouk
    if (!this.inRegister) {
269
      this.addInterface(currentInterface);
270
    } else {
271 59904 stefania.m
      this.successMessage = 'The harvesting settings are valid!';
272 54479 myrto.kouk
      console.log('SAVED !');
273
      this.interfaceToExport = currentInterface;
274
    }
275
  }
276
277
  addInterface(newInterface: RepositoryInterface) {
278
    this.loadingMessage = formSubmitting;
279
    this.repoService.addInterface(this.currentRepo.datasourceType,
280
                                  this.currentRepo.id,
281
                                  this.currentRepo.registeredBy,
282 60937 andreas.ma
                                  this.currentRepo.comments,
283 54479 myrto.kouk
                                  newInterface).subscribe(
284
      addedInterface => {
285
        console.log(`addInterface responded ${JSON.stringify(addedInterface)}`);
286
        this.currentInterface = addedInterface;
287
      },
288
      error => {
289
        console.log(error);
290
        this.loadingMessage = '';
291
        this.errorMessage = formErrorWasntSaved;
292
        this.currentInterface = null;
293
      },
294
      () => {
295
        this.loadingMessage = '';
296
        if (this.currentInterface.id) {
297
          this.successMessage = formSuccessAddedInterface;
298
          this.getExistingCompatibilityLevel();
299
        } else {
300
          this.errorMessage = formErrorWasntSaved;
301
        }
302
      }
303
    );
304
305
  }
306
307
308 59151 andreas.ma
  updateCurrent (baseUrl: string, valset: string, compLvl: string, comment: string) {
309 54479 myrto.kouk
    this.currentInterface.baseUrl = baseUrl;
310
    this.currentInterface.accessSet = valset;
311
    this.currentInterface.accessParams['set'] = valset;
312
    this.currentInterface.desiredCompatibilityLevel = compLvl;
313
    this.currentInterface.compliance = compLvl;
314
    this.currentInterface.typology = this.currentRepo.datasourceClass;
315 60937 andreas.ma
    this.currentInterface.comments = comment;
316 54479 myrto.kouk
317
    if (!this.inRegister) {
318
      this.updateInterface();
319
    } else {
320 59904 stefania.m
      this.successMessage = 'The harvesting settings are valid!';
321 54479 myrto.kouk
      console.log('SAVED !');
322
      this.interfaceToExport = this.currentInterface;
323
    }
324
  }
325
326
  updateInterface() {
327
    this.loadingMessage = formSubmitting;
328
    this.repoService.updateInterface(this.currentRepo.id,
329
                                     this.currentRepo.registeredBy,
330 60937 andreas.ma
                                     this.currentRepo.comments,
331 54479 myrto.kouk
                                     this.currentInterface).subscribe(
332
      response => {
333
        console.log(`updateRepository responded ${JSON.stringify(response)}`);
334
        if (response) {
335
          this.currentInterface = response;
336
          this.successMessage = formSuccessUpdatedInterface;
337
        } else {
338
          this.errorMessage = formErrorWasntSaved;
339
        }
340
      },
341
      error => {
342
        console.log(error);
343
        this.loadingMessage = '';
344
        this.errorMessage = formErrorWasntSaved;
345
      },
346
      () => {
347
        this.loadingMessage = '';
348
        this.getExistingCompatibilityLevel();
349
      }
350
    );
351
  }
352
353
  removeInterface() {
354
    this.errorMessage = '';
355
    this.successMessage = '';
356
    if (this.interfaceID > 0) {
357 55618 myrto.kouk
      if (this.currentInterface && (this.currentInterface.id !== null) && !this.inRegister) {
358 54479 myrto.kouk
        this.repoService.deleteInterface(this.currentInterface.id, this.currentRepo.registeredBy).subscribe(
359
          res => console.log(`deleteInterface responded: ${JSON.stringify(res)}`),
360
          er => console.log(er),
361
          () => this.emitDeleteInterface.emit(this.interfaceID)
362
        );
363
      } else {
364
        this.emitDeleteInterface.emit(this.interfaceID);
365
      }
366
    } else {
367
      this.errorMessage = nonRemovableInterface;
368
    }
369
  }
370
371
  getInterface() {
372
    return this.interfaceToExport;
373
  }
374
375
}