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 { Repository, RepositoryInterface } from '../../../domain/typeScriptClasses';
7
import { DatasourceUpdateFormComponent } from '../sources-forms/datasource-update-form.component';
8
import { RegisterDatasourceShareableComponent } from './register-datasource-shareable.component';
9
import { DatasourceInterfaceFormComponent } from '../sources-forms/datasource-interface-form.component';
10
import { FormBuilder, FormGroup } from '@angular/forms';
11
import { Description, interfaceFormDesc } from '../../../domain/oa-description';
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

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

    
22
export class SrLiteratureComponent implements OnInit {
23
  loadingMessage: string;
24
  errorMessage: string;
25

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

    
34
  datasourceId: string;
35
  repo: Repository;
36

    
37
  /* queryParams is used to change the queryParams without refreshing the page
38
   * This was needed for Help Service [which sends back info according to the current router.url]
39
   * the param that is used is 'step' and the values are: 'selectDatasource','basicInformation','interfaces','finish'
40
   */
41
  queryParams: Params = Object.assign({}, this.route.snapshot.queryParams);
42

    
43
  @ViewChild('datasourcesByCountry')
44
  public datasourcesByCountry: RegisterDatasourceShareableComponent;
45

    
46
  @ViewChild('updateDatasource')
47
  public updateDatasource: DatasourceUpdateFormComponent;
48

    
49
  @ViewChild('interfaceFormArray')
50
    public interfaceFormArray: MyArray;
51

    
52
  group: FormGroup;
53
  interfaceFormDesc: Description = interfaceFormDesc;
54
  updateDatasourceInterfaces: Type<any> = DatasourceInterfaceFormComponent;
55
  repoInterfaces: RepositoryInterface[] = [];
56

    
57
  constructor(
58
    private fb: FormBuilder,
59
    private route: ActivatedRoute,
60
    private router: Router,
61
    private repoService: RepositoryService) {}
62

    
63
  ngOnInit() {
64
    this.setQueryParam('selectDatasource');
65
    this.showRepositories=true;
66
  }
67

    
68
  moveAStep(){
69
    this.errorMessage = '';
70
    if(this.showRepositories) {
71
      if (this.datasourcesByCountry.goToNextStep()) {
72
        this.setQueryParam('basicInformation');
73
        this.showRepositories = false;
74
        this.showForm = true;
75
        this.step2 = 'active';
76
        console.log(`got datasource with id ${this.datasourceId}`);
77
      }
78
    } else if(this.showForm) {
79
        this.updateDatasource.updateRepo();
80
    } else if(this.showInterfaces) {
81
      if (this.interfaceFormArray.checkIfOneElementExists()) {
82
        this.setQueryParam('finish');
83
        this.showInterfaces = false;
84
        this.showFinish = true;
85
        this.step4 = 'active';
86
      } else {
87
        this.errorMessage = noInterfacesSaved;
88
      }
89
    }
90
  }
91

    
92
  moveBackAStep(){
93
    if(this.showForm) {
94
      this.setQueryParam('baseUrl');
95
      this.showRepositories = true;
96
      this.showForm = false;
97
      this.step2 = '';
98
    } else if(this.showInterfaces) {
99
      this.setQueryParam('basicInformation');
100
      this.showForm = true;
101
      this.showInterfaces = false;
102
      this.step3 = '';
103
    } else if(this.showFinish) {
104
      this.setQueryParam('interfaces');
105
      this.showInterfaces = true;
106
      this.showFinish = false;
107
      this.step4 = '';
108
    }
109
  }
110

    
111
  goToStep2(emitted: boolean) {
112
    if (emitted) {
113
      this.moveAStep();
114
    }
115
  }
116

    
117
  getRepoId(emitedId: string) {
118
    this.datasourceId = emitedId;
119
    this.getRepo();
120
  }
121

    
122
  getRepo() {
123
    this.loadingMessage = formInfoLoading;
124
    if (this.datasourceId) {
125
      this.repoService.getRepositoryById(this.datasourceId).subscribe(
126
        repo => {
127
          this.repo = repo;
128
        },
129
        error => {
130
          console.log(error);
131
          this.loadingMessage = '';
132
          this.errorMessage = loadingRepoError;
133
        },
134
        () => {
135
          this.loadingMessage = '';
136
        }
137
      );
138
    }
139
  }
140

    
141
  getUpdatedRepo(repo: Repository){
142
    this.repo = repo;
143
    console.log(`repo was updated!`);
144
    this.group = this.fb.group({});
145
    this.getRepoInterfaces();
146
  }
147

    
148
  getRepoInterfaces() {
149
    this.repoService.getRepositoryInterface(this.datasourceId).subscribe(
150
      interfaces => {
151
        this.repoInterfaces = interfaces.sort( function(a,b) {
152
          if(a.id<b.id){
153
            return -1;
154
          } else if(a.id>b.id){
155
            return 1;
156
          } else {
157
            return 0;
158
          }
159
        });
160
        console.log(`the number of interfaces is ${this.repoInterfaces.length}`);
161
      },
162
      error => console.log(error),
163
      () => {
164
        this.setQueryParam('interfaces');
165
        this.showForm = false;
166
        this.showInterfaces = true;
167
        this.step3 = 'active';
168
      }
169
    );
170
  }
171

    
172
  downloadLogo() {
173
    window.open("../../../assets/imgs/3_0ValidatedLogo.png","_blank", "enabledstatus=0,toolbar=0,menubar=0,location=0");
174
  }
175

    
176
  setQueryParam(value: string) {
177
    // set param for step
178
    this.queryParams['step'] = value;
179
    this.router.navigate([], { relativeTo: this.route, queryParams: this.queryParams });
180
  }
181

    
182
}
(10-10/10)