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,null,2)}`);
62
    if (this.data && this.data.length) {
63
      this.currentInterface = this.data[0];
64
      this.patchData.next({
65
          baseUrl: this.currentInterface.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

    
83
    if (this.currentInterface) {
84
      console.log(`accessParams is ${JSON.stringify(this.currentInterface.accessParams)}`);
85
    }
86

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

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

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

    
149
  getCompatibilityClasses() {
150
    this.repoService.getCompatibilityClasses(this.currentRepository.datasourceType).subscribe(
151
      classes => {
152
        this.compClasses = classes;
153
        for (let key in this.compClasses) {
154
          this.classCodes.push(key);
155
        }
156
      },
157
      error => {
158
        this.errorMessage = noServiceMessage;
159
        console.log(error);
160
      }
161
    );
162
  }
163

    
164
  saveInterface() {
165
    this.groupErrorMessage = '';
166
    this.errorMessage = '';
167
    this.successMessage = '';
168
    if (this.group.valid && ( this.getMyControl('selectValidationSet').value || this.getMyControl('customValidationSet').value ) ) {
169
      if (this.identifiedBaseUrl) {
170
        let baseUrl = this.getMyControl('baseUrl').value;
171
        let valset: string = '';
172
        if (this.getMyControl('selectValidationSet').enabled) {
173
          valset = this.getMyControl('selectValidationSet').value;
174
        } else {
175
          valset = this.getMyControl('customValidationSet').value;
176
        }
177
        let compLvl = this.getMyControl('compatibilityLevel').value;
178

    
179
        if (this.currentInterface) {
180
          this.updateCurrent(baseUrl,valset,compLvl);
181
        } else {
182
          this.addCurrent(baseUrl,valset,compLvl);
183
        }
184
      } else {
185
        this.errorMessage = invalidCustomBaseUrl;
186
      }
187
    } else {
188
      this.errorMessage = formErrorRequiredFields;
189
      this.successMessage = '';
190
    }
191
  }
192

    
193
  updateCurrent (baseUrl: string, valset: string, compLvl: string) {
194
    this.successMessage = '';
195
    this.errorMessage = '';
196
    this.loadingMessage = formSubmitting;
197
    this.currentInterface.baseUrl = baseUrl;
198
    this.currentInterface.accessSet = valset;
199
    this.currentInterface.accessParams['set'] = valset;
200
    this.currentInterface.desiredCompatibilityLevel = compLvl;
201
    this.currentInterface.compliance = compLvl;
202
    this.currentInterface.typology = this.currentRepository.datasourceClass;
203
    this.repoService.updateInterface(this.currentRepository.id, this.currentInterface).subscribe(
204
      response => {
205
        console.log(`updateRepository responded ${JSON.stringify(response)}`);
206
        if (response) {
207
          this.successMessage = formSuccessUpdatedInterface;
208
          this.wasSaved = true;
209
        } else {
210
          this.errorMessage = formErrorWasntSaved;
211
        }
212
      },
213
      error => {
214
        console.log(error);
215
        this.loadingMessage = '';
216
        this.errorMessage = formErrorWasntSaved;
217
      },
218
      () => {
219
        this.loadingMessage = '';
220
      }
221
    );
222
  }
223

    
224
  addCurrent (baseUrl: string, valset: string, compLvl: string) {
225
    this.errorMessage = '';
226
    this.successMessage = '';
227
    this.loadingMessage = formSubmitting;
228
    this.currentInterface = new RepositoryInterface();
229
    this.currentInterface.baseUrl = baseUrl;
230
    this.currentInterface.accessSet = valset;
231
    this.currentInterface.accessParams = {'set': valset};
232
    this.currentInterface.desiredCompatibilityLevel = compLvl;
233
    this.currentInterface.compliance = compLvl;
234
    this.currentInterface.typology = this.currentRepository.datasourceClass;
235
    this.repoService.addInterface(this.currentRepository.datasourceType, this.currentRepository.id, this.currentInterface).subscribe(
236
      addedInterface => {
237
        console.log(`addInterface responded ${JSON.stringify(addedInterface)}`);
238
        this.currentInterface = addedInterface;
239
      },
240
      error => {
241
        console.log(error);
242
        this.loadingMessage = '';
243
        this.errorMessage = formErrorWasntSaved;
244
      },
245
      () => {
246
        if (this.currentInterface.id) {
247
          this.successMessage = formSuccessAddedInterface;
248
          this.wasSaved = true;
249
        } else {
250
          this.errorMessage = formErrorWasntSaved;
251
        }
252
        this.loadingMessage = '';
253
      }
254
    );
255
  }
256

    
257
  ngOnDestroy() {
258
    if (this.currentInterface && this.currentInterface.id && this.toBeDeleted) {
259
      this.repoService.deleteInterface(this.currentInterface.id).subscribe(
260
        response => console.log(`deleteInterface responded: ${response}`),
261
        error => console.log(error),
262
        () => console.log(`deleted ${this.currentInterface.id}`)
263
      );
264
    } else {
265
      console.log(`deleting empty interface form`);
266
    }
267
  }
268

    
269
}
(4-4/6)