1
|
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
|
2
|
import {
|
3
|
formErrorRequiredFields,
|
4
|
formErrorWasntSaved,
|
5
|
formInfoLoading, formSubmitting,
|
6
|
formSuccessUpdatedRepo, loadingRepoError,
|
7
|
noServiceMessage
|
8
|
} from '../../../domain/shared-messages';
|
9
|
import { RepositoryService } from "../../../services/repository.service";
|
10
|
import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
|
11
|
import { Country, Repository } from '../../../domain/typeScriptClasses';
|
12
|
import { typologies } from '../../../domain/typologies';
|
13
|
import { timezones } from '../../../domain/timezones';
|
14
|
import {
|
15
|
Description,
|
16
|
softwarePlatformDesc,
|
17
|
platformNameDesc,
|
18
|
officialNameDesc,
|
19
|
repoDescriptionDesc,
|
20
|
countryDesc,
|
21
|
longtitudeDesc,
|
22
|
latitudeDesc,
|
23
|
websiteUrlDesc,
|
24
|
institutionNameDesc,
|
25
|
englishNameDesc,
|
26
|
logoUrlDesc,
|
27
|
timezoneDesc,
|
28
|
datasourceTypeDesc,
|
29
|
adminEmailDesc, lissnDesc, eissnDesc, issnDesc
|
30
|
} from '../../../domain/oa-description';
|
31
|
|
32
|
@Component ({
|
33
|
selector: 'datasource-info-form',
|
34
|
templateUrl: './datasource-info-form.component.html'
|
35
|
})
|
36
|
|
37
|
export class DatasourceInfoFormComponent implements OnInit {
|
38
|
|
39
|
errorMessage: string;
|
40
|
successMessage: string;
|
41
|
loadingMessage: string;
|
42
|
|
43
|
typologies = typologies;
|
44
|
timezones = timezones;
|
45
|
countries: Country[] = [];
|
46
|
datasourceClasses: Map<string,string> = new Map<string,string>();
|
47
|
classCodes: string[] = [];
|
48
|
|
49
|
selectedRepo: Repository;
|
50
|
id: string;
|
51
|
|
52
|
|
53
|
@Input() datasourceId: string;
|
54
|
|
55
|
@Input() showButton: boolean;
|
56
|
|
57
|
@Output() emittedInfo: EventEmitter<Repository> = new EventEmitter();
|
58
|
|
59
|
updateGroup: FormGroup;
|
60
|
readonly updateGroupDefinition = {
|
61
|
softwarePlatform : '',
|
62
|
platformName : '',
|
63
|
officialName : ['', Validators.required],
|
64
|
issn : ['', [Validators.minLength(8), Validators.maxLength(8)]],
|
65
|
eissn : ['', [Validators.minLength(8), Validators.maxLength(8)]],
|
66
|
lissn : ['', [Validators.minLength(8), Validators.maxLength(8)]],
|
67
|
repoDescription : ['', Validators.required],
|
68
|
country : ['', Validators.required],
|
69
|
longtitude : ['', [Validators.required, Validators.maxLength(9), Validators.min(-180), Validators.max(180)] ],
|
70
|
latitude : ['', [Validators.required, Validators.maxLength(9), Validators.min(-90), Validators.max(90)] ],
|
71
|
websiteUrl : ['', Validators.required],
|
72
|
institutionName : ['', Validators.required],
|
73
|
englishName: ['', Validators.required],
|
74
|
logoUrl: '',
|
75
|
timezone: ['', Validators.required],
|
76
|
datasourceType: ['', Validators.required],
|
77
|
adminEmail: ['', [Validators.required, Validators.email]]
|
78
|
};
|
79
|
|
80
|
softwarePlatformDesc : Description = softwarePlatformDesc;
|
81
|
platformNameDesc : Description = platformNameDesc;
|
82
|
officialNameDesc : Description = officialNameDesc;
|
83
|
issnDesc : Description = issnDesc;
|
84
|
eissnDesc : Description = eissnDesc;
|
85
|
lissnDesc : Description = lissnDesc;
|
86
|
repoDescriptionDesc : Description = repoDescriptionDesc;
|
87
|
countryDesc : Description = countryDesc;
|
88
|
longtitudeDesc : Description = longtitudeDesc;
|
89
|
latitudeDesc : Description = latitudeDesc;
|
90
|
websiteUrlDesc : Description = websiteUrlDesc;
|
91
|
institutionNameDesc : Description = institutionNameDesc;
|
92
|
englishNameDesc : Description = englishNameDesc;
|
93
|
logoUrlDesc : Description = logoUrlDesc;
|
94
|
timezoneDesc : Description = timezoneDesc;
|
95
|
datasourceTypeDesc : Description = datasourceTypeDesc;
|
96
|
adminEmailDesc : Description = adminEmailDesc;
|
97
|
|
98
|
constructor(
|
99
|
private fb: FormBuilder,
|
100
|
private repoService: RepositoryService
|
101
|
) {}
|
102
|
|
103
|
ngOnInit(){
|
104
|
this.loadForm();
|
105
|
}
|
106
|
|
107
|
loadForm() {
|
108
|
this.updateGroup = this.fb.group(this.updateGroupDefinition, {validator: checkPlatform});
|
109
|
this.getRepo();
|
110
|
}
|
111
|
|
112
|
getRepo() {
|
113
|
this.loadingMessage = formInfoLoading;
|
114
|
if (this.datasourceId) {
|
115
|
this.repoService.getRepositoryById(this.datasourceId).subscribe(
|
116
|
repo => {
|
117
|
this.selectedRepo = repo;
|
118
|
},
|
119
|
error => {
|
120
|
console.log(error);
|
121
|
this.loadingMessage = '';
|
122
|
this.errorMessage = loadingRepoError;
|
123
|
},
|
124
|
() => {
|
125
|
this.setupUpdateForm();
|
126
|
this.getDatasourceClasses();
|
127
|
this.getCountries();
|
128
|
this.emittedInfo.emit(this.selectedRepo);
|
129
|
this.loadingMessage = '';
|
130
|
}
|
131
|
);
|
132
|
}
|
133
|
}
|
134
|
|
135
|
setupUpdateForm(){
|
136
|
if (this.selectedRepo) {
|
137
|
this.updateGroup.setValue({
|
138
|
softwarePlatform: this.selectedRepo.typology,
|
139
|
platformName: '',
|
140
|
officialName: this.selectedRepo.officialName,
|
141
|
repoDescription: this.selectedRepo.description,
|
142
|
country: this.selectedRepo.countryCode,
|
143
|
longtitude: this.selectedRepo.longitude,
|
144
|
latitude: this.selectedRepo.latitude,
|
145
|
websiteUrl: this.selectedRepo.websiteUrl,
|
146
|
institutionName: this.selectedRepo.organization,
|
147
|
englishName: this.selectedRepo.englishName,
|
148
|
logoUrl: this.selectedRepo.logoUrl,
|
149
|
timezone: this.selectedRepo.timezone,
|
150
|
datasourceType: this.selectedRepo.datasourceClass,
|
151
|
adminEmail: this.selectedRepo.contactEmail
|
152
|
});
|
153
|
if (this.updateGroup.get('softwarePlatform').value == '') {
|
154
|
this.updateGroup.setValue({platformName: this.selectedRepo.typology});
|
155
|
}
|
156
|
if (this.selectedRepo.datasourceType == 'journal') {
|
157
|
this.updateGroup.setValue({
|
158
|
issn: this.selectedRepo.issn,
|
159
|
eissn: this.selectedRepo.eissn,
|
160
|
lissn: this.selectedRepo.lissn
|
161
|
});
|
162
|
}
|
163
|
}
|
164
|
}
|
165
|
|
166
|
getCountries(){
|
167
|
this.repoService.getCountries()
|
168
|
.subscribe(
|
169
|
countries => this.countries = countries.sort( function(a,b){
|
170
|
if(a.name<b.name){
|
171
|
return -1;
|
172
|
} else if(a.name>b.name){
|
173
|
return 1;
|
174
|
} else {
|
175
|
return 0;
|
176
|
}
|
177
|
} ),
|
178
|
error => {
|
179
|
this.errorMessage = noServiceMessage;
|
180
|
console.log(error);
|
181
|
});
|
182
|
}
|
183
|
|
184
|
getDatasourceClasses() {
|
185
|
this.repoService.getDatasourceClasses(this.selectedRepo.datasourceType).subscribe(
|
186
|
classes => this.datasourceClasses = classes,
|
187
|
error => {
|
188
|
this.errorMessage = noServiceMessage;
|
189
|
console.log(error);
|
190
|
},
|
191
|
() => {
|
192
|
for (let key in this.datasourceClasses){
|
193
|
this.classCodes.push(key);
|
194
|
}
|
195
|
}
|
196
|
);
|
197
|
}
|
198
|
|
199
|
updateRepo(): boolean {
|
200
|
let result: boolean;
|
201
|
this.errorMessage = '';
|
202
|
this.loadingMessage = '';
|
203
|
this.successMessage = '';
|
204
|
|
205
|
if (this.updateGroup.valid) {
|
206
|
if ( this.selectedRepo.datasourceType != 'journal' || this.updateGroup.get('issn').value ) {
|
207
|
this.refreshSelectedRepo();
|
208
|
this.loadingMessage = formSubmitting;
|
209
|
this.errorMessage = '';
|
210
|
this.repoService.updateRepository(this.selectedRepo).subscribe(
|
211
|
response => {
|
212
|
console.log(`updateRepository responded: ${response}`);
|
213
|
if (response == '200') {
|
214
|
result = true;
|
215
|
} else {
|
216
|
result = false;
|
217
|
}
|
218
|
},
|
219
|
error => {
|
220
|
console.log(error);
|
221
|
this.errorMessage = formErrorWasntSaved;
|
222
|
},
|
223
|
() => {
|
224
|
this.loadingMessage = '';
|
225
|
if (result) {
|
226
|
this.successMessage = formSuccessUpdatedRepo;
|
227
|
this.emittedInfo.emit(this.selectedRepo);
|
228
|
} else {
|
229
|
this.errorMessage = formErrorWasntSaved;
|
230
|
}
|
231
|
}
|
232
|
);
|
233
|
} else {
|
234
|
this.errorMessage = formErrorRequiredFields;
|
235
|
result = false;
|
236
|
}
|
237
|
} else {
|
238
|
this.errorMessage = formErrorRequiredFields;
|
239
|
result = false;
|
240
|
}
|
241
|
return result;
|
242
|
}
|
243
|
|
244
|
refreshSelectedRepo() {
|
245
|
if (this.updateGroup.get('platformName').value.trim() ) {
|
246
|
this.selectedRepo.typology = this.updateGroup.get('platformName').value;
|
247
|
} else if (this.updateGroup.get('softwarePlatform').value){
|
248
|
this.selectedRepo.typology = this.updateGroup.get('softwarePlatform').value;
|
249
|
}
|
250
|
this.selectedRepo.officialName = this.updateGroup.get('officialName').value;
|
251
|
this.selectedRepo.description = this.updateGroup.get('repoDescription').value;
|
252
|
this.selectedRepo.countryCode = this.updateGroup.get('country').value;
|
253
|
this.selectedRepo.countryName = this.countries.filter(x => x.code == this.updateGroup.get('country').value)[0].name;
|
254
|
this.selectedRepo.longitude = this.updateGroup.get('longtitude').value;
|
255
|
this.selectedRepo.latitude = this.updateGroup.get('latitude').value;
|
256
|
this.selectedRepo.websiteUrl = this.updateGroup.get('websiteUrl').value;
|
257
|
this.selectedRepo.organization = this.updateGroup.get('institutionName').value;
|
258
|
this.selectedRepo.englishName = this.updateGroup.get('englishName').value;
|
259
|
this.selectedRepo.logoUrl = this.updateGroup.get('logoUrl').value;
|
260
|
this.selectedRepo.timezone = this.updateGroup.get('timezone').value;
|
261
|
this.selectedRepo.datasourceClass = this.updateGroup.get('datasourceType').value;
|
262
|
this.selectedRepo.contactEmail = this.updateGroup.get('adminEmail').value;
|
263
|
if (this.selectedRepo.datasourceType == 'journal') {
|
264
|
this.selectedRepo.issn = this.updateGroup.get('issn').value;
|
265
|
this.selectedRepo.eissn = this.updateGroup.get('eissn').value;
|
266
|
this.selectedRepo.lissn = this.updateGroup.get('lissn').value;
|
267
|
}
|
268
|
}
|
269
|
|
270
|
updateEnglishName() {
|
271
|
let status: boolean;
|
272
|
this.repoService.updateEnglishName(this.selectedRepo.id,this.updateGroup.get('englishName').value).subscribe(
|
273
|
response => {
|
274
|
console.log(response);
|
275
|
status = true;
|
276
|
},
|
277
|
error => {
|
278
|
console.log(error);
|
279
|
this.errorMessage = formErrorWasntSaved;
|
280
|
status = false;
|
281
|
}
|
282
|
);
|
283
|
return status;
|
284
|
}
|
285
|
|
286
|
}
|
287
|
|
288
|
export function checkPlatform(c: AbstractControl) {
|
289
|
if ( c.get('softwarePlatform').value || c.get('platformName').value )
|
290
|
return null;
|
291
|
return 'invalid';
|
292
|
}
|