Project

General

Profile

1
import { Component, OnInit, ViewChild } from '@angular/core';
2
import { ActivatedRoute, 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
  currentStep: number;
30
  @ViewChild('topHelperContent', { static: true })
31
  public topHelperContent: HelpContentComponent;
32
  @ViewChild('leftHelperContent', { static: true })
33
  public leftHelperContent: AsideHelpContentComponent;
34
  @ViewChild('rightHelperContent', { static: true })
35
  public rightHelperContent: AsideHelpContentComponent;
36
  @ViewChild('bottomHelperContent', { static: true })
37
  public bottomHelperContent: HelpContentComponent;
38

    
39
  baseUrlList: string[] = [];
40
  ruleSets: RuleSet[] = [];
41
  valSets: string[] = [];
42

    
43
  chosenUrl: string;
44
  identifiedUrl: boolean;
45
  chosenGuidelinesAcronym: string;
46
  chosenContentRules: number[];
47
  chosenUsageRules: number[];
48
  chosenValSet: string;
49
  noOfRecords: number;
50
  xPath: string;
51
  chosenCrisEntities: string[];
52
  crisRefIntegrity: boolean;
53

    
54
  errorMessage: string;
55
  loadingMessage: string;
56

    
57
  @ViewChild('step1ChooseBaseUrl', { static: false }) step1ChooseBaseUrl: CompatibilityValidateStep1Component;
58
  @ViewChild('step2ChooseGuidelines', { static: false }) step2ChooseGuidelines: CompatibilityValidateStep2Component;
59
  @ViewChild('step3ChooseParameters', { static: false }) step3ChooseParameters: CompatibilityValidateStep3Component;
60
  @ViewChild('step3ChooseCrisEntities', { static: false }) step3ChooseCrisEntities: CompatibilityValidateStep3CrisComponent;
61

    
62
  constructor(private route: ActivatedRoute,
63
              private router: Router,
64
              private authService: AuthenticationService,
65
              private repoService: RepositoryService,
66
              private valService: ValidatorService) {}
67

    
68
  ngOnInit() {
69
    if (this.route.snapshot.paramMap.has('type')) {
70
      this.type = this.route.snapshot.paramMap.get('type');
71
      this.getBaseUrlList();
72
      this.route.queryParams.subscribe(
73
        () => this.getStep()
74
      );
75
    }
76
    let body = document.getElementsByTagName('body')[0];
77
    body.classList.remove("top_bar_active");   //remove the class
78
    body.classList.remove("page_heading_active");
79
    body.classList.remove("landing");
80
    body.classList.add("dashboard");
81
  }
82

    
83
  getStep() {
84
    this.currentStep = 0;
85
    if (this.route.snapshot.queryParamMap.has('step')) {
86
      const stepName = this.route.snapshot.queryParamMap.get('step');
87
      if (stepName === 'guidelines') {
88
        if (!this.identifiedUrl) {
89
          this.router.navigateByUrl(`/compatibility/validate/${this.type}?step=baseUrl`);
90
        } else {
91
          this.currentStep = 1;
92
        }
93
      } else if ((stepName === 'parameters') || (stepName === 'crisEntities')) {
94
        if (!this.chosenUrl) {
95
          this.router.navigateByUrl(`/compatibility/validate/${this.type}?step=baseUrl`);
96
        } else {
97
          this.currentStep = 2;
98
        }
99
      } else if (stepName === 'finish') {
100
        this.currentStep = 3;
101
      }
102
    }
103
    this.rightHelperContent.ngOnInit();
104
    this.topHelperContent.ngOnInit();
105
    this.leftHelperContent.ngOnInit();
106
    this.bottomHelperContent.ngOnInit();
107
  }
108

    
109
  /* retrieves the baseUrl list for the registered repositories of the user */
110
  getBaseUrlList() {
111
    this.loadingMessage = loadingReposMessage;
112
    this.repoService.getUrlsOfUserRepos()
113
      .subscribe(
114
        repos => this.baseUrlList = repos.sort( function(a , b) {
115
          if (a < b ) {
116
            return -1;
117
          } else if (a > b ) {
118
            return 1;
119
          } else {
120
            return 0;
121
          }
122
        }),
123
        error => {
124
          console.log(error);
125
          this.loadingMessage = '';
126
          this.errorMessage = loadingUserRepoInfoError;
127
          window.scroll(1, 1);
128
        },
129
        () => {
130
          this.loadingMessage = '';
131
        }
132
      );
133
  }
134

    
135
  moveAStep() {
136
    this.errorMessage = '';
137
    if (this.currentStep === 0) {
138
      this.step1ChooseBaseUrl.submitForm();
139
    } else if (this.currentStep === 1) {
140
      this.step2ChooseGuidelines.saveChanges();
141
      console.log(this.chosenContentRules);
142
      if (this.chosenContentRules.length || this.chosenUsageRules.length) {
143
        if (this.type === 'cris') {
144
          this.router.navigateByUrl(`/compatibility/validate/${this.type}?step=crisEntities`);
145
        } else {
146
          this.getValidationSets();
147
          this.router.navigateByUrl(`/compatibility/validate/${this.type}?step=parameters`);
148
        }
149
      } else {
150
        this.errorMessage = didntSelectRules;
151
        window.scroll(1, 1);
152
      }
153
    } else if ((this.currentStep === 2) && (this.type !== 'cris')) {
154
      this.step3ChooseParameters.submitChanges();
155
      // save all changes
156
      this.submitForValidation();
157
    } else if ((this.currentStep === 2) && (this.type === 'cris')) {
158
      this.step3ChooseCrisEntities.saveChanges();
159
      if (this.chosenCrisEntities.length) {
160
        // save all changes
161
        this.submitForValidation();
162
      } else {
163
        this.errorMessage = didntSelectCrisEntities;
164
        window.scroll(1, 1);
165
      }
166
    }
167
  }
168

    
169
  moveBackAStep () {
170
    this.errorMessage = '';
171
    if (this.currentStep === 1) {
172
      this.router.navigateByUrl(`/compatibility/validate/${this.type}?step=baseUrl`);
173
    } else if ((this.currentStep === 2) && (this.type !== 'cris')) {
174
      this.router.navigateByUrl(`/compatibility/validate/${this.type}?step=guidelines`);
175
    } else if ((this.currentStep === 2) && (this.type === 'cris')) {
176
      this.router.navigateByUrl(`/compatibility/validate/${this.type}?step=guidelines`);
177
    }
178
  }
179

    
180
  identifyUrl() {
181
    this.loadingMessage = identifyingUrl;
182
    console.log(`identifying ${this.chosenUrl}`);
183
    this.valService.identifyRepository(this.chosenUrl).subscribe(
184
      res => {
185
        this.identifiedUrl = res;
186
        console.log(`identifyRepository responded: ${this.identifiedUrl}`);
187
      },
188
      error =>  {
189
        console.log(error);
190
        this.loadingMessage = '';
191
        this.identifiedUrl = false;
192
        this.errorMessage = noServiceMessage;
193
        window.scroll(1, 1);
194
      }, () => {
195
        this.loadingMessage = '';
196
        if (this.identifiedUrl) {
197
          this.getRuleSetsForType();
198
        } else {
199
          this.errorMessage = invalidCustomBaseUrl;
200
          window.scroll(1, 1);
201
        }
202
      }
203
    );
204
  }
205

    
206
  getRuleSetsForType() {
207
    this.loadingMessage = loadingRuleSets;
208
    this.valService.getRuleSets(this.type)
209
      .subscribe(
210
        rules => this.ruleSets = rules,
211
        error => {
212
          this.loadingMessage = '';
213
          this.errorMessage = loadingRuleSetsError;
214
          window.scroll(1, 1);
215
        },
216
        () => {
217
          this.loadingMessage = '';
218
          if (this.ruleSets && this.ruleSets.length) {
219
            this.router.navigateByUrl(`/compatibility/validate/${this.type}?step=guidelines`);
220
          } else {
221
            this.errorMessage = noRuleSets;
222
            window.scroll(1, 1);
223
          }
224
        }
225
      );
226
  }
227

    
228
  getValidationSets() {
229
    this.loadingMessage = loadingValSets;
230
    this.valService.getSetsOfRepository(this.chosenUrl)
231
      .subscribe(
232
        sets => this.valSets = sets,
233
        error => {
234
          this.errorMessage = loadingValSetsError;
235
          window.scroll(1, 1);
236
        },
237
        () => {
238
          this.loadingMessage = '';
239
          this.currentStep = 2;
240
        }
241
      );
242
  }
243

    
244
  getChosenUrl(url: string) {
245
    this.chosenUrl = url;
246
    this.identifyUrl();
247
  }
248

    
249
  getChosenRules(rules: any[]) {
250
    this.chosenGuidelinesAcronym = rules[0];
251
    this.chosenContentRules = rules[1];
252
    this.chosenUsageRules = rules[2];
253
  }
254

    
255
  getChosenParameters (params: string[]) {
256
    this.chosenValSet = params[0];
257
    this.noOfRecords = +params[1];
258
    this.xPath = params[2];
259
  }
260

    
261
  getChosenCrisEntities (crisParams: any[]) {
262
    this.chosenCrisEntities = crisParams[0];
263
    console.log(this.chosenCrisEntities);
264
    this.crisRefIntegrity = crisParams[1];
265
  }
266

    
267

    
268
  submitForValidation() {
269
    let isCris: boolean;
270
    if (this.type === 'cris') {
271
      isCris = true;
272
      this.chosenValSet = 'none';
273
      this.noOfRecords = -1;
274
    } else {
275
      isCris = false;
276
      this.crisRefIntegrity = null;
277
      this.chosenCrisEntities = null;
278
    }
279

    
280
    const newJob: JobForValidation = {
281
      selectedCrisEntities: this.chosenCrisEntities,
282
      selectedContentRules: this.chosenContentRules,
283
      selectedUsageRules: this.chosenUsageRules,
284

    
285
      desiredCompatibilityLevel: this.chosenGuidelinesAcronym,
286
      baseUrl: this.chosenUrl,
287

    
288
      validationSet: this.chosenValSet,
289
      records: this.noOfRecords,
290
      groupByXpath: this.xPath,
291

    
292
      cris: isCris,
293
      crisReferentialChecks: this.crisRefIntegrity,
294

    
295
      userEmail: this.authService.getUserEmail(),
296

    
297
      adminEmails: [],
298
      officialName: '',
299
      datasourceId: '',
300
      interfaceId: '',
301
      activationId: '',
302
      repoType: '',
303
      interfaceIdOld: '',
304
      metadataPrefix: '',
305

    
306
      registration: false,
307
      updateExisting: false
308
    };
309
    console.log(JSON.stringify(newJob));
310
    this.valService.submitJobForValidation(newJob).subscribe(
311
      job => console.log(JSON.stringify(job)),
312
      error => {
313
        this.errorMessage = submittingJobError;
314
        window.scroll(1, 1);
315
      },
316
      () => {
317
        this.router.navigateByUrl(`/compatibility/validate/${this.type}?step=finish`);
318
      }
319
    );
320
  }
321

    
322
}
(2-2/11)