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
    body.classList.remove("page_heading_active");
79
  }
80

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

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

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

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

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

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

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

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

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

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

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

    
265

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

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

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

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

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

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

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

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

    
320
}
(8-8/17)