Project

General

Profile

1
/*
2
*  updated by myrto on 19/12/2018
3
*/
4

    
5
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
6
import { Country, Repository, Timezone, Typology } from '../../../domain/typeScriptClasses';
7
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
8
import { adminEmailDesc, countryDesc, datasourceTypeDesc, Description, eissnDesc, englishNameDesc, institutionNameDesc,
9
         issnDesc, latitudeDesc, lissnDesc, logoUrlDesc, longtitudeDesc, officialNameDesc, repoDescriptionDesc,
10
         softwarePlatformDesc, timezoneDesc, websiteUrlDesc } from '../../../domain/oa-description';
11
import { ActivatedRoute } from '@angular/router';
12
import { RepositoryService } from '../../../services/repository.service';
13
import { AuthenticationService } from '../../../services/authentication.service';
14
import { formErrorRequiredFields, noServiceMessage } from '../../../domain/shared-messages';
15

    
16
@Component ({
17
  selector: 'datasource-create-form',
18
  templateUrl: './datasource-create-form.component.html'
19
})
20

    
21
export class DatasourceCreateFormComponent implements OnInit {
22
  errorMessage: string;
23
  successMessage: string;
24
  loadingMessage: string;
25

    
26
  typologies: Typology[] = [];
27
  timezones: Timezone[] = [];
28
  countries: Country[] = [];
29
  datasourceClasses: Map<string, string> = new Map<string, string>();
30
  classCodes: string[] = [];
31

    
32
  @Input() mode: string;
33

    
34
  @Output() emittedInfo: EventEmitter<Repository> = new EventEmitter();
35

    
36
  @Input() selectedRepo: Repository;
37

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

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

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

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

    
87
  loadForm() {
88
    if (!this.mode) {
89
      this.mode = this.route.snapshot.url[0].path.toString();
90
      console.log(`my mode is ${this.mode}`);
91
    }
92
    this.group = this.fb.group(this.groupDefinition);
93
    if (this.mode === 'journal') {
94
      this.group.get('issn').setValidators([Validators.required, Validators.pattern('^\\d\\d\\d\\d[-]\\d\\d\\d\\d$')]);
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 in 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
      if ( this.mode !== 'journal' || this.group.get('issn').value ) {
203
        if (this.selectedRepo) {
204
          this.emittedInfo.emit(this.selectedRepo);
205
        } else {
206
          const newRepo = this.createNewRepository();
207
          this.emittedInfo.emit(newRepo);
208
        }
209
      } else {
210
        this.errorMessage = formErrorRequiredFields;
211
      }
212
    } else {
213
      this.errorMessage = formErrorRequiredFields;
214
    }
215
  }
216

    
217
  createNewRepository(): Repository {
218
    const newRepo: Repository = new Repository();
219
    newRepo.officialName = this.group.get('officialName').value;
220
    newRepo.englishName = this.group.get('englishName').value;
221
    newRepo.websiteUrl = this.group.get('websiteUrl').value;
222
    newRepo.logoUrl = this.group.get('logoUrl').value;
223
    newRepo.contactEmail = this.group.get('adminEmail').value;
224
    newRepo.countryName = this.countries.filter(x => x.code === this.group.get('country').value)[0].name;
225
    newRepo.countryCode = this.group.get('country').value;
226
    newRepo.organization = this.group.get('institutionName').value;
227
    newRepo.latitude = this.group.get('latitude').value;
228
    newRepo.longitude = this.group.get('longtitude').value;
229
    newRepo.timezone = this.group.get('timezone').value;
230
    newRepo.datasourceClass = this.group.get('datasourceType').value;
231
    newRepo.typology = this.group.get('softwarePlatform').value;
232
    newRepo.description = this.group.get('repoDescription').value;
233
    newRepo.issn = '';
234
    newRepo.eissn = '';
235
    newRepo.lissn = '';
236

    
237
    if ( this.group.get('issn').value ) {
238
      let ssnParts = this.group.get('issn').value.split('-');
239
      let correctSSN = ssnParts[0] + ssnParts[1];
240
      newRepo.issn = correctSSN;
241
      if ( this.group.get('eissn').value ) {
242
        ssnParts = this.group.get('eissn').value.split('-');
243
        correctSSN = ssnParts[0] + ssnParts[1];
244
        newRepo.eissn = correctSSN;
245
      }
246
      if ( this.group.get('lissn').value ) {
247
        ssnParts = this.group.get('lissn').value.split('-');
248
        correctSSN = ssnParts[0] + ssnParts[1];
249
        newRepo.lissn = correctSSN;
250
      }
251
    }
252

    
253
    newRepo.registeredBy = this.authService.getUserEmail();
254

    
255
    /* THE BELOW FIELDS ARE NOT SET IN GWT CODE*/
256
    newRepo.datasourceType = this.mode;
257
    newRepo.dateOfCreation = new Date(Date.now()); // NOT NEEDED ??
258
    newRepo.registered = true;
259
    newRepo.registrationDate = new Date(Date.now()); // NOT NEEDED ??
260

    
261
    return newRepo;
262
  }
263

    
264

    
265
}
(2-2/8)