Skip to content

Commit fbd8606

Browse files
FrancescoMolinaroatarix83
authored andcommitted
Merged in task/dspace-cris-2023_02_x/DSC-2165 (pull request DSpace#2774)
[DSC-2165] add language mapping to qualdrop Approved-by: Giuseppe Digilio
2 parents 8f97a2f + 9e53ce1 commit fbd8606

7 files changed

Lines changed: 90 additions & 26 deletions

File tree

src/app/core/json-patch/builder/json-patch-operations-builder.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ export class JsonPatchOperationsBuilder {
3636
* A boolean representing if the value to be added is the first of an array
3737
* @param plain
3838
* A boolean representing if the value to be added is a plain text value
39+
* @param languages
3940
*/
40-
add(path: JsonPatchOperationPathObject, value, first = false, plain = false) {
41+
add(path: JsonPatchOperationPathObject, value, first = false, plain = false, languages: string[] = null) {
4142
this.store.dispatch(
4243
new NewPatchAddOperationAction(
4344
path.rootElement,
4445
path.subRootElement,
45-
path.path, this.prepareValue(value, plain, first)));
46+
path.path, this.prepareValue(value, plain, first, null, languages)));
4647
}
4748

4849
/**
@@ -55,8 +56,9 @@ export class JsonPatchOperationsBuilder {
5556
* @param plain
5657
* a boolean representing if the value to be added is a plain text value
5758
* @param securityLevel
59+
* @param language
5860
*/
59-
replace(path: JsonPatchOperationPathObject, value, plain = false, securityLevel = null) {
61+
replace(path: JsonPatchOperationPathObject, value, plain = false, securityLevel = null, language = null) {
6062
if (hasNoValue(value) || (typeof value === 'object' && hasNoValue(value.value))) {
6163
this.remove(path);
6264
} else {
@@ -65,7 +67,7 @@ export class JsonPatchOperationsBuilder {
6567
path.rootElement,
6668
path.subRootElement,
6769
path.path,
68-
this.prepareValue(value, plain, false, securityLevel)));
70+
this.prepareValue(value, plain, false, securityLevel, language)));
6971
}
7072
}
7173

@@ -102,23 +104,23 @@ export class JsonPatchOperationsBuilder {
102104
path.path));
103105
}
104106

