Project

General

Profile

1 54479 myrto.kouk
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 57095 stefania.m
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 54479 myrto.kouk
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 54722 myrto.kouk
54
  // old issn regex
55
  // issn : ['', [Validators.pattern('^\\d\\d\\d\\d[-]\\d\\d\\d\\d$')] ],
56 54479 myrto.kouk
  readonly groupDefinition = {
57
    softwarePlatform : ['', Validators.required],
58 57095 stefania.m
    platformName : '',
59 54479 myrto.kouk
    officialName : ['', Validators.required],
60 54722 myrto.kouk
    issn : ['', [Validators.pattern('^\\d{4}-\\d{3}[\\dxX]$')] ],
61
    eissn : ['', Validators.pattern('^\\d{4}-\\d{3}[\\dxX]$') ],
62
    lissn : ['', Validators.pattern('^\\d{4}-\\d{3}[\\dxX]$') ],
63 54479 myrto.kouk
    repoDescription : ['', Validators.required],
64
    country : ['', Validators.required],
65
    longtitude : ['', [Validators.required, Validators.min(-180), Validators.max(180)] ],
66
    latitude : ['', [Validators.required, Validators.min(-90), Validators.max(90)] ],
67
    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})?(\\/.*)?$')] ],
68
    institutionName : ['', Validators.required],
69
    englishName: ['', Validators.required],
70
    logoUrl: ['', Validators.pattern('^(http:\\/\\/www\\.|https:\\/\\/www\\.|http:\\/\\/|https:\\/\\/)?[a-z0-9]+([\\-\\.]{1}[a-z0-9]+)*\\.[a-z]{2,5}(:[0-9]{1,5})?(\\/.*)?$') ],
71
    timezone: ['', Validators.required],
72
    datasourceType: ['', Validators.required],
73
    adminEmail: ['', [Validators.required, Validators.email] ]
74
  };
75
76
  softwarePlatformDesc: Description = softwarePlatformDesc;
77 57095 stefania.m
  platformNameDesc: Description = platformNameDesc;
78 54479 myrto.kouk
  officialNameDesc: Description = officialNameDesc;
79
  issnDesc: Description = issnDesc;
80
  eissnDesc: Description = eissnDesc;
81
  lissnDesc: Description = lissnDesc;
82
  repoDescriptionDesc: Description = repoDescriptionDesc;
83
  countryDesc: Description = countryDesc;
84
  longtitudeDesc: Description = longtitudeDesc;
85
  latitudeDesc: Description = latitudeDesc;
86
  websiteUrlDesc: Description = websiteUrlDesc;
87
  institutionNameDesc: Description = institutionNameDesc;
88
  englishNameDesc: Description = englishNameDesc;
89
  logoUrlDesc: Description = logoUrlDesc;
90
  timezoneDesc: Description = timezoneDesc;
91
  datasourceTypeDesc: Description = datasourceTypeDesc;
92
  adminEmailDesc: Description = adminEmailDesc;
93
94
  constructor(
95
    private fb: FormBuilder,
96
    private route: ActivatedRoute,
97
    private repoService: RepositoryService,
98
    private authService: AuthenticationService
99
  ) {}
100
101
  ngOnInit() {
102
    this.loadForm();
103
  }
104
105
  loadForm() {
106
    if (!this.mode) {
107
      this.mode = this.route.snapshot.url[0].path.toString();
108
      console.log(`my mode is ${this.mode}`);
109
    }
110
    this.group = this.fb.group(this.groupDefinition);
111
    if (this.mode === 'journal') {
112 54747 myrto.kouk
      this.group.get('issn').clearValidators();
113 54722 myrto.kouk
      this.group.get('issn').setValidators([Validators.required, Validators.pattern('^\\d{4}-\\d{3}[\\dxX]$')]);
114 54479 myrto.kouk
    }
115
    this.getTypologies();
116
    this.getTimezones();
117
    this.getCountries();
118
    this.getDatasourceClasses();
119
120
    if (this.selectedRepo) {
121
      this.setupForm();
122
    }
123
  }
124
125
  setupForm() {
126
    if (this.selectedRepo) {
127
      console.log(`my datasource type is: ${this.selectedRepo.datasourceType}`);
128
129
      this.group.setValue({
130
        softwarePlatform: this.selectedRepo.typology,
131 57095 stefania.m
        platformName: '',
132 54479 myrto.kouk
        officialName: this.selectedRepo.officialName,
133
        issn: '',
134
        eissn: '',
135
        lissn: '',
136
        repoDescription: this.selectedRepo.description,
137 62341 andreas.ma
        country: this.selectedRepo.organizations[0].country, // countryCode
138 54479 myrto.kouk
        longtitude: this.selectedRepo.longitude,
139
        latitude: this.selectedRepo.latitude,
140
        websiteUrl: this.selectedRepo.websiteUrl,
141 62341 andreas.ma
        institutionName: this.selectedRepo.organizations[0].legalname,
142 54479 myrto.kouk
        englishName: this.selectedRepo.englishName,
143
        logoUrl: this.selectedRepo.logoUrl,
144
        timezone: this.selectedRepo.timezone,
145 62341 andreas.ma
        datasourceType: this.selectedRepo.eoscDatasourceType,
146 54479 myrto.kouk
        adminEmail: this.selectedRepo.contactEmail
147
      });
148
149
      if (this.selectedRepo.datasourceType === 'journal') {
150
151
        let ssnToShow = this.selectedRepo.issn.slice(0, 4) + '-' + this.selectedRepo.issn.toString().slice(4);
152
        this.group.get('issn').setValue(ssnToShow);
153
154
        if (this.selectedRepo.eissn.trim().length) {
155
          ssnToShow = this.selectedRepo.eissn.slice(0, 4) + '-' + this.selectedRepo.eissn.toString().slice(4);
156
          this.group.get('eissn').setValue(ssnToShow);
157
        }
158
159
        if (this.selectedRepo.lissn.trim().length) {
160
          ssnToShow = this.selectedRepo.lissn.slice(0, 4) + '-' + this.selectedRepo.lissn.toString().slice(4);
161
          this.group.get('lissn').setValue(ssnToShow);
162
        }
163
164
      }
165
    }
166
  }
