Project

General

Profile

1
import { Component, OnInit, Type, ViewChild } from '@angular/core';
2
import { FormBuilder, FormGroup } from '@angular/forms';
3
import { Description, interfaceFormDesc } from '../../../domain/oa-description';
4
import { DatasourceInterfaceFormComponent } from '../sources-forms/datasource-interface-form.component';
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 {RepositoryService} from "../../../services/repository.service";
15
import {ConfirmationDialogComponent} from "../../../shared/reusablecomponents/confirmation-dialog.component";
16
import {AuthenticationService} from "../../../services/authentication.service";
17

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

    
23
export class SrJournalComponent 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 ('registerJournal')
51
  registerJournal: 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
    private authService: AuthenticationService) {}
66

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

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

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

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

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

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

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

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

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

    
193

    
194
}
(8-8/10)