Project

General

Profile

1 54479 myrto.kouk
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 58098 stefania.m
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 54479 myrto.kouk
import { from, of } from 'rxjs';
10
import { concatMap } from 'rxjs/operators';
11 55618 myrto.kouk
import { errorsInInterfaces, formErrorRegisterRepo, noInterfacesSaved } from '../../../domain/shared-messages';
12 62103 antonis.le
import {DatasourceUpdateTermsFormComponent} from '../../../shared/reusablecomponents/sources-forms/datasource-update-terms-form.component';
13 54479 myrto.kouk
14
@Component({
15
  selector: 'app-register-new-datasource',
16
  templateUrl: './register-new-datasource.component.html'
17
})
18 62103 antonis.le
19 54479 myrto.kouk
export class RegisterNewDatasourceComponent implements OnInit {
20
  loadingMessage: string;
21
  errorMessage: string;
22
23
  datasourceType: string;
24
  repo: Repository = null;
25
  repoInterfaces: RepositoryInterface[] = [];
26 55618 myrto.kouk
  interfacesToDelete: string[] = [];
27 59207 andreas.ma
  // comments: string;
28 54479 myrto.kouk
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 61424 stefania.m
  @ViewChild('topHelperContent', { static: true })
36 54479 myrto.kouk
  public topHelperContent: HelpContentComponent;
37 61424 stefania.m
  @ViewChild('leftHelperContent', { static: true })
38 54479 myrto.kouk
  public leftHelperContent: AsideHelpContentComponent;
39 61424 stefania.m
  @ViewChild('rightHelperContent', { static: true })
40 54479 myrto.kouk
  public rightHelperContent: AsideHelpContentComponent;
41 61424 stefania.m
  @ViewChild('bottomHelperContent', { static: true })
42 54479 myrto.kouk
  public bottomHelperContent: HelpContentComponent;
43
44 61424 stefania.m
  @ViewChild('registerDatasource')
45 54479 myrto.kouk
  registerDatasource: DatasourceCreateFormComponent;
46
47 61424 stefania.m
  @ViewChild('interfaceComments')
48 59207 andreas.ma
  interfaceComments: DatasourceNewInterfaceFormComponent;
49
50 54479 myrto.kouk
  @ViewChildren('interfacesArray') interfacesArray: QueryList<DatasourceNewInterfaceFormComponent>;
51
  dataForInterfaceComp: any[] = [];
52
53 62080 stefania.m
  @ViewChild('updateTermsForm')
54
  updateTermsForm: DatasourceUpdateTermsFormComponent;
55
56 54479 myrto.kouk
  constructor(private fb: FormBuilder,
57
              private route: ActivatedRoute,
58
              private router: Router,
59
              private repoService: RepositoryService) {}
60
61
  ngOnInit() {
62
    if (this.datasourceType) {
63 55584 myrto.kouk
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 54479 myrto.kouk
    }
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 55585 myrto.kouk
          this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=basicInformation`);
83 54479 myrto.kouk
        } else {
84
          this.currentStep = 2;
85
        }
86 62080 stefania.m
      } else if (stepName === 'termsOfUse') {
87 58412 andreas.ma
        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 62080 stefania.m
        this.currentStep = 4;
94 54479 myrto.kouk
      }
95
    }
96 55585 myrto.kouk
    this.rightHelperContent.ngOnInit();
97
    this.topHelperContent.ngOnInit();
98
    this.leftHelperContent.ngOnInit();
99
    this.bottomHelperContent.ngOnInit();
100 54479 myrto.kouk
  }
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 55618 myrto.kouk
        errors => {
109
          if (errors > 0) {
110
            this.errorMessage = errorsInInterfaces;
111
            window.scrollTo(1, 1);
112 54479 myrto.kouk
          } else {
113 55618 myrto.kouk
            if (this.repoInterfaces.length > 0) {
114 62080 stefania.m
              this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=termsOfUse`);
115 55618 myrto.kouk
            } else {
116
              this.errorMessage = noInterfacesSaved;
117
              window.scrollTo(1, 1);
118
            }
119 54479 myrto.kouk
          }
120
        }
121
      );
122 62080 stefania.m
    } else if ( this.currentStep === 3 ) {
123
      this.addRepository();
124 54479 myrto.kouk
    }
125
  }
