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 {
14
  formErrorWasntSaved,
15
  formInfoLoading, formSubmitting, formSuccessAddedInterface, formSuccessUpdatedInterface, formSuccessUpdatedRepo,
16
  loadingRepoError,
17
  noInterfacesSaved
18
} from '../../../domain/shared-messages';
19
import {ActivatedRoute, Params, Router} from "@angular/router";
20
import {MyArray} from "../../../shared/reusablecomponents/forms/my-array.interface";
21
import {
22
  AsideHelpContentComponent,
23
  HelpContentComponent
24
} from "../../../shared/reusablecomponents/help-content.component";
25
import {ConfirmationDialogComponent} from "../../../shared/reusablecomponents/confirmation-dialog.component";
26
import {AuthenticationService} from "../../../services/authentication.service";
27

    
28
@Component ({
29
  selector:'app-sr-literature',
30
  templateUrl: 'sr-literature.component.html'
31
})
32

    
33
export class SrLiteratureComponent implements OnInit {
34
  loadingMessage: string;
35
  errorMessage: string;
36

    
37
  showRepositories: boolean;
38
  showForm: boolean;
39
  showInterfaces: boolean;
40
  showFinish: boolean;
41
  step2: string = '';
42
  step3: string = '';
43
  step4: string = '';
44

    
45
  datasourceId: string;
46
  repo: Repository;
47

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

    
62
  @ViewChild('datasourcesByCountry')
63
  public datasourcesByCountry: RegisterDatasourceShareableComponent;
64

    
65
  @ViewChild('updateDatasource')
66
  public updateDatasource: DatasourceUpdateFormComponent;
67

    
68
  @ViewChild('interfaceFormArray')
69
  public interfaceFormArray: MyArray;
70

    
71

    
72
  group: FormGroup;
73
  interfaceFormDesc: Description = interfaceFormDesc;
74
  updateDatasourceInterfaces: Type<any> = DatasourceInterfaceFormComponent;
75
  repoInterfaces: RepositoryInterface[] = [];
76

    
77
  constructor(
78
    private fb: FormBuilder,
79
    private route: ActivatedRoute,
80
    private router: Router,
81
    private repoService: RepositoryService) {}
82

    
83
  ngOnInit() {
84
    this.setQueryParam('selectDatasource');
85
    this.showRepositories=true;
86
  }
87

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

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

    
129
  goToStep2(emitted: boolean) {
130
    if (emitted) {
131
      this.moveAStep();
132
    }
133
  }
134

    
135
  getRepoId(emitedId: string) {
136
    this.datasourceId = emitedId;
137
    this.getRepo();
138
  }
139

    
140
  getRepo() {
141
    this.loadingMessage = formInfoLoading;
142
    if (this.datasourceId) {
143
      this.repoService.getRepositoryById(this.datasourceId).subscribe(
144
        repo => {
145
          this.repo = repo;
146
        },
147
        error => {
148
          console.log(error);
149
          this.loadingMessage = '';
150
          this.errorMessage = loadingRepoError;
151
        },
152
        () => {
153
          this.loadingMessage = '';
154
        }
155
      );
156
    }
157
  }
158

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

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

    
197

    
198
  downloadLogo() {
199
    window.open("../../../assets/imgs/3_0ValidatedLogo.png","_blank", "enabledstatus=0,toolbar=0,menubar=0,location=0");
200
  }
201

    
202
  setQueryParam(value: string) {
203
    // set param for step
204
    this.queryParams['step'] = value;
205
    this.router.navigate([], { relativeTo: this.route, queryParams: this.queryParams });
206
    this.rightHelperContent.ngOnInit();
207
    this.topHelperContent.ngOnInit();
208
    this.leftHelperContent.ngOnInit();
209
    this.bottomHelperContent.ngOnInit();
210
  }
211

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

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

    
276
  getNewInterfaces (interfaces: RepositoryInterface[]) {
277
    this.repoInterfaces = interfaces;
278
    console.log('new interfaces is ',this.repoInterfaces);
279
  }
280

    
281
}
(10-10/10)