-
Notifications
You must be signed in to change notification settings - Fork 384
/
files-edit.component.ts
339 lines (307 loc) · 13.2 KB
/
files-edit.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import {Component, Input, ViewChild} from '@angular/core';
import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
import {SectionUploadService} from '../section-upload.service';
import {hasValue, isNotEmpty, isNotUndefined} from '../../../../shared/empty.util';
import {
DynamicDateControlModel,
DynamicDatePickerModel,
DynamicFormArrayGroupModel,
DynamicFormArrayModel,
DynamicFormControlEvent,
DynamicFormControlModel,
DynamicSelectModel
} from '@ng-dynamic-forms/core';
import {
BITSTREAM_ACCESS_CONDITIONS_FORM_ARRAY_CLS,
BITSTREAM_ACCESS_CONDITIONS_FORM_ARRAY_CONFIG,
BITSTREAM_FORM_ACCESS_CONDITION_END_DATE_CLS,
BITSTREAM_FORM_ACCESS_CONDITION_END_DATE_CONFIG,
BITSTREAM_FORM_ACCESS_CONDITION_GROUPS_CLS,
BITSTREAM_FORM_ACCESS_CONDITION_GROUPS_CONFIG,
BITSTREAM_FORM_ACCESS_CONDITION_START_DATE_CLS,
BITSTREAM_FORM_ACCESS_CONDITION_START_DATE_CONFIG,
BITSTREAM_FORM_ACCESS_CONDITION_TYPE_CLS,
BITSTREAM_FORM_ACCESS_CONDITION_TYPE_CONFIG,
BITSTREAM_METADATA_FORM_MODEL
} from './files-edit.model';
import {FormComponent} from '../../../../shared/form/form.component';
import {FormService} from '../../../../shared/form/form.service';
import {FormBuilderService} from '../../../../shared/form/builder/form-builder.service';
import {JsonPatchOperationsBuilder} from '../../../../core/json-patch/builder/json-patch-operations-builder';
import {SubmissionRestService} from '../../../submission-rest.service';
import {JsonPatchOperationPathCombiner} from '../../../../core/json-patch/builder/json-patch-operation-path-combiner';
@Component({
selector: 'ds-submission-submit-form-box-files-edit',
templateUrl: './files-edit.component.html',
})
export class FilesEditComponent {
// The 'ViewChild' maps the variable only after the view init. And be sure to do not put the
// ref inside an *ngIf or the output will be null until that part of HTML will be rendered.
@ViewChild('formRef') formRef: FormComponent;
@Input() fileId;
@Input() fileIndex;
@Input() sectionId;
@Input() submissionId;
@Input() configUrl;
@Input() accessConditions: any[];
@Input() accessConditionGroups;
public fileData;
public formId;
public readMode = true;
public initialized = false;
public formModel: DynamicFormControlModel[];
protected pathCombiner: JsonPatchOperationPathCombiner;
protected subscriptions = [];
constructor(private formService: FormService,
private formBuilderService: FormBuilderService,
private modalService: NgbModal,
private operationsBuilder: JsonPatchOperationsBuilder,
private restService: SubmissionRestService,
private uploadService: SectionUploadService,) {
}
ngOnInit() {
this.pathCombiner = new JsonPatchOperationPathCombiner('sections', this.sectionId, 'files', this.fileIndex);
// Retrieve the uploaded file
this.subscriptions.push(
this.uploadService
.getFileData(this.submissionId, this.sectionId, this.fileId)
.filter((bitstream) => isNotUndefined(bitstream))
// .take(1)
.subscribe((bitstream) => {
this.fileData = bitstream;
this.formId = 'form_' + this.fileId;
this.formModel = this.buildBitsreamEditForm();
}
)
);
}
protected buildBitsreamEditForm() {
const formModel = Object.create(BITSTREAM_METADATA_FORM_MODEL);
const accessConditionTypeModel = Object.create(BITSTREAM_FORM_ACCESS_CONDITION_TYPE_CONFIG);
const accessConditionsArrayConfig = Object.create(BITSTREAM_ACCESS_CONDITIONS_FORM_ARRAY_CONFIG);
const accessConditionTypeOptions = [];
if (this.accessConditions.length > 0) {
for (const accessCondition of this.accessConditions) {
accessConditionTypeOptions.push(
{
label: accessCondition.name,
value: accessCondition.name
}
);
}
accessConditionTypeModel.options = accessConditionTypeOptions;
// Dynamic assign of relation in config. For startdate, endDate, groups.
const hasStart = [];
const hasEnd = [];
const hasGroups = [];
this.accessConditions.forEach((condition) => {
const showStart: boolean = condition.hasStartDate === true;
const showEnd: boolean = condition.hasEndDate === true;
const showGroups: boolean = showStart || showEnd;
if (showStart) {
hasStart.push({id: 'name', value: condition.name});
}
if (showEnd) {
hasEnd.push({id: 'name', value: condition.name});
}
if (showGroups) {
hasGroups.push({id: 'name', value: condition.name});
}
});
const confStart = {relation: [{action: 'ENABLE', connective: 'OR', when: hasStart}]};
const confEnd = {relation: [{action: 'ENABLE', connective: 'OR', when: hasEnd}]};
const confGroup = {relation: [{action: 'ENABLE', connective: 'OR', when: hasGroups}]};
accessConditionsArrayConfig.groupFactory = () => {
const type = new DynamicSelectModel(accessConditionTypeModel, BITSTREAM_FORM_ACCESS_CONDITION_TYPE_CLS);
const startDateConfig = Object.assign(BITSTREAM_FORM_ACCESS_CONDITION_START_DATE_CONFIG, confStart);
const endDateConfig = Object.assign(BITSTREAM_FORM_ACCESS_CONDITION_END_DATE_CONFIG, confEnd);
const groupsConfig = Object.assign(BITSTREAM_FORM_ACCESS_CONDITION_GROUPS_CONFIG, confGroup);
const startDate = new DynamicDatePickerModel(startDateConfig, BITSTREAM_FORM_ACCESS_CONDITION_START_DATE_CLS);
const endDate = new DynamicDatePickerModel(endDateConfig, BITSTREAM_FORM_ACCESS_CONDITION_END_DATE_CLS);
const groups = new DynamicSelectModel(groupsConfig, BITSTREAM_FORM_ACCESS_CONDITION_GROUPS_CLS);
return [type, startDate, endDate, groups];
};
//Number of access conditions blocks in form
let repeat = this.fileData.accessConditions.length;
repeat = (repeat == 0) ? 1 : repeat;
for(let i=0; i<repeat; i++) {
formModel.push(
new DynamicFormArrayModel(accessConditionsArrayConfig, BITSTREAM_ACCESS_CONDITIONS_FORM_ARRAY_CLS)
);
}
}
return formModel;
}
public setFormInitialMetadata() {
if (!this.initialized) {
this.initialized = true;
// Alternative mode to assign:
// this.formRef.formGroup.controls['files-data'].controls.title.setValue(...);
// Cannot put the following lines into 'ngOnInit' because 'this.formRef' is available only after the view init.
// Cannot put the following lines into 'ngAfterViewInit' because of 'ExpressionChangedAfterItHasBeenCheckedError'.
// TODO bitstreamList[key].metadata['dc.title'][0].value, as in section-upload
// dc.title
let title = '';
if (isNotUndefined(this.fileData.metadata['dc.title'])) {
// Case /edit
title = this.fileData.metadata['dc.title'][0].value;
} else {
// Case /submit
title = this.fileData.metadata[0].value;
}
const description = this.fileData.metadata.length > 1 ? this.fileData.metadata[1].value : '';
const accessConditions = this.fileData.accessConditions;
this.formRef.formGroup.get('metadata').get('dc_title').setValue(title);
this.formRef.formGroup.get('metadata').get('dc_description').setValue(description);
this.fileData.accessConditions.forEach( (condition, index) => {
const accessConditionControl: any = this.formBuilderService.getFormControlById(
'accessConditions',
this.formRef.formGroup,
this.formModel,
index);
if (accessConditionControl) {
const controls = Object.keys(accessConditionControl.controls);
controls.forEach( (key) => {
// TODO Verificare valori non ripetuti nel form
accessConditionControl.get(key).setValue(condition[key]);
});
}
});
// this.formRef.formGroup.get('accessConditions').setValue(accessConditions);
}
}
public switchMode(mode: boolean) {
this.setFormInitialMetadata();
this.readMode = mode;
}
public onChange(event: DynamicFormControlEvent) {
if (event.model.id === 'name') {
const accessCondition = this.accessConditions.filter((element) => element.name === event.control.value);
if (isNotEmpty(accessCondition)) {
const showGroups: boolean = accessCondition[0].hasStartDate === true || accessCondition[0].hasEndDate === true;
if (isNotUndefined(accessCondition[0].groupUUID)) {
// const hiddenGroupControl = event.group.get('hiddenGroupUUID');
const groupModel = this.formBuilderService.findById(
'groupUUID',
(event.model.parent as DynamicFormArrayGroupModel).group) as DynamicSelectModel<any>;
if (isNotUndefined(this.accessConditionGroups[accessCondition[0].groupUUID])) {
groupModel.options = [
{
label: this.accessConditionGroups[accessCondition[0].groupUUID].name,
value: this.accessConditionGroups[accessCondition[0].groupUUID].uuid
}
];
} else {
groupModel.options = []
}
// if (event.control.value !== 'lease' && event.control.value !== 'embargo') {
// if (!showGroups) {
// hiddenGroupControl.setValue(this.accessConditionGroups[accessCondition[0].groupUUID].uuid);
// }
if (showGroups) {
if (accessCondition[0].hasStartDate) {
const startDateModel = this.formBuilderService.findById(
'startDate',
(event.model.parent as DynamicFormArrayGroupModel).group) as DynamicDateControlModel;
startDateModel.min = new Date(accessCondition[0].maxStartDate);
}
if (accessCondition[0].hasEndDate) {
const endDateModel = this.formBuilderService.findById(
'endDate',
(event.model.parent as DynamicFormArrayGroupModel).group) as DynamicDateControlModel;
endDateModel.max = new Date(accessCondition[0].maxEndDate);
}
}
}
}
const path = this.formBuilderService.getPath(event.model);
const segmentpath = this.pathCombiner.getPath(this.formBuilderService.getPath(event.model));
console.log('p', path, 's', segmentpath);
}
}
public deleteBitstream() {
this.uploadService.deleteBitstream(this.submissionId, this.sectionId, this.fileId);
this.operationsBuilder.remove(this.pathCombiner.getPath());
this.restService.jsonPatchByResourceID(
this.submissionId,
this.pathCombiner.rootElement,
this.pathCombiner.subRootElement)
.subscribe();
}
public editBitstream() {
this.switchMode(true);
}
public saveBitstreamData() {
this.subscriptions.push(
this.formService.isValid(this.formRef.formUniqueId)
.take(1)
.subscribe((isValid) => {
if (isValid) {
const formData = this.formRef.formGroup.value;
const metadatum = [];
Object.keys((formData.metadata))
.forEach((key) => {
const keyDot = key.replace(/\_/g, '.');
metadatum.push({key: keyDot, value: formData.metadata[key]})
});
const fileData = Object.assign({}, this.fileData, {
metadata: metadatum,
accessConditions: formData.accessConditions
});
const titlePath = `metadata/dc.title`;
const descriptionPath = `metadata/dc.description`;
const accessConditionPath = `accessConditions`;
/*const accessCondition = [
{ name: formData['files-policies'][0].policies,
groupUUID: '3522e898-fe96-45ea-8538-65f1cf9128a8',
endDate: null }];*/
this.operationsBuilder.replace(this.pathCombiner.getPath(titlePath), formData.metadata.dc_title);
this.operationsBuilder.replace(this.pathCombiner.getPath(descriptionPath), formData.metadata.dc_description);
formData.accessConditions.forEach( (condition) => {
this.operationsBuilder.add(this.pathCombiner.getPath(accessConditionPath), condition);
});
this.restService.jsonPatchByResourceID(
this.submissionId,
this.pathCombiner.rootElement,
this.pathCombiner.subRootElement)
.subscribe();
this.uploadService.editBitstream(this.submissionId, this.sectionId, this.fileId, fileData);
} else {
this.formService.validateAllFormFields(this.formRef.formGroup);
}
})
);
}
public openModal(content) {
this.modalService.open(content).result.then(
(result) => {
if (result === 'ok') {
this.deleteBitstream();
}
}
);
}
/**
* Method provided by Angular. Invoked when the instance is destroyed.
*/
ngOnDestroy() {
this.subscriptions
.filter((subscription) => hasValue(subscription))
.forEach((subscription) => subscription.unsubscribe());
}
}
//
// interface MyAccessConditionOption {
// name: string;
// groupUUID: string;
// groups: AccessConditionGroup[],
// hasStartDate: boolean;
// hasEndDate: boolean;
// maxStartDate: string;
// maxEndDate: string;
// }
//
// interface AccessConditionGroup {
// name: string,
// uuid: string
// }