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 { 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.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
            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.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
    // 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.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
        // 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
    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
  addCurrent (baseUrl: string, valset: string, compLvl: string, comment: string) {
259
    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
    currentInterface.comments = comment;
267

    
268
    if (!this.inRegister) {
269
      this.addInterface(currentInterface);
270
    } else {
271
      this.successMessage = 'The harvesting settings are valid!';
272
      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
                                  this.currentRepo.comments,
283
                                  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
  updateCurrent (baseUrl: string, valset: string, compLvl: string, comment: string) {
309
    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
    this.currentInterface.comments = comment;
316

    
317
    if (!this.inRegister) {
318
      this.updateInterface();
319
    } else {
320
      this.successMessage = 'The harvesting settings are valid!';
321
      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
                                     this.currentRepo.comments,
331
                                     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
      if (this.currentInterface && (this.currentInterface.id !== null) && !this.inRegister) {
358
        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
}
(4-4/8)