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
  // classCodes: string[] = [];
45

    
46
  @Input() mode: string;
47

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

    
50
  @Input() selectedRepo: Repository;
51

    
52
  formSubmitted = false;
53
  group: FormGroup;
54

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

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

    
95
  constructor(
96
    private fb: FormBuilder,
97
    private route: ActivatedRoute,
98
    private repoService: RepositoryService,
99
    private authService: AuthenticationService
100
  ) {}
101

    
102
  ngOnInit() {
103
    this.loadForm();
104
  }
105

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

    
121
    if (this.selectedRepo) {
122
      this.setupForm();
123
    }
124
  }
125

    
126
  setupForm() {
127
    if (this.selectedRepo) {
128
      console.log(`my datasource type is: ${this?.selectedRepo?.eoscDatasourceType}`);
129

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

    
150
      if (this.selectedRepo.eoscDatasourceType === 'Journal archive') {
151

    
152
        let ssnToShow = this.selectedRepo.issn.slice(0, 4) + '-' + this.selectedRepo.issn.toString().slice(4);
153
        this.group.get('issn').setValue(ssnToShow);
154

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

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

    
165
      }
166
    }
167
  }
168

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

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

    
204
  getTypologies() {
205
    this.repoService.getTypologies().subscribe(
206
      types => this.typologies = types,
207
      error => console.log(error)
208
    );
209
  }
210

    
211
  getTimezones() {
212
    this.repoService.getTimezones().subscribe(
213
      zones => this.timezones = zones,
214
      error => console.log(error)
215
    );
216
  }
217

    
218
  registerDatasource() {
219
    this.formSubmitted = true;
220
    this.errorMessage = '';
221
    this.successMessage = '';
222
    window.scroll(1, 1);
223

    
224
    if (this.group.valid) {
225
      this.selectedRepo = this.createNewRepository();
226
      this.emittedInfo.emit(this.selectedRepo);
227
    } else {
228
      this.errorMessage = formErrorRequiredFields;
229
    }
230
  }
231

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

    
264
    if ( this.group.get('issn').value ) {
265
      let ssnParts = this.group.get('issn').value.split('-');
266
      let correctSSN = ssnParts[0] + ssnParts[1];
267
      newRepo.issn = correctSSN;
268
      if ( this.group.get('eissn').value ) {
269
        ssnParts = this.group.get('eissn').value.split('-');
270
        correctSSN = ssnParts[0] + ssnParts[1];
271
        newRepo.eissn = correctSSN;
272
      }
273
      if ( this.group.get('lissn').value ) {
274
        ssnParts = this.group.get('lissn').value.split('-');
275
        correctSSN = ssnParts[0] + ssnParts[1];
276
        newRepo.lissn = correctSSN;
277
      }
278
    }
279

    
280
    newRepo.registeredby = this.authService.getUserEmail();
281

    
282
    /* THE BELOW FIELDS ARE NOT SET IN GWT CODE*/
283
    newRepo.eoscDatasourceType = this.mode; // TODO: delete this?
284
    console.warn(newRepo.eoscDatasourceType);
285
    newRepo.managed = true;
286

    
287
    const now = new Date(Date.now());
288
    newRepo.consentTermsOfUseDate = now;
289
    newRepo.lastConsentTermsOfUseDate = now;
290
    newRepo.registrationdate = now;
291
    return newRepo;
292
  }
293

    
294

    
295
}
(2-2/8)