Project

General

Profile

1
/*
2
*  created by myrto on 12/12/2017
3
*/
4

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

    
23
@Component ({
24
  selector: 'app-sr-data',
25
  templateUrl: 'sr-data.component.html'
26
})
27

    
28
export class SrDataComponent implements OnInit {
29
  loadingMessage: string;
30
  errorMessage: string;
31

    
32
  showRepositories: boolean;
33
  showForm: boolean;
34
  showInterfaces: boolean;
35
  showFinish: boolean;
36
  step2: string = '';
37
  step3: string = '';
38
  step4: string = '';
39

    
40
  datasourceId: string;
41
  repo: Repository;
42

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

    
57
  @ViewChild('datasourcesByCountry')
58
  public datasourcesByCountry: RegisterDatasourceShareableComponent;
59

    
60
  @ViewChild('updateDatasource')
61
  public updateDatasource: DatasourceUpdateFormComponent;
62

    
63
  @ViewChild('interfaceFormArray')
64
  public interfaceFormArray: MyArray;
65

    
66

    
67
  group: FormGroup;
68
  interfaceFormDesc: Description = interfaceFormDesc;
69
  updateDatasourceInterfaces: Type<any> = DatasourceInterfaceFormComponent;
70
  repoInterfaces: RepositoryInterface[] = [];
71

    
72
  constructor(
73
    private fb: FormBuilder,
74
    private route: ActivatedRoute,
75
    private router: Router,
76
    private repoService: RepositoryService) {}
77

    
78
  ngOnInit() {
79
    this.setQueryParam('selectDatasource');
80
    this.showRepositories=true;
81
  }
82

    
83
  moveAStep(){
84
    this.errorMessage = '';
85
    if(this.showRepositories) {
86
      if (this.datasourcesByCountry.goToNextStep()) {
87
        this.setQueryParam('basicInformation');
88
        this.showRepositories = false;
89
        this.showForm = true;
90
        this.step2 = 'active';
91
        console.log(`got datasource with id ${this.datasourceId}`);
92
      }
93
    } else if(this.showForm) {
94
        this.updateDatasource.updateRepo();
95
    } else if(this.showInterfaces) {
96
      if (this.interfaceFormArray.checkIfOneElementExists()) {
97
        this.updateRepository();
98
      } else {
99
        this.errorMessage = noInterfacesSaved;
100
      }
101
    }
102
  }
103

    
104
  moveBackAStep(){
105
    if(this.showForm) {
106
      this.setQueryParam('selectDatasource');
107
      this.showRepositories = true;
108
      this.showForm = false;
109
      this.step2 = '';
110
    } else if(this.showInterfaces) {
111
      this.interfaceFormArray.emitExportedDataArray();
112
      this.setQueryParam('basicInformation');
113
      this.showForm = true;
114
      this.showInterfaces = false;
115
      this.step3 = '';
116
    } else if(this.showFinish) {
117
      this.setQueryParam('interfaces');
118
      this.showInterfaces = true;
119
      this.showFinish = false;
120
      this.step4 = '';
121
    }
122
  }
123

    
124
  goToStep2(emitted: boolean) {
125
    if (emitted) {
126
      this.moveAStep();
127
    }
128
  }
129

    
130
  getRepoId(emitedId: string) {
131
    this.datasourceId = emitedId;
132
    this.getRepo();
133
  }
134

    
135
  getRepo() {
136
    this.loadingMessage = formInfoLoading;
137
    if (this.datasourceId) {
138
      this.repoService.getRepositoryById(this.datasourceId).subscribe(
139
        repo => {
140
          this.repo = repo;
141
        },
142
        error => {
143
          console.log(error);
144
          this.loadingMessage = '';
145
          this.errorMessage = loadingRepoError;
146
        },
147
        () => {
148
          this.loadingMessage = '';
149
        }
150
      );
151
    }
152
  }
153

    
154
  getUpdatedRepo(repo: Repository){
155
    this.repo = repo;
156
    console.log(`repo was updated!`);
157
    this.group = this.fb.group({});
158
    if (this.repoInterfaces.length == 0) {
159
      this.getRepoInterfaces();
160
    } else {
161
      this.setQueryParam('interfaces');
162
      this.showForm = false;
163
      this.showInterfaces = true;
164
      this.step3 = 'active';
165
    }
166
  }
167

    
168
  getRepoInterfaces() {
169
    this.repoService.getRepositoryInterface(this.datasourceId).subscribe(
170
      interfaces => {
171
        this.repoInterfaces = interfaces.sort( function(a,b) {
172
          if (a.id<b.id) {
173
            return -1;
174
          } else if (a.id>b.id) {
175
            return 1;
176
          } else {
177
            return 0;
178
          }
179
        });
180
        console.log(`the number of interfaces is ${this.repoInterfaces.length}`);
181
      },
182
      error => console.log(error),
183
      () => {
184
        this.setQueryParam('interfaces');
185
        this.showForm = false;
186
        this.showInterfaces = true;
187
        this.step3 = 'active';
188
      }
189
    );
190
  }
191

    
192

    
193
  downloadLogo() {
194
    window.open("../../../assets/imgs/3_0ValidatedLogo.png","_blank", "enabledstatus=0,toolbar=0,menubar=0,location=0");
195
  }
196

    
197
  setQueryParam(value: string) {
198
    // set param for step
199
    this.queryParams['step'] = value;
200
    this.router.navigate([], { relativeTo: this.route, queryParams: this.queryParams });
201
    this.rightHelperContent.ngOnInit();
202
    this.topHelperContent.ngOnInit();
203
    this.leftHelperContent.ngOnInit();
204
    this.bottomHelperContent.ngOnInit();
205
  }
206

    
207
  updateRepository() {
208
    this.loadingMessage = 'Saving changes';
209
    this.errorMessage = '';
210
    this.repoService.updateRepository(this.repo).subscribe (
211
      response => {
212
        if (response) {
213
          this.repo = response;
214
          console.log(`updateRepository responded: ${response.id}, ${response.registeredBy}`);
215
        }
216
      },
217
      error => {
218
        console.log(error);
219
        this.loadingMessage = '';
220
        this.errorMessage = 'The changes could not be saved';
221
      },
222
      () => {
223
        this.saveNewInterfaces();
224
      }
225
    );
226
  }
227

    
228
  saveNewInterfaces() {
229
    if (this.repoInterfaces) {
230
      let failed: boolean = false;
231
      for (let intrf of this.repoInterfaces) {
232
        if (intrf.id) {
233
          this.repoService.updateInterface(this.repo.id, this.repo.registeredBy, intrf).subscribe(
234
            response => {
235
              console.log(`updateRepository responded ${JSON.stringify(response)}`);
236
              intrf = response;
237
            },
238
            error => {
239
              console.log(error);
240
              failed = true;
241
            }
242
          );
243
        } else {
244
          this.repoService.addInterface(this.repo.datasourceType, this.repo.id, this.repo.registeredBy, intrf).subscribe (
245
            addedInterface => {
246
              console.log(`addInterface responded ${JSON.stringify(addedInterface)}`);
247
              intrf = addedInterface;
248
            },
249
            error => {
250
              console.log(error);
251
              failed = true;
252
            }
253
          );
254
        }
255
        if (failed) {
256
          break;
257
        }
258
      }
259
      this.loadingMessage = '';
260
      if (failed) {
261
        this.errorMessage = 'The changes could not be saved. Please try again';
262
      } else {
263
        this.setQueryParam('finish');
264
        this.showInterfaces = false;
265
        this.showFinish = true;
266
        this.step4 = 'active';
267
      }
268
    }
269
  }
270

    
271
  getNewInterfaces (interfaces: RepositoryInterface[]) {
272
    this.repoInterfaces = interfaces;
273
    console.log('new interfaces is ',this.repoInterfaces);
274
  }
275

    
276
}
(6-6/10)