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')
31
  public topHelperContent: HelpContentComponent;
32
  @ViewChild('leftHelperContent')
33
  public leftHelperContent: AsideHelpContentComponent;
34
  @ViewChild('rightHelperContent')
35
  public rightHelperContent: AsideHelpContentComponent;
36
  @ViewChild('bottomHelperContent')
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') step1ChooseBaseUrl: CompatibilityValidateStep1Component;
58
  @ViewChild('step2ChooseGuidelines') step2ChooseGuidelines: CompatibilityValidateStep2Component;
59
  @ViewChild('step3ChooseParameters') step3ChooseParameters: CompatibilityValidateStep3Component;
60
  @ViewChild('step3ChooseCrisEntities') 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
  }
79

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

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

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

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

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

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

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

    
241
  getChosenUrl(url: string) {
242
    this.chosenUrl = url;
243
    this.identifyUrl();
244
  }
245

    
246
  getChosenRules(rules: any[]) {
247
    this.chosenGuidelinesAcronym = rules[0];
248
    this.chosenContentRules = rules[1];
249
    this.chosenUsageRules = rules[2];
250
  }
251

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

    
258
  getChosenCrisEntities (crisParams: any[]) {
259
    this.chosenCrisEntities = crisParams[0];
260
    console.log(this.chosenCrisEntities);
261
    this.crisRefIntegrity = crisParams[1];
262
  }
263

    
264

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

    
277
    const newJob: JobForValidation = {
278
      selectedCrisEntities: this.chosenCrisEntities,
279
      selectedContentRules: this.chosenContentRules,
280
      selectedUsageRules: this.chosenUsageRules,
281

    
282
      desiredCompatibilityLevel: this.chosenGuidelinesAcronym,
283
      baseUrl: this.chosenUrl,
284

    
285
      validationSet: this.chosenValSet,
286
      records: this.noOfRecords,
287
      groupByXpath: this.xPath,
288

    
289
      cris: isCris,
290
      crisReferentialChecks: this.crisRefIntegrity,
291

    
292
      userEmail: this.authService.getUserEmail(),
293

    
294
      adminEmails: [],
295
      officialName: '',
296
      datasourceId: '',
297
      interfaceId: '',
298
      activationId: '',
299
      repoType: '',
300
      interfaceIdOld: '',
301
      metadataPrefix: '',
302

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

    
319
}
(8-8/17)