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
  compClasses: Map<string,string> = new Map<string,string>();
39
  classCodes: string[] = [];
40

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

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

    
58
  ngOnInit() {
59
    this.currentRepository = <Repository>this.otherData;
60
    this.getCompatibilityClasses();
61
    console.log(`other data is: ${JSON.stringify(this.otherData)}`);
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
    }
74

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

    
79
    if (this.currentInterface) {
80
      console.log(`accessParams is ${JSON.stringify(this.currentInterface.accessParams)}`);
81
    }
82

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

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

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

    
142
  getCompatibilityClasses() {
143
    this.repoService.getCompatibilityClasses(this.currentRepository.datasourceType).subscribe(
144
      classes => {
145
        this.compClasses = classes;
146
        for (let key in this.compClasses) {
147
          this.classCodes.push(key);
148
        }
149
      },
150
      error => {
151
        this.errorMessage = noServiceMessage;
152
        console.log(error);
153
      }
154
    );
155
  }
156

    
157
  saveInterface() {
158
    this.errorMessage = '';
159
    this.successMessage = '';
160
    if (this.group.valid && ( this.getMyControl('selectValidationSet').value || this.getMyControl('customValidationSet').value ) ) {
161
      if (this.identifiedBaseUrl) {
162
        let baseUrl = this.getMyControl('baseUrl').value;
163
        let valset: string = '';
164
        if (this.getMyControl('selectValidationSet').enabled) {
165
          valset = this.getMyControl('selectValidationSet').value;
166
        } else {
167
          valset = this.getMyControl('customValidationSet').value;
168
        }
169
        let compLvl = this.getMyControl('compatibilityLevel').value;
170

    
171
        if (this.currentInterface) {
172
          this.updateCurrent(baseUrl,valset,compLvl);
173
        } else {
174
          this.addCurrent(baseUrl,valset,compLvl);
175
        }
176
      } else {
177
        this.errorMessage = invalidCustomBaseUrl;
178
      }
179
    } else {
180
      this.errorMessage = formErrorRequiredFields;
181
      this.successMessage = '';
182
    }
183
  }
184

    
185
  updateCurrent (baseUrl: string, valset: string, compLvl: string) {
186
    this.successMessage = '';
187
    this.errorMessage = '';
188
    this.loadingMessage = formSubmitting;
189
    this.currentInterface.baseUrl = baseUrl;
190
    this.currentInterface.accessSet = valset;
191
    this.currentInterface.accessParams['set'] = valset;
192
    this.currentInterface.desiredCompatibilityLevel = compLvl;
193
    this.currentInterface.compliance = compLvl;
194
    this.currentInterface.typology = this.currentRepository.datasourceClass;
195
    this.repoService.updateInterface(this.currentRepository.id, this.currentInterface).subscribe(
196
      response => {
197
        console.log(`updateRepository responded ${JSON.stringify(response)}`);
198
        if (response) {
199
          this.successMessage = formSuccessUpdatedInterface;
200
        } else {
201
          this.errorMessage = formErrorWasntSaved;
202
        }
203
      },
204
      error => {
205
        console.log(error);
206
        this.loadingMessage = '';
207
        this.errorMessage = formErrorWasntSaved;
208
      },
209
      () => this.loadingMessage = ''
210
    );
211
  }
212

    
213
  addCurrent (baseUrl: string, valset: string, compLvl: string) {
214
    this.errorMessage = '';
215
    this.successMessage = '';
216
    this.loadingMessage = formSubmitting;
217
    this.currentInterface = new RepositoryInterface();
218
    this.currentInterface.baseUrl = baseUrl;
219
    this.currentInterface.accessSet = valset;
220
    this.currentInterface.accessParams['set'] = valset;
221
    this.currentInterface.desiredCompatibilityLevel = compLvl;
222
    this.currentInterface.compliance = compLvl;
223
    this.currentInterface.typology = this.currentRepository.datasourceClass;
224
    this.repoService.addInterface(this.currentRepository.datasourceType, this.currentRepository.id, this.currentInterface).subscribe(
225
      addedInterface => {
226
        console.log(`addInterface responded ${JSON.stringify(addedInterface)}`);
227
        this.currentInterface = addedInterface;
228
      },
229
      error => {
230
        console.log(error);
231
        this.loadingMessage = '';
232
        this.errorMessage = formErrorWasntSaved;
233
      },
234
      () => {
235
        this.loadingMessage = '';
236
        if (this.currentInterface.id) {
237
          this.successMessage = formSuccessAddedInterface;
238
        } else {
239
          this.errorMessage = formErrorWasntSaved;
240
        }
241
      }
242
    );
243
  }
244

    
245
  ngOnDestroy() {
246
    if (this.currentInterface && this.currentInterface.id && this.toBeDeleted) {
247
/*      this.repoService.deleteInterface(this.currentInterface.id).subscribe(
248
        response => console.log(`deleteInterface responded: ${response}`),
249
        error => console.log(error)
250
      );*/
251
      console.log(`deleting ${this.currentInterface.id}`);
252
    } else {
253
      console.log(`deleting empty interface form`);
254
    }
255
  }
256

    
257
}
(4-4/6)