Project

General

Profile

1
import { Component, OnInit, ViewChild } from '@angular/core';
2
import { ActivatedRoute, Params, Router} from '@angular/router';
3
import { CompatibilityValidateStep1Component } from './compatibility-validate-forms/compatibility-validate-step1.component';
4
import { RepositoryService } from '../../services/repository.service';
5
import { JobForValidation, RuleSet } from '../../domain/typeScriptClasses';
6
import { AuthenticationService } from '../../services/authentication.service';
7
import { didntSelectCrisEntities, didntSelectRules, identifyingUrl, invalidCustomBaseUrl,
8
         loadingReposMessage, loadingRuleSets, loadingRuleSetsError, loadingUserRepoInfoError,
9
         loadingValSets, loadingValSetsError, noRuleSets, noServiceMessage, submittingJobError
10
} from '../../domain/shared-messages';
11
import { ValidatorService } from '../../services/validator.service';
12
import { CompatibilityValidateStep2Component } from './compatibility-validate-forms/compatibility-validate-step2.component';
13
import { CompatibilityValidateStep3Component } from './compatibility-validate-forms/compatibility-validate-step3.component';
14
import { CompatibilityValidateStep3CrisComponent } from './compatibility-validate-forms/compatibility-validate-step3-cris.component';
15
import { AsideHelpContentComponent, HelpContentComponent } from '../../shared/reusablecomponents/help-content.component';
16

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

    
22
export class CompatibilityValidateTypeComponent implements OnInit {
23
  type = '';
24

    
25
  /* queryParams is used to change the queryParams without refreshing the page
26
   * This was needed for Help Service [which sends back info according to the current router.url]
27
   * the param that is used is 'step' and the values are: 'baseUrl','guidelines','crisEntities'/'parameters','finish'
28
   */
29
  queryParams: Params = Object.assign({}, this.route.snapshot.queryParams);
30
  currentStep: number;
31
  @ViewChild('topHelperContent')
32
  public topHelperContent: HelpContentComponent;
33
  @ViewChild('leftHelperContent')
34
  public leftHelperContent: AsideHelpContentComponent;
35
  @ViewChild('rightHelperContent')
36
  public rightHelperContent: AsideHelpContentComponent;
37
  @ViewChild('bottomHelperContent')
38
  public bottomHelperContent: HelpContentComponent;
39

    
40
  showDatasource: boolean;
41
  showGuidelines: boolean;
42
  showParameters: boolean;
43
  showCrisEntities: boolean;
44
  showFinish: boolean;
45

    
46
  step2 = '';
47
  step3 = '';
48
  step4 = '';
49

    
50
  baseUrlList: string[] = [];
51
  ruleSets: RuleSet[] = [];
52
  valSets: string[] = [];
53

    
54
  chosenUrl: string;
55
  identifiedUrl: boolean;
56
  chosenGuidelinesAcronym: string;
57
  chosenContentRules: number[];
58
  chosenUsageRules: number[];
59
  chosenValSet: string;
60
  noOfRecords: number;
61
  xPath: string;
62
  chosenCrisEntities: string[];
63
  crisRefIntegrity: boolean;
64

    
65
  errorMessage: string;
66
  loadingMessage: string;
67

    
68
  @ViewChild('step1ChooseBaseUrl') step1ChooseBaseUrl: CompatibilityValidateStep1Component;
69
  @ViewChild('step2ChooseGuidelines') step2ChooseGuidelines: CompatibilityValidateStep2Component;
70
  @ViewChild('step3ChooseParameters') step3ChooseParameters: CompatibilityValidateStep3Component;
71
  @ViewChild('step3ChooseCrisEntities') step3ChooseCrisEntities: CompatibilityValidateStep3CrisComponent;
72

    
73
  constructor(private route: ActivatedRoute,
74
              private router: Router,
75
              private authService: AuthenticationService,
76
              private repoService: RepositoryService,
77
              private valService: ValidatorService) {}
78

    
79
  ngOnInit() {
80
    if (this.route.snapshot.paramMap.has('type')) {
81
      this.type = this.route.snapshot.paramMap.get('type');
82
      this.getBaseUrlList();
83
      this.getStep();
84
    }
85
  }
86

    
87
  getStep() {
88
    this.currentStep = 0;
89
    if (this.route.snapshot.queryParamMap.has('step')) {
90
      const stepName = this.route.snapshot.queryParamMap.get('step');
91
      if (stepName === 'guidelines') {
92
        if (!this.identifiedUrl) {
93
          this.navigateToStep('baseUrl');
94
        } else {
95
          this.currentStep = 1;
96
        }
97
      } else if ((stepName === 'parameters') || (stepName === 'crisEntities')) {
98
        if (!this.chosenUrl) {
99
          this.navigateToStep('baseUrl');
100
        } else {
101
          this.currentStep = 2;
102
        }
103
      } else if (stepName === 'finish') {
104
        this.currentStep = 3;
105
      }
106
    }
107
  }
108

    
109
  navigateToStep(step: string) {
110
    this.router.navigateByUrl(`/compatibility/validate/${this.type}?step=${step}`)
111
      .then( () => {
112
          this.getStep();
113
          this.rightHelperContent.ngOnInit();
114
          this.topHelperContent.ngOnInit();
115
          this.leftHelperContent.ngOnInit();
116
          this.bottomHelperContent.ngOnInit();
117
        }
118
      );
119
  }
120

    
121
  /* retrieves the baseUrl list for the registered repositories of the user */
122
  getBaseUrlList() {
123
    this.loadingMessage = loadingReposMessage;
124
    this.repoService.getUrlsOfUserRepos(this.authService.getUserEmail())
125
      .subscribe(
126
        repos => this.baseUrlList = repos.sort( function(a , b) {
127
          if (a < b ) {
128
            return -1;
129
          } else if (a > b ) {
130
            return 1;
131
          } else {
132
            return 0;
133
          }
134
        }),
135
        error => {
136
          console.log(error);
137
          this.loadingMessage = '';
138
          this.errorMessage = loadingUserRepoInfoError;
139
        },
140
        () => {
141
          this.loadingMessage = '';
142
        }
143
      );
144
  }
145

    
146
  moveAStep() {
147
    this.errorMessage = '';
148
    if (this.currentStep === 0) {
149
      this.step1ChooseBaseUrl.submitForm();
150
    } else if (this.currentStep === 1) {
151
      this.step2ChooseGuidelines.saveChanges();
152
      console.log(this.chosenContentRules);
153
      if (this.chosenContentRules.length || this.chosenUsageRules.length) {
154
        if (this.type === 'cris') {
155
          this.navigateToStep('crisEntities');
156
        } else {
157
          this.getValidationSets();
158
          this.navigateToStep('parameters');
159
        }
160
      } else {
161
        this.errorMessage = didntSelectRules;
162
      }
163
    } else if ((this.currentStep === 2) && (this.type !== 'cris')) {
164
      this.step3ChooseParameters.submitChanges();
165
      // save all changes
166
      this.submitForValidation();
167
    } else if ((this.currentStep === 2) && (this.type === 'cris')) {
168
      this.step3ChooseCrisEntities.saveChanges();
169
      if (this.chosenCrisEntities.length) {
170
        // save all changes
171
        this.submitForValidation();
172
      } else {
173
        this.errorMessage = didntSelectCrisEntities;
174
      }
175
    }
176
  }
177

    
178
  moveBackAStep () {
179
    this.errorMessage = '';
180
    if (this.currentStep === 1) {
181
      this.navigateToStep('baseUrl');
182
    } else if ((this.currentStep === 2) && (this.type !== 'cris')) {
183
      this.navigateToStep('guidelines');
184
    } else if ((this.currentStep === 2) && (this.type === 'cris')) {
185
      this.navigateToStep('guidelines');
186
    }
187
  }
188

    
189
  identifyUrl() {
190
    this.loadingMessage = identifyingUrl;
191
    console.log(`identifying ${this.chosenUrl}`);
192
    this.valService.identifyRepository(this.chosenUrl).subscribe(
193
      res => {
194
        this.identifiedUrl = res;
195
        console.log(`identifyRepository responded: ${this.identifiedUrl}`);
196
      },
197
      error =>  {
198
        console.log(error);
199
        this.loadingMessage = '';
200
        this.identifiedUrl = false;
201
        this.errorMessage = noServiceMessage;
202
      }, () => {
203
        this.loadingMessage = '';
204
        if (this.identifiedUrl) {
205
          this.getRuleSetsForType();
206
        } else {
207
          this.errorMessage = invalidCustomBaseUrl;
208
        }
209
      }
210
    );
211
  }
212

    
213
  getRuleSetsForType() {
214
    this.loadingMessage = loadingRuleSets;
215
    this.valService.getRuleSets(this.type)
216
      .subscribe(
217
        rules => this.ruleSets = rules,
218
        error => {
219
          this.loadingMessage = '';
220
          this.errorMessage = loadingRuleSetsError;
221
        },
222
        () => {
223
          this.loadingMessage = '';
224
          if (this.ruleSets && this.ruleSets.length) {
225
            this.navigateToStep('guidelines');
226
          } else {
227
            this.errorMessage = noRuleSets;
228
          }
229
        }
230
      );
231
  }
232

    
233
  getValidationSets() {
234
    this.loadingMessage = loadingValSets;
235
    this.valService.getSetsOfRepository(this.chosenUrl)
236
      .subscribe(
237
        sets => this.valSets = sets,
238
        error => {
239
          this.errorMessage = loadingValSetsError;
240
        },
241
        () => {
242
          this.loadingMessage = '';
243
          this.step2 = 'active';
244
          this.showParameters = true;
245
        }
246
      );
247
  }
248

    
249
  getChosenUrl(url: string) {
250
    this.chosenUrl = url;
251
    this.identifyUrl();
252
  }
253

    
254
  getChosenRules(rules: any[]) {
255
    this.chosenGuidelinesAcronym = rules[0];
256
    this.chosenContentRules = rules[1];
257
    this.chosenUsageRules = rules[2];
258
  }
259

    
260
  getChosenParameters (params: string[]) {
261
    this.chosenValSet = params[0];
262
    this.noOfRecords = +params[1];
263
    this.xPath = params[2];
264
  }
265

    
266
  getChosenCrisEntities (crisParams: any[]) {
267
    this.chosenCrisEntities = crisParams[0];
268
    console.log(this.chosenCrisEntities);
269
    this.crisRefIntegrity = crisParams[1];
270
  }
271

    
272

    
273
  submitForValidation() {
274
    let isCris: boolean;
275
    if (this.type === 'cris') {
276
      isCris = true;
277
      this.chosenValSet = 'none';
278
      this.noOfRecords = -1;
279
    } else {
280
      isCris = false;
281
      this.crisRefIntegrity = null;
282
      this.chosenCrisEntities = null;
283
    }
284

    
285
    const newJob: JobForValidation = {
286
      selectedCrisEntities: this.chosenCrisEntities,
287
      selectedContentRules: this.chosenContentRules,
288
      selectedUsageRules: this.chosenUsageRules,
289

    
290
      desiredCompatibilityLevel: this.chosenGuidelinesAcronym,
291
      baseUrl: this.chosenUrl,
292

    
293
      validationSet: this.chosenValSet,
294
      records: this.noOfRecords,
295
      groupByXpath: this.xPath,
296

    
297
      cris: isCris,
298
      crisReferentialChecks: this.crisRefIntegrity,
299

    
300
      userEmail: this.authService.getUserEmail(),
301

    
302
      adminEmails: [],
303
      officialName: '',
304
      datasourceId: '',
305
      interfaceId: '',
306
      activationId: '',
307
      repoType: '',
308
      interfaceIdOld: '',
309
      metadataPrefix: '',
310

    
311
      registration: false,
312
      updateExisting: false
313
    };
314
    console.log(JSON.stringify(newJob));
315
    this.valService.submitJobForValidation(newJob).subscribe(
316
      job => console.log(JSON.stringify(job)),
317
      error => {
318
        this.errorMessage = submittingJobError;
319
      },
320
      () => {
321
        this.navigateToStep('finish');
322
      }
323
    );
324
  }
325

    
326
  setQueryParam(value: string) {
327
    // set param for step
328
    this.queryParams['step'] = value;
329
    this.router.navigate([], { relativeTo: this.route, queryParams: this.queryParams });
330
    this.rightHelperContent.ngOnInit();
331
    this.topHelperContent.ngOnInit();
332
    this.leftHelperContent.ngOnInit();
333
    this.bottomHelperContent.ngOnInit();
334
  }
335

    
336
}
(8-8/17)