Project

General

Profile

1
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
2
import { Country, Repository, Timezone, Typology } from '../../../domain/typeScriptClasses';
3
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
4
import {
5
  adminEmailDesc,
6
  countryDesc,
7
  datasourceTypeDesc,
8
  Description,
9
  eissnDesc,
10
  englishNameDesc,
11
  institutionNameDesc,
12
  issnDesc,
13
  latitudeDesc,
14
  lissnDesc,
15
  logoUrlDesc,
16
  longtitudeDesc,
17
  officialNameDesc,
18
  platformNameDesc,
19
  repoDescriptionDesc,
20
  softwarePlatformDesc,
21
  timezoneDesc,
22
  websiteUrlDesc
23
} from '../../../domain/oa-description';
24
import { ActivatedRoute } from '@angular/router';
25
import { RepositoryService } from '../../../services/repository.service';
26
import { AuthenticationService } from '../../../services/authentication.service';
27
import { formErrorRequiredFields, noServiceMessage } from '../../../domain/shared-messages';
28

    
29
@Component ({
30
  selector: 'datasource-create-form',
31
  templateUrl: './datasource-create-form.component.html'
32
})
33

    
34
export class DatasourceCreateFormComponent implements OnInit {
35
  errorMessage: string;
36
  successMessage: string;
37
  loadingMessage: string;
38

    
39
  typologies: Typology[] = [];
40
  timezones: Timezone[] = [];
41
  countries: Country[] = [];
42
  datasourceClasses: Map<string, string> = new Map<string, string>();
43
  classCodes: string[] = [];
44

    
45
  @Input() mode: string;
46

    
47
  @Output() emittedInfo: EventEmitter<Repository> = new EventEmitter();
48

    
49
  @Input() selectedRepo: Repository;
50

    
51
  formSubmitted = false;
52
  group: FormGroup;
53

    
54
  readonly groupDefinition = {
55
    softwarePlatform : ['', Validators.required],
56
    platformName : '',
57
    officialName : ['', Validators.required],
58
    issn : ['', [Validators.pattern('^(\\d{4}-?\\d{3}[\\dxX])$')] ],
59
    eissn : ['', Validators.pattern('^(\\d{4}-?\\d{3}[\\dxX])$') ],
60
    lissn : ['', Validators.pattern('^(\\d{4}-?\\d{3}[\\dxX])$') ],
61
    repoDescription : ['', Validators.required],
62
    country : ['', Validators.required],
63
    longtitude : ['', [Validators.required, Validators.min(-180), Validators.max(180)] ],
64
    latitude : ['', [Validators.required, Validators.min(-90), Validators.max(90)] ],
65
    websiteUrl : ['', [Validators.required, Validators.pattern('^(http:\\/\\/www\\.|https:\\/\\/www\\.|http:\\/\\/|https:\\/\\/)?[a-z0-9]+([\\-\\.]{1}[a-z0-9]+)*\\.[a-z]{2,5}(:[0-9]{1,5})?(\\/.*)?$')] ],
66
    institutionName : ['', Validators.required],
67
    englishName: ['', Validators.required],
68
    logoUrl: ['', Validators.pattern('^(http:\\/\\/www\\.|https:\\/\\/www\\.|http:\\/\\/|https:\\/\\/)?[a-z0-9]+([\\-\\.]{1}[a-z0-9]+)*\\.[a-z]{2,5}(:[0-9]{1,5})?(\\/.*)?$') ],
69
    timezone: ['', Validators.required],
70
    datasourceType: ['', Validators.required],
71
    adminEmail: ['', [Validators.required, Validators.email] ]
72
  };
73

    
74
  softwarePlatformDesc: Description = softwarePlatformDesc;
75
  platformNameDesc: Description = platformNameDesc;
76
  officialNameDesc: Description = officialNameDesc;
77
  issnDesc: Description = issnDesc;
78
  eissnDesc: Description = eissnDesc;
79
  lissnDesc: Description = lissnDesc;
80
  repoDescriptionDesc: Description = repoDescriptionDesc;
81
  countryDesc: Description = countryDesc;
82
  longtitudeDesc: Description = longtitudeDesc;
83
  latitudeDesc: Description = latitudeDesc;
84
  websiteUrlDesc: Description = websiteUrlDesc;
85
  institutionNameDesc: Description = institutionNameDesc;
86
  englishNameDesc: Description = englishNameDesc;
87
  logoUrlDesc: Description = logoUrlDesc;
88
  timezoneDesc: Description = timezoneDesc;
89
  datasourceTypeDesc: Description = datasourceTypeDesc;
90
  adminEmailDesc: Description = adminEmailDesc;
91

    
92
  constructor(
93
    private fb: FormBuilder,
94
    private route: ActivatedRoute,
95
    private repoService: RepositoryService,
96
    private authService: AuthenticationService
97
  ) {}
98

    
99
  ngOnInit() {
100
    this.loadForm();
101
  }
102

    
103
  loadForm() {
104
    if (!this.mode) {
105
      this.mode = this.route.snapshot.url[0].path.toString();
106
      console.log(`my mode is ${this.mode}`);
107
    }
108
    this.group = this.fb.group(this.groupDefinition);
109
    if (this.mode === 'journal') {
110
      this.group.get('issn').clearValidators();
111
      this.group.get('issn').setValidators([Validators.required, Validators.pattern('^(\\d{4}-?\\d{3}[\\dxX])$')]);
112
    }
113
    this.getTypologies();
114
    this.getTimezones();
115
    this.getCountries();
116
    this.getDatasourceClasses();
117

    
118
    if (this.selectedRepo) {
119
      this.setupForm();
120
    }
121
  }
122

    
123
  setupForm() {
124
    if (this.selectedRepo) {
125
      console.log(`my datasource type is: ${this?.selectedRepo?.eoscDatasourceType}`);
126

    
127
      this.group.setValue({
128
        softwarePlatform: this.selectedRepo.platform,
129
        platformName: '',
130
        officialName: this.selectedRepo.officialname,
131
        issn: '',
132
        eissn: '',
133
        lissn: '',
134
        repoDescription: this.selectedRepo.description,
135
        country: this.selectedRepo.organizations[0].country, // countryCode
136
        longtitude: this.selectedRepo.longitude,
137
        latitude: this.selectedRepo.latitude,
138
        websiteUrl: this.selectedRepo.websiteurl,
139
        institutionName: this.selectedRepo.organizations[0].legalname,
140
        englishName: this.selectedRepo.englishname,
141
        logoUrl: this.selectedRepo.logourl,
142
        timezone: this.selectedRepo.timezone,
143
        datasourceType: this.selectedRepo.eoscDatasourceType, // TODO: still needed? should it be typology? typology exists on that stage?
144
        adminEmail: this.selectedRepo.contactemail
145
      });
146

    
147
      if (this.selectedRepo.eoscDatasourceType === 'Journal archive') {
148

    
149
        let ssnToShow = this.selectedRepo.issn.slice(0, 4) + '-' + this.selectedRepo.issn.toString().slice(4);
150
        this.group.get('issn').setValue(ssnToShow);
151

    
152
        if (this.selectedRepo.eissn.trim().length) {
153
          ssnToShow = this.selectedRepo.eissn.slice(0, 4) + '-' + this.selectedRepo.eissn.toString().slice(4);
154
          this.group.get('eissn').setValue(ssnToShow);
155
        }
156

    
157
        if (this.selectedRepo.lissn.trim().length) {
158
          ssnToShow = this.selectedRepo.lissn.slice(0, 4) + '-' + this.selectedRepo.lissn.toString().slice(4);
159
          this.group.get('lissn').setValue(ssnToShow);
160
        }
161

    
162
      }
163
    }
164
  }
165

    
166
  getCountries() {
167
    this.repoService.getCountries()
168
      .subscribe(
169
        countries => this.countries = countries.sort( function(a, b) {
170
          if (a.name < b.name) {
171
            return -1;
172
          } else if (a.name > b.name) {
173
            return 1;
174
          } else {
175
            return 0;
176
          }
177
        } ),
178
        error => {
179
          this.errorMessage = noServiceMessage;
180
          console.log(error);
181
        });
182
  }
183

    
184
  getDatasourceClasses() {
185
    this.repoService.getDatasourceClasses(this.mode).subscribe(
186
      classes => {
187
        for (const [key, value] of Object.entries(classes)) {
188
          this.datasourceClasses.set(key, value);
189
        }
190
      },
191
      error => {
192
        this.errorMessage = noServiceMessage;
193
        console.log(error);
194
      },
195
      () => {
196
        this.classCodes = Array.from(this.datasourceClasses.keys());
197
      }
198
    );
199
  }
200

    
201
  getTypologies() {
202
    this.repoService.getTypologies().subscribe(
203
      types => this.typologies = types,
204
      error => console.log(error)
205
    );
206
  }
207

    
208
  getTimezones() {
209
    this.repoService.getTimezones().subscribe(
210
      zones => this.timezones = zones,
211
      error => console.log(error)
212
    );
213
  }
214

    
215
  registerDatasource() {
216
    this.formSubmitted = true;
217
    this.errorMessage = '';
218
    this.successMessage = '';
219
    window.scroll(1, 1);
220

    
221
    if (this.group.valid) {
222
      this.selectedRepo = this.createNewRepository();
223
      this.emittedInfo.emit(this.selectedRepo);
224
    } else {
225
      this.errorMessage = formErrorRequiredFields;
226
    }
227
  }
228

    
229
  createNewRepository(): Repository {
230
    const newRepo = new Repository();
231
    newRepo.officialname = this.group.get('officialName').value.toString();
232
    newRepo.englishname = this.group.get('englishName').value.toString();
233
    newRepo.websiteurl = this.group.get('websiteUrl').value;
234
    newRepo.logourl = this.group.get('logoUrl').value;
235
    newRepo.contactemail = this.group.get('adminEmail').value;
236
    newRepo.organizations.push({
237
      legalshortname: null,
238
      legalname: this.group.get('institutionName').value.toString(),
239
      websiteurl: null,
240
      logourl: null,
241
      country: this.group.get('country').value
242
    });
243
    newRepo.latitude = this.group.get('latitude').value;
244
    newRepo.longitude = this.group.get('longtitude').value;
245
    newRepo.timezone = this.group.get('timezone').value;
246
    if (this.group.get('softwarePlatform').value !== '') {
247
      newRepo.platform = this.group.get('softwarePlatform').value;
248
    } else if (this.group.get('platformName').value) {
249
      newRepo.platform = this.group.get('platformName').value;
250
    }
251
    newRepo.typology = this.group.get('datasourceType').value;
252
    newRepo.description = this.group.get('repoDescription').value.toString();
253
    newRepo.issn = '';
254
    newRepo.eissn = '';
255
    newRepo.lissn = '';
256

    
257
    if ( this.group.get('issn').value ) {
258
      let ssnParts = this.group.get('issn').value.split('-');
259
      let correctSSN = ssnParts[0] + ssnParts[1];
260
      newRepo.issn = correctSSN;
261
      if ( this.group.get('eissn').value ) {
262
        ssnParts = this.group.get('eissn').value.split('-');
263
        correctSSN = ssnParts[0] + ssnParts[1];
264
        newRepo.eissn = correctSSN;
265
      }
266
      if ( this.group.get('lissn').value ) {
267
        ssnParts = this.group.get('lissn').value.split('-');
268
        correctSSN = ssnParts[0] + ssnParts[1];
269
        newRepo.lissn = correctSSN;
270
      }
271
    }
272

    
273
    newRepo.registeredby = this.authService.getUserEmail();
274

    
275
    newRepo.eoscDatasourceType = this.mode; // keep this
276
    newRepo.managed = true;
277

    
278
    const now = new Date(Date.now());
279
    newRepo.consentTermsOfUseDate = now;
280
    newRepo.lastConsentTermsOfUseDate = now;
281
    newRepo.registrationdate = now;
282
    return newRepo;
283
  }
284

    
285

    
286
}
(2-2/8)