Project

General

Profile

1
import { Component, OnInit, Type, ViewChild } from '@angular/core';
2
import { DatasourceInterfaceFormComponent } from '../sources-forms/datasource-interface-form.component';
3
import { Description, interfaceFormDesc } from '../../../domain/oa-description';
4
import { FormBuilder, FormGroup } from '@angular/forms';
5
import {Repository, RepositoryInterface} from '../../../domain/typeScriptClasses';
6
import { DatasourceCreateFormComponent } from '../sources-forms/datasource-create-form.component';
7
import {ActivatedRoute, Params, Router} from "@angular/router";
8
import {MyArray} from "../../../shared/reusablecomponents/forms/my-array.interface";
9
import {noInterfacesSaved} from "../../../domain/shared-messages";
10
import {
11
  AsideHelpContentComponent,
12
  HelpContentComponent
13
} from "../../../shared/reusablecomponents/help-content.component";
14
import {ConfirmationDialogComponent} from "../../../shared/reusablecomponents/confirmation-dialog.component";
15
import {RepositoryService} from "../../../services/repository.service";
16
import {AuthenticationService} from "../../../services/authentication.service";
17

    
18
@Component ({
19
  selector: 'sr-aggregator',
20
  templateUrl: 'sr-aggregator.component.html'
21
})
22

    
23
export class SrAggregatorComponent implements OnInit {
24
  loadingMessage: string;
25
  errorMessage: string;
26

    
27
  showForm: boolean;
28
  showInterfaces: boolean;
29
  showFinish: boolean;
30
  step2: string = '';
31
  step3: string = '';
32

    
33
  repo: Repository = null;
34
  repoInterfaces: RepositoryInterface[] = [];
35

    
36
  /* queryParams is used to change the queryParams without refreshing the page
37
   * This was needed for Help Service [which sends back info according to the current router.url]
38
   * the param that is used is 'step' and the values are: 'basicInformation','interfaces','finish'
39
   */
40
  queryParams: Params = Object.assign({}, this.route.snapshot.queryParams);
41
  @ViewChild('topHelperContent')
42
  public topHelperContent: HelpContentComponent;
43
  @ViewChild('leftHelperContent')
44
  public leftHelperContent: AsideHelpContentComponent;
45
  @ViewChild('rightHelperContent')
46
  public rightHelperContent: AsideHelpContentComponent;
47
  @ViewChild('bottomHelperContent')
48
  public bottomHelperContent: HelpContentComponent;
49

    
50
  @ViewChild ('registerAggregator')
51
  registerAggregator: DatasourceCreateFormComponent;
52

    
53
  @ViewChild('interfaceFormArray')
54
  public interfaceFormArray: MyArray;
55

    
56
  group: FormGroup;
57
  interfaceFormDesc: Description = interfaceFormDesc;
58
  addDatasourceInterfaces: Type<any> = DatasourceInterfaceFormComponent;
59

    
60
  constructor(
61
    private fb: FormBuilder,
62
    private route: ActivatedRoute,
63
    private router: Router,
64
    private repoService: RepositoryService) {}
65

    
66
  ngOnInit() {
67
    this.setQueryParam('basicInformation');
68
    this.showForm = true;
69
  }
70

    
71
  moveAStep(){
72
    this.errorMessage = '';
73
    if (this.showForm) {
74
      this.registerAggregator.registerDatasource();
75
    } else if (this.showInterfaces) {
76
      if (this.interfaceFormArray.checkIfOneElementExists()) {
77
        this.addRepository();
78
      } else {
79
        this.errorMessage = noInterfacesSaved;
80
      }
81
    }
82
  }
83

    
84
  moveBackAStep(){
85
    this.errorMessage = '';
86
    if (this.showInterfaces) {
87
      this.interfaceFormArray.emitExportedDataArray();
88
      this.setQueryParam('basicInformation');
89
      this.showForm = true;
90
      this.showInterfaces = false;
91
      this.step2 = '';
92
    } else if (this.showFinish) {
93
      this.setQueryParam('interfaces');
94
      this.showInterfaces = true;
95
      this.showFinish = false;
96
      this.step3 = '';
97
    }
98
  }
99

    
100
  getCurrentRepo(repo: Repository) {
101
    this.repo = repo;
102
    this.setQueryParam('interfaces');
103
    this.showForm = false;
104
    this.showInterfaces = true;
105
    this.step2 = 'active';
106
    this.group = this.fb.group({});
107
  }
108

    
109
  downloadLogo() {
110
    window.open("../../../assets/imgs/3_0ValidatedLogo.png","_blank", "enabledstatus=0,toolbar=0,menubar=0,location=0");
111
  }
112

    
113
  setQueryParam(value: string) {
114
    // set param for step
115
    this.queryParams['step'] = value;
116
    this.router.navigate([], { relativeTo: this.route, queryParams: this.queryParams });
117
    this.rightHelperContent.ngOnInit();
118
    this.topHelperContent.ngOnInit();
119
    this.leftHelperContent.ngOnInit();
120
    this.bottomHelperContent.ngOnInit();
121
  }
122

    
123
  addRepository() {
124
    if (this.repo) {
125
      this.loadingMessage = 'Saving changes';
126
      this.errorMessage = '';
127
      this.repoService.addRepository(this.repo.datasourceType, this.repo).subscribe(
128
        response => {
129
          console.log(`addRepository responded: ${response.id}, ${response.registeredBy}`);
130
          this.repo = response;
131
        },
132
        error => {
133
          console.log(error);
134
          this.loadingMessage = '';
135
          this.errorMessage = 'The changes could not be saved';
136
        },
137
        () => {
138
          this.saveNewInterfaces();
139
        }
140
      );
141
    }
142
  }
143

    
144
  saveNewInterfaces() {
145
    if (this.repoInterfaces) {
146
      let failed: boolean = false;
147
      for (let intrf of this.repoInterfaces) {
148
        if (intrf.id) {
149
          this.repoService.updateInterface(this.repo.id, this.repo.registeredBy, intrf).subscribe(
150
            response => {
151
              console.log(`updateInterface responded ${JSON.stringify(response)}`);
152
              intrf = response;
153
            },
154
            error => {
155
              console.log(error);
156
              failed = true;
157
            }
158
          );
159
        } else {
160
          this.repoService.addInterface(this.repo.datasourceType, this.repo.id, this.repo.registeredBy, intrf).subscribe (
161
            addedInterface => {
162
              console.log(`addInterface responded ${JSON.stringify(addedInterface)}`);
163
              intrf = addedInterface;
164
            },
165
            error => {
166
              console.log(error);
167
              failed = true;
168
            }
169
          );
170
        }
171
        if (failed) {
172
          break;
173
        }
174
      }
175
      this.loadingMessage = '';
176
      if (failed) {
177
        this.errorMessage = 'The changes could not be saved. Please try again';
178
      } else {
179
        this.setQueryParam('finish');
180
        this.showInterfaces = false;
181
        this.showFinish = true;
182
        this.step3 = 'active';
183
      }
184
    }
185
  }
186

    
187
  getNewInterfaces (interfaces: RepositoryInterface[]) {
188
    this.repoInterfaces = interfaces;
189
    console.log('new interfaces is ',this.repoInterfaces);
190
  }
191

    
192

    
193
}
(4-4/10)