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

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

    
26
export class SrDataComponent implements OnInit {
27
  loadingMessage: string;
28
  errorMessage: string;
29

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

    
38
  datasourceId: string;
39
  repo: Repository;
40

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

    
55

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

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

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

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

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

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

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

    
105
  moveBackAStep(){
106
    if(this.showForm) {
107
      this.setQueryParam('selectDatasource');
108
      this.showRepositories = true;
109
      this.showForm = false;
110
      this.step2 = '';
111
    } else if(this.showInterfaces) {
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
    this.getRepoInterfaces();
159
  }
160

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

    
185
  downloadLogo() {
186
    window.open("../../../assets/imgs/3_0ValidatedLogo.png","_blank", "enabledstatus=0,toolbar=0,menubar=0,location=0");
187
  }
188

    
189
  setQueryParam(value: string) {
190
    // set param for step
191
    this.queryParams['step'] = value;
192
    this.router.navigate([], { relativeTo: this.route, queryParams: this.queryParams });
193
    this.rightHelperContent.ngOnInit();
194
    this.topHelperContent.ngOnInit();
195
    this.leftHelperContent.ngOnInit();
196
    this.bottomHelperContent.ngOnInit();
197
  }
198

    
199
}
(6-6/10)