Project

General

Profile

1
import { Component, Injector, OnDestroy, OnInit } from '@angular/core';
2
import { MyGroup } from '../../../shared/reusablecomponents/forms/my-group.interface';
3
import { Validators } from '@angular/forms';
4
import { formErrorRequiredFields, formErrorWasntSaved, formInfoLoading, formSubmitting, formSuccessAddedInterface,
5
         formSuccessUpdatedInterface, invalidCustomBaseUrl, noServiceMessage } from '../../../domain/shared-messages';
6
import { ValidatorService } from '../../../services/validator.service';
7
import { RepositoryService } from '../../../services/repository.service';
8
import { InterfaceInformation, Repository, RepositoryInterface } from '../../../domain/typeScriptClasses';
9
import { baseUrlDesc, compatibilityLevelDesc, customValSetDesc, Description, existingValSetDesc } from '../../../domain/oa-description';
10

    
11
@Component ({
12
  selector: 'datasource-interface-form',
13
  templateUrl: './datasource-interface-form.component.html'
14
})
15

    
16
export class DatasourceInterfaceFormComponent extends MyGroup implements OnDestroy, OnInit {
17

    
18
  loadingMessage: string;
19
  successMessage: string;
20
  errorMessage: string;
21

    
22
  currentRepository: Repository;
23
  oldInterface: boolean;
24

    
25
  identifiedBaseUrl: boolean;
26
  existingValSet: boolean;
27
  interfaceInfo: InterfaceInformation;
28
  currentInterface: RepositoryInterface;
29
  valsetList: string[] = [];
30

    
31
  existingCompLevel: string;
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: ['']
40
  };
41
  baseUrlDesc: Description = baseUrlDesc;
42
  existingValSetDesc: Description = existingValSetDesc;
43
  customValSetDesc: Description = customValSetDesc;
44
  compatibilityLevelDesc: Description = compatibilityLevelDesc;
45

    
46
  constructor(injector: Injector,
47
              private valService: ValidatorService,
48
              private repoService: RepositoryService){
49
    super(injector);
50
  }
51

    
52
  ngOnInit() {
53
    this.currentRepository = <Repository>this.otherData;
54
    console.log(`other data is: ${JSON.stringify(this.otherData, null, 2)}`);
55
    if (this.data && this.data.length) {
56
      this.currentInterface = this.data[0];
57
      this.patchData.next({
58
        baseUrl: this.data[0].baseUrl,
59
        selectValidationSet: '',
60
        customValidationSet: '',
61
        compatibilityLevel: this.data[0].desiredCompatibilityLevel
62
      });
63
      this.getInterfaceInfo(this.data[0].baseUrl);
64
      this.data.splice(0, 1);
65
      this.oldInterface = true;
66
      console.log(`received an interface!`);
67
      /*if (this.interfaceInfo && this.interfaceInfo.identified &&
68
          this.currentInterface.desiredCompatibilityLevel) {
69
        this.wasSaved = true;
70
      }*/
71
    }
72

    
73
    /* initializes MyGroup parent component and the FormGroup */
74
    super.ngOnInit();
75
    console.log(this.group, this.parentGroup);
76
    this.getCompatibilityClasses();
77

    
78
    /*  NOT ANYMORE
79
    if (this.currentInterface) {
80
      this.getMyControl('baseUrl').disable();
81
    }
82
*/
83
    this.existingValSet = true;
84
    this.getMyControl('customValidationSet').disable();
85
  }
86

    
87
  chooseValSet(existingValSet: boolean) {
88
    if (existingValSet) {
89
      this.existingValSet = true;
90
      this.getMyControl('selectValidationSet').enable();
91
      this.getMyControl('customValidationSet').disable();
92
    }  else {
93
      this.existingValSet = false;
94
      this.getMyControl('selectValidationSet').disable();
95
      this.getMyControl('customValidationSet').enable();
96
    }
97
    this.checkIfValid();
98
  }
99

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

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

    
172
      }
173
    );
174
  }