126
127
  moveBackAStep() {
128
    this.errorMessage = '';
129
    if (this.currentStep === 2) {
130
      of(this.getInterfaces()).subscribe(
131 55585 myrto.kouk
        () => this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=basicInformation`)
132 54479 myrto.kouk
      );
133 62080 stefania.m
    } else if (this.currentStep === 3) {
134
      this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=interfaces`);
135 54479 myrto.kouk
    }
136
  }
137
138
  addInterfaceToList(intrf?: RepositoryInterface) {
139
    const curIndex = this.dataForInterfaceComp.length;
140 62388 andreas.ma
    const curRepoInfo = { id: this.repo.id, datasourceType: this.repo.eoscDatasourceType,
141
      datasourceClass: this.repo.eoscDatasourceType, registeredBy: this.repo.registeredby };
142 54479 myrto.kouk
    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 55618 myrto.kouk
    let invalidFormsCount = 0;
160 54479 myrto.kouk
    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 55618 myrto.kouk
      } 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 54479 myrto.kouk
      }
171
    }
172 55618 myrto.kouk
    console.log('new interfaces is ', this.repoInterfaces);
173
    return invalidFormsCount;
174 54479 myrto.kouk
  }
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 62388 andreas.ma
            datasourceType: this.repo.eoscDatasourceType,
184 62341 andreas.ma
            datasourceClass: this.repo.eoscDatasourceType,
185 62388 andreas.ma
            registeredBy: this.repo.registeredby
186 54479 myrto.kouk
          },
187
          this.repoInterfaces[i]
188
        ]);
189
      }
190
    } else {
191
      this.dataForInterfaceComp.push([
192
        true, 0,
193
        { id: this.repo.id,
194 62388 andreas.ma
          datasourceType: this.repo.eoscDatasourceType,
195 62341 andreas.ma
          datasourceClass: this.repo.eoscDatasourceType,
196 62388 andreas.ma
          registeredBy: this.repo.registeredby
197 54479 myrto.kouk
        }
198
      ]);
199
    }
200
  }
201
202
  getCurrentRepo(repo: Repository) {
203
    this.repo = repo;
204
    of (this.fillInterfacesForms()).subscribe(
205 55585 myrto.kouk
      () => this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=interfaces`)
206 54479 myrto.kouk
    );
207
  }
208
209 62103 antonis.le
  // recheck if needed
210 62080 stefania.m
  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 54479 myrto.kouk
  downloadLogo() {
217
    window.open('../../../../assets/imgs/3_0ValidatedLogo.png', '_blank', 'enabledstatus=0,toolbar=0,menubar=0,location=0');
218
  }
219
220
  addRepository() {
221 62080 stefania.m
    console.log('in addRepository, step ===', this.currentStep);
222 54479 myrto.kouk
    if (this.repo) {
223
      this.loadingMessage = 'Saving changes';
224
      this.errorMessage = '';
225 62080 stefania.m
      console.log('add this.repo', this.repo);
226 62388 andreas.ma
      this.repoService.addRepository(this.repo.eoscDatasourceType, this.repo).subscribe(
227 54479 myrto.kouk
        response => {
228 62388 andreas.ma
          console.log(`addRepository responded: ${response.id}, ${response.registeredby}`);
229 54479 myrto.kouk
          this.repo = response;
230
        },
231
        error => {
232
          console.log(error);
233
          this.loadingMessage = '';
234 54936 myrto.kouk
          this.errorMessage = formErrorRegisterRepo;
235 54479 myrto.kouk
        },
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 60937 andreas.ma
            // console.log('comments', intrf.comments);
249 62388 andreas.ma
            return this.repoService.updateInterface(this.repo.id, this.repo.registeredby, intrf.comments, intrf);
250 54479 myrto.kouk
          } else {
251 60937 andreas.ma
            // console.log('comments', intrf.comments);
252 62388 andreas.ma
            return this.repoService.addInterface(this.repo.eoscDatasourceType, this.repo.id, this.repo.registeredby, intrf.comments, intrf);
253 54479 myrto.kouk
          }
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 55584 myrto.kouk
          this.repo = null;
265
          this.repoInterfaces = [];
266 55585 myrto.kouk
          this.router.navigateByUrl(`/sources/register/${this.datasourceType}?step=finish`);
267 54479 myrto.kouk
        }
268
      );
269
    }
270
  }
271
}