Skip to content

Commit

Permalink
Merged in task/main-cris/DSC-2165 (pull request DSpace#2776)
Browse files Browse the repository at this point in the history
Task/main cris/DSC-2165

Approved-by: Giuseppe Digilio
  • Loading branch information
FrancescoMolinaro authored and atarix83 committed Feb 11, 2025
2 parents 129b1f4 + ad3ab26 commit da8427a
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 25 deletions.
16 changes: 9 additions & 7 deletions src/app/core/json-patch/builder/json-patch-operations-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ export class JsonPatchOperationsBuilder {
* A boolean representing if the value to be added is the first of an array
* @param plain
* A boolean representing if the value to be added is a plain text value
* @param languages
*/
add(path: JsonPatchOperationPathObject, value, first = false, plain = false) {
add(path: JsonPatchOperationPathObject, value, first = false, plain = false, languages: string[] = null) {
this.store.dispatch(
new NewPatchAddOperationAction(
path.rootElement,
path.subRootElement,
path.path, this.prepareValue(value, plain, first)));
path.path, this.prepareValue(value, plain, first, null, languages)));
}

/**
Expand All @@ -66,8 +67,9 @@ export class JsonPatchOperationsBuilder {
* @param plain
* a boolean representing if the value to be added is a plain text value
* @param securityLevel
* @param language
*/
replace(path: JsonPatchOperationPathObject, value, plain = false, securityLevel = null) {
replace(path: JsonPatchOperationPathObject, value, plain = false, securityLevel = null, language = null) {
if (hasNoValue(value) || (typeof value === 'object' && hasNoValue(value.value))) {
this.remove(path);
} else {
Expand All @@ -76,7 +78,7 @@ export class JsonPatchOperationsBuilder {
path.rootElement,
path.subRootElement,
path.path,
this.prepareValue(value, plain, false, securityLevel)));
this.prepareValue(value, plain, false, securityLevel, language)));
}
}

Expand Down Expand Up @@ -127,23 +129,23 @@ export class JsonPatchOperationsBuilder {
path.path));
}

