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
  // comments: string;
26

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

    
42
  @ViewChild('registerDatasource', { static: false })
43
  registerDatasource: DatasourceCreateFormComponent;
44

    
45
  @ViewChild('interfaceComments', { static: false })
46
  interfaceComments: DatasourceNewInterfaceFormComponent;
47

    
48
  @ViewChildren('interfacesArray') interfacesArray: QueryList<DatasourceNewInterfaceFormComponent>;
49
  dataForInterfaceComp: any[] = [];
50

    
51
  constructor(private fb: FormBuilder,
52
              private route: ActivatedRoute,
53
              private router: Router,
54
              private repoService: RepositoryService) {}
55

    
56
  // @ViewChild('updateTermsForm')
57

    
58
  ngOnInit() {
59
    if (this.datasourceType) {
60

    
61
      // will execute getStep() every time there is a change in query params
62
      this.route.queryParams.subscribe(
63
        params => {
64
          this.getStep();
65
        }
66
      );
67
    }
68
  }
69

    
70

    
71
  getStep() {
72
    this.currentStep = 1;
73
    if (this.route.snapshot.queryParamMap.has('step')) {
74
      const stepName = this.route.snapshot.queryParamMap.get('step');
75
      if (stepName === 'basicInformation') {
76
        this.currentStep = 1;
77
      } else if (stepName === 'interfaces') {
78
        if (!this.repo) {
79
          this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=basicInformation`);
80
        } else {
81
          this.currentStep = 2;
82
        }
83
      } else if (stepName === 'finish') {
84
        this.currentStep = 3;
85
        // ToU: to enable ToU delete the 2 lines above and uncomment the section below
86
      /*} else if (stepName === 'termsOfUse') {
87
        if (this.interfacesArray && this.interfacesArray.length === 0) {
88
          this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=basicInformation`);
89
        } else {
90
          this.currentStep = 3;
91
        }
92
      } else if (stepName === 'finish') {
93
        this.currentStep = 4;*/
94
      }
95
    }
96
    this.rightHelperContent.ngOnInit();
97
    this.topHelperContent.ngOnInit();
98
    this.leftHelperContent.ngOnInit();
99
    this.bottomHelperContent.ngOnInit();
100
  }
101

    
102
  moveAStep() {
103
    this.errorMessage = '';
104
    if (this.currentStep === 1) {
105
      this.registerDatasource.registerDatasource();
106
    } else if (this.currentStep === 2) {
107
      of(this.getInterfaces()).subscribe(
108
        errors => {
109
          if (errors > 0) {
110
            this.errorMessage = errorsInInterfaces;
111
            window.scrollTo(1, 1);
112
          } else {
113
            if (this.repoInterfaces.length > 0) {
114
              this.addRepository();
115
// ToU: replace above line with the comment below
116
//               this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=termsOfUse`);
117
            } else {
118
              this.errorMessage = noInterfacesSaved;
119
              window.scrollTo(1, 1);
120
            }
121
          }
122
        }
123
      );
124
      // ToU: uncomment these lines
125
    // } else if ( this.currentStep === 3 ) {
126
    //   this.addRepository();
127
    }
128
  }
129

    
130
  moveBackAStep() {
131
    this.errorMessage = '';
132
    if (this.currentStep === 2) {
133
      of(this.getInterfaces()).subscribe(
134
        () => this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=basicInformation`)
135
      );
136
      // ToU: uncomment these lines
137
    // } else if (this.currentStep === 3) {
138
    //   this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=interfaces`);
139
    }
140
  }
141

    
142
  addInterfaceToList(intrf?: RepositoryInterface) {
143
    const curIndex = this.dataForInterfaceComp.length;
144
    const curRepoInfo = { id: this.repo.id, datasourceType: this.repo.datasourceType,
145
      datasourceClass: this.repo.datasourceClass, registeredBy: this.repo.registeredBy };
146
    if (intrf) {
147
      this.dataForInterfaceComp.push([true, curIndex, curRepoInfo, intrf]);
148
    } else {
149
      this.dataForInterfaceComp.push([true, curIndex, curRepoInfo]);
150
    }
151
  }
