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
import {DatasourceUpdateTermsFormComponent} from '../../../shared/reusablecomponents/sources-forms/datasource-update-terms-form.component';
13

    
14
@Component({
15
  selector: 'app-register-new-datasource',
16
  templateUrl: './register-new-datasource.component.html'
17
})
18

    
19
export class RegisterNewDatasourceComponent implements OnInit {
20
  loadingMessage: string;
21
  errorMessage: string;
22

    
23
  datasourceType: string;
24
  repo: Repository = null;
25
  repoInterfaces: RepositoryInterface[] = [];
26
  interfacesToDelete: string[] = [];
27
  // comments: string;
28

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

    
44
  @ViewChild('registerDatasource')
45
  registerDatasource: DatasourceCreateFormComponent;
46

    
47
  @ViewChild('interfaceComments')
48
  interfaceComments: DatasourceNewInterfaceFormComponent;
49

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

    
53
  @ViewChild('updateTermsForm')
54
  updateTermsForm: DatasourceUpdateTermsFormComponent;
55

    
56
  constructor(private fb: FormBuilder,
57
              private route: ActivatedRoute,
58
              private router: Router,
59
              private repoService: RepositoryService) {}
60

    
61
  ngOnInit() {
62
    if (this.datasourceType) {
63

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

    
73

    
74
  getStep() {
75
    this.currentStep = 1;
76
    if (this.route.snapshot.queryParamMap.has('step')) {
77
      const stepName = this.route.snapshot.queryParamMap.get('step');
78
      if (stepName === 'basicInformation') {
79
        this.currentStep = 1;
80
      } else if (stepName === 'interfaces') {
81
        if (!this.repo) {
82
          this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=basicInformation`);
83
        } else {
84
          this.currentStep = 2;
85
        }
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.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=termsOfUse`);
115
            } else {
116
              this.errorMessage = noInterfacesSaved;
117
              window.scrollTo(1, 1);
118
            }
119
          }
120
        }
121
      );
122
    } else if ( this.currentStep === 3 ) {
123
      this.addRepository();
124
    }
125
  }
126

    
127
  moveBackAStep() {
128
    this.errorMessage = '';
129
    if (this.currentStep === 2) {
130
      of(this.getInterfaces()).subscribe(
131
        () => this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=basicInformation`)
132
      );
133
    } else if (this.currentStep === 3) {
134
      this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=interfaces`);
135
    }
136
  }
137

    
138
  addInterfaceToList(intrf?: RepositoryInterface) {
139
    const curIndex = this.dataForInterfaceComp.length;
140
    const curRepoInfo = { id: this.repo.id, datasourceType: this.repo.eoscDatasourceType,
141
      datasourceClass: this.repo.eoscDatasourceType, registeredBy: this.repo.registeredby };
142
    if (intrf) {
143
      this.dataForInterfaceComp.push([true, curIndex, curRepoInfo, intrf]);
144
    } else {
145
      this.dataForInterfaceComp.push([true, curIndex, curRepoInfo]);
146
    }
147
  }
148

    
149
  removeInterfaceFromList(i: number) {
150
    const tempArray = this.dataForInterfaceComp;
151
    this.dataForInterfaceComp = [];
152
    tempArray.splice(i, 1);
153
    this.dataForInterfaceComp = tempArray;
154
    console.log(JSON.stringify(this.dataForInterfaceComp));
155
  }
156

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

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

    
202
  getCurrentRepo(repo: Repository) {
203
    this.repo = repo;
204
    of (this.fillInterfacesForms()).subscribe(
205
      () => this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=interfaces`)
206
    );
207
  }
208

    
209
  // recheck if needed
210
  getTerms(repo: Repository) {
211
    console.log('this.repo', this.repo.consentTermsOfUse, this.repo.fullTextDownload);
212
    console.log('repo', repo.consentTermsOfUse, repo.fullTextDownload);
213
    this.repo = repo;
214
  }
215

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

    
220
  addRepository() {
221
    console.log('in addRepository, step ===', this.currentStep);
222
    if (this.repo) {
223
      this.loadingMessage = 'Saving changes';
224
      this.errorMessage = '';
225
      console.log('add this.repo', this.repo);
226
      this.repoService.addRepository(this.repo.eoscDatasourceType, this.repo).subscribe(
227
        response => {
228
          console.log(`addRepository responded: ${response.id}, ${response.registeredby}`);
229
          this.repo = response;
230
        },
231
        error => {
232
          console.log(error);
233
          this.loadingMessage = '';
234
          this.errorMessage = formErrorRegisterRepo;
235
        },
236
        () => {
237
          this.saveNewInterfaces();
238
        }
239
      );
240
    }
241
  }
242

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