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 { adminEmailDesc, countryDesc, datasourceTypeDesc, Description, eissnDesc, englishNameDesc, institutionNameDesc,
5
         issnDesc, latitudeDesc, lissnDesc, logoUrlDesc, longtitudeDesc, officialNameDesc, repoDescriptionDesc,
6
         softwarePlatformDesc, timezoneDesc, websiteUrlDesc } from '../../../domain/oa-description';
7
import { ActivatedRoute } from '@angular/router';
8
import { RepositoryService } from '../../../services/repository.service';
9
import { AuthenticationService } from '../../../services/authentication.service';
10
import { formErrorRequiredFields, noServiceMessage } from '../../../domain/shared-messages';
11

    
12
@Component ({
13
  selector: 'datasource-create-form',
14
  templateUrl: './datasource-create-form.component.html'
15
})
16

    
17
export class DatasourceCreateFormComponent implements OnInit {
18
  errorMessage: string;
19
  successMessage: string;
20
  loadingMessage: string;
21

    
22
  typologies: Typology[] = [];
23
  timezones: Timezone[] = [];
24
  countries: Country[] = [];
25
  datasourceClasses: Map<string, string> = new Map<string, string>();
26
  classCodes: string[] = [];
27

    
28
  @Input() mode: string;
29

    
30
  @Output() emittedInfo: EventEmitter<Repository> = new EventEmitter();
31

    
32
  @Input() selectedRepo: Repository;
33

    
34
  formSubmitted = false;
35
  group: FormGroup;
36

    
37
  // old issn regex
38
  // issn : ['', [Validators.pattern('^\\d\\d\\d\\d[-]\\d\\d\\d\\d$')] ],
39
  readonly groupDefinition = {
40
    softwarePlatform : ['', Validators.required],
41
    officialName : ['', Validators.required],
42
    issn : ['', [Validators.pattern('^\\d{4}-\\d{3}[\\dxX]$')] ],
43
    eissn : ['', Validators.pattern('^\\d{4}-\\d{3}[\\dxX]$') ],
44
    lissn : ['', Validators.pattern('^\\d{4}-\\d{3}[\\dxX]$') ],
45
    repoDescription : ['', Validators.required],
46
    country : ['', Validators.required],
47
    longtitude : ['', [Validators.required, Validators.min(-180), Validators.max(180)] ],
48
    latitude : ['', [Validators.required, Validators.min(-90), Validators.max(90)] ],
49
    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})?(\\/.*)?$')] ],
50
    institutionName : ['', Validators.required],
51
    englishName: ['', Validators.required],
52
    logoUrl: ['', Validators.pattern('^(http:\\/\\/www\\.|https:\\/\\/www\\.|http:\\/\\/|https:\\/\\/)?[a-z0-9]+([\\-\\.]{1}[a-z0-9]+)*\\.[a-z]{2,5}(:[0-9]{1,5})?(\\/.*)?$') ],
53
    timezone: ['', Validators.required],
54
    datasourceType: ['', Validators.required],
55
    adminEmail: ['', [Validators.required, Validators.email] ]
56
  };
57

    
58
  softwarePlatformDesc: Description = softwarePlatformDesc;
59
  officialNameDesc: Description = officialNameDesc;
60
  issnDesc: Description = issnDesc;
61
  eissnDesc: Description = eissnDesc;
62
  lissnDesc: Description = lissnDesc;
63
  repoDescriptionDesc: Description = repoDescriptionDesc;
64
  countryDesc: Description = countryDesc;
65
  longtitudeDesc: Description = longtitudeDesc;
66
  latitudeDesc: Description = latitudeDesc;
67
  websiteUrlDesc: Description = websiteUrlDesc;
68
  institutionNameDesc: Description = institutionNameDesc;
69
  englishNameDesc: Description = englishNameDesc;
70
  logoUrlDesc: Description = logoUrlDesc;
71
  timezoneDesc: Description = timezoneDesc;
72
  datasourceTypeDesc: Description = datasourceTypeDesc;
73
  adminEmailDesc: Description = adminEmailDesc;
74

    
75
  constructor(
76
    private fb: FormBuilder,
77
    private route: ActivatedRoute,
78
    private repoService: RepositoryService,
79
    private authService: AuthenticationService
80
  ) {}
81

    
82
  ngOnInit() {
83
    this.loadForm();
84
  }
85

    
86
  loadForm() {
87
    if (!this.mode) {
88
      this.mode = this.route.snapshot.url[0].path.toString();
89
      console.log(`my mode is ${this.mode}`);
90
    }
91
    this.group = this.fb.group(this.groupDefinition);
92
    if (this.mode === 'journal') {
93
      this.group.get('issn').clearValidators();
94
      this.group.get('issn').setValidators([Validators.required, Validators.pattern('^\\d{4}-\\d{3}[\\dxX]$')]);
95
    }
96
    this.getTypologies();
97
    this.getTimezones();
98
    this.getCountries();
99
    this.getDatasourceClasses();
100

    
101
    if (this.selectedRepo) {
102
      this.setupForm();
103
    }
104
  }