152

    
153
  removeInterfaceFromList(i: number) {
154
    const tempArray = this.dataForInterfaceComp;
155
    this.dataForInterfaceComp = [];
156
    tempArray.splice(i, 1);
157
    this.dataForInterfaceComp = tempArray;
158
    console.log(JSON.stringify(this.dataForInterfaceComp));
159
  }
160

    
161
  getInterfaces() {
162
    this.repoInterfaces = [];
163
    let invalidFormsCount = 0;
164
    for (const el of this.interfacesArray.toArray()) {
165
      const intrf = el.getInterface();
166
      if (intrf) {
167
        this.repoInterfaces.push(intrf);
168
        console.log(JSON.stringify(intrf));
169
      } else {
170
        invalidFormsCount = invalidFormsCount + 1;
171
        const repo_interface = el.getCurrentValues();
172
        this.repoInterfaces.push(repo_interface);
173
        console.log(JSON.stringify(repo_interface));
174
      }
175
    }
176
    console.log('new interfaces is ', this.repoInterfaces);
177
    return invalidFormsCount;
178
  }
179

    
180
  fillInterfacesForms() {
181
    this.dataForInterfaceComp = [];
182
    if (this.repoInterfaces && (this.repoInterfaces.length > 0)) {
183
      for (let i = 0; i < this.repoInterfaces.length; i++) {
184
        this.dataForInterfaceComp.push([
185
          true, i,
186
          { id: this.repo.id,
187
            datasourceType: this.repo.datasourceType,
188
            datasourceClass: this.repo.datasourceClass,
189
            registeredBy: this.repo.registeredBy
190
          },
191
          this.repoInterfaces[i]
192
        ]);
193
      }
194
    } else {
195
      this.dataForInterfaceComp.push([
196
        true, 0,
197
        { id: this.repo.id,
198
          datasourceType: this.repo.datasourceType,
199
          datasourceClass: this.repo.datasourceClass,
200
          registeredBy: this.repo.registeredBy
201
        }
202
      ]);
203
    }
204
  }
205

    
206
  getCurrentRepo(repo: Repository) {
207
    this.repo = repo;
208
    of (this.fillInterfacesForms()).subscribe(
209
      () => this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=interfaces`)
210
    );
211
  }
212

    
213
  downloadLogo() {
214
    window.open('../../../../assets/imgs/3_0ValidatedLogo.png', '_blank', 'enabledstatus=0,toolbar=0,menubar=0,location=0');
215
  }
216

    
217
  addRepository() {
218
    if (this.repo) {
219
      this.loadingMessage = 'Saving changes';
220
      this.errorMessage = '';
221
      this.repoService.addRepository(this.repo.datasourceType, this.repo).subscribe(
222
        response => {
223
          console.log(`addRepository responded: ${response.id}, ${response.registeredBy}`);
224
          this.repo = response;
225
        },
226
        error => {
227
          console.log(error);
228
          this.loadingMessage = '';
229
          this.errorMessage = formErrorRegisterRepo;
230
        },
231
        () => {
232
          this.saveNewInterfaces();
233
          // TODO: update terms when backend is ready, maybe POST with updateRepository
234
        }
235
      );
236
    }
237
  }
238

    
239
  saveNewInterfaces() {
240
    if (this.repoInterfaces && (this.repoInterfaces.length > 0)) {
241
      from(this.repoInterfaces).pipe(
242
        concatMap(intrf => {
243
          if (intrf.id) {
244
            // console.log('comments', intrf.comments);
245
            return this.repoService.updateInterface(this.repo.id, this.repo.registeredBy, intrf.comments, intrf);
246
          } else {
247
            // console.log('comments', intrf.comments);
248
            return this.repoService.addInterface(this.repo.datasourceType, this.repo.id, this.repo.registeredBy, intrf.comments, intrf);
249
          }
250
        })
251
      ).subscribe(
252
        res => console.log('after save interfaces', JSON.stringify(res)),
253
        er => {
254
          console.log(er);
255
          this.loadingMessage = '';
256
          this.errorMessage = 'Not all changes were saved. Please try again';
257
        },
258
        () => {
259
          this.loadingMessage = '';
260
          this.repo = null;
261
          this.repoInterfaces = [];
262
          this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=finish`);
263
        }
264
      );
265
    }
266
  }
267
}
(6-6/10)