Project

General

Profile

1 50944 myrto.kouk
import { Component, Injector, OnDestroy } from '@angular/core';
2 50346 myrto.kouk
import { MyGroup } from '../../../shared/reusablecomponents/forms/my-group.interface';
3 50936 myrto.kouk
import { FormBuilder, Validators } from '@angular/forms';
4 50432 myrto.kouk
import {
5 51015 myrto.kouk
  formErrorRequiredFields, formErrorWasntSaved, formInfoLoading, formSubmitting, formSuccessAddedInterface,
6
  formSuccessUpdatedInterface,
7 50592 myrto.kouk
  invalidCustomBaseUrl, noServiceMessage
8 50432 myrto.kouk
} from '../../../domain/shared-messages';
9
import { ValidatorService } from '../../../services/validator.service';
10 50536 myrto.kouk
import { ActivatedRoute } from '@angular/router';
11
import { RepositoryService } from '../../../services/repository.service';
12 50977 myrto.kouk
import { InterfaceInformation, Repository, RepositoryInterface } from '../../../domain/typeScriptClasses';
13 51015 myrto.kouk
import {
14
  baseUrlDesc, compatibilityLevelDesc, customValSetDesc, Description,
15
  existingValSetDesc
16
} from '../../../domain/oa-description';
17 50247 myrto.kouk
18
@Component ({
19 50346 myrto.kouk
  selector: 'datasource-interface-form',
20
  templateUrl: './datasource-interface-form.component.html'
21 50247 myrto.kouk
})
22
23 50944 myrto.kouk
export class DatasourceInterfaceFormComponent extends MyGroup implements OnDestroy {
24 50307 myrto.kouk
25 50977 myrto.kouk
  loadingMessage: string;
26 50247 myrto.kouk
  successMessage: string;
27
  errorMessage: string;
28 50536 myrto.kouk
29 50977 myrto.kouk
  currentRepository: Repository;
30 51055 myrto.kouk
  oldInterface: boolean;
31 50536 myrto.kouk
32
  identifiedBaseUrl: boolean;
33 50290 myrto.kouk
  existingValSet: boolean;
34 50592 myrto.kouk
  interfaceInfo: InterfaceInformation;
35 50944 myrto.kouk
  currentInterface: RepositoryInterface;
36 50977 myrto.kouk
  valsetList: string[] = [];
37 50247 myrto.kouk
38 51675 myrto.kouk
  existingCompLevel: string;
39 50536 myrto.kouk
  compClasses: Map<string,string> = new Map<string,string>();
40
  classCodes: string[] = [];
41
42 50255 myrto.kouk
  readonly groupDefinition = {
43 50259 myrto.kouk
    baseUrl: ['', Validators.required],
44
    selectValidationSet: [''],
45 50290 myrto.kouk
    customValidationSet: [''],
46 51771 myrto.kouk
    compatibilityLevel: ['']
47 50259 myrto.kouk
  };
48 51015 myrto.kouk
  baseUrlDesc: Description = baseUrlDesc;
49
  existingValSetDesc: Description = existingValSetDesc;
50
  customValSetDesc: Description = customValSetDesc;
51
  compatibilityLevelDesc: Description = compatibilityLevelDesc;
52 50255 myrto.kouk
53 50536 myrto.kouk
  constructor(injector: Injector,
54
              private valService: ValidatorService,
55 50977 myrto.kouk
              private repoService: RepositoryService){
56 50432 myrto.kouk
    super(injector);
57
  }
58 50247 myrto.kouk
59 50536 myrto.kouk
  ngOnInit() {
60 51015 myrto.kouk
    this.currentRepository = <Repository>this.otherData;
61 51581 myrto.kouk
    console.log(`other data is: ${JSON.stringify(this.otherData,null,2)}`);
62 51015 myrto.kouk
    if (this.data && this.data.length) {
63
      this.currentInterface = this.data[0];
64
      this.patchData.next({
65 51675 myrto.kouk
        baseUrl: this.data[0].baseUrl,
66
        selectValidationSet: '',
67
        customValidationSet: '',
68
        compatibilityLevel:this.data[0].desiredCompatibilityLevel
69 51015 myrto.kouk
      });
70
      this.getInterfaceInfo(this.data[0].baseUrl);
71
      this.data.splice(0,1);
72 51055 myrto.kouk
      this.oldInterface = true;
73 51543 myrto.kouk
      console.log(`received an interface!`);
74 51581 myrto.kouk
      if (this.currentInterface.baseUrl && this.currentInterface.accessParams['set'] && this.currentInterface.desiredCompatibilityLevel) {
75
        this.wasSaved = true;
76
      }
77 51015 myrto.kouk
    }
78 51055 myrto.kouk
79
    /* initializes MyGroup parent component and the FormGroup */
80 51015 myrto.kouk
    super.ngOnInit();
81
    console.log(this.group, this.parentGroup);
82 51771 myrto.kouk
    this.getCompatibilityClasses();
83 51118 myrto.kouk
84 51416 myrto.kouk
    /*  NOT ANYMORE
85
    if (this.currentInterface) {
86 51037 myrto.kouk
      this.getMyControl('baseUrl').disable();
87
    }
88 51118 myrto.kouk
*/
89 51015 myrto.kouk
    this.existingValSet = true;
90
    this.getMyControl('customValidationSet').disable();
91 50592 myrto.kouk
  }
92 50259 myrto.kouk
93
  chooseValSet(existingValSet: boolean) {
94 51675 myrto.kouk
    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 51771 myrto.kouk
    this.checkIfValid();
104 50259 myrto.kouk
  }
105 50269 myrto.kouk
106 50592 myrto.kouk
  getInterfaceInfo(baseUrl: string) {
107 51015 myrto.kouk
    this.successMessage = '';
108
    this.errorMessage = '';
109 51581 myrto.kouk
    this.groupErrorMessage = '';
110 51118 myrto.kouk
    if (baseUrl) {
111 51359 myrto.kouk
      this.loadingMessage = formInfoLoading;
112 50936 myrto.kouk
      this.valService.getInterfaceInformation(baseUrl).subscribe(
113 50944 myrto.kouk
        info => {
114
          this.interfaceInfo = info;
115
          if (this.interfaceInfo.identified) {
116 50936 myrto.kouk
            this.identifiedBaseUrl = true;
117
          } else {
118
            this.errorMessage = invalidCustomBaseUrl;
119
          }
120 50944 myrto.kouk
          if (this.interfaceInfo.sets) {
121 50977 myrto.kouk
            this.valsetList = this.interfaceInfo.sets;
122
            console.log(this.valsetList);
123 50936 myrto.kouk
          }
124 50944 myrto.kouk
        },
125
        error => {
126
          console.log(error);
127 51015 myrto.kouk
          this.loadingMessage = '';
128 50944 myrto.kouk
          this.identifiedBaseUrl = false;
129
          this.errorMessage = noServiceMessage;
130 51015 myrto.kouk
        },
131 51416 myrto.kouk
        () => {
132
          if ( this.currentInterface && this.currentInterface.accessParams && this.currentInterface.accessParams['set'] ) {
133 51771 myrto.kouk
            if ( this.valsetList.some( x => x === this.currentInterface.accessParams['set']) ) {
134 51416 myrto.kouk
              this.patchData.next({selectValidationSet:this.currentInterface.accessParams['set']});
135
            } else {
136
              this.patchData.next({customValidationSet:this.currentInterface.accessParams['set']});
137 51564 myrto.kouk
              this.getMyControl('selectValidationSet').enable();
138
              this.getMyControl('customValidationSet').disable();
139 51416 myrto.kouk
            }
140
          }
141
          this.loadingMessage = '';
142
        }
143 50936 myrto.kouk
      );
144
    }
145 50536 myrto.kouk
  }
146
147
  getCompatibilityClasses() {
148 50977 myrto.kouk
    this.repoService.getCompatibilityClasses(this.currentRepository.datasourceType).subscribe(
149 50536 myrto.kouk
      classes => {
150
        this.compClasses = classes;
151 51416 myrto.kouk
        for (let key in this.compClasses) {
152 50536 myrto.kouk
          this.classCodes.push(key);
153
        }
154
      },
155
      error => {
156
        this.errorMessage = noServiceMessage;
157
        console.log(error);
158 51771 myrto.kouk
      },
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 50536 myrto.kouk
      }
176
    );
177
  }