105

    
106
  setupForm() {
107
    if (this.selectedRepo) {
108
      console.log(`my datasource type is: ${this.selectedRepo.datasourceType}`);
109

    
110
      this.group.setValue({
111
        softwarePlatform: this.selectedRepo.typology,
112
        officialName: this.selectedRepo.officialName,
113
        issn: '',
114
        eissn: '',
115
        lissn: '',
116
        repoDescription: this.selectedRepo.description,
117
        country: this.selectedRepo.countryCode,
118
        longtitude: this.selectedRepo.longitude,
119
        latitude: this.selectedRepo.latitude,
120
        websiteUrl: this.selectedRepo.websiteUrl,
121
        institutionName: this.selectedRepo.organization,
122
        englishName: this.selectedRepo.englishName,
123
        logoUrl: this.selectedRepo.logoUrl,
124
        timezone: this.selectedRepo.timezone,
125
        datasourceType: this.selectedRepo.datasourceClass,
126
        adminEmail: this.selectedRepo.contactEmail
127
      });
128

    
129
      if (this.selectedRepo.datasourceType === 'journal') {
130

    
131
        let ssnToShow = this.selectedRepo.issn.slice(0, 4) + '-' + this.selectedRepo.issn.toString().slice(4);
132
        this.group.get('issn').setValue(ssnToShow);
133

    
134
        if (this.selectedRepo.eissn.trim().length) {
135
          ssnToShow = this.selectedRepo.eissn.slice(0, 4) + '-' + this.selectedRepo.eissn.toString().slice(4);
136
          this.group.get('eissn').setValue(ssnToShow);
137
        }
138

    
139
        if (this.selectedRepo.lissn.trim().length) {
140
          ssnToShow = this.selectedRepo.lissn.slice(0, 4) + '-' + this.selectedRepo.lissn.toString().slice(4);
141
          this.group.get('lissn').setValue(ssnToShow);
142
        }
143

    
144
      }
145
    }
146
  }
147

    
148
  getCountries() {
149
    this.repoService.getCountries()
150
      .subscribe(
151
        countries => this.countries = countries.sort( function(a, b) {
152
          if (a.name < b.name) {
153
            return -1;
154
          } else if (a.name > b.name) {
155
            return 1;
156
          } else {
157
            return 0;
158
          }
159
        } ),
160
        error => {
161
          this.errorMessage = noServiceMessage;
162
          console.log(error);
163
        });
164
  }
165

    
166
  getDatasourceClasses() {
167
    this.repoService.getDatasourceClasses(this.mode).subscribe(
168
      classes => this.datasourceClasses = classes,
169
      error => {
170
        this.errorMessage = noServiceMessage;
171
        console.log(error);
172
      },
173
      () => {
174
        for (const key of Object.keys(this.datasourceClasses)) {
175
          this.classCodes.push(key);
176
        }
177
      }
178
    );
179
  }
180

    
181
  getTypologies() {
182
    this.repoService.getTypologies().subscribe(
183
      types => this.typologies = types,
184
      error => console.log(error)
185
    );
186
  }
187

    
188
  getTimezones() {
189
    this.repoService.getTimezones().subscribe(
190
      zones => this.timezones = zones,
191
      error => console.log(error)
192
    );
193
  }
194

    
195
  registerDatasource() {
196
    this.formSubmitted = true;
197
    this.errorMessage = '';
198
    this.successMessage = '';
199
    window.scroll(1, 1);
200

    
201
    if (this.group.valid) {
202
      this.selectedRepo = this.createNewRepository();
203
      this.emittedInfo.emit(this.selectedRepo);
204
    } else {
205
      this.errorMessage = formErrorRequiredFields;
206
    }
207
  }
208

    
209
  createNewRepository(): Repository {
210
    const newRepo: Repository = new Repository();
211
    newRepo.officialName = this.group.get('officialName').value.toString();
212
    newRepo.englishName = this.group.get('englishName').value.toString();
213
    newRepo.websiteUrl = this.group.get('websiteUrl').value;
214
    newRepo.logoUrl = this.group.get('logoUrl').value;
215
    newRepo.contactEmail = this.group.get('adminEmail').value;
216
    newRepo.countryName = this.countries.filter(x => x.code === this.group.get('country').value)[0].name;
217
    newRepo.countryCode = this.group.get('country').value;
218
    newRepo.organization = this.group.get('institutionName').value.toString();
219
    newRepo.latitude = this.group.get('latitude').value;
220
    newRepo.longitude = this.group.get('longtitude').value;
221
    newRepo.timezone = this.group.get('timezone').value;
222
    newRepo.datasourceClass = this.group.get('datasourceType').value;
223
    newRepo.typology = this.group.get('softwarePlatform').value;
224
    newRepo.description = this.group.get('repoDescription').value.toString();
225
    newRepo.issn = '';
226
    newRepo.eissn = '';
227
    newRepo.lissn = '';
228

    
229
    if ( this.group.get('issn').value ) {
230
      let ssnParts = this.group.get('issn').value.split('-');
231
      let correctSSN = ssnParts[0] + ssnParts[1];
232
      newRepo.issn = correctSSN;
233
      if ( this.group.get('eissn').value ) {
234
        ssnParts = this.group.get('eissn').value.split('-');
235
        correctSSN = ssnParts[0] + ssnParts[1];
236
        newRepo.eissn = correctSSN;
237
      }
238
      if ( this.group.get('lissn').value ) {
239
        ssnParts = this.group.get('lissn').value.split('-');
240
        correctSSN = ssnParts[0] + ssnParts[1];
241
        newRepo.lissn = correctSSN;
242
      }
243
    }
244

    
245
    newRepo.registeredBy = this.authService.getUserEmail();
246

    
247
    /* THE BELOW FIELDS ARE NOT SET IN GWT CODE*/
248
    newRepo.datasourceType = this.mode;
249
    newRepo.dateOfCreation = new Date(Date.now()); // NOT NEEDED ??
250
    newRepo.registered = true;
251
    newRepo.registrationDate = new Date(Date.now()); // NOT NEEDED ??
252

    
253
    return newRepo;
254
  }
255

    
256

    
257
}
(2-2/8)