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 '../sources-forms/datasource-create-form.component';
8
import { DatasourceNewInterfaceFormComponent } from '../sources-forms/datasource-new-interface-form.component';
9
import { from, of } from 'rxjs';
10
import { concatMap } from 'rxjs/operators';
11
import { 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

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

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

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

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

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

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

    
63

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

    
86
  moveAStep() {
87
    this.errorMessage = '';
88
    if (this.currentStep === 1) {
89
      this.registerDatasource.registerDatasource();
90
    } else if (this.currentStep === 2) {
91
      of(this.getInterfaces()).subscribe(
92
        () => {
93
          if (this.repoInterfaces.length > 0) {
94
            this.addRepository();
95
          } else {
96
            this.errorMessage = noInterfacesSaved;
97
          }
98
        }
99
      );
100
    }
101
  }
102

    
103
  moveBackAStep() {
104
    this.errorMessage = '';
105
    if (this.currentStep === 2) {
106
      of(this.getInterfaces()).subscribe(
107
        () => this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=basicInformation`)
108
      );
109
    }
110
  }
111

    
112
  addInterfaceToList(intrf?: RepositoryInterface) {
113
    const curIndex = this.dataForInterfaceComp.length;
114
    const curRepoInfo = { id: this.repo.id, datasourceType: this.repo.datasourceType,
115
      datasourceClass: this.repo.datasourceClass, registeredBy: this.repo.registeredBy };
116
    if (intrf) {
117
      this.dataForInterfaceComp.push([true, curIndex, curRepoInfo, intrf]);
118
    } else {
119
      this.dataForInterfaceComp.push([true, curIndex, curRepoInfo]);
120
    }
121
  }
122

    
123
  removeInterfaceFromList(i: number) {
124
    const tempArray = this.dataForInterfaceComp;
125
    this.dataForInterfaceComp = [];
126
    tempArray.splice(i, 1);
127
    this.dataForInterfaceComp = tempArray;
128
    console.log(JSON.stringify(this.dataForInterfaceComp));
129
  }
130

    
131
  getInterfaces() {
132
    this.repoInterfaces = [];
133
    for (const el of this.interfacesArray.toArray()) {
134
      const intrf = el.getInterface();
135
      if (intrf) {
136
        this.repoInterfaces.push(intrf);
137
        console.log(JSON.stringify(intrf));
138
      }
139
    }
140
  }
141

    
142
  fillInterfacesForms() {
143
    this.dataForInterfaceComp = [];
144
    if (this.repoInterfaces && (this.repoInterfaces.length > 0)) {
145
      for (let i = 0; i < this.repoInterfaces.length; i++) {
146
        this.dataForInterfaceComp.push([
147
          true, i,
148
          { id: this.repo.id,
149
            datasourceType: this.repo.datasourceType,
150
            datasourceClass: this.repo.datasourceClass,
151
            registeredBy: this.repo.registeredBy
152
          },
153
          this.repoInterfaces[i]
154
        ]);
155
      }
156
    } else {
157
      this.dataForInterfaceComp.push([
158
        true, 0,
159
        { id: this.repo.id,
160
          datasourceType: this.repo.datasourceType,
161
          datasourceClass: this.repo.datasourceClass,
162
          registeredBy: this.repo.registeredBy
163
        }
164
      ]);
165
    }
166
  }
167

    
168
  getCurrentRepo(repo: Repository) {
169
    this.repo = repo;
170
    of (this.fillInterfacesForms()).subscribe(
171
      () => this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=interfaces`)
172
    );
173
  }
174

    
175
  downloadLogo() {
176
    window.open('../../../../assets/imgs/3_0ValidatedLogo.png', '_blank', 'enabledstatus=0,toolbar=0,menubar=0,location=0');
177
  }
178

    
179
  addRepository() {
180
    if (this.repo) {
181
      this.loadingMessage = 'Saving changes';
182
      this.errorMessage = '';
183
      this.repoService.addRepository(this.repo.datasourceType, this.repo).subscribe(
184
        response => {
185
          console.log(`addRepository responded: ${response.id}, ${response.registeredBy}`);
186
          this.repo = response;
187
        },
188
        error => {
189
          console.log(error);
190
          this.loadingMessage = '';
191
          this.errorMessage = formErrorRegisterRepo;
192
        },
193
        () => {
194
          this.saveNewInterfaces();
195
        }
196
      );
197
    }
198
  }
199

    
200
  saveNewInterfaces() {
201
    if (this.repoInterfaces && (this.repoInterfaces.length > 0)) {
202
      from(this.repoInterfaces).pipe(
203
        concatMap(intrf => {
204
          if (intrf.id) {
205
            return this.repoService.updateInterface(this.repo.id, this.repo.registeredBy, intrf);
206
          } else {
207
            return this.repoService.addInterface(this.repo.datasourceType, this.repo.id, this.repo.registeredBy, intrf);
208
          }
209
        })
210
      ).subscribe(
211
        res => console.log('after save interfaces', JSON.stringify(res)),
212
        er => {
213
          console.log(er);
214
          this.loadingMessage = '';
215
          this.errorMessage = 'Not all changes were saved. Please try again';
216
        },
217
        () => {
218
          this.loadingMessage = '';
219
          this.repo = null;
220
          this.repoInterfaces = [];
221
          this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=finish`);
222
        }
223
      );
224
    }
225
  }
226
}
(6-6/10)