Skip to content

Commit 369df31

Browse files
committed
Merge branch 'task/dspace-cris-2023_02_x/DSC-2397' into task/dspace-cris-2024_02_x/DSC-2397
# Conflicts: # src/app/shared/form/builder/ds-dynamic-form-ui/models/dynamic-vocabulary.component.ts # src/app/shared/form/builder/ds-dynamic-form-ui/models/onebox/dynamic-onebox.component.spec.ts
2 parents ca21a72 + 459c237 commit 369df31

3 files changed

Lines changed: 118 additions & 35 deletions

File tree

src/app/core/submission/vocabularies/models/vocabulary.model.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ export class Vocabulary implements CacheableObject {
7474
@autoserialize
7575
externalSource: VocabularyExternalSourceMap;
7676

77+
78+
/**
79+
* A boolean variable that indicates whether the functionality of
80+
* multiple value generation is enabled within a generator context.
81+
*/
82+
@autoserialize
83+
multiValueOnGenerator: boolean;
84+
7785
/**
7886
* A string representing the kind of Vocabulary model
7987
*/

src/app/shared/form/builder/ds-dynamic-form-ui/models/dynamic-vocabulary.component.ts

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
distinctUntilChanged,
2222
filter,
2323
map,
24-
take,
24+
take, tap,
2525
} from 'rxjs/operators';
2626

2727
import { Metadata } from '../../../../../core/shared/metadata.utils';
@@ -76,6 +76,8 @@ export abstract class DsDynamicVocabularyComponent extends DynamicFormControlCom
7676
public otherInfoValues: string[] = [];
7777
public otherInfoValuesUnformatted: string[] = [];
7878

79+
multiValueOnGenerator: boolean;
80+
7981
protected constructor(protected vocabularyService: VocabularyService,
8082
protected layoutService: DynamicFormLayoutService,
8183
protected validationService: DynamicFormValidationService,
@@ -195,6 +197,7 @@ export abstract class DsDynamicVocabularyComponent extends DynamicFormControlCom
195197
this.vocabulary$ = this.vocabularyService.findVocabularyById(this.model.vocabularyOptions.name).pipe(
196198
getFirstSucceededRemoteDataPayload(),
197199
distinctUntilChanged(),
200+
tap((vocabulary: Vocabulary) => this.multiValueOnGenerator = vocabulary.multiValueOnGenerator),
198201
);
199202
}
200203

@@ -277,12 +280,14 @@ export abstract class DsDynamicVocabularyComponent extends DynamicFormControlCom
277280
for (const key in otherInformation) {
278281
if (otherInformation.hasOwnProperty(key) && key.startsWith('data-')) {
279282
const fieldId = key.replace('data-', '');
280-
const newValue: FormFieldMetadataValueObject = this.getOtherInformationValue(otherInformation[key], key);
281-
if (isNotEmpty(newValue)) {
282-
const updatedModel = this.formBuilderService.updateModelValue(fieldId, newValue);
283-
if (isNotEmpty(updatedModel)) {
284-
updatedModels.push(updatedModel);
285-
}
283+
const newValues: FormFieldMetadataValueObject[] = this.getOtherInformationValue(otherInformation[key], key);
284+
if (isNotEmpty(newValues)) {
285+
newValues.forEach((newValue) => {
286+
const updatedModel = this.formBuilderService.updateModelValue(fieldId, newValue);
287+
if (isNotEmpty(updatedModel)) {
288+
updatedModels.push(updatedModel);
289+
}
290+
});
286291
}
287292
}
288293
}
@@ -297,43 +302,53 @@ export abstract class DsDynamicVocabularyComponent extends DynamicFormControlCom
297302
}
298303
}
299304