protected prepareValue(value: any, plain: boolean, first: boolean, securityLevel = null) {
protected prepareValue(value: any, plain: boolean, first: boolean, securityLevel = null, languages: string[] = null) {
let operationValue: any = null;
if (hasValue(value)) {
if (plain) {
operationValue = value;
} else {
if (Array.isArray(value)) {
operationValue = [];
value.forEach((entry) => {
value.forEach((entry, index) => {
if ((typeof entry === 'object')) {
if (isNotEmpty(securityLevel)) {
operationValue.push(this.prepareObjectValue(entry, securityLevel));
} else {
operationValue.push(this.prepareObjectValue(entry));
}
} else {
operationValue.push(new FormFieldMetadataValueObject(entry, null, securityLevel));
operationValue.push(new FormFieldMetadataValueObject(entry, languages ? languages[index] : null, securityLevel));
}
});
} else if (typeof value === 'object') {
Expand Down
19 changes: 16 additions & 3 deletions src/app/shared/form/builder/form-builder.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export class FormBuilderService extends DynamicFormService {
iterateControlModels(groupModel);
}

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

const normalizeValue = (controlModel, controlValue, controlModelIndex) => {
let securityLevel = null;
let controlLanguage = (controlModel as DsDynamicInputModel).hasLanguages ? (controlModel as DsDynamicInputModel).language : null;
if (controlModel instanceof DynamicQualdropModel) {
// get the security value inside in the metadataValue of input
if (controlModel.group) {
Expand All @@ -234,6 +235,18 @@ export class FormBuilderService extends DynamicFormService {
}
});
}

let qualdropLanguageControl = null;
for (const control of controlModel.group) {
if (hasValue((control as DsDynamicInputModel).language)) {
qualdropLanguageControl = control as DsDynamicInputModel;
break;
}
}
if (qualdropLanguageControl) {
controlModel.language = controlLanguage ?? qualdropLanguageControl.language;
controlLanguage = controlModel.language;
}
}
if (controlModel && (controlModel as any).securityLevel !== undefined) {
securityLevel = (controlModel as any).securityLevel;
Expand All @@ -246,13 +259,13 @@ export class FormBuilderService extends DynamicFormService {
}
}
}
const controlLanguage = (controlModel as DsDynamicInputModel).hasLanguages ? (controlModel as DsDynamicInputModel).language : null;

if (controlModel?.metadataValue?.authority?.includes(VIRTUAL_METADATA_PREFIX)) {
return controlModel.metadataValue;
}
if (isString(controlValue)) {
return new FormFieldMetadataValueObject(controlValue, controlLanguage, securityLevel, null, controlModelIndex);
const lang = controlModel instanceof DynamicQualdropModel ? controlModel.language : controlLanguage;
return new FormFieldMetadataValueObject(controlValue, lang, securityLevel, null, controlModelIndex);
} else if (isNgbDateStruct(controlValue)) {
return new FormFieldMetadataValueObject(dateToString(controlValue));
} else if (isObject(controlValue)) {
Expand Down
2 changes: 1 addition & 1 deletion src/app/shared/form/builder/parsers/onebox-field-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class OneboxFieldParser extends FieldParser {
inputSelectGroup.readOnly = selectModelConfig.disabled && inputModelConfig.readOnly;
// in case of qualdrop do not show toggle of security
inputModelConfig.toggleSecurityVisibility = false;

inputSelectGroup.language = inputModelConfig.language;
inputSelectGroup.group.push(new DynamicSelectModel(selectModelConfig, clsSelect));
inputSelectGroup.group.push(new DsDynamicInputModel(inputModelConfig, clsInput));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ describe('SectionFormOperationsService test suite', () => {

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

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

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

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

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

it('should dispatch a json-path remove operation when a map entry has changed', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,19 @@ export class SectionFormOperationsService {
* the [[FormFieldPreviousValueObject]] for the specified operation
* @param hasStoredValue
* representing if field value related to the specified operation has stored value
* @param languageMap
*/
public dispatchOperationsFromEvent(pathCombiner: JsonPatchOperationPathCombiner,
event: DynamicFormControlEvent,
previousValue: FormFieldPreviousValueObject,
hasStoredValue: boolean): void {
hasStoredValue: boolean,
languageMap: Map<string, string[]> = null): void {
switch (event.type) {
case 'remove':
this.dispatchOperationsFromRemoveEvent(pathCombiner, event, previousValue);
break;
case 'change':
this.dispatchOperationsFromChangeEvent(pathCombiner, event, previousValue, hasStoredValue);
this.dispatchOperationsFromChangeEvent(pathCombiner, event, previousValue, hasStoredValue, languageMap);
break;
case 'move':
this.dispatchOperationsFromMoveEvent(pathCombiner, event, previousValue);
Expand Down Expand Up @@ -383,11 +385,13 @@ export class SectionFormOperationsService {
* the [[FormFieldPreviousValueObject]] for the specified operation
* @param hasStoredValue
* representing if field value related to the specified operation has stored value
* @param languageMap
*/
protected dispatchOperationsFromChangeEvent(pathCombiner: JsonPatchOperationPathCombiner,
event: DynamicFormControlEvent,
previousValue: FormFieldPreviousValueObject,
hasStoredValue: boolean): void {
hasStoredValue: boolean,
languageMap?: Map<string, string[]>): void {
if (event.context && event.context instanceof DynamicFormArrayGroupModel) {
// Model is a DynamicRowArrayModel
this.handleArrayGroupPatch(pathCombiner, event, (event as any).context.context, previousValue);
Expand All @@ -405,7 +409,7 @@ export class SectionFormOperationsService {
if (this.formBuilder.isQualdropGroup(event.model.parent as DynamicFormControlModel)
|| this.formBuilder.isQualdropGroup(event.model as DynamicFormControlModel)) {
// It's a qualdrup model
this.dispatchOperationsFromMap(this.getQualdropValueMap(event), pathCombiner, event, previousValue);
this.dispatchOperationsFromMap(this.getQualdropValueMap(event), pathCombiner, event, previousValue, languageMap);
} else if (this.formBuilder.isRelationGroup(event.model)) {
// It's a relation model
this.dispatchOperationsFromMap(this.getValueMap(value), pathCombiner, event, previousValue);
Expand Down Expand Up @@ -474,11 +478,13 @@ export class SectionFormOperationsService {
* the [[DynamicFormControlEvent]] for the specified operation
* @param previousValue
* the [[FormFieldPreviousValueObject]] for the specified operation
* @param languageMap
*/
protected dispatchOperationsFromMap(valueMap: Map<string, any>,
pathCombiner: JsonPatchOperationPathCombiner,
event: DynamicFormControlEvent,
previousValue: FormFieldPreviousValueObject): void {
previousValue: FormFieldPreviousValueObject,
languageMap: Map<string, string[]> = null): void {
const currentValueMap = valueMap;
if (event.type === 'remove') {
const path = this.getQualdropItemPathFromEvent(event);
Expand All @@ -489,7 +495,8 @@ export class SectionFormOperationsService {
const currentValue = currentValueMap.get(index);
if (currentValue) {
if (!isEqual(entry, currentValue)) {
this.operationsBuilder.add(pathCombiner.getPath(index), currentValue, true);
const metadataFromPath = pathCombiner.getPath(index).path.split('/').slice(-1)[0];
this.operationsBuilder.add(pathCombiner.getPath(index), currentValue, true, false, languageMap?.get(metadataFromPath));
}
currentValueMap.delete(index);
} else if (!currentValue) {
Expand All @@ -502,7 +509,8 @@ export class SectionFormOperationsService {
// The last item of the group has been deleted so make a remove op
this.operationsBuilder.remove(pathCombiner.getPath(index));
} else {
this.operationsBuilder.add(pathCombiner.getPath(index), entry, true);
const metadataFromPath = pathCombiner.getPath(index).path.split('/').slice(-1)[0];
this.operationsBuilder.add(pathCombiner.getPath(index), entry, true, null, languageMap?.get(metadataFromPath));
}
});
}
Expand Down
45 changes: 40 additions & 5 deletions src/app/submission/sections/form/section-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
isNotEmpty,
isUndefined,
} from '../../../shared/empty.util';
import { DynamicQualdropModel } from '../../../shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-qualdrop.model';
import { FormBuilderService } from '../../../shared/form/builder/form-builder.service';
import { FormFieldPreviousValueObject } from '../../../shared/form/builder/models/form-field-previous-value-object';
import { FormComponent } from '../../../shared/form/form.component';
Expand Down Expand Up @@ -447,11 +448,45 @@ export class SubmissionSectionFormComponent extends SectionModelComponent implem
* the [[DynamicFormControlEvent]] emitted
*/
onChange(event: DynamicFormControlEvent): void {
this.formOperationsService.dispatchOperationsFromEvent(
this.pathCombiner,
event,
this.previousValue,
this.hasStoredValue(this.formBuilderService.getId(event.model), this.formOperationsService.getArrayIndexFromEvent(event)));
const languageMap = new Map();
const isQualdrop = event.model.parent instanceof DynamicQualdropModel;

if (isQualdrop) {
const qualdropMap = this.formOperationsService.getQualdropValueMap(event);

if (qualdropMap) {
const groupMetadata = qualdropMap.keys();
this.formService.getForm(this.formId).pipe(take(1)).subscribe((form) => {
for (const metadata of groupMetadata) {
if (hasValue(form.data[metadata]) && form.data[metadata].length > 1) {
form.data[metadata].forEach((entry: any) => {
languageMap.set(metadata, [...(languageMap.get(metadata) ?? []), entry.language]);
});
} else {
languageMap.set(metadata, [form.data[metadata][0].language]);
}
}
});
}

this.formOperationsService.dispatchOperationsFromEvent(
this.pathCombiner,
event,
this.previousValue,
this.hasStoredValue(this.formBuilderService.getId(event.model), this.formOperationsService.getArrayIndexFromEvent(event)),
languageMap,
);
} else {
this.formOperationsService.dispatchOperationsFromEvent(
this.pathCombiner,
event,
this.previousValue,
this.hasStoredValue(this.formBuilderService.getId(event.model), this.formOperationsService.getArrayIndexFromEvent(event)),
null,
);
}


const metadata = this.formOperationsService.getFieldPathSegmentedFromChangeEvent(event);
const value = this.formOperationsService.getFieldValueFromChangeEvent(event);

Expand Down

0 comments on commit da8427a

Please sign in to comment.