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 } 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 {
8
  formErrorWasntSaved,
9
  formInfoLoading, formSubmitting, formSuccessAddedInterface, formSuccessUpdatedInterface, invalidCustomBaseUrl,
10
  nonRemovableInterface,
11
  noServiceMessage
12
} from '../../../domain/shared-messages';
13

    
14
export class RepoFields {
15
  id: string;
16
  datasourceType: string;
17
  datasourceClass: string;
18
  registeredBy: string;
19
}
20

    
21
@Component({
22
  selector: 'app-repository-interface-form',
23
  templateUrl: './datasource-new-interface-form.component.html'
24
})
25
export class DatasourceNewInterfaceFormComponent implements OnInit {
26
  loadingMessage: string;
27
  successMessage: string;
28
  errorMessage: string;
29

    
30
  @Input() data: any[] = []; // expects an array containing at least 3 of the 4 below fields in this order
31
  inRegister: boolean;
32
  interfaceID: number;     // holds the interface index in the interfaces array as displayed
33
  currentRepo: RepoFields; // a fraction of the Repository class
34
  currentInterface: RepositoryInterface;
35

    
36
  @Output() emitDeleteInterface: EventEmitter<number> = new EventEmitter<number>();
37
  interfaceToExport: RepositoryInterface;
38

    
39
  repoInterfaceForm: FormGroup;
40
  readonly repoInterfaceFormDef = {
41
    baseUrl: ['', Validators.required],
42
    selectValidationSet: [''],
43
    customValidationSet: [''],
44
    compatibilityLevel: ['']
45
  };
46
  baseUrlDesc: Description = baseUrlDesc;
47
  existingValSetDesc: Description = existingValSetDesc;
48
  customValSetDesc: Description = customValSetDesc;
49
  compatibilityLevelDesc: Description = compatibilityLevelDesc;
50

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

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

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

    
80

    
81
  getInterfaceInfo() {
82
    this.successMessage = '';
83
    this.errorMessage = '';
84

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

    
123
  getCompatibilityClasses() {
124
    this.repoService.getCompatibilityClasses(this.currentRepo.datasourceType).subscribe(
125
      classes => {
126
        this.compClasses = classes;
127
        this.classCodes = Object.keys(this.compClasses);
128
      },
129
      error => {
130
        this.errorMessage = noServiceMessage;
131
        console.log(error);
132
      },
133
      () => {
134
        this.getExistingCompatibilityLevel();
135
        this.repoInterfaceForm.updateValueAndValidity();
136
      }
137
    );
138
  }
139

    
140
  getExistingCompatibilityLevel() {
141
    if (this.currentInterface) {
142
      if (this.currentInterface.desiredCompatibilityLevel &&
143
        this.classCodes.some( x => x === this.currentInterface.desiredCompatibilityLevel ) ) {
144
        this.existingCompLevel = this.compClasses[this.currentInterface.desiredCompatibilityLevel];
145
      } else {
146
        this.repoInterfaceForm.get('compatibilityLevel').setValue('');
147
        this.existingCompLevel = this.currentInterface.desiredCompatibilityLevel;
148
      }
149
    }
150
  }
151

    
152
  chooseValSet(fromList: boolean) {
153
    this.existingValSet = fromList;
154
    if (this.existingValSet) {
155
      this.repoInterfaceForm.get('selectValidationSet').enable();
156
      this.repoInterfaceForm.get('customValidationSet').disable();
157
    }  else {
158
      this.repoInterfaceForm.get('selectValidationSet').disable();
159
      this.repoInterfaceForm.get('customValidationSet').enable();
160
    }
161
    this.checkIfValid();
162
  }
163

    
164
  checkIfCompatibilityLevelWasChosen() {
165
    return ( (this.repoInterfaceForm.get('compatibilityLevel').value !== '') ||
166
             (this.existingCompLevel && (this.existingCompLevel !== '')) );
167
  }
168

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

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

    
186
  }
187

    
188
  saveInterface() {
189
    this.errorMessage = '';
190
    this.successMessage = '';
191
    if (this.formIsValid()) {
192
      const baseUrl = this.repoInterfaceForm.get('baseUrl').value;
193
      let valset = '';
194
      if (this.existingValSet) {
195
        valset = this.repoInterfaceForm.get('selectValidationSet').value;
196
      } else {
197
        valset = this.repoInterfaceForm.get('customValidationSet').value;
198
      }
199
      let compLvl = '';
200
      if (this.repoInterfaceForm.get('compatibilityLevel').value) {
201
        this.existingCompLevel = this.compClasses[this.repoInterfaceForm.get('compatibilityLevel').value];
202
        console.log('this.existingCompLevel is', this.existingCompLevel);
203
        compLvl = this.repoInterfaceForm.get('compatibilityLevel').value;
204
      } else {
205
        compLvl = this.existingCompLevel;
206
      }
207

    
208
      if (this.currentInterface) {
209
        this.updateCurrent(baseUrl, valset, compLvl);
210
      } else {
211
        this.addCurrent(baseUrl, valset, compLvl);
212
      }
213
    } else {
214
      this.interfaceToExport = null;
215
    }
216
  }
217

    
218
  addCurrent (baseUrl: string, valset: string, compLvl: string) {
219
    const currentInterface = new RepositoryInterface();
220
    currentInterface.baseUrl = baseUrl;
221
    currentInterface.accessSet = valset;
222
    currentInterface.accessParams = {'set': valset};
223
    currentInterface.desiredCompatibilityLevel = compLvl;
224
    currentInterface.compliance = compLvl;
225
    currentInterface.typology = this.currentRepo.datasourceClass;
226
    if (!this.inRegister) {
227
      this.addInterface(currentInterface);
228
    } else {
229
      this.successMessage = 'The interface will be stored when the registration procedure is completed';
230
      console.log('SAVED !');
231
      this.interfaceToExport = currentInterface;
232
    }
233
  }
234

    
235
  addInterface(newInterface: RepositoryInterface) {
236
    this.loadingMessage = formSubmitting;
237
    this.repoService.addInterface(this.currentRepo.datasourceType,
238
                                  this.currentRepo.id,
239
                                  this.currentRepo.registeredBy,
240
                                  newInterface).subscribe(
241
      addedInterface => {
242
        console.log(`addInterface responded ${JSON.stringify(addedInterface)}`);
243
        this.currentInterface = addedInterface;
244
      },
245
      error => {
246
        console.log(error);
247
        this.loadingMessage = '';
248
        this.errorMessage = formErrorWasntSaved;
249
        this.currentInterface = null;
250
      },
251
      () => {
252
        this.loadingMessage = '';
253
        if (this.currentInterface.id) {
254
          this.successMessage = formSuccessAddedInterface;
255
          this.getExistingCompatibilityLevel();
256
        } else {
257
          this.errorMessage = formErrorWasntSaved;
258
        }
259
      }
260
    );
261

    
262
  }
263

    
264

    
265
  updateCurrent (baseUrl: string, valset: string, compLvl: string) {
266
    this.currentInterface.baseUrl = baseUrl;
267
    this.currentInterface.accessSet = valset;
268
    this.currentInterface.accessParams['set'] = valset;
269
    this.currentInterface.desiredCompatibilityLevel = compLvl;
270
    this.currentInterface.compliance = compLvl;
271
    this.currentInterface.typology = this.currentRepo.datasourceClass;
272

    
273
    if (!this.inRegister) {
274
      this.updateInterface();
275
    } else {
276
      this.successMessage = 'The interface will be stored when the registration procedure is completed';
277
      console.log('SAVED !');
278
      this.interfaceToExport = this.currentInterface;
279
    }
280
  }
281

    
282
  updateInterface() {
283
    this.loadingMessage = formSubmitting;
284
    this.repoService.updateInterface(this.currentRepo.id,
285
                                     this.currentRepo.registeredBy,
286
                                     this.currentInterface).subscribe(
287
      response => {
288
        console.log(`updateRepository responded ${JSON.stringify(response)}`);
289
        if (response) {
290
          this.currentInterface = response;
291
          this.successMessage = formSuccessUpdatedInterface;
292
        } else {
293
          this.errorMessage = formErrorWasntSaved;
294
        }
295
      },
296
      error => {
297
        console.log(error);
298
        this.loadingMessage = '';
299
        this.errorMessage = formErrorWasntSaved;
300
      },
301
      () => {
302
        this.loadingMessage = '';
303
        this.getExistingCompatibilityLevel();
304
      }
305
    );
306
  }
307

    
308
  removeInterface() {
309
    this.errorMessage = '';
310
    this.successMessage = '';
311
    if (this.interfaceID > 0) {
312
      if (this.currentInterface && (this.currentInterface.id !== null)) {
313
        this.repoService.deleteInterface(this.currentInterface.id, this.currentRepo.registeredBy).subscribe(
314
          res => console.log(`deleteInterface responded: ${JSON.stringify(res)}`),
315
          er => console.log(er),
316
          () => this.emitDeleteInterface.emit(this.interfaceID)
317
        );
318
      } else {
319
        this.emitDeleteInterface.emit(this.interfaceID);
320
      }
321
    } else {
322
      this.errorMessage = nonRemovableInterface;
323
    }
324
  }
325

    
326
  getInterface() {
327
    return this.interfaceToExport;
328
  }
329

    
330
}
(6-6/8)