167
168
  getCountries() {
169
    this.repoService.getCountries()
170
      .subscribe(
171
        countries => this.countries = countries.sort( function(a, b) {
172
          if (a.name < b.name) {
173
            return -1;
174
          } else if (a.name > b.name) {
175
            return 1;
176
          } else {
177
            return 0;
178
          }
179
        } ),
180
        error => {
181
          this.errorMessage = noServiceMessage;
182
          console.log(error);
183
        });
184
  }
185
186
  getDatasourceClasses() {
187 62348 andreas.ma
    this.repoService.getDatasourceClasses(this.mode).subscribe(
188 54479 myrto.kouk
      classes => this.datasourceClasses = classes,
189
      error => {
190
        this.errorMessage = noServiceMessage;
191
        console.log(error);
192
      },
193
      () => {
194 55055 myrto.kouk
        for (const key of Object.keys(this.datasourceClasses)) {
195 54479 myrto.kouk
          this.classCodes.push(key);
196
        }
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 54747 myrto.kouk
      this.selectedRepo = this.createNewRepository();
223
      this.emittedInfo.emit(this.selectedRepo);
224 54479 myrto.kouk
    } else {
225
      this.errorMessage = formErrorRequiredFields;
226
    }
227
  }
228
229
  createNewRepository(): Repository {
230
    const newRepo: Repository = new Repository();
231 55056 myrto.kouk
    newRepo.officialName = this.group.get('officialName').value.toString();
232
    newRepo.englishName = this.group.get('englishName').value.toString();
233 54479 myrto.kouk
    newRepo.websiteUrl = this.group.get('websiteUrl').value;
234
    newRepo.logoUrl = this.group.get('logoUrl').value;
235
    newRepo.contactEmail = this.group.get('adminEmail').value;
236 62341 andreas.ma
    newRepo.organizations[0].country = this.group.get('country').value; // countryCode
237
    newRepo.organizations[0].legalname = this.group.get('institutionName').value.toString();
238 54479 myrto.kouk
    newRepo.latitude = this.group.get('latitude').value;
239
    newRepo.longitude = this.group.get('longtitude').value;
240
    newRepo.timezone = this.group.get('timezone').value;
241 62341 andreas.ma
    newRepo.eoscDatasourceType = this.group.get('datasourceType').value;
242 57095 stefania.m
    if (this.group.get('softwarePlatform').value ) {
243
      newRepo.typology = this.group.get('softwarePlatform').value;
244
    } else if (this.group.get('platformName').value) {
245
      newRepo.typology = this.group.get('platformName').value;
246
    }
247
    // newRepo.typology = this.group.get('softwarePlatform').value;
248 55056 myrto.kouk
    newRepo.description = this.group.get('repoDescription').value.toString();
249 54479 myrto.kouk
    newRepo.issn = '';
250
    newRepo.eissn = '';
251
    newRepo.lissn = '';
252
253
    if ( this.group.get('issn').value ) {
254
      let ssnParts = this.group.get('issn').value.split('-');
255
      let correctSSN = ssnParts[0] + ssnParts[1];
256
      newRepo.issn = correctSSN;
257
      if ( this.group.get('eissn').value ) {
258
        ssnParts = this.group.get('eissn').value.split('-');
259
        correctSSN = ssnParts[0] + ssnParts[1];
260
        newRepo.eissn = correctSSN;
261
      }
262
      if ( this.group.get('lissn').value ) {
263
        ssnParts = this.group.get('lissn').value.split('-');
264
        correctSSN = ssnParts[0] + ssnParts[1];
265
        newRepo.lissn = correctSSN;
266
      }
267
    }
268
269
    newRepo.registeredBy = this.authService.getUserEmail();
270
271
    /* THE BELOW FIELDS ARE NOT SET IN GWT CODE*/
272
    newRepo.datasourceType = this.mode;
273 62341 andreas.ma
    newRepo.managed = true;
274 54479 myrto.kouk
275 62347 andreas.ma
    const now = new Date(Date.now());
276
    newRepo.consentTermsOfUseDate = now;
277
    newRepo.lastConsentTermsOfUseDate = now;
278
    newRepo.registrationdate = now;
279 54479 myrto.kouk
    return newRepo;
280
  }
281
282
283
}