Project

General

Profile

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, formInfoLoading, formSubmitting, formSuccessAddedInterface,
6
  formSuccessUpdatedInterface,
7
  invalidCustomBaseUrl, noServiceMessage
8
} from '../../../domain/shared-messages';
9
import { ValidatorService } from '../../../services/validator.service';
10
import { ActivatedRoute } from '@angular/router';
11
import { RepositoryService } from '../../../services/repository.service';
12
import { InterfaceInformation, Repository, RepositoryInterface } from '../../../domain/typeScriptClasses';
13
import {
14
  baseUrlDesc, compatibilityLevelDesc, customValSetDesc, Description,
15
  existingValSetDesc
16
} from '../../../domain/oa-description';
17

    
18
@Component ({
19
  selector: 'datasource-interface-form',
20
  templateUrl: './datasource-interface-form.component.html'
21
})
22

    
23
export class DatasourceInterfaceFormComponent extends MyGroup implements OnDestroy {
24

    
25
  loadingMessage: string;
26
  successMessage: string;
27
  errorMessage: string;
28

    
29
  currentRepository: Repository;
30
  oldInterface: boolean;
31

    
32
  identifiedBaseUrl: boolean;
33
  existingValSet: boolean;
34
  interfaceInfo: InterfaceInformation;
35
  currentInterface: RepositoryInterface;
36
  valsetList: string[] = [];
37

    
38
  existingCompLevel: string;
39
  compClasses: Map<string,string> = new Map<string,string>();
40
  classCodes: string[] = [];
41

    
42
  readonly groupDefinition = {
43
    baseUrl: ['', Validators.required],
44
    selectValidationSet: [''],
45
    customValidationSet: [''],
46
    compatibilityLevel: ['']
47
  };
48
  baseUrlDesc: Description = baseUrlDesc;
49
  existingValSetDesc: Description = existingValSetDesc;
50
  customValSetDesc: Description = customValSetDesc;
51
  compatibilityLevelDesc: Description = compatibilityLevelDesc;
52

    
53
  constructor(injector: Injector,
54
              private valService: ValidatorService,
55
              private repoService: RepositoryService){
56
    super(injector);
57
  }
58

    
59
  ngOnInit() {
60
    this.currentRepository = <Repository>this.otherData;
61
    console.log(`other data is: ${JSON.stringify(this.otherData,null,2)}`);
62
    if (this.data && this.data.length) {
63
      this.currentInterface = this.data[0];
64
      this.patchData.next({
65
        baseUrl: this.data[0].baseUrl,
66
        selectValidationSet: '',
67
        customValidationSet: '',
68
        compatibilityLevel:this.data[0].desiredCompatibilityLevel
69
      });
70
      this.getInterfaceInfo(this.data[0].baseUrl);
71
      this.data.splice(0,1);
72
      this.oldInterface = true;
73
      console.log(`received an interface!`);
74
      if (this.currentInterface.baseUrl && this.currentInterface.accessParams['set'] && this.currentInterface.desiredCompatibilityLevel) {
75
        this.wasSaved = true;
76
      }
77
    }
78

    
79
    /* initializes MyGroup parent component and the FormGroup */
80
    super.ngOnInit();
81
    console.log(this.group, this.parentGroup);
82
    this.getCompatibilityClasses();
83

    
84
    /*  NOT ANYMORE
85
    if (this.currentInterface) {
86
      this.getMyControl('baseUrl').disable();
87
    }
88
*/
89
    this.existingValSet = true;
90
    this.getMyControl('customValidationSet').disable();
91
  }
92

    
93
  chooseValSet(existingValSet: boolean) {
94
    if(existingValSet) {
95
      this.existingValSet = true;
96
      this.getMyControl('selectValidationSet').enable();
97
      this.getMyControl('customValidationSet').disable();
98
    }  else {
99
      this.existingValSet = false;
100
      this.getMyControl('selectValidationSet').disable();
101
      this.getMyControl('customValidationSet').enable();
102
    }
103
    this.checkIfValid();
104
  }
105

    
106
  getInterfaceInfo(baseUrl: string) {
107
    this.successMessage = '';
108
    this.errorMessage = '';
109
    this.groupErrorMessage = '';
110
    if (baseUrl) {
111
      this.loadingMessage = formInfoLoading;
112
      this.valService.getInterfaceInformation(baseUrl).subscribe(
113
        info => {
114
          this.interfaceInfo = info;
115
          if (this.interfaceInfo.identified) {
116
            this.identifiedBaseUrl = true;
117
          } else {
118
            this.errorMessage = invalidCustomBaseUrl;
119
          }
120
          if (this.interfaceInfo.sets) {
121
            this.valsetList = this.interfaceInfo.sets;
122
            console.log(this.valsetList);
123
          }
124
        },
125
        error => {
126
          console.log(error);
127
          this.loadingMessage = '';
128
          this.identifiedBaseUrl = false;
129
          this.errorMessage = noServiceMessage;
130
        },
131
        () => {
132
          if ( this.currentInterface && this.currentInterface.accessParams && this.currentInterface.accessParams['set'] ) {
133
            if ( this.valsetList.some( x => x === this.currentInterface.accessParams['set']) ) {
134
              this.patchData.next({selectValidationSet:this.currentInterface.accessParams['set']});
135
            } else {
136
              this.patchData.next({customValidationSet:this.currentInterface.accessParams['set']});
137
              this.getMyControl('selectValidationSet').enable();
138
              this.getMyControl('customValidationSet').disable();
139
            }
140
          }
141
          this.loadingMessage = '';
142
        }
143
      );
144
    }
145
  }
146

    
147
  getCompatibilityClasses() {
148
    this.repoService.getCompatibilityClasses(this.currentRepository.datasourceType).subscribe(
149
      classes => {
150
        this.compClasses = classes;
151
        for (let key in this.compClasses) {
152
          this.classCodes.push(key);
153
        }
154
      },
155
      error => {
156
        this.errorMessage = noServiceMessage;
157
        console.log(error);
158
      },
159
      () => {
160
        if (this.currentInterface) {
161
          console.log(`accessParams is ${JSON.stringify(this.currentInterface.accessParams)}`);
162
          if ( !this.currentInterface.desiredCompatibilityLevel || !this.classCodes.some( x => x == this.currentInterface.desiredCompatibilityLevel ) ) {
163
            this.patchData.next({compatibilityLevel:''});
164
            this.existingCompLevel = this.currentInterface.desiredCompatibilityLevel;
165
          } else {
166
            this.existingCompLevel = this.compClasses[this.currentInterface.desiredCompatibilityLevel];
167
          }
168
          if (this.group.valid ) {
169
            if ( this.getMyControl('selectValidationSet').value || this.getMyControl('customValidationSet').value ) {
170
              this.exportedData = this.currentInterface;
171
            }
172
          }
173
        }
174

    
175
      }
176
    );
177
  }
178

    
179
  saveInterface() {
180
    this.groupErrorMessage = '';
181
    this.errorMessage = '';
182
    this.successMessage = '';
183
    if (this.group.valid && this.checkIfValsetWasChosen() && this.checkIfCompatibilityLevelWasChosen() ) {
184
      if (this.identifiedBaseUrl) {
185
        let baseUrl = this.getMyControl('baseUrl').value;
186
        let valset: string = '';
187
        if (this.getMyControl('selectValidationSet').enabled) {
188
          valset = this.getMyControl('selectValidationSet').value;
189
        } else {
190
          valset = this.getMyControl('customValidationSet').value;
191
        }
192
        let compLvl: string = '';
193
        if (this.getMyControl('compatibilityLevel').value) {
194
          compLvl = this.getMyControl('compatibilityLevel').value;
195
        } else {
196
          compLvl = this.existingCompLevel;
197
        }
198

    
199

    
200
        if (this.currentInterface) {
201
          this.updateCurrent(baseUrl,valset,compLvl);
202
        } else {
203
          this.addCurrent(baseUrl,valset,compLvl);
204
        }
205
      } else {
206
        this.errorMessage = invalidCustomBaseUrl;
207
      }
208
    } else {
209
      this.errorMessage = formErrorRequiredFields;
210
      this.successMessage = '';
211
    }
212
  }
213

    
214
  checkIfValsetWasChosen() {
215
    return ( ( this.getMyControl('selectValidationSet').enabled && this.getMyControl('selectValidationSet').value !='' ) || ( this.getMyControl('customValidationSet').enabled && this.getMyControl('customValidationSet').value !='' ) );
216
  }
217

    
218
  checkIfCompatibilityLevelWasChosen() {
219
    return ( this.getMyControl('compatibilityLevel').value != '' || (this.existingCompLevel && this.existingCompLevel != '') );
220
  }
221

    
222
  checkIfValid() {
223
    console.log(this.existingCompLevel);
224
    if (this.inRegister) {
225
      if ( this.group.valid && this.checkIfValsetWasChosen() && this.checkIfCompatibilityLevelWasChosen() ) {
226
        if ( this.identifiedBaseUrl ) {
227
          let baseUrl = this.getMyControl('baseUrl').value;
228

    
229
          let valset: string = '';
230
          if (this.getMyControl('selectValidationSet').enabled) {
231
            valset = this.getMyControl('selectValidationSet').value;
232
          } else {
233
            valset = this.getMyControl('customValidationSet').value;
234
          }
235

    
236
          let compLvl: string = '';
237
          if (this.getMyControl('compatibilityLevel').value) {
238
            compLvl = this.getMyControl('compatibilityLevel').value;
239
          } else {
240
            compLvl = this.existingCompLevel;
241
          }
242

    
243
          if (this.currentInterface) {
244
            this.updateCurrent(baseUrl,valset,compLvl);
245
          } else {
246
            this.addCurrent(baseUrl,valset,compLvl);
247
          }
248
        }
249
      } else {
250
        this.exportedData = null;
251
        this.wasSaved = false;
252
        this.successMessage = '';
253
      }
254
    }
255
  }
256

    
257
  updateCurrent (baseUrl: string, valset: string, compLvl: string) {
258
    this.successMessage = '';
259
    this.errorMessage = '';
260
    this.currentInterface.baseUrl = baseUrl;
261
    this.currentInterface.accessSet = valset;
262
    this.currentInterface.accessParams['set'] = valset;
263
    this.currentInterface.desiredCompatibilityLevel = compLvl;
264
    this.currentInterface.compliance = compLvl;
265
    this.currentInterface.typology = this.currentRepository.datasourceClass;
266
    this.exportedData = this.currentInterface;
267
    if (!this.inRegister) {
268
      this.loadingMessage = formSubmitting;
269
      this.updateInterface();
270
    } else {
271
      this.loadingMessage = '';
272
      this.wasSaved = true;
273
      this.successMessage = 'The interface will be stored when the registration procedure is completed';
274
    }
275
  }
276

    
277
  addCurrent (baseUrl: string, valset: string, compLvl: string) {
278
    this.errorMessage = '';
279
    this.successMessage = '';
280
    this.currentInterface = new RepositoryInterface();
281
    this.currentInterface.baseUrl = baseUrl;
282
    this.currentInterface.accessSet = valset;
283
    this.currentInterface.accessParams = {'set': valset};
284
    this.currentInterface.desiredCompatibilityLevel = compLvl;
285
    this.currentInterface.compliance = compLvl;
286
    this.currentInterface.typology = this.currentRepository.datasourceClass;
287
    this.exportedData = this.currentInterface;
288
    if (!this.inRegister) {
289
      this.loadingMessage = formSubmitting;
290
      this.addInterface();
291
    } else {
292
      this.loadingMessage = '';
293
      this.wasSaved = true;
294
      this.successMessage = 'The interface will be stored when the registration procedure is completed';
295
    }
296
  }
297

    
298
  addInterface() {
299
    this.repoService.addInterface(this.currentRepository.datasourceType, this.currentRepository.id, this.currentInterface).subscribe(
300
      addedInterface => {
301
        console.log(`addInterface responded ${JSON.stringify(addedInterface)}`);
302
        this.currentInterface = addedInterface;
303
      },
304
      error => {
305
        console.log(error);
306
        this.loadingMessage = '';
307
        this.errorMessage = formErrorWasntSaved;
308
        this.currentInterface = null;
309
      },
310
      () => {
311
        if (this.currentInterface.id) {
312
          this.successMessage = formSuccessAddedInterface;
313
          this.wasSaved = true;
314
        } else {
315
          this.errorMessage = formErrorWasntSaved;
316
        }
317
        this.loadingMessage = '';
318
      }
319
    );
320

    
321
  }
322

    
323
  updateInterface() {
324
    this.repoService.updateInterface(this.currentRepository.id, this.currentInterface).subscribe(
325
      response => {
326
        console.log(`updateRepository responded ${JSON.stringify(response)}`);
327
        if (response) {
328
          this.successMessage = formSuccessUpdatedInterface;
329
          this.wasSaved = true;
330
        } else {
331
          this.errorMessage = formErrorWasntSaved;
332
        }
333
      },
334
      error => {
335
        console.log(error);
336
        this.loadingMessage = '';
337
        this.errorMessage = formErrorWasntSaved;
338
      },
339
      () => {
340
        this.loadingMessage = '';
341
      }
342
    );
343
  }
344

    
345
  ngOnDestroy() {
346
    if (this.currentInterface && this.currentInterface.id && this.toBeDeleted) {
347
      this.repoService.deleteInterface(this.currentInterface.id).subscribe(
348
        response => console.log(`deleteInterface responded: ${JSON.stringify(response)}`),
349
        error => console.log(error),
350
        () => console.log(`deleted ${this.currentInterface.id}`)
351
      );
352
    } else {
353
      console.log(`deleting empty interface form`);
354
    }
355
  }
356

    
357
}
(4-4/6)