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
  }
77

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

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

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

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

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

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

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

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

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

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

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

    
262

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

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

    
280
      desiredCompatibilityLevel: this.chosenGuidelinesAcronym,
281
      baseUrl: this.chosenUrl,
282

    
283
      validationSet: this.chosenValSet,
284
      records: this.noOfRecords,
285
      groupByXpath: this.xPath,
286

    
287
      cris: isCris,
288
      crisReferentialChecks: this.crisRefIntegrity,
289

    
290
      userEmail: this.authService.getUserEmail(),
291

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

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

    
317
}
(8-8/17)