1
|
import { Component, Injector, OnDestroy } from '@angular/core';
|
2
|
import { MyGroup } from '../../../shared/reusablecomponents/forms/my-group.interface';
|
3
|
import { FormBuilder, Validators } from '@angular/forms';
|
4
|
import {
|
5
|
formErrorRequiredFields, formErrorWasntSaved, formSubmitting, formSuccessAddedInterface, formSuccessUpdatedInterface,
|
6
|
invalidCustomBaseUrl, noServiceMessage
|
7
|
} from '../../../domain/shared-messages';
|
8
|
import { ValidatorService } from '../../../services/validator.service';
|
9
|
import { ActivatedRoute } from '@angular/router';
|
10
|
import { RepositoryService } from '../../../services/repository.service';
|
11
|
import { InterfaceInformation, Repository, RepositoryInterface } from '../../../domain/typeScriptClasses';
|
12
|
|
13
|
@Component ({
|
14
|
selector: 'datasource-interface-form',
|
15
|
templateUrl: './datasource-interface-form.component.html'
|
16
|
})
|
17
|
|
18
|
export class DatasourceInterfaceFormComponent extends MyGroup implements OnDestroy {
|
19
|
|
20
|
loadingMessage: string;
|
21
|
successMessage: string;
|
22
|
errorMessage: string;
|
23
|
|
24
|
currentRepository: Repository;
|
25
|
|
26
|
identifiedBaseUrl: boolean;
|
27
|
existingValSet: boolean;
|
28
|
interfaceInfo: InterfaceInformation;
|
29
|
currentInterface: RepositoryInterface;
|
30
|
valsetList: string[] = [];
|
31
|
|
32
|
compClasses: Map<string,string> = new Map<string,string>();
|
33
|
classCodes: string[] = [];
|
34
|
|
35
|
readonly groupDefinition = {
|
36
|
baseUrl: ['', Validators.required],
|
37
|
selectValidationSet: [''],
|
38
|
customValidationSet: [''],
|
39
|
compatibilityLevel: ['', Validators.required]
|
40
|
};
|
41
|
|
42
|
constructor(injector: Injector,
|
43
|
private valService: ValidatorService,
|
44
|
private repoService: RepositoryService){
|
45
|
super(injector);
|
46
|
}
|
47
|
|
48
|
ngOnInit() {
|
49
|
this.currentRepository = <Repository>this.otherData;
|
50
|
this.getCompatibilityClasses();
|
51
|
console.log(`other data is: ${JSON.stringify(this.otherData)}`);
|
52
|
if (this.data && this.data.length) {
|
53
|
this.currentInterface = this.data[0];
|
54
|
this.patchData.next({
|
55
|
baseUrl: this.data[0].baseUrl,
|
56
|
selectValidationSet: '',
|
57
|
customValidationSet: '',
|
58
|
compatibilityLevel:this.data[0].desiredCompatibilityLevel
|
59
|
}
|
60
|
);
|
61
|
this.getInterfaceInfo(this.data[0].baseUrl);
|
62
|
this.data.splice(0,1);
|
63
|
}
|
64
|
super.ngOnInit();
|
65
|
console.log(this.group, this.parentGroup);
|
66
|
this.existingValSet = true;
|
67
|
this.getMyControl('customValidationSet').disable();
|
68
|
}
|
69
|
|
70
|
chooseValSet(existingValSet: boolean) {
|
71
|
if(existingValSet) {
|
72
|
this.existingValSet = true;
|
73
|
this.getMyControl('selectValidationSet').enable();
|
74
|
this.getMyControl('customValidationSet').disable();
|
75
|
} else {
|
76
|
this.existingValSet = false;
|
77
|
this.getMyControl('selectValidationSet').disable();
|
78
|
this.getMyControl('customValidationSet').enable();
|
79
|
}
|
80
|
}
|
81
|
|
82
|
saveInterface() {
|
83
|
this.errorMessage = '';
|
84
|
this.successMessage = '';
|
85
|
if (this.group.valid) {
|
86
|
if (this.identifiedBaseUrl) {
|
87
|
let baseUrl = this.getMyControl('baseUrl').value;
|
88
|
let valset: string = '';
|
89
|
if (this.getMyControl('selectValidationSet').enabled) {
|
90
|
valset = this.getMyControl('selectValidationSet').value;
|
91
|
} else {
|
92
|
valset = this.getMyControl('customValidationSet').value;
|
93
|
}
|
94
|
let compLvl = this.getMyControl('compatibilityLevel').value;
|
95
|
|
96
|
if (this.currentInterface) {
|
97
|
this.updateCurrent(baseUrl,valset,compLvl);
|
98
|
} else {
|
99
|
this.addCurrent(baseUrl,valset,compLvl);
|
100
|
}
|
101
|
} else {
|
102
|
this.errorMessage = invalidCustomBaseUrl;
|
103
|
}
|
104
|
} else {
|
105
|
this.errorMessage = formErrorRequiredFields;
|
106
|
this.successMessage = '';
|
107
|
}
|
108
|
}
|
109
|
|
110
|
getInterfaceInfo(baseUrl: string) {
|
111
|
if(baseUrl) {
|
112
|
this.valService.getInterfaceInformation(baseUrl).subscribe(
|
113
|
info => {
|
114
|
this.interfaceInfo = info;
|
115
|
if (this.interfaceInfo.identified) {
|
116
|
this.identifiedBaseUrl = true;
|
117
|
this.errorMessage = '';
|
118
|
} else {
|
119
|
this.errorMessage = invalidCustomBaseUrl;
|
120
|
}
|
121
|
if (this.interfaceInfo.sets) {
|
122
|
this.valsetList = this.interfaceInfo.sets;
|
123
|
console.log(this.valsetList);
|
124
|
}
|
125
|
},
|
126
|
error => {
|
127
|
console.log(error);
|
128
|
this.identifiedBaseUrl = false;
|
129
|
this.errorMessage = noServiceMessage;
|
130
|
}
|
131
|
);
|
132
|
}
|
133
|
}
|
134
|
|
135
|
getCompatibilityClasses() {
|
136
|
this.repoService.getCompatibilityClasses(this.currentRepository.datasourceType).subscribe(
|
137
|
classes => {
|
138
|
this.compClasses = classes;
|
139
|
for (let key in this.compClasses){
|
140
|
this.classCodes.push(key);
|
141
|
}
|
142
|
},
|
143
|
error => {
|
144
|
this.errorMessage = noServiceMessage;
|
145
|
console.log(error);
|
146
|
}
|
147
|
);
|
148
|
}
|
149
|
|
150
|
updateCurrent (baseUrl: string, valset: string, compLvl: string) {
|
151
|
this.successMessage = '';
|
152
|
this.errorMessage = '';
|
153
|
this.loadingMessage = formSubmitting;
|
154
|
this.currentInterface.baseUrl = baseUrl;
|
155
|
this.currentInterface.accessSet = valset;
|
156
|
this.currentInterface.desiredCompatibilityLevel = compLvl;
|
157
|
this.currentInterface.typology = this.currentRepository.datasourceClass;
|
158
|
this.repoService.updateInterface(this.currentInterface).subscribe(
|
159
|
response => {
|
160
|
console.log(`updateRepository responded ${response}`);
|
161
|
this.loadingMessage = '';
|
162
|
if (response == '200') {
|
163
|
this.successMessage = formSuccessUpdatedInterface;
|
164
|
} else {
|
165
|
this.errorMessage = formErrorWasntSaved;
|
166
|
}
|
167
|
},
|
168
|
error => {
|
169
|
console.log(error);
|
170
|
this.loadingMessage = '';
|
171
|
this.errorMessage = formErrorWasntSaved;
|
172
|
}
|
173
|
);
|
174
|
}
|
175
|
|
176
|
addCurrent (baseUrl: string, valset: string, compLvl: string) {
|
177
|
this.errorMessage = '';
|
178
|
this.successMessage = '';
|
179
|
this.loadingMessage = formSubmitting;
|
180
|
this.currentInterface = new RepositoryInterface();
|
181
|
this.currentInterface.baseUrl = baseUrl;
|
182
|
this.currentInterface.accessSet = valset;
|
183
|
this.currentInterface.desiredCompatibilityLevel = compLvl;
|
184
|
this.currentInterface.typology = this.currentRepository.datasourceClass;
|
185
|
this.repoService.addInterface(this.currentRepository.datasourceType, this.currentRepository.id, this.currentInterface).subscribe(
|
186
|
addedInterface => {
|
187
|
console.log(`addInterface responded ${addedInterface}`);
|
188
|
this.currentInterface = addedInterface;
|
189
|
},
|
190
|
error => {
|
191
|
console.log(error);
|
192
|
this.loadingMessage = '';
|
193
|
this.errorMessage = formErrorWasntSaved;
|
194
|
},
|
195
|
() => {
|
196
|
this.loadingMessage = '';
|
197
|
if (this.currentInterface.id) {
|
198
|
this.successMessage = formSuccessAddedInterface;
|
199
|
} else {
|
200
|
this.errorMessage = formErrorWasntSaved;
|
201
|
}
|
202
|
}
|
203
|
);
|
204
|
}
|
205
|
|
206
|
ngOnDestroy() {
|
207
|
if (this.currentInterface) {
|
208
|
this.repoService.deleteInterface(this.currentInterface.id).subscribe(
|
209
|
response => console.log(`deleteInterface responded: ${response}`),
|
210
|
error => console.log(error)
|
211
|
);
|
212
|
console.log(`deleting ${this.currentInterface.id}`);
|
213
|
} else {
|
214
|
console.log(`deleting empty interface form`);
|
215
|
}
|
216
|
}
|
217
|
|
218
|
|
219
|
}
|