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
    console.log(this.type);
92
  }
93

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

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

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

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

    
209
  getRuleSetsForType() {
210
    this.loadingMessage = loadingRuleSets;
211
    this.valService.getRuleSets(this.type)
212
      .subscribe(
213
        rules => this.ruleSets = rules,
214
        error => {
215
          this.loadingMessage = '';
216
          this.errorMessage = loadingRuleSetsError;
217
        },
218
        () => {
219
          this.loadingMessage = '';
220
          this.showDatasource = false;
221
          this.step2 = 'active';
222
          if (this.ruleSets.length) {
223
            this.showGuidelines = true;
224
            this.setQueryParam('guidelines');
225
          } else {
226
            this.errorMessage = noRuleSets;
227
          }
228
        }
229
      );
230
  }
231

    
232
  getValidationSets() {
233
    this.loadingMessage = loadingValSets;
234
    this.valService.getSetsOfRepository(this.chosenUrl)
235
      .subscribe(
236
        sets => this.valSets = sets,
237
        error => {
238
          this.errorMessage = loadingValSetsError
239
        },
240
        () => {
241
          this.loadingMessage = '';
242
          this.step2 = 'active';
243
          this.showParameters = true;
244
        }
245
      );
246
  }
247

    
248
  getChosenUrl(url: string) {
249
    this.chosenUrl = url;
250
    this.identifyUrl();
251
  }
252

    
253
  getChosenRules(rules: any[]) {
254
    this.chosenGuidelinesAcronym = rules[0];
255
    this.chosenContentRules = rules[1];
256
    this.chosenUsageRules = rules[2];
257
  }
258

    
259
  getChosenParameters (params: string[]) {
260
    this.chosenValSet = params[0];
261
    this.noOfRecords = +params[1];
262
    this.xPath = params[2];
263
  }
264

    
265
  getChosenCrisEntities (crisParams: any[]) {
266
    this.chosenCrisEntities = crisParams[0];
267
    console.log(this.chosenCrisEntities);
268
    this.crisRefIntegrity = crisParams[1];
269
  }
270

    
271

    
272
  submitForValidation() {
273
    let isCris: boolean;
274
    if (this.type == 'cris') {
275
      isCris = true;
276
    } else {
277
      isCris = false;
278
      this.crisRefIntegrity = null;
279
      this.chosenCrisEntities = null;
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(newJob),
313
      error => {
314
        this.errorMessage = submittingJobError;
315
      }
316
    );
317
  }
318

    
319
  setQueryParam(value: string) {
320
    // set param for step
321
    this.queryParams['step'] = value;
322
    this.router.navigate([], { relativeTo: this.route, queryParams: this.queryParams });
323
    this.rightHelperContent.ngOnInit();
324
    this.topHelperContent.ngOnInit();
325
    this.leftHelperContent.ngOnInit();
326
    this.bottomHelperContent.ngOnInit();
327
  }
328

    
329
}
(6-6/15)