178
179 51055 myrto.kouk
  saveInterface() {
180 51543 myrto.kouk
    this.groupErrorMessage = '';
181 51055 myrto.kouk
    this.errorMessage = '';
182
    this.successMessage = '';
183 51771 myrto.kouk
    if (this.group.valid && this.checkIfValsetWasChosen() && this.checkIfCompatibilityLevelWasChosen() ) {
184 51055 myrto.kouk
      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 51771 myrto.kouk
        let compLvl: string = '';
193
        if (this.getMyControl('compatibilityLevel').value) {
194
          compLvl = this.getMyControl('compatibilityLevel').value;
195
        } else {
196
          compLvl = this.existingCompLevel;
197
        }
198 51055 myrto.kouk
199 51771 myrto.kouk
200 51055 myrto.kouk
        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 51771 myrto.kouk
  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 51675 myrto.kouk
  checkIfValid() {
223 51771 myrto.kouk
    console.log(this.existingCompLevel);
224 51675 myrto.kouk
    if (this.inRegister) {
225 51771 myrto.kouk
      if ( this.group.valid && this.checkIfValsetWasChosen() && this.checkIfCompatibilityLevelWasChosen() ) {
226
        if ( this.identifiedBaseUrl ) {
227 51675 myrto.kouk
          let baseUrl = this.getMyControl('baseUrl').value;
228 51771 myrto.kouk
229 51675 myrto.kouk
          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 51771 myrto.kouk
          let compLvl: string = '';
237
          if (this.getMyControl('compatibilityLevel').value) {
238
            compLvl = this.getMyControl('compatibilityLevel').value;
239
          } else {
240
            compLvl = this.existingCompLevel;
241
          }
242
243 51675 myrto.kouk
          if (this.currentInterface) {
244
            this.updateCurrent(baseUrl,valset,compLvl);
245
          } else {
246
            this.addCurrent(baseUrl,valset,compLvl);
247
          }
248
        }
249 51771 myrto.kouk
      } else {
250
        this.exportedData = null;
251
        this.wasSaved = false;
252
        this.successMessage = '';
253 51675 myrto.kouk
      }
254
    }
255
  }
256
257 50977 myrto.kouk
  updateCurrent (baseUrl: string, valset: string, compLvl: string) {
258
    this.successMessage = '';
259
    this.errorMessage = '';
260
    this.currentInterface.baseUrl = baseUrl;
261
    this.currentInterface.accessSet = valset;
262 51561 myrto.kouk
    this.currentInterface.accessParams['set'] = valset;
263 50977 myrto.kouk
    this.currentInterface.desiredCompatibilityLevel = compLvl;
264 51416 myrto.kouk
    this.currentInterface.compliance = compLvl;
265 50977 myrto.kouk
    this.currentInterface.typology = this.currentRepository.datasourceClass;
266 51651 myrto.kouk
    this.exportedData = this.currentInterface;
267
    if (!this.inRegister) {
268 51675 myrto.kouk
      this.loadingMessage = formSubmitting;
269 51651 myrto.kouk
      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 50977 myrto.kouk
  }
276
277
  addCurrent (baseUrl: string, valset: string, compLvl: string) {
278
    this.errorMessage = '';
279
    this.successMessage = '';
280
    this.currentInterface = new RepositoryInterface();
281 51563 myrto.kouk
    this.currentInterface.baseUrl = baseUrl;
282 50977 myrto.kouk
    this.currentInterface.accessSet = valset;
283 51561 myrto.kouk
    this.currentInterface.accessParams = {'set': valset};
284 50977 myrto.kouk
    this.currentInterface.desiredCompatibilityLevel = compLvl;
285 51416 myrto.kouk
    this.currentInterface.compliance = compLvl;
286 50977 myrto.kouk
    this.currentInterface.typology = this.currentRepository.datasourceClass;
287 51651 myrto.kouk
    this.exportedData = this.currentInterface;
288
    if (!this.inRegister) {
289 51675 myrto.kouk
      this.loadingMessage = formSubmitting;
290 51651 myrto.kouk
      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 50977 myrto.kouk
    this.repoService.addInterface(this.currentRepository.datasourceType, this.currentRepository.id, this.currentInterface).subscribe(
300
      addedInterface => {
301 51416 myrto.kouk
        console.log(`addInterface responded ${JSON.stringify(addedInterface)}`);
302 50977 myrto.kouk
        this.currentInterface = addedInterface;
303
      },
304
      error => {
305
        console.log(error);
306
        this.loadingMessage = '';
307
        this.errorMessage = formErrorWasntSaved;
308
      },
309
      () => {
310
        if (this.currentInterface.id) {
311
          this.successMessage = formSuccessAddedInterface;
312 51581 myrto.kouk
          this.wasSaved = true;
313 50977 myrto.kouk
        } else {
314
          this.errorMessage = formErrorWasntSaved;
315
        }
316 51427 myrto.kouk
        this.loadingMessage = '';
317 50977 myrto.kouk
      }
318
    );
319 51651 myrto.kouk
320 50977 myrto.kouk
  }
321
322 51651 myrto.kouk
  updateInterface() {
323
    this.repoService.updateInterface(this.currentRepository.id, this.currentInterface).subscribe(
324
      response => {
325
        console.log(`updateRepository responded ${JSON.stringify(response)}`);
326
        if (response) {
327
          this.successMessage = formSuccessUpdatedInterface;
328
          this.wasSaved = true;
329
        } else {
330
          this.errorMessage = formErrorWasntSaved;
331
        }
332
      },
333
      error => {
334
        console.log(error);
335
        this.loadingMessage = '';
336
        this.errorMessage = formErrorWasntSaved;
337
      },
338
      () => {
339
        this.loadingMessage = '';
340
      }
341
    );
342
  }
343
344 50944 myrto.kouk
  ngOnDestroy() {
345 51015 myrto.kouk
    if (this.currentInterface && this.currentInterface.id && this.toBeDeleted) {
346 51427 myrto.kouk
      this.repoService.deleteInterface(this.currentInterface.id).subscribe(
347 50944 myrto.kouk
        response => console.log(`deleteInterface responded: ${response}`),
348 51427 myrto.kouk
        error => console.log(error),
349
        () => console.log(`deleted ${this.currentInterface.id}`)
350
      );
351 50944 myrto.kouk
    } else {
352
      console.log(`deleting empty interface form`);
353
    }
354
  }
355 50536 myrto.kouk
356 50247 myrto.kouk
}