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.interfaceInfo && this.interfaceInfo.identified &&
75
          this.currentInterface.desiredCompatibilityLevel) {
76
        this.wasSaved = true;
77
      }*/
78
    }
79

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

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

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

    
107
  getInterfaceInfo(baseUrl: string) {
108
    this.successMessage = '';
109
    this.errorMessage = '';
110
    this.groupErrorMessage = '';
111
    if (baseUrl) {
112
      this.loadingMessage = formInfoLoading;
113
      this.valService.getInterfaceInformation(baseUrl).subscribe(
114
        info => {
115
          this.interfaceInfo = info;
116
          if (this.interfaceInfo.identified) {
117
            this.identifiedBaseUrl = true;
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.loadingMessage = '';
129
          this.identifiedBaseUrl = false;
130
          this.errorMessage = noServiceMessage;
131
        },
132
        () => {
133
          if (this.interfaceInfo && this.interfaceInfo.identified &&
134
              this.currentInterface && this.currentInterface.desiredCompatibilityLevel) {
135
            this.wasSaved = true;
136
          }
137
          if ( this.currentInterface && this.currentInterface.accessParams && this.currentInterface.accessParams['set'] ) {
138
            if ( this.valsetList.some( x => x === this.currentInterface.accessParams['set']) ) {
139
              this.patchData.next({selectValidationSet:this.currentInterface.accessParams['set']});
140
            } else {
141
              this.patchData.next({customValidationSet:this.currentInterface.accessParams['set']});
142
              this.getMyControl('selectValidationSet').enable();
143
              this.getMyControl('customValidationSet').disable();
144
            }
145
          }
146
          this.loadingMessage = '';
147
        }
148
      );
149
    }
150
  }
151

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

    
178
      }
179
    );
180
  }
181

    
182
  saveInterface() {
183
    this.groupErrorMessage = '';
184
    this.errorMessage = '';
185
    this.successMessage = '';
186

    
187
    // if decided that valset is required add && this.checkIfValsetWasChosen() to the condition
188
    if (this.group.valid && this.checkIfCompatibilityLevelWasChosen() ) {
189
      if (this.identifiedBaseUrl) {
190
        let baseUrl = this.getMyControl('baseUrl').value;
191
        let valset: string = '';
192
        if (this.getMyControl('selectValidationSet').enabled) {
193
          valset = this.getMyControl('selectValidationSet').value;
194
        } else {
195
          valset = this.getMyControl('customValidationSet').value;
196
        }
197
        let compLvl: string = '';
198
        if (this.getMyControl('compatibilityLevel').value) {
199
          compLvl = this.getMyControl('compatibilityLevel').value;
200
        } else {
201
          compLvl = this.existingCompLevel;
202
        }
203

    
204

    
205
        if (this.currentInterface) {
206
          this.updateCurrent(baseUrl,valset,compLvl);
207
        } else {
208
          this.addCurrent(baseUrl,valset,compLvl);
209
        }
210
      } else {
211
        this.errorMessage = invalidCustomBaseUrl;
212
      }
213
    } else {
214
      this.errorMessage = formErrorRequiredFields;
215
      this.successMessage = '';
216
    }
217
  }
218

    
219
  checkIfValsetWasChosen() {
220
    return ( ( this.getMyControl('selectValidationSet').enabled &&
221
               this.getMyControl('selectValidationSet').value !='' ) ||
222
             ( this.getMyControl('customValidationSet').enabled &&
223
               this.getMyControl('customValidationSet').value !='' ) );
224
  }
225

    
226
  checkIfCompatibilityLevelWasChosen() {
227
    return ( this.getMyControl('compatibilityLevel').value != '' || (this.existingCompLevel && (this.existingCompLevel != '')) );
228
  }
229

    
230
  checkIfValid() {
231
    if (this.inRegister) {
232
      // if decided that valset is required add && this.checkIfValsetWasChosen() to the condition
233
      if ( this.group.valid && this.checkIfCompatibilityLevelWasChosen() ) {
234
        if ( this.identifiedBaseUrl ) {
235
          let baseUrl = this.getMyControl('baseUrl').value;
236

    
237
          let valset: string = '';
238
          if (this.getMyControl('selectValidationSet').enabled) {
239
            valset = this.getMyControl('selectValidationSet').value;
240
          } else {
241
            valset = this.getMyControl('customValidationSet').value;
242
          }
243

    
244
          let compLvl: string = '';
245
          if (this.getMyControl('compatibilityLevel').value) {
246
            this.existingCompLevel = this.compClasses[this.getMyControl('compatibilityLevel').value];
247
            console.log('this.existingCompLevel is',this.existingCompLevel);
248
            compLvl = this.getMyControl('compatibilityLevel').value;
249
          } else {
250
            compLvl = this.existingCompLevel;
251
          }
252

    
253
          if (this.currentInterface) {
254
            this.updateCurrent(baseUrl,valset,compLvl);
255
          } else {
256
            this.addCurrent(baseUrl,valset,compLvl);
257
          }
258
        }
259
      } else {
260
        this.exportedData = null;
261
        this.wasSaved = false;
262
        this.successMessage = '';
263
      }
264
    }
265
  }
266

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

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

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

    
337
  }
338

    
339
  updateInterface() {
340
    this.repoService.updateInterface(this.currentRepository.id, this.currentRepository.registeredBy, this.currentInterface).subscribe(
341
      response => {
342
        console.log(`updateRepository responded ${JSON.stringify(response)}`);
343
        if (response) {
344
          this.successMessage = formSuccessUpdatedInterface;
345
          this.wasSaved = true;
346
        } else {
347
          this.errorMessage = formErrorWasntSaved;
348
        }
349
      },
350
      error => {
351
        console.log(error);
352
        this.loadingMessage = '';
353
        this.errorMessage = formErrorWasntSaved;
354
      },
355
      () => {
356
        this.loadingMessage = '';
357
        if ( !this.currentInterface.desiredCompatibilityLevel || !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/6)