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
|
compatibilityLevel: null,
|
43
|
desiredCompatibilityLevel: null,
|
44
|
compatibilityLevelOverride: null,
|
45
|
comment: ['']
|
46
|
};
|
47
|
baseUrlDesc: Description = baseUrlDesc;
|
48
|
existingValSetDesc: Description = existingValSetDesc;
|
49
|
customValSetDesc: Description = customValSetDesc;
|
50
|
compatibilityLevelDesc: Description = compatibilityLevelDesc;
|
51
|
commentDesc: Description = commentDesc;
|
52
|
|
53
|
identifiedBaseUrl: boolean;
|
54
|
canEdit = true;
|
55
|
showIdentifiedBaseUrl: boolean = null;
|
56
|
valsetList: string[] = [];
|
57
|
existingCompLevel: string;
|
58
|
classCodes: string[] = [];
|
59
|
compClasses: Map<string, string> = new Map<string, string>();
|
60
|
interfaceInfo: InterfaceInformation;
|
61
|
|
62
|
constructor(private fb: FormBuilder,
|
63
|
private valService: ValidatorService,
|
64
|
private repoService: RepositoryService) {}
|
65
|
|
66
|
ngOnInit() {
|
67
|
if (this.data && (this.data.length >= 3)) {
|
68
|
this.inRegister = this.data[0];
|
69
|
this.interfaceID = this.data[1];
|
70
|
this.currentRepo = this.data[2];
|
71
|
this.repoInterfaceForm = this.fb.group(this.repoInterfaceFormDef);
|
72
|
// this.chooseValSet(true);
|
73
|
if (this.data[3]) {
|
74
|
this.currentInterface = this.data[3];
|
75
|
if (this.currentInterface.baseurl !== null && this.currentInterface.baseurl !== '') {
|
76
|
this.canEdit = false;
|
77
|
this.repoInterfaceForm.get('baseurl').setValue(this.currentInterface.baseurl);
|
78
|
}
|
79
|
this.repoInterfaceForm.get('compatibilityLevel').setValue(this.currentInterface.compatibility);
|
80
|
this.repoInterfaceForm.get('compatibilityLevelOverride').setValue(this.currentInterface.compatibilityOverride);
|
81
|
this.repoService.getInterfaceDesiredCompatibilityLevel(this.currentInterface.datasource, this.currentInterface.id).subscribe(
|
82
|
res => {
|
83
|
if (res !== null) {
|
84
|
this.repoInterfaceForm.get('desiredCompatibilityLevel').setValue(res['desiredCompatibilityLevel']);
|
85
|
}
|
86
|
}
|
87
|
);
|
88
|
}
|
89
|
this.getInterfaceInfo();
|
90
|
this.getCompatibilityClasses();
|
91
|
}
|
92
|
}
|
93
|
|
94
|
|
95
|
getInterfaceInfo() {
|
96
|
this.successMessage = '';
|
97
|
this.errorMessage = '';
|
98
|
|
99
|
const baseurl = this.repoInterfaceForm.get('baseurl').value;
|
100
|
if (baseurl) {
|
101
|
this.loadingMessage = formInfoLoading;
|
102
|
this.valService.getInterfaceInformation(baseurl).subscribe(
|
103
|
info => {
|
104
|
this.interfaceInfo = info;
|
105
|
if (this.interfaceInfo.identified) {
|
106
|
this.identifiedBaseUrl = true;
|
107
|
this.showIdentifiedBaseUrl = true;
|
108
|
} else {
|
109
|
this.errorMessage = invalidCustomBaseUrl;
|
110
|
this.identifiedBaseUrl = true; // pass interface without baseurl identification
|
111
|
this.showIdentifiedBaseUrl = false;
|
112
|
}
|
113
|
if (this.interfaceInfo.sets) {
|
114
|
this.valsetList = this.interfaceInfo.sets;
|
115
|
// console.log(this.valsetList);
|
116
|
}
|
117
|
},
|
118
|
error => {
|
119
|
console.log(error);
|
120
|
this.loadingMessage = '';
|
121
|
// this.identifiedBaseUrl = false;
|
122
|
this.errorMessage = noServiceMessage;
|
123
|
},
|
124
|
() => {
|
125
|
if (this.currentInterface?.apiParams?.find(entry => entry.param === 'set')) {
|
126
|
this.repoInterfaceForm.get('selectValidationSet').setValue(this.currentInterface.apiParams
|
127
|
.find(entry => entry.param === 'set').value);
|
128
|
this.repoInterfaceForm.updateValueAndValidity();
|
129
|
this.checkIfValid();
|
130
|
}
|
131
|
this.loadingMessage = '';
|
132
|
}
|
133
|
);
|
134
|
}
|
135
|
}
|
136
|
|
137
|
getCompatibilityClasses() {
|
138
|
// FIXME: Use eoscDatasourceType when we support the new model
|
139
|
if (this.mode === null) {
|
140
|
this.mode = this.currentRepo.datasourceType;
|
141
|
}
|
142
|
this.repoService.getCompatibilityClasses(this.mode).subscribe(
|
143
|
classes => {
|
144
|
this.compClasses = classes;
|
145
|
this.classCodes = Object.keys(this.compClasses);
|
146
|
},
|
147
|
error => {
|
148
|
this.errorMessage = noServiceMessage;
|
149
|
console.log(error);
|
150
|
},
|
151
|
() => {
|
152
|
this.getExistingCompatibilityLevel();
|
153
|
this.repoInterfaceForm.updateValueAndValidity();
|
154
|
}
|
155
|
);
|
156
|
}
|
157
|
|
158
|
getExistingCompatibilityLevel() {
|
159
|
if (this.currentInterface) {
|
160
|
if (this.currentInterface.compatibility
|
161
|
&& this.classCodes.some( x => x === this.currentInterface.compatibilityOverride)) {
|
162
|
this.existingCompLevel = this.compClasses[this.currentInterface.compatibility];
|
163
|
} else {
|
164
|
// this.repoInterfaceForm.get('compatibilityLevel').setValue('');
|
165
|
this.existingCompLevel = this.currentInterface.compatibility;
|
166
|
}
|
167
|
}
|
168
|
}
|
169
|
|
170
|
checkIfCompatibilityLevelWasChosen() {
|
171
|
return ( (this.repoInterfaceForm.get('compatibilityLevel').value !== '') ||
|
172
|
(this.existingCompLevel && (this.existingCompLevel !== '')) );
|
173
|
}
|
174
|
|
175
|
formIsValid() {
|
176
|
return (this.repoInterfaceForm.valid && this.identifiedBaseUrl && this.checkIfCompatibilityLevelWasChosen());
|
177
|
}
|
178
|
|
179
|
checkIfValid() {
|
180
|
if (this.formIsValid()) {
|
181
|
if (this.inRegister) {
|
182
|
// this.successMessage = 'The interface will be stored when the registration procedure is completed.';
|
183
|
this.successMessage = 'The harvesting settings are valid!';
|
184
|
this.saveInterface();
|
185
|
}
|
186
|
} else {
|
187
|
this.successMessage = '';
|
188
|
// this.interfaceToExport = null;
|
189
|
}
|
190
|
|
191
|
}
|
192
|
|
193
|
saveInterface() {
|
194
|
this.errorMessage = '';
|
195
|
this.successMessage = '';
|
196
|
if (this.formIsValid()) {
|
197
|
const baseurl = this.repoInterfaceForm.get('baseurl').value;
|
198
|
const valset = this.repoInterfaceForm.get('selectValidationSet').value;
|
199
|
const desiredCompLvl = this.repoInterfaceForm.get('desiredCompatibilityLevel').value;
|
200
|
const compLvl = this.existingCompLevel;
|
201
|
let comment = '';
|
202
|
if (this.repoInterfaceForm.get('comment').value) {
|
203
|
comment = this.repoInterfaceForm.get('comment').value;
|
204
|
}
|
205
|
|
206
|
if (this.currentInterface) {
|
207
|
this.updateCurrent(baseurl, valset, desiredCompLvl, compLvl, comment);
|
208
|
} else {
|
209
|
this.addCurrent(baseurl, valset, desiredCompLvl, compLvl, comment);
|
210
|
}
|
211
|
} else {
|
212
|
// this.interfaceToExport = null;
|
213
|
this.errorMessage = 'Please make sure all required fields are filled with acceptable values.';
|
214
|
}
|
215
|
}
|
216
|
|
217
|
getCurrentValues() {
|
218
|
console.log('getcurrentvalues');
|
219
|
let intrf = this.currentInterface;
|
220
|
if (intrf == null) {
|
221
|
intrf = new RepositoryInterface();
|
222
|
}
|
223
|
intrf.baseurl = this.repoInterfaceForm.get('baseurl').value;
|
224
|
this.updateValidationSet(intrf, this.repoInterfaceForm.get('selectValidationSet').value);
|
225
|
console.log(intrf);
|
226
|
if (this.repoInterfaceForm.get('compatibilityLevel').value) {
|
227
|
intrf.compatibilityOverride = this.repoInterfaceForm.get('compatibilityLevel').value;
|
228
|
intrf.compatibility = this.repoInterfaceForm.get('compatibilityLevel').value;
|
229
|
} else {
|
230
|
intrf.compatibilityOverride = this.existingCompLevel;
|
231
|
intrf.compatibility = this.existingCompLevel;
|
232
|
}
|
233
|
intrf.typology = this.currentRepo.datasourceClass;
|
234
|
|
235
|
return intrf;
|
236
|
}
|
237
|
|
238
|
updateValidationSet(intrf: RepositoryInterface, value: string) {
|
239
|
let validationSet = intrf.apiParams.find(element => element.param === 'set');
|
240
|
if (!validationSet) {
|
241
|
validationSet = new ApiParamDetails('set', value);
|
242
|
intrf.apiParams.push(validationSet);
|
243
|
} else {
|
244
|
validationSet.value = this.repoInterfaceForm.get('selectValidationSet').value;
|
245
|
}
|
246
|
}
|
247
|
|
248
|
addCurrent (baseurl: string, valset: string, desiredCompLvl: string, compLvl: string, comment: string) {
|
249
|
console.log('add current');
|
250
|
this.currentInterface = new RepositoryInterface();
|
251
|
this.updateValidationSet(this.currentInterface, valset);
|
252
|
this.currentInterface.baseurl = baseurl;
|
253
|
this.currentInterface.desiredCompatibilityLevel = desiredCompLvl;
|
254
|
this.currentInterface.compatibility = compLvl;
|
255
|
this.currentInterface.typology = this.currentRepo.datasourceClass;
|
256
|
this.currentInterface.comments = comment;
|
257
|
|
258
|
if (!this.inRegister) {
|
259
|
this.addInterface();
|
260
|
} else {
|
261
|
this.successMessage = 'The harvesting settings are valid!';
|
262
|
console.log('SAVED !');
|
263
|
this.interfaceToExport = this.currentInterface;
|
264
|
}
|
265
|
}
|
266
|
|
267
|
addInterface() {
|
268
|
this.loadingMessage = formSubmitting;
|
269
|
this.repoService.addInterface(this.currentRepo.datasourceType, this.currentRepo.id,
|
270
|
this.currentRepo.registeredBy, this.currentRepo.comments, this.currentInterface,
|
271
|
this.repoInterfaceForm.get('desiredCompatibilityLevel').value).subscribe(
|
272
|
addedInterface => {
|
273
|
console.log(`addInterface responded ${JSON.stringify(addedInterface)}`);
|
274
|
this.currentInterface = addedInterface;
|
275
|
},
|
276
|
error => {
|
277
|
console.log(error);
|
278
|
this.loadingMessage = '';
|
279
|
this.errorMessage = formErrorWasntSaved;
|
280
|
this.currentInterface = null;
|
281
|
},
|
282
|
() => {
|
283
|
this.loadingMessage = '';
|
284
|
if (this.currentInterface.id) {
|
285
|
this.successMessage = formSuccessAddedInterface;
|
286
|
this.getExistingCompatibilityLevel();
|
287
|
} else {
|
288
|
this.errorMessage = formErrorWasntSaved;
|
289
|
}
|
290
|
}
|
291
|
);
|
292
|
|
293
|
}
|
294
|
|
295
|
updateCurrent (baseurl: string, valset: string, desiredCompLvl: string, compLvl: string, comment: string) {
|
296
|
console.log('update current');
|
297
|
this.updateValidationSet(this.currentInterface, valset);
|
298
|
this.currentInterface.baseurl = baseurl;
|
299
|
this.currentInterface.desiredCompatibilityLevel = desiredCompLvl;
|
300
|
console.log(this.currentInterface.desiredCompatibilityLevel);
|
301
|
this.currentInterface.compatibility = compLvl;
|
302
|
this.currentInterface.typology = this.currentRepo.datasourceClass;
|
303
|
this.currentInterface.comments = comment;
|
304
|
|
305
|
if (!this.inRegister) {
|
306
|
this.updateInterface();
|
307
|
} else {
|
308
|
this.successMessage = 'The harvesting settings are valid!';
|
309
|
console.log('SAVED !');
|
310
|
this.interfaceToExport = this.currentInterface;
|
311
|
}
|
312
|
}
|
313
|
|
314
|
updateInterface() {
|
315
|
this.loadingMessage = formSubmitting;
|
316
|
this.repoService.updateInterface(this.currentRepo.id, this.currentRepo.registeredBy, this.currentRepo.comments,
|
317
|
this.currentInterface, this.repoInterfaceForm.get('desiredCompatibilityLevel').value).subscribe(
|
318
|
response => {
|
319
|
console.log(`updateRepository responded ${JSON.stringify(response)}`);
|
320
|
if (response) {
|
321
|
this.currentInterface = response;
|
322
|
this.successMessage = formSuccessUpdatedInterface;
|
323
|
} else {
|
324
|
this.errorMessage = formErrorWasntSaved;
|
325
|
}
|
326
|
},
|
327
|
error => {
|
328
|
console.log(error);
|
329
|
this.loadingMessage = '';
|
330
|
this.errorMessage = formErrorWasntSaved;
|
331
|
},
|
332
|
() => {
|
333
|
this.loadingMessage = '';
|
334
|
this.getExistingCompatibilityLevel();
|
335
|
}
|
336
|
);
|
337
|
}
|
338
|
|
339
|
removeInterface() {
|
340
|
this.errorMessage = '';
|
341
|
this.successMessage = '';
|
342
|
if (this.interfaceID > 0) {
|
343
|
if (this.currentInterface && (this.currentInterface.id !== null) && !this.inRegister) {
|
344
|
this.repoService.deleteInterface(this.currentInterface.id, this.currentRepo.registeredBy).subscribe(
|
345
|
res => console.log(`deleteInterface responded: ${JSON.stringify(res)}`),
|
346
|
er => console.log(er),
|
347
|
() => this.emitDeleteInterface.emit(this.interfaceID)
|
348
|
);
|
349
|
} else {
|
350
|
this.emitDeleteInterface.emit(this.interfaceID);
|
351
|
}
|
352
|
} else {
|
353
|
this.errorMessage = nonRemovableInterface;
|
354
|
}
|
355
|
}
|
356
|
|
357
|
getInterface() {
|
358
|
return this.interfaceToExport;
|
359
|
}
|
360
|
|
361
|
}
|