300-
getOtherInformationValue(value: string, key: string): FormFieldMetadataValueObject {
305+
getOtherInformationValue(value: string, key: string): FormFieldMetadataValueObject[] {
301306
if (isEmpty(value) || key === 'alternative-names' ) {
302307
return null;
303308
}
304309

305-
let returnValue;
306-
if (value.indexOf('::') === -1) {
307-
returnValue = new FormFieldMetadataValueObject(value);
308-
} else if (value.indexOf('|||') === -1) {
309-
returnValue = new FormFieldMetadataValueObject(
310-
value.substring(0, value.lastIndexOf('::')),
311-
null,
312-
null,
313-
value.substring(value.lastIndexOf('::') + 2),
314-
);
310+
let returnValue = [];
311+
if (value.indexOf('|||') === -1) {
312+
returnValue.push(this.generateFormField(value));
315313
} else if (value.indexOf('|||') !== -1 && this.otherInfoValue) {
316-
const unformattedValue = this.otherInfoValuesUnformatted.find(otherInfoValue => otherInfoValue.includes(this.otherInfoValue || this.otherName));
317-
const authorityValue = hasValue(unformattedValue) ? unformattedValue.substring(unformattedValue.lastIndexOf('::') + 2) : null;
318-
const otherInfo = {};
319-
let alternativeValue;
320-
otherInfo[key] = value;
321-
if (hasValue(this.otherName)) {
322-
const otherValues = value.split('|||');
323-
alternativeValue = otherValues[0].substring(0, otherValues[0].lastIndexOf('::'));
314+
const otherValues: string[] = value.split('|||');
315+
if (this.multiValueOnGenerator) {
316+
otherValues.forEach((tmpValue) => returnValue.push(this.generateFormField(tmpValue)));
317+
} else {
318+
const unformattedValue = this.otherInfoValuesUnformatted.find(otherInfoValue => otherInfoValue.includes(this.otherInfoValue || this.otherName));
319+
const authorityValue = hasValue(unformattedValue) ? unformattedValue.substring(unformattedValue.lastIndexOf('::') + 2) : null;
320+
let otherInfo = {};
321+
let alternativeValue: string;
322+
otherInfo[key] = value;
323+
if (hasValue(this.otherName)) {
324+
alternativeValue = otherValues[0].substring(0, otherValues[0].lastIndexOf('::'));
325+
}
326+
returnValue.push(new FormFieldMetadataValueObject(
327+
hasValue(alternativeValue) ? alternativeValue : this.otherInfoValue,
328+
null,
329+
null,
330+
authorityValue,
331+
null,
332+
null,
333+
null,
334+
otherInfo
335+
));
324336
}
325-
returnValue = new FormFieldMetadataValueObject(
326-
hasValue(alternativeValue) ? alternativeValue : this.otherInfoValue,
327-
null,
328-
null,
329-
authorityValue,
330-
null,
337+
}
338+
return returnValue;
339+
}
340+
341+
private generateFormField(value: string): FormFieldMetadataValueObject {
342+
if (value.indexOf('::') === -1) {
343+
return new FormFieldMetadataValueObject(value);
344+
} else {
345+
return new FormFieldMetadataValueObject(
346+
value.substring(0, value.lastIndexOf('::')),
331347
null,
332348
null,
333-
otherInfo,
349+
value.substring(value.lastIndexOf('::') + 2)
334350
);
335351
}
336-
return returnValue;
337352
}
338353

339354
private hasValidAuthority(formMetadataValue: FormFieldMetadataValueObject) {

src/app/shared/form/builder/ds-dynamic-form-ui/models/onebox/dynamic-onebox.component.spec.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ describe('DsDynamicOneboxComponent test suite', () => {
104104

105105
let scheduler: TestScheduler;
106106
let testComp: TestComponent;
107-
let oneboxComponent: DsDynamicOneboxComponent;
107+
let oneboxComponent: DsDynamicOneboxComponent|any;
108108
let testFixture: ComponentFixture<TestComponent>;
109109
let debugElement: DebugElement;
110110
let oneboxCompFixture: ComponentFixture<DsDynamicOneboxComponent>;
@@ -570,6 +570,66 @@ describe('DsDynamicOneboxComponent test suite', () => {
570570
expect(oneboxComponent.currentValue.authority).toBeUndefined();
571571
});
572572
});
573+
574+
describe('test metadata enrichment', () => {
575+
beforeEach(() => {
576+
oneboxCompFixture = TestBed.createComponent(DsDynamicOneboxComponent);
577+
debugElement = oneboxCompFixture.debugElement;
578+
oneboxComponent = oneboxCompFixture.componentInstance;
579+
oneboxComponent.currentValue = new FormFieldMetadataValueObject('test', null, null, null, 'testDisplay');
580+
oneboxComponent.model = new DynamicOneboxModel(ONEBOX_TEST_MODEL_CONFIG);
581+
582+
spyOn(oneboxComponent, 'onSelectItem').and.returnValue(undefined);
583+
spyOn(oneboxComponent, 'toggleOtherInfoSelection').and.returnValue(undefined);
584+
});
585+
586+
587+
it('should return null if value is empty', () => {
588+
expect(oneboxComponent.getOtherInformationValue('', 'some-key')).toBeNull();
589+
});
590+
591+
it('should return null if key is "alternative-names"', () => {
592+
expect(oneboxComponent.getOtherInformationValue('some-value', 'alternative-names')).toBeNull();
593+
});
594+
595+
it('should return single FormFieldMetadataValueObject if no "|||" in value', () => {
596+
const result = oneboxComponent.getOtherInformationValue('value::authority', 'some-key');
597+
expect(result.length).toBe(1);
598+
expect(result[0]).toEqual(jasmine.any(FormFieldMetadataValueObject));
599+
expect(result[0].value).toBe('value');
600+
expect(result[0].authority).toBe('authority');
601+
});
602+
603+
it('should handle multiple values with multiValueOnGenerator true', () => {
604+
oneboxComponent.multiValueOnGenerator = true;
605+
oneboxComponent.otherInfoValue = 'someValue';
606+
const result = oneboxComponent.getOtherInformationValue('val1::auth1|||val2::auth2', 'some-key');
607+
expect(result.length).toBe(2);
608+
expect(result[0].value).toBe('val1');
609+
expect(result[0].authority).toBe('auth1');
610+
expect(result[1].value).toBe('val2');
611+
expect(result[1].authority).toBe('auth2');
612+
});
613+
614+
it('should handle multiple values with multiValueOnGenerator false', () => {
615+
oneboxComponent.multiValueOnGenerator = false;
616+
oneboxComponent.otherInfoValue = 'val1';
617+
oneboxComponent.otherInfoValuesUnformatted = ['val1::auth1'];
618+
const result = oneboxComponent.getOtherInformationValue('val1::auth1|||val2::auth2', 'data-key');
619+
expect(result.length).toBe(1);
620+
expect(result[0].value).toBe('val1');
621+
expect(result[0].authority).toBe('auth1');
622+
expect(result[0].otherInformation['data-key']).toBe('val1::auth1|||val2::auth2');
623+
});
624+
625+
it('should handle value without "::"', () => {
626+
const result = oneboxComponent.getOtherInformationValue('simpleValue', 'some-key');
627+
expect(result.length).toBe(1);
628+
expect(result[0].value).toBe('simpleValue');
629+
expect(result[0].authority).toBeNull();
630+
});
631+
632+
});
573633
});
574634
});
575635

0 commit comments

Comments
 (0)