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 { InterfaceInformation, JobForValidation, Repository, RuleSet } from '../../domain/typeScriptClasses';
6
import { AuthenticationService } from '../../services/authentication.service';
7
import {
8
  didntSelectCrisEntities,
9
  didntSelectRules,
10
  identifyingUrl, invalidCustomBaseUrl,
11
  loadingReposMessage, loadingRuleSets, loadingRuleSetsError,
12
  loadingUserRepoInfoError, loadingValSets, loadingValSetsError, noRuleSets, noServiceMessage, submittingJobError
13
} from '../../domain/shared-messages';
14
import { ValidatorService } from '../../services/validator.service';
15
import { CompatibilityValidateStep2Component } from './compatibility-validate-forms/compatibility-validate-step2.component';
16
import { CompatibilityValidateStep3Component } from './compatibility-validate-forms/compatibility-validate-step3.component';
17
import { CompatibilityValidateStep3CrisComponent } from './compatibility-validate-forms/compatibility-validate-step3-cris.component';
18
import {AsideHelpContentComponent, HelpContentComponent} from "../../shared/reusablecomponents/help-content.component";
19

    
20
@Component ({
21
  selector: 'compatibility-validate-literature',
22
  templateUrl: 'compatibility-validate-type.component.html'
23
})
24

    
25
export class CompatibilityValidateTypeComponent implements OnInit {
26
  type: string = '';
27

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

    
42

    
43
  showDatasource: boolean;
44
  showGuidelines: boolean;
45
  showParameters: boolean;
46
  showCrisEntities: boolean;
47
  showFinish: boolean;
48

    
49
  step2: string = '';
50
  step3: string = '';
51
  step4: string = '';
52

    
53
  baseUrlList: string[] = [];
54
  ruleSets: RuleSet[] = [];
55
  valSets: string[] = [];
56

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

    
68
  errorMessage: string;
69
  loadingMessage: string;
70

    
71
  @ViewChild('step1ChooseBaseUrl') step1ChooseBaseUrl : CompatibilityValidateStep1Component;
72
  @ViewChild('step2ChooseGuidelines') step2ChooseGuidelines : CompatibilityValidateStep2Component;
73
  @ViewChild('step3ChooseParameters') step3ChooseParameters : CompatibilityValidateStep3Component;
74
  @ViewChild('step3ChooseCrisEntities') step3ChooseCrisEntities : CompatibilityValidateStep3CrisComponent;
75

    
76
  constructor(private route: ActivatedRoute,
77
              private router: Router,
78
              private authService: AuthenticationService,
79
              private repoService: RepositoryService,
80
              private valService: ValidatorService) {}
81

    
82
  ngOnInit() {
83
    this.readType();
84
    this.setQueryParam('baseUrl');
85
    this.showDatasource = true;
86
    this.getBaseUrlList();
87
  }
88

    
89
  readType() {
90
    this.type = this.route.snapshot.paramMap.get('type');
91
    if (this.type == 'cris') {
92
      this.chosenValSet = 'none';
93
      this.noOfRecords = -1;
94
    }
95
    console.log(this.type);
96
  }
97

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

    
123
  moveAStep() {
124
    this.errorMessage = '';
125
    if (this.showDatasource) {
126
      this.step1ChooseBaseUrl.submitForm();
127
    } else if (this.showGuidelines) {
128
      this.step2ChooseGuidelines.saveChanges();
129
      console.log(this.chosenContentRules);
130
      if (this.chosenContentRules.length || this.chosenUsageRules.length){
131
        if (this.type == 'cris'){
132
          this.setQueryParam('crisEntities');
133
          this.showCrisEntities = true;
134
        } else {
135
          this.getValidationSets();
136
          this.setQueryParam('parameters');
137
          this.showParameters = true;
138
        }
139
        this.showGuidelines = false;
140
        this.step3 = 'active';
141
      } else {
142
        this.errorMessage = didntSelectRules;
143
      }
144
    } else if (this.showParameters) {
145
      this.step3ChooseParameters.submitChanges();
146
      //save all changes
147
      this.submitForValidation();
148
    } else if (this.showCrisEntities) {
149
      this.step3ChooseCrisEntities.saveChanges();
150
      if (this.chosenCrisEntities.length) {
151
        //save all changes
152
        this.submitForValidation();
153
      } else {
154
        this.errorMessage = didntSelectCrisEntities;
155
      }
156
    }
157
  }
158

    
159
  moveBackAStep () {
160
    if (this.showGuidelines) {
161
      this.setQueryParam('baseUrl');
162
      this.showDatasource = true;
163
      this.showGuidelines = false;
164
      this.step2 = '';
165
      this.errorMessage = '';
166
    } else if (this.showParameters) {
167
      this.setQueryParam('guidelines');
168
      this.step3 = '';
169
      this.showGuidelines = true;
170
      this.showParameters = false;
171
      this.errorMessage = '';
172
    } else if (this.showCrisEntities) {
173
      this.setQueryParam('guidelines');
174
      this.step3 = '';
175
      this.showGuidelines = true;
176
      this.showCrisEntities = false;
177
      this.errorMessage = '';
178
    }
179
  }
180

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

    
205
  getRuleSetsForType() {
206
    this.loadingMessage = loadingRuleSets;
207
    this.valService.getRuleSets(this.type)
208
      .subscribe(
209
        rules => this.ruleSets = rules,
210
        error => {
211
          this.loadingMessage = '';
212
          this.errorMessage = loadingRuleSetsError;
213
        },
214
        () => {
215
          this.loadingMessage = '';
216
          this.showDatasource = false;
217
          this.step2 = 'active';
218
          if (this.ruleSets && this.ruleSets.length) {
219
            this.showGuidelines = true;
220
            this.setQueryParam('guidelines');
221
          } else {
222
            this.errorMessage = noRuleSets;
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
        },
236
        () => {
237
          this.loadingMessage = '';
238
          this.step2 = 'active';
239
          this.showParameters = true;
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
    } else {
273
      isCris = false;
274
      this.crisRefIntegrity = null;
275
      this.chosenCrisEntities = null;
276
      if ( this.chosenValSet==='' ) {
277
        this.chosenValSet= 'none';
278
      }
279
    }
280

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

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

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

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

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

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

    
307
      registration: false,
308
      updateExisting: false
309
    };
310
    console.log(JSON.stringify(newJob));
311
    this.valService.submitJobForValidation(newJob).subscribe(
312
      newJob => console.log(JSON.stringify(newJob)),
313
      error => {
314
        this.errorMessage = submittingJobError;
315
      },
316
      () => {
317
        this.setQueryParam('finish');
318
        this.showFinish = true;
319
        this.showCrisEntities = false;
320
        this.showParameters = false;
321
        this.step4 = 'active';
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)