105-
protected prepareValue(value: any, plain: boolean, first: boolean, securityLevel = null) {
107+
protected prepareValue(value: any, plain: boolean, first: boolean, securityLevel = null, languages: string[] = null) {
106108
let operationValue: any = null;
107109
if (hasValue(value)) {
108110
if (plain) {
109111
operationValue = value;
110112
} else {
111113
if (Array.isArray(value)) {
112114
operationValue = [];
113-
value.forEach((entry) => {
115+
value.forEach((entry, index) => {
114116
if ((typeof entry === 'object')) {
115117
if (isNotEmpty(securityLevel)) {
116118
operationValue.push(this.prepareObjectValue(entry, securityLevel));
117119
} else {
118120
operationValue.push(this.prepareObjectValue(entry));
119121
}
120122
} else {
121-
operationValue.push(new FormFieldMetadataValueObject(entry, null, securityLevel));
123+
operationValue.push(new FormFieldMetadataValueObject(entry, languages ? languages[index] : null, securityLevel));
122124
}
123125
});
124126
} else if (typeof value === 'object') {

src/app/shared/form/builder/form-builder.service.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ export class FormBuilderService extends DynamicFormService {
181181
iterateControlModels(groupModel);
182182
}
183183

184-
getValueFromModel(groupModel: DynamicFormControlModel[]): void {
184+
getValueFromModel(groupModel: DynamicFormControlModel[]): any {
185185
let result = Object.create({});
186186
const customizer = (objValue, srcValue) => {
187187
if (Array.isArray(objValue)) {
@@ -191,6 +191,7 @@ export class FormBuilderService extends DynamicFormService {
191191

192192
const normalizeValue = (controlModel, controlValue, controlModelIndex) => {
193193
let securityLevel = null;
194+
let controlLanguage = (controlModel as DsDynamicInputModel).hasLanguages ? (controlModel as DsDynamicInputModel).language : null;
194195
if (controlModel instanceof DynamicQualdropModel) {
195196
// get the security value inside in the metadataValue of input
196197
if (controlModel.group) {
@@ -215,6 +216,18 @@ export class FormBuilderService extends DynamicFormService {
215216
}
216217
});
217218
}
219+
220+
let qualdropLanguageControl = null;
221+
for (const control of controlModel.group) {
222+
if (hasValue((control as DsDynamicInputModel).language)) {
223+
qualdropLanguageControl = control as DsDynamicInputModel;
224+
break;
225+
}
226+
}
227+
if (qualdropLanguageControl) {
228+
controlModel.language = controlLanguage ?? qualdropLanguageControl.language;
229+
controlLanguage = controlModel.language;
230+
}
218231
}
219232
if (controlModel && (controlModel as any).securityLevel !== undefined) {
220233
securityLevel = (controlModel as any).securityLevel;
@@ -227,13 +240,13 @@ export class FormBuilderService extends DynamicFormService {
227240
}
228241
}
229242
}
230-
const controlLanguage = (controlModel as DsDynamicInputModel).hasLanguages ? (controlModel as DsDynamicInputModel).language : null;
231243

232244
if (controlModel?.metadataValue?.authority?.includes(VIRTUAL_METADATA_PREFIX)) {
233245
return controlModel.metadataValue;
234246
}
235247
if (isString(controlValue)) {
236-
return new FormFieldMetadataValueObject(controlValue, controlLanguage, securityLevel, null, controlModelIndex);
248+
const lang = controlModel instanceof DynamicQualdropModel ? controlModel.language : controlLanguage;
249+
return new FormFieldMetadataValueObject(controlValue, lang, securityLevel, null, controlModelIndex);
237250
} else if (isNgbDateStruct(controlValue)) {
238251
return new FormFieldMetadataValueObject(dateToString(controlValue));
239252
} else if (isObject(controlValue)) {

src/app/shared/form/builder/parsers/onebox-field-parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class OneboxFieldParser extends FieldParser {
7373
inputSelectGroup.readOnly = selectModelConfig.disabled && inputModelConfig.readOnly;
7474
// in case of qualdrop do not show toggle of security
7575
inputModelConfig.toggleSecurityVisibility = false;
76-
76+
inputSelectGroup.language = inputModelConfig.language;
7777
inputSelectGroup.group.push(new DynamicSelectModel(selectModelConfig, clsSelect));
7878
inputSelectGroup.group.push(new DsDynamicInputModel(inputModelConfig, clsInput));
7979

src/app/statistics-page/cris-statistics-page/cris-statistics-page.component.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ describe('CrisStatisticsPageComponent', () => {
5858
beforeEach(async () => {
5959
await TestBed.configureTestingModule({
6060
imports : [
61-
RouterTestingModule.withRoutes([]),
61+
RouterTestingModule.withRoutes([
62+
{
63+
path: 'fake-url',
64+
redirectTo: '/',
65+
},
66+
]),
6267
SharedModule,
6368
TranslateModule.forRoot({
6469
loader: {

src/app/submission/sections/form/section-form-operations.service.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ describe('SectionFormOperationsService test suite', () => {
740740

741741
serviceAsAny.dispatchOperationsFromMap(valueMap, pathCombiner, dynamicFormControlChangeEvent, previousValue);
742742

743-
expect(jsonPatchOpBuilder.add).toHaveBeenCalledWith(pathCombiner.getPath('path'), ['testMapNew'], true);
743+
expect(jsonPatchOpBuilder.add).toHaveBeenCalledWith(pathCombiner.getPath('path'), ['testMapNew'], true, null, undefined);
744744
});
745745

746746
it('should dispatch a json-path add operation when a map entry has changed', () => {
@@ -753,7 +753,7 @@ describe('SectionFormOperationsService test suite', () => {
753753

754754
serviceAsAny.dispatchOperationsFromMap(valueMap, pathCombiner, dynamicFormControlChangeEvent, previousValue);
755755

756-
expect(jsonPatchOpBuilder.add).toHaveBeenCalledWith(pathCombiner.getPath('path'), ['testMapNew'], true);
756+
expect(jsonPatchOpBuilder.add).toHaveBeenCalledWith(pathCombiner.getPath('path'), ['testMapNew'], true, false, undefined);
757757
});
758758

759759
it('should dispatch a json-path remove operation when a map entry has changed', () => {

src/app/submission/sections/form/section-form-operations.service.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,19 @@ export class SectionFormOperationsService {
5555
* the [[FormFieldPreviousValueObject]] for the specified operation
5656
* @param hasStoredValue
5757
* representing if field value related to the specified operation has stored value
58+
* @param languageMap
5859
*/
5960
public dispatchOperationsFromEvent(pathCombiner: JsonPatchOperationPathCombiner,
6061
event: DynamicFormControlEvent,
6162
previousValue: FormFieldPreviousValueObject,
62-
hasStoredValue: boolean): void {
63+
hasStoredValue: boolean,
64+
languageMap: Map<string, string[]> = null): void {
6365
switch (event.type) {
6466
case 'remove':
6567
this.dispatchOperationsFromRemoveEvent(pathCombiner, event, previousValue);
6668
break;
6769
case 'change':
68-
this.dispatchOperationsFromChangeEvent(pathCombiner, event, previousValue, hasStoredValue);
70+
this.dispatchOperationsFromChangeEvent(pathCombiner, event, previousValue, hasStoredValue, languageMap);
6971
break;
7072
case 'move':
7173
this.dispatchOperationsFromMoveEvent(pathCombiner, event, previousValue);
@@ -374,11 +376,13 @@ export class SectionFormOperationsService {
374376
* the [[FormFieldPreviousValueObject]] for the specified operation
375377
* @param hasStoredValue
376378
* representing if field value related to the specified operation has stored value
379+
* @param languageMap
377380
*/
378381
protected dispatchOperationsFromChangeEvent(pathCombiner: JsonPatchOperationPathCombiner,
379382
event: DynamicFormControlEvent,
380383
previousValue: FormFieldPreviousValueObject,
381-
hasStoredValue: boolean): void {
384+
hasStoredValue: boolean,
385+
languageMap?: Map<string, string[]>): void {
382386
if (event.context && event.context instanceof DynamicFormArrayGroupModel) {
383387
// Model is a DynamicRowArrayModel
384388
this.handleArrayGroupPatch(pathCombiner, event, (event as any).context.context, previousValue);
@@ -396,7 +400,7 @@ export class SectionFormOperationsService {
396400
if (this.formBuilder.isQualdropGroup(event.model.parent as DynamicFormControlModel)
397401
|| this.formBuilder.isQualdropGroup(event.model as DynamicFormControlModel)) {
398402
// It's a qualdrup model
399-
this.dispatchOperationsFromMap(this.getQualdropValueMap(event), pathCombiner, event, previousValue);
403+
this.dispatchOperationsFromMap(this.getQualdropValueMap(event), pathCombiner, event, previousValue, languageMap);
400404
} else if (this.formBuilder.isRelationGroup(event.model)) {
401405
// It's a relation model
402406
this.dispatchOperationsFromMap(this.getValueMap(value), pathCombiner, event, previousValue);
@@ -465,11 +469,13 @@ export class SectionFormOperationsService {
465469
* the [[DynamicFormControlEvent]] for the specified operation
466470
* @param previousValue
467471
* the [[FormFieldPreviousValueObject]] for the specified operation
472+
* @param languageMap
468473
*/
469474
protected dispatchOperationsFromMap(valueMap: Map<string, any>,
470475
pathCombiner: JsonPatchOperationPathCombiner,
471476
event: DynamicFormControlEvent,
472-
previousValue: FormFieldPreviousValueObject): void {
477+
previousValue: FormFieldPreviousValueObject,
478+
languageMap: Map<string, string[]> = null): void {
473479
const currentValueMap = valueMap;
474480
if (event.type === 'remove') {
475481
const path = this.getQualdropItemPathFromEvent(event);
@@ -480,7 +486,8 @@ export class SectionFormOperationsService {
480486
const currentValue = currentValueMap.get(index);
481487
if (currentValue) {
482488
if (!isEqual(entry, currentValue)) {
483-
this.operationsBuilder.add(pathCombiner.getPath(index), currentValue, true);
489+
const metadataFromPath = pathCombiner.getPath(index).path.split('/').slice(-1)[0];
490+
this.operationsBuilder.add(pathCombiner.getPath(index), currentValue, true, false, languageMap?.get(metadataFromPath));
484491
}
485492
currentValueMap.delete(index);
486493
} else if (!currentValue) {
@@ -493,7 +500,8 @@ export class SectionFormOperationsService {
493500
// The last item of the group has been deleted so make a remove op
494501
this.operationsBuilder.remove(pathCombiner.getPath(index));
495502
} else {
496-
this.operationsBuilder.add(pathCombiner.getPath(index), entry, true);
503+
const metadataFromPath = pathCombiner.getPath(index).path.split('/').slice(-1)[0];
504+
this.operationsBuilder.add(pathCombiner.getPath(index), entry, true, null, languageMap?.get(metadataFromPath));
497505
}
498506
});
499507
}
@@ -576,4 +584,5 @@ export class SectionFormOperationsService {
576584
}
577585
}
578586
}
587+
579588
}

src/app/submission/sections/form/section-form.component.ts

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import { FormRowModel } from '../../../core/config/models/config-submission-form
4040
import { SubmissionVisibility } from '../../utils/visibility.util';
4141
import { MetadataSecurityConfiguration } from '../../../core/submission/models/metadata-security-configuration';
4242
import { SubmissionVisibilityType } from '../../../core/config/models/config-submission-section.model';
43+
import { DynamicQualdropModel } from '../../../shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-qualdrop.model';
4344

4445
/**
4546
* This component represents a section that contains a Form.
@@ -409,11 +410,45 @@ export class SubmissionSectionFormComponent extends SectionModelComponent implem
409410
* the [[DynamicFormControlEvent]] emitted
410411
*/
411412
onChange(event: DynamicFormControlEvent): void {
412-
this.formOperationsService.dispatchOperationsFromEvent(
413-
this.pathCombiner,
414-
event,
415-
this.previousValue,
416-
this.hasStoredValue(this.formBuilderService.getId(event.model), this.formOperationsService.getArrayIndexFromEvent(event)));
413+
const languageMap = new Map();
414+
const isQualdrop = event.model.parent instanceof DynamicQualdropModel;
415+
416+
if (isQualdrop) {
417+
const qualdropMap = this.formOperationsService.getQualdropValueMap(event);
418+
419+
if (qualdropMap) {
420+
const groupMetadata = qualdropMap.keys();
421+
this.formService.getForm(this.formId).pipe(take(1)).subscribe((form) => {
422+
for (const metadata of groupMetadata) {
423+
if (hasValue(form.data[metadata]) && form.data[metadata].length > 1) {
424+
form.data[metadata].forEach((entry: any) => {
425+
languageMap.set(metadata, [...(languageMap.get(metadata) ?? []), entry.language]);
426+
});
427+
} else {
428+
languageMap.set(metadata, [form.data[metadata][0].language]);
429+
}
430+
}
431+
});
432+
}
433+
434+
this.formOperationsService.dispatchOperationsFromEvent(
435+
this.pathCombiner,
436+
event,
437+
this.previousValue,
438+
this.hasStoredValue(this.formBuilderService.getId(event.model), this.formOperationsService.getArrayIndexFromEvent(event)),
439+
languageMap
440+
);
441+
} else {
442+
this.formOperationsService.dispatchOperationsFromEvent(
443+
this.pathCombiner,
444+
event,
445+
this.previousValue,
446+
this.hasStoredValue(this.formBuilderService.getId(event.model), this.formOperationsService.getArrayIndexFromEvent(event)),
447+
null
448+
);
449+
}
450+
451+
417452
const metadata = this.formOperationsService.getFieldPathSegmentedFromChangeEvent(event);
418453
const value = this.formOperationsService.getFieldValueFromChangeEvent(event);
419454

0 commit comments

Comments
 (0)