175

    
176
  saveInterface() {
177
    this.groupErrorMessage = '';
178
    this.errorMessage = '';
179
    this.successMessage = '';
180

    
181
    // if decided that valset is required add && this.checkIfValsetWasChosen() to the condition
182
    if (this.group.valid && this.checkIfCompatibilityLevelWasChosen() ) {
183
      if (this.identifiedBaseUrl) {
184
        const baseUrl = this.getMyControl('baseUrl').value;
185
        let valset = '';
186
        if (this.getMyControl('selectValidationSet').enabled) {
187
          valset = this.getMyControl('selectValidationSet').value;
188
        } else {
189
          valset = this.getMyControl('customValidationSet').value;
190
        }
191
        let compLvl = '';
192
        if (this.getMyControl('compatibilityLevel').value) {
193
          compLvl = this.getMyControl('compatibilityLevel').value;
194
        } else {
195
          compLvl = this.existingCompLevel;
196
        }
197

    
198

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

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

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

    
225
  checkIfValid() {
226
    if (this.inRegister) {
227
      // if decided that valset is required add && this.checkIfValsetWasChosen() to the condition
228
      if ( this.group.valid && this.checkIfCompatibilityLevelWasChosen() ) {
229
        if ( this.identifiedBaseUrl ) {
230
          const baseUrl = this.getMyControl('baseUrl').value;
231

    
232
          let valset = '';
233
          if (this.getMyControl('selectValidationSet').enabled) {
234
            valset = this.getMyControl('selectValidationSet').value;
235
          } else {
236
            valset = this.getMyControl('customValidationSet').value;
237
          }
238

    
239
          let compLvl = '';
240
          if (this.getMyControl('compatibilityLevel').value) {
241
            this.existingCompLevel = this.compClasses[this.getMyControl('compatibilityLevel').value];
242
            console.log('this.existingCompLevel is', this.existingCompLevel);
243
            compLvl = this.getMyControl('compatibilityLevel').value;
244
          } else {
245
            compLvl = this.existingCompLevel;
246
          }
247

    
248
          if (this.currentInterface) {
249
            this.updateCurrent(baseUrl, valset, compLvl);
250
          } else {
251
            this.addCurrent(baseUrl, valset, compLvl);
252
          }
253
        }
254
      } else {
255
        this.exportedData = null;
256
        this.wasSaved = false;
257
        this.successMessage = '';
258
      }
259
    }
260
  }
261

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

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

    
303
  addInterface() {
304
    this.repoService.addInterface(this.currentRepository.datasourceType,
305
                                  this.currentRepository.id,
306
                                  this.currentRepository.registeredBy,
307
                                  this.currentInterface).subscribe(
308
      addedInterface => {
309
        console.log(`addInterface responded ${JSON.stringify(addedInterface)}`);
310
        this.currentInterface = addedInterface;
311
      },
312
      error => {
313
        console.log(error);
314
        this.loadingMessage = '';
315
        this.errorMessage = formErrorWasntSaved;
316
        this.currentInterface = null;
317
      },
318
      () => {
319
        this.loadingMessage = '';
320
        if (this.currentInterface.id) {
321
          this.successMessage = formSuccessAddedInterface;
322
          this.wasSaved = true;
323
          if ( !this.currentInterface.desiredCompatibilityLevel ||
324
               !this.classCodes.some( x => x === this.currentInterface.desiredCompatibilityLevel ) ) {
325
            this.patchData.next({compatibilityLevel: ''});
326
            this.existingCompLevel = this.currentInterface.desiredCompatibilityLevel;
327
          } else {
328
            this.existingCompLevel = this.compClasses[this.currentInterface.desiredCompatibilityLevel];
329
          }
330
        } else {
331
          this.errorMessage = formErrorWasntSaved;
332
        }
333
      }
334
    );
335

    
336
  }
337

    
338
  updateInterface() {
339
    this.repoService.updateInterface(this.currentRepository.id, this.currentRepository.registeredBy, this.currentInterface).subscribe(
340
      response => {
341
        console.log(`updateRepository responded ${JSON.stringify(response)}`);
342
        if (response) {
343
          this.successMessage = formSuccessUpdatedInterface;
344
          this.wasSaved = true;
345
        } else {
346
          this.errorMessage = formErrorWasntSaved;
347
        }
348
      },
349
      error => {
350
        console.log(error);
351
        this.loadingMessage = '';
352
        this.errorMessage = formErrorWasntSaved;
353
      },
354
      () => {
355
        this.loadingMessage = '';
356
        if ( !this.currentInterface.desiredCompatibilityLevel ||
357
             !this.classCodes.some( x => x === this.currentInterface.desiredCompatibilityLevel ) ) {
358
          this.patchData.next({compatibilityLevel: ''});
359
          this.existingCompLevel = this.currentInterface.desiredCompatibilityLevel;
360
        } else {
361
          this.existingCompLevel = this.compClasses[this.currentInterface.desiredCompatibilityLevel];
362
        }
363
      }
364
    );
365
  }
366

    
367
  ngOnDestroy() {
368
    if (this.currentInterface && this.currentInterface.id && this.toBeDeleted) {
369
      this.repoService.deleteInterface(this.currentInterface.id, this.currentRepository.registeredBy).subscribe(
370
        response => console.log(`deleteInterface responded: ${JSON.stringify(response)}`),
371
        error => console.log(error),
372
        () => console.log(`deleted ${this.currentInterface.id}`)
373
      );
374
    } else {
375
      console.log(`deleting empty interface form`);
376
    }
377
  }
378

    
379
}
(4-4/8)