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
/*  NOT ANYMORE
80
    if (this.currentInterface) {
81
      this.getMyControl('baseUrl').disable();
82
    }
83
*/
84
    this.existingValSet = true;
85
    this.getMyControl('customValidationSet').disable();
86
  }
87

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

    
100
  getInterfaceInfo(baseUrl: string) {
101
    this.successMessage = '';
102
    this.errorMessage = '';
103
    this.loadingMessage = formInfoLoading;
104
    if (baseUrl) {
105
      this.valService.getInterfaceInformation(baseUrl).subscribe(
106
        info => {
107
          this.interfaceInfo = info;
108
          if (this.interfaceInfo.identified) {
109
            this.identifiedBaseUrl = true;
110
          } else {
111
            this.errorMessage = invalidCustomBaseUrl;
112
          }
113
          if (this.interfaceInfo.sets) {
114
            this.valsetList = this.interfaceInfo.sets;
115
            console.log(this.valsetList);
116
          }
117
        },
118
        error => {
119
          console.log(error);
120
          this.loadingMessage = '';
121
          this.identifiedBaseUrl = false;
122
          this.errorMessage = noServiceMessage;
123
        },
124
        () => this.loadingMessage = ''
125
      );
126
    }
127
  }
128

    
129
  getCompatibilityClasses() {
130
    this.repoService.getCompatibilityClasses(this.currentRepository.datasourceType).subscribe(
131
      classes => {
132
        this.compClasses = classes;
133
        for (let key in this.compClasses){
134
          this.classCodes.push(key);
135
        }
136
      },
137
      error => {
138
        this.errorMessage = noServiceMessage;
139
        console.log(error);
140
      }
141
    );
142
  }
143

    
144
  saveInterface() {
145
    this.errorMessage = '';
146
    this.successMessage = '';
147
    if (this.group.valid && ( this.getMyControl('selectValidationSet').value || this.getMyControl('customValidationSet').value ) ) {
148
      if (this.identifiedBaseUrl) {
149
        let baseUrl = this.getMyControl('baseUrl').value;
150
        let valset: string = '';
151
        if (this.getMyControl('selectValidationSet').enabled) {
152
          valset = this.getMyControl('selectValidationSet').value;
153
        } else {
154
          valset = this.getMyControl('customValidationSet').value;
155
        }
156
        let compLvl = this.getMyControl('compatibilityLevel').value;
157

    
158
        if (this.currentInterface) {
159
          this.updateCurrent(baseUrl,valset,compLvl);
160
        } else {
161
          this.addCurrent(baseUrl,valset,compLvl);
162
        }
163
      } else {
164
        this.errorMessage = invalidCustomBaseUrl;
165
      }
166
    } else {
167
      this.errorMessage = formErrorRequiredFields;
168
      this.successMessage = '';
169
    }
170
  }
171

    
172
  updateCurrent (baseUrl: string, valset: string, compLvl: string) {
173
    this.successMessage = '';
174
    this.errorMessage = '';
175
    this.loadingMessage = formSubmitting;
176
    this.currentInterface.baseUrl = baseUrl;
177
    this.currentInterface.accessSet = valset;
178
    this.currentInterface.desiredCompatibilityLevel = compLvl;
179
    this.currentInterface.typology = this.currentRepository.datasourceClass;
180
    this.repoService.updateInterface(this.currentInterface).subscribe(
181
      response => {
182
        console.log(`updateRepository responded ${response}`);
183
        if (response == '200') {
184
          this.successMessage = formSuccessUpdatedInterface;
185
        } else {
186
          this.errorMessage = formErrorWasntSaved;
187
        }
188
      },
189
      error => {
190
        console.log(error);
191
        this.loadingMessage = '';
192
        this.errorMessage = formErrorWasntSaved;
193
      },
194
      () => this.loadingMessage = ''
195
    );
196
  }
197

    
198
  addCurrent (baseUrl: string, valset: string, compLvl: string) {
199
    this.errorMessage = '';
200
    this.successMessage = '';
201
    this.loadingMessage = formSubmitting;
202
    this.currentInterface = new RepositoryInterface();
203
    this.currentInterface.baseUrl = baseUrl;
204
    this.currentInterface.accessSet = valset;
205
    this.currentInterface.desiredCompatibilityLevel = compLvl;
206
    this.currentInterface.typology = this.currentRepository.datasourceClass;
207
    this.repoService.addInterface(this.currentRepository.datasourceType, this.currentRepository.id, this.currentInterface).subscribe(
208
      addedInterface => {
209
        console.log(`addInterface responded ${addedInterface}`);
210
        this.currentInterface = addedInterface;
211
      },
212
      error => {
213
        console.log(error);
214
        this.loadingMessage = '';
215
        this.errorMessage = formErrorWasntSaved;
216
      },
217
      () => {
218
        this.loadingMessage = '';
219
        if (this.currentInterface.id) {
220
          this.successMessage = formSuccessAddedInterface;
221
        } else {
222
          this.errorMessage = formErrorWasntSaved;
223
        }
224
      }
225
    );
226
  }
227

    
228
  ngOnDestroy() {
229
    if (this.currentInterface && this.currentInterface.id && this.toBeDeleted) {
230
/*      this.repoService.deleteInterface(this.currentInterface.id).subscribe(
231
        response => console.log(`deleteInterface responded: ${response}`),
232
        error => console.log(error)
233
      );*/
234
      console.log(`deleting ${this.currentInterface.id}`);
235
    } else {
236
      console.log(`deleting empty interface form`);
237
    }
238
  }
239

    
240
}
(4-4/6)