Project

General

Profile

1
import { Component, OnInit, QueryList, ViewChild, ViewChildren } from '@angular/core';
2
import { Repository, RepositoryInterface } from '../../../domain/typeScriptClasses';
3
import { ActivatedRoute, Router } from '@angular/router';
4
import { FormBuilder } from '@angular/forms';
5
import { AsideHelpContentComponent, HelpContentComponent } from '../../../shared/reusablecomponents/help-content.component';
6
import { RepositoryService } from '../../../services/repository.service';
7
import { DatasourceCreateFormComponent } from '../../../shared/reusablecomponents/sources-forms/datasource-create-form.component';
8
import { DatasourceNewInterfaceFormComponent } from '../../../shared/reusablecomponents/sources-forms/datasource-new-interface-form.component';
9
import { from, of } from 'rxjs';
10
import { concatMap } from 'rxjs/operators';
11
import { errorsInInterfaces, formErrorRegisterRepo, noInterfacesSaved } from '../../../domain/shared-messages';
12

    
13
@Component({
14
  selector: 'app-register-new-datasource',
15
  templateUrl: './register-new-datasource.component.html'
16
})
17
export class RegisterNewDatasourceComponent implements OnInit {
18
  loadingMessage: string;
19
  errorMessage: string;
20

    
21
  datasourceType: string;
22
  repo: Repository = null;
23
  repoInterfaces: RepositoryInterface[] = [];
24
  interfacesToDelete: string[] = [];
25

    
26
  /* queryParams are used to follow the steps without refreshing the page
27
   * This was needed for Help Service [which sends back info according to the current router.url].
28
   * The param that is used is 'step' and the values are: 'basicInformation','interfaces','finish'
29
   * currentStep represents the number of the current step
30
   */
31
  currentStep: number;
32
  @ViewChild('topHelperContent')
33
  public topHelperContent: HelpContentComponent;
34
  @ViewChild('leftHelperContent')
35
  public leftHelperContent: AsideHelpContentComponent;
36
  @ViewChild('rightHelperContent')
37
  public rightHelperContent: AsideHelpContentComponent;
38
  @ViewChild('bottomHelperContent')
39
  public bottomHelperContent: HelpContentComponent;
40

    
41
  @ViewChild ('registerDatasource')
42
  registerDatasource: DatasourceCreateFormComponent;
43

    
44
  @ViewChildren('interfacesArray') interfacesArray: QueryList<DatasourceNewInterfaceFormComponent>;
45
  dataForInterfaceComp: any[] = [];
46

    
47
  constructor(private fb: FormBuilder,
48
              private route: ActivatedRoute,
49
              private router: Router,
50
              private repoService: RepositoryService) {}
51

    
52
  ngOnInit() {
53
    if (this.datasourceType) {
54

    
55
      // will execute getStep() every time there is a change in query params
56
      this.route.queryParams.subscribe(
57
        params => {
58
          this.getStep();
59
        }
60
      );
61
    }
62
  }
63

    
64

    
65
  getStep() {
66
    this.currentStep = 1;
67
    if (this.route.snapshot.queryParamMap.has('step')) {
68
      const stepName = this.route.snapshot.queryParamMap.get('step');
69
      if (stepName === 'basicInformation') {
70
        this.currentStep = 1;
71
      } else if (stepName === 'interfaces') {
72
        if (!this.repo) {
73
          this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=basicInformation`);
74
        } else {
75
          this.currentStep = 2;
76
        }
77
      } else if (stepName === 'finish') {
78
        this.currentStep = 3;
79
      }
80
    }
81
    this.rightHelperContent.ngOnInit();
82
    this.topHelperContent.ngOnInit();
83
    this.leftHelperContent.ngOnInit();
84
    this.bottomHelperContent.ngOnInit();
85
  }
86

    
87
  moveAStep() {
88
    this.errorMessage = '';
89
    if (this.currentStep === 1) {
90
      this.registerDatasource.registerDatasource();
91
    } else if (this.currentStep === 2) {
92
      of(this.getInterfaces()).subscribe(
93
        errors => {
94
          if (errors > 0) {
95
            this.errorMessage = errorsInInterfaces;
96
            window.scrollTo(1, 1);
97
          } else {
98
            if (this.repoInterfaces.length > 0) {
99
              this.addRepository();
100
            } else {
101
              this.errorMessage = noInterfacesSaved;
102
              window.scrollTo(1, 1);
103
            }
104
          }
105
        }
106
      );
107
    }
108
  }
109

    
110
  moveBackAStep() {
111
    this.errorMessage = '';
112
    if (this.currentStep === 2) {
113
      of(this.getInterfaces()).subscribe(
114
        () => this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=basicInformation`)
115
      );
116
    }
117
  }
118

    
119
  addInterfaceToList(intrf?: RepositoryInterface) {
120
    const curIndex = this.dataForInterfaceComp.length;
121
    const curRepoInfo = { id: this.repo.id, datasourceType: this.repo.datasourceType,
122
      datasourceClass: this.repo.datasourceClass, registeredBy: this.repo.registeredBy };
123
    if (intrf) {
124
      this.dataForInterfaceComp.push([true, curIndex, curRepoInfo, intrf]);
125
    } else {
126
      this.dataForInterfaceComp.push([true, curIndex, curRepoInfo]);
127
    }
128
  }
129

    
130
  removeInterfaceFromList(i: number) {
131
    const tempArray = this.dataForInterfaceComp;
132
    this.dataForInterfaceComp = [];
133
    tempArray.splice(i, 1);
134
    this.dataForInterfaceComp = tempArray;
135
    console.log(JSON.stringify(this.dataForInterfaceComp));
136
  }
137

    
138
  getInterfaces() {
139
    this.repoInterfaces = [];
140
    let invalidFormsCount = 0;
141
    for (const el of this.interfacesArray.toArray()) {
142
      const intrf = el.getInterface();
143
      if (intrf) {
144
        this.repoInterfaces.push(intrf);
145
        console.log(JSON.stringify(intrf));
146
      } else {
147
        invalidFormsCount = invalidFormsCount + 1;
148
        const repo_interface = el.getCurrentValues();
149
        this.repoInterfaces.push(repo_interface);
150
        console.log(JSON.stringify(repo_interface));
151
      }
152
    }
153
    console.log('new interfaces is ', this.repoInterfaces);
154
    return invalidFormsCount;
155
  }
156

    
157
  fillInterfacesForms() {
158
    this.dataForInterfaceComp = [];
159
    if (this.repoInterfaces && (this.repoInterfaces.length > 0)) {
160
      for (let i = 0; i < this.repoInterfaces.length; i++) {
161
        this.dataForInterfaceComp.push([
162
          true, i,
163
          { id: this.repo.id,
164
            datasourceType: this.repo.datasourceType,
165
            datasourceClass: this.repo.datasourceClass,
166
            registeredBy: this.repo.registeredBy
167
          },
168
          this.repoInterfaces[i]
169
        ]);
170
      }
171
    } else {
172
      this.dataForInterfaceComp.push([
173
        true, 0,
174
        { id: this.repo.id,
175
          datasourceType: this.repo.datasourceType,
176
          datasourceClass: this.repo.datasourceClass,
177
          registeredBy: this.repo.registeredBy
178
        }
179
      ]);
180
    }
181
  }
182

    
183
  getCurrentRepo(repo: Repository) {
184
    this.repo = repo;
185
    of (this.fillInterfacesForms()).subscribe(
186
      () => this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=interfaces`)
187
    );
188
  }
189

    
190
  downloadLogo() {
191
    window.open('../../../../assets/imgs/3_0ValidatedLogo.png', '_blank', 'enabledstatus=0,toolbar=0,menubar=0,location=0');
192
  }
193

    
194
  addRepository() {
195
    if (this.repo) {
196
      this.loadingMessage = 'Saving changes';
197
      this.errorMessage = '';
198
      this.repoService.addRepository(this.repo.datasourceType, this.repo).subscribe(
199
        response => {
200
          console.log(`addRepository responded: ${response.id}, ${response.registeredBy}`);
201
          this.repo = response;
202
        },
203
        error => {
204
          console.log(error);
205
          this.loadingMessage = '';
206
          this.errorMessage = formErrorRegisterRepo;
207
        },
208
        () => {
209
          this.saveNewInterfaces();
210
        }
211
      );
212
    }
213
  }
214

    
215
  saveNewInterfaces() {
216
    if (this.repoInterfaces && (this.repoInterfaces.length > 0)) {
217
      from(this.repoInterfaces).pipe(
218
        concatMap(intrf => {
219
          if (intrf.id) {
220
            return this.repoService.updateInterface(this.repo.id, this.repo.registeredBy, intrf);
221
          } else {
222
            return this.repoService.addInterface(this.repo.datasourceType, this.repo.id, this.repo.registeredBy, intrf);
223
          }
224
        })
225
      ).subscribe(
226
        res => console.log('after save interfaces', JSON.stringify(res)),
227
        er => {
228
          console.log(er);
229
          this.loadingMessage = '';
230
          this.errorMessage = 'Not all changes were saved. Please try again';
231
        },
232
        () => {
233
          this.loadingMessage = '';
234
          this.repo = null;
235
          this.repoInterfaces = [];
236
          this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=finish`);
237
        }
238
      );
239
    }
240
  }
241
}
(6-6/10)