1
|
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
|
2
|
import { formErrorRequiredFields, formErrorWasntSaved, formSubmitting, formSuccessUpdatedRepo, loadingRepoError,
|
3
|
loadingRepoMessage, noServiceMessage } from '../../../domain/shared-messages';
|
4
|
import { RepositoryService } from '../../../services/repository.service';
|
5
|
import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
|
6
|
import { Country, Repository, Timezone, Typology } from '../../../domain/typeScriptClasses';
|
7
|
import { Description, softwarePlatformDesc, platformNameDesc, officialNameDesc, repoDescriptionDesc, countryDesc,
|
8
|
longtitudeDesc, latitudeDesc, websiteUrlDesc, institutionNameDesc, englishNameDesc, logoUrlDesc, timezoneDesc,
|
9
|
datasourceTypeDesc, adminEmailDesc, lissnDesc, eissnDesc, issnDesc } from '../../../domain/oa-description';
|
10
|
import { AuthenticationService } from '../../../services/authentication.service';
|
11
|
import {SharedService} from "../../../services/shared.service";
|
12
|
|
13
|
@Component ({
|
14
|
selector: 'datasource-update-form',
|
15
|
templateUrl: './datasource-update-form.component.html'
|
16
|
})
|
17
|
|
18
|
export class DatasourceUpdateFormComponent implements OnInit {
|
19
|
|
20
|
errorMessage: string;
|
21
|
successMessage: string;
|
22
|
loadingMessage: string;
|
23
|
|
24
|
typologies: Typology[] = [];
|
25
|
timezones: Timezone[] = [];
|
26
|
countries: Country[] = [];
|
27
|
datasourceClasses: Map<string, string> = new Map<string, string>();
|
28
|
classCodes: string[] = [];
|
29
|
|
30
|
/* in sources/register (in literature or data mode) the updated repository is emitted */
|
31
|
@Output() emittedInfo: EventEmitter<Repository> = new EventEmitter();
|
32
|
|
33
|
@Input() selectedRepo: Repository;
|
34
|
|
35
|
@Input() showButton: boolean;
|
36
|
|
37
|
repoId: string;
|
38
|
formSubmitted = false;
|
39
|
updateGroup: FormGroup;
|
40
|
readonly updateGroupDefinition = {
|
41
|
softwarePlatform : '',
|
42
|
platformName : '',
|
43
|
officialName : ['', Validators.required],
|
44
|
issn : ['', [Validators.pattern('^\\d{4}-\\d{3}[\\dxX]$')] ],
|
45
|
eissn : ['', Validators.pattern('^\\d{4}-\\d{3}[\\dxX]$') ],
|
46
|
lissn : ['', Validators.pattern('^\\d{4}-\\d{3}[\\dxX]$') ],
|
47
|
repoDescription : ['', Validators.required],
|
48
|
country : '',
|
49
|
longtitude : '',
|
50
|
latitude : '',
|
51
|
websiteUrl : [''],
|
52
|
institutionName : ['', Validators.required],
|
53
|
englishName: ['', Validators.required],
|
54
|
logoUrl: ['', Validators.pattern('^(http:\\/\\/www\\.|https:\\/\\/www\\.|http:\\/\\/|https:\\/\\/)?[a-z0-9]+([\\-\\.]{1}[a-z0-9]+)*\\.[a-z]{2,5}(:[0-9]{1,5})?(\\/.*)?$') ],
|
55
|
timezone: ['', Validators.required],
|
56
|
datasourceType: ['', Validators.required],
|
57
|
adminEmail: ['', [Validators.required, Validators.email]]
|
58
|
};
|
59
|
|
60
|
softwarePlatformDesc: Description = softwarePlatformDesc;
|
61
|
platformNameDesc: Description = platformNameDesc;
|
62
|
officialNameDesc: Description = officialNameDesc;
|
63
|
issnDesc: Description = issnDesc;
|
64
|
eissnDesc: Description = eissnDesc;
|
65
|
lissnDesc: Description = lissnDesc;
|
66
|
repoDescriptionDesc: Description = repoDescriptionDesc;
|
67
|
countryDesc: Description = countryDesc;
|
68
|
longtitudeDesc: Description = longtitudeDesc;
|
69
|
latitudeDesc: Description = latitudeDesc;
|
70
|
websiteUrlDesc: Description = websiteUrlDesc;
|
71
|
institutionNameDesc: Description = institutionNameDesc;
|
72
|
englishNameDesc: Description = englishNameDesc;
|
73
|
logoUrlDesc: Description = logoUrlDesc;
|
74
|
timezoneDesc: Description = timezoneDesc;
|
75
|
datasourceTypeDesc: Description = datasourceTypeDesc;
|
76
|
adminEmailDesc: Description = adminEmailDesc;
|
77
|
|
78
|
constructor(
|
79
|
private fb: FormBuilder,
|
80
|
private repoService: RepositoryService,
|
81
|
private sharedService: SharedService,
|
82
|
private authService: AuthenticationService
|
83
|
) {}
|
84
|
|
85
|
ngOnInit() {
|
86
|
this.loadForm();
|
87
|
}
|
88
|
|
89
|
loadForm() {
|
90
|
if (this.selectedRepo) {
|
91
|
this.repoId = this.selectedRepo.id.split('::')[1];
|
92
|
this.loadingMessage = loadingRepoMessage;
|
93
|
this.updateGroup = this.fb.group(this.updateGroupDefinition, {validator: checkPlatform});
|
94
|
this.getDatasourceClasses();
|
95
|
} else {
|
96
|
this.errorMessage = loadingRepoError;
|
97
|
}
|
98
|
}
|
99
|
|
100
|
setupUpdateForm() {
|
101
|
if (this.selectedRepo) {
|
102
|
console.log(`my datasource type is: ${this.selectedRepo.datasourceType}`);
|
103
|
|
104
|
this.updateGroup.setValue({
|
105
|
softwarePlatform: this.selectedRepo.typology,
|
106
|
platformName: '',
|
107
|
officialName: this.selectedRepo.officialName,
|
108
|
issn: '',
|
109
|
eissn: '',
|
110
|
lissn: '',
|
111
|
repoDescription: this.selectedRepo.description,
|
112
|
country: this.selectedRepo.countryCode,
|
113
|
longtitude: this.selectedRepo.longitude,
|
114
|
latitude: this.selectedRepo.latitude,
|
115
|
websiteUrl: this.selectedRepo.websiteUrl,
|
116
|
institutionName: this.selectedRepo.organization,
|
117
|
englishName: this.selectedRepo.englishName,
|
118
|
logoUrl: this.selectedRepo.logoUrl,
|
119
|
timezone: this.selectedRepo.timezone,
|
120
|
datasourceType: this.selectedRepo.datasourceClass,
|
121
|
adminEmail: this.selectedRepo.contactEmail
|
122
|
});
|
123
|
|
124
|
if ( this.selectedRepo.typology === '' || !this.typologies.some(x => x.value === this.selectedRepo.typology) ) {
|
125
|
this.updateGroup.get('softwarePlatform').setValue('');
|
126
|
this.updateGroup.get('platformName').setValue(this.selectedRepo.typology);
|
127
|
}
|
128
|
|
129
|
if ((this.selectedRepo.datasourceType === 'opendoar') ||
|
130
|
(this.selectedRepo.datasourceType === 're3data')) {
|
131
|
|
132
|
// this.updateGroup.get('officialName').disable();
|
133
|
this.updateGroup.get('country').disable();
|
134
|
// this.updateGroup.get('longtitude').disable();
|
135
|
// this.updateGroup.get('latitude').disable();
|
136
|
this.updateGroup.get('websiteUrl').disable();
|
137
|
// this.updateGroup.get('institutionName').disable();
|
138
|
|
139
|
}
|
140
|
|
141
|
if (this.selectedRepo.datasourceType === 'journal') {
|
142
|
|
143
|
let ssnToShow = this.selectedRepo.issn.slice(0, 4) + '-' + this.selectedRepo.issn.toString().slice(4);
|
144
|
this.updateGroup.get('issn').setValue(ssnToShow);
|
145
|
this.updateGroup.get('issn').clearValidators();
|
146
|
this.updateGroup.get('issn').setValidators([Validators.required, Validators.pattern('^\\d{4}-\\d{3}[\\dxX]$')]);
|
147
|
|
148
|
if (this.selectedRepo.eissn.trim().length) {
|
149
|
ssnToShow = this.selectedRepo.eissn.slice(0, 4) + '-' + this.selectedRepo.eissn.toString().slice(4);
|
150
|
this.updateGroup.get('eissn').setValue(ssnToShow);
|
151
|
}
|
152
|
|
153
|
if (this.selectedRepo.lissn.trim().length) {
|
154
|
ssnToShow = this.selectedRepo.lissn.slice(0, 4) + '-' + this.selectedRepo.lissn.toString().slice(4);
|
155
|
this.updateGroup.get('lissn').setValue(ssnToShow);
|
156
|
}
|
157
|
|
158
|
/* it was decided that all fields will be open, 21-12-2018 */
|
159
|
/*this.updateGroup.get('issn').disable();
|
160
|
this.updateGroup.get('eissn').disable();
|
161
|
this.updateGroup.get('lissn').disable();*/
|
162
|
}
|
163
|
/*this.getDatasourceClasses();*/
|
164
|
}
|
165
|
}
|
166
|
|
167
|
getDatasourceClasses() {
|
168
|
this.repoService.getDatasourceClasses(this.selectedRepo.datasourceType).subscribe(
|
169
|
classes => this.datasourceClasses = classes,
|
170
|
error => {
|
171
|
this.loadingMessage = '';
|
172
|
this.errorMessage = noServiceMessage;
|
173
|
console.log(error);
|
174
|
},
|
175
|
() => {
|
176
|
for (const key of Object.keys(this.datasourceClasses)) {
|
177
|
this.classCodes.push(key);
|
178
|
}
|
179
|
this.getCountries();
|
180
|
}
|
181
|
);
|
182
|
}
|
183
|
|
184
|
getCountries() {
|
185
|
this.repoService.getCountries()
|
186
|
.subscribe(
|
187
|
countries => this.countries = countries.sort( function(a, b) {
|
188
|
if (a.name < b.name) {
|
189
|
return -1;
|
190
|
} else if (a.name > b.name) {
|
191
|
return 1;
|
192
|
} else {
|
193
|
return 0;
|
194
|
}
|
195
|
} ),
|
196
|
error => {
|
197
|
this.loadingMessage = '';
|
198
|
this.errorMessage = noServiceMessage;
|
199
|
console.log(error);
|
200
|
}, () => {
|
201
|
this.getTypologies();
|
202
|
});
|
203
|
}
|
204
|
|
205
|
getTypologies() {
|
206
|
this.repoService.getTypologies().subscribe(
|
207
|
types => this.typologies = types,
|
208
|
error => {
|
209
|
this.loadingMessage = '';
|
210
|
console.log(error);
|
211
|
},
|
212
|
() => {
|
213
|
this.getTimezones();
|
214
|
}
|
215
|
);
|
216
|
}
|
217
|
|
218
|
getTimezones() {
|
219
|
this.repoService.getTimezones().subscribe(
|
220
|
zones => this.timezones = zones,
|
221
|
error => {
|
222
|
this.loadingMessage = '';
|
223
|
console.log(error);
|
224
|
},
|
225
|
() => {
|
226
|
this.loadingMessage = '';
|
227
|
this.setupUpdateForm();
|
228
|
}
|
229
|
);
|
230
|
}
|
231
|
|
232
|
updateRepo() {
|
233
|
this.formSubmitted = true;
|
234
|
this.errorMessage = '';
|
235
|
this.successMessage = '';
|
236
|
window.scroll(1, 1);
|
237
|
|
238
|
if (this.updateGroup.valid) {
|
239
|
if ( this.selectedRepo.datasourceType !== 'journal' || this.updateGroup.get('issn').value ) {
|
240
|
this.refreshSelectedRepo();
|
241
|
|
242
|
/*
|
243
|
call the api only if the current page is sources/update
|
244
|
[otherwise the repository will be updated during the registration procedure, after the first interface is saved]
|
245
|
*/
|
246
|
if (this.showButton) {
|
247
|
this.loadingMessage = formSubmitting;
|
248
|
this.errorMessage = '';
|
249
|
// this.repoService.up
|
250
|
this.repoService.updateRepository(this.selectedRepo).subscribe(
|
251
|
response => {
|
252
|
if (response) {
|
253
|
this.selectedRepo = response;
|
254
|
console.log(`updateRepository responded: ${JSON.stringify(response)}`);
|
255
|
}
|
256
|
},
|
257
|
error => {
|
258
|
console.log(error);
|
259
|
this.loadingMessage = '';
|
260
|
this.errorMessage = formErrorWasntSaved;
|
261
|
},
|
262
|
() => {
|
263
|
this.loadingMessage = '';
|
264
|
if (!this.selectedRepo) {
|
265
|
this.errorMessage = formErrorWasntSaved;
|
266
|
} else {
|
267
|
this.successMessage = formSuccessUpdatedRepo;
|
268
|
}
|
269
|
//fixme is this the place to update the subject??
|
270
|
this.sharedService.setRepository(this.selectedRepo);
|
271
|
}
|
272
|
);
|
273
|
}
|
274
|
} else {
|
275
|
this.errorMessage = formErrorRequiredFields;
|
276
|
}
|
277
|
} else {
|
278
|
this.errorMessage = formErrorRequiredFields;
|
279
|
}
|
280
|
}
|
281
|
|
282
|
refreshSelectedRepo() {
|
283
|
if (this.updateGroup.get('softwarePlatform').value ) {
|
284
|
this.selectedRepo.typology = this.updateGroup.get('softwarePlatform').value;
|
285
|
} else if (this.updateGroup.get('platformName').value) {
|
286
|
this.selectedRepo.typology = this.updateGroup.get('platformName').value;
|
287
|
}
|
288
|
this.selectedRepo.officialName = this.updateGroup.get('officialName').value.toString();
|
289
|
this.selectedRepo.description = this.updateGroup.get('repoDescription').value.toString();
|
290
|
this.selectedRepo.countryCode = this.updateGroup.get('country').value;
|
291
|
this.selectedRepo.countryName = this.countries.filter(x => x.code === this.updateGroup.get('country').value)[0].name;
|
292
|
this.selectedRepo.longitude = this.updateGroup.get('longtitude').value;
|
293
|
this.selectedRepo.latitude = this.updateGroup.get('latitude').value;
|
294
|
this.selectedRepo.websiteUrl = this.updateGroup.get('websiteUrl').value;
|
295
|
this.selectedRepo.organization = this.updateGroup.get('institutionName').value.toString();
|
296
|
this.selectedRepo.englishName = this.updateGroup.get('englishName').value.toString();
|
297
|
this.selectedRepo.logoUrl = this.updateGroup.get('logoUrl').value;
|
298
|
this.selectedRepo.timezone = this.updateGroup.get('timezone').value;
|
299
|
this.selectedRepo.datasourceClass = this.updateGroup.get('datasourceType').value;
|
300
|
this.selectedRepo.contactEmail = this.updateGroup.get('adminEmail').value;
|
301
|
if (this.selectedRepo.datasourceType === 'journal') {
|
302
|
let ssnParts = this.updateGroup.get('issn').value.split('-');
|
303
|
let correctSSN = ssnParts[0] + ssnParts[1];
|
304
|
this.selectedRepo.issn = correctSSN;
|
305
|
if ( this.updateGroup.get('eissn').value ) {
|
306
|
ssnParts = this.updateGroup.get('eissn').value.split('-');
|
307
|
correctSSN = ssnParts[0] + ssnParts[1];
|
308
|
this.selectedRepo.eissn = correctSSN;
|
309
|
}
|
310
|
if ( this.updateGroup.get('lissn').value ) {
|
311
|
ssnParts = this.updateGroup.get('lissn').value.split('-');
|
312
|
correctSSN = ssnParts[0] + ssnParts[1];
|
313
|
this.selectedRepo.lissn = correctSSN;
|
314
|
}
|
315
|
}
|
316
|
if (!this.showButton) {
|
317
|
this.selectedRepo.registeredBy = this.authService.getUserEmail();
|
318
|
this.selectedRepo.registered = true;
|
319
|
this.selectedRepo.registrationDate = new Date(Date.now()); // NOT NEEDED ??
|
320
|
this.emittedInfo.emit(this.selectedRepo);
|
321
|
}
|
322
|
}
|
323
|
|
324
|
}
|
325
|
|
326
|
export function checkPlatform(c: AbstractControl) {
|
327
|
if ( c.get('softwarePlatform').value || c.get('platformName').value ) {
|
328
|
return null;
|
329
|
}
|
330
|
return 'invalid';
|
331
|
}
|