From 3bb2ac15e9edd6de4d00d9e57884369935f611ae Mon Sep 17 00:00:00 2001 From: lotte Date: Fri, 19 Jul 2019 16:24:40 +0200 Subject: [PATCH] added workspace item to fieldparsers --- .../models/ds-dynamic-input.model.ts | 4 +++ .../dynamic-relation-group.components.ts | 1 + .../form/builder/form-builder.service.ts | 6 +++-- .../builder/parsers/concat-field-parser.ts | 10 +++++-- .../form/builder/parsers/field-parser.ts | 5 ++-- .../form/builder/parsers/name-field-parser.ts | 5 ++-- .../shared/form/builder/parsers/row-parser.ts | 4 ++- .../builder/parsers/series-field-parser.ts | 5 ++-- src/app/shared/mocks/mock-form-models.ts | 26 +++++++++++++------ .../form/section-form.component.spec.ts | 5 ++++ .../sections/form/section-form.component.ts | 26 +++++++++++++++---- .../submission/sections/sections.service.ts | 2 +- .../section-upload-file-edit.component.ts | 1 + 13 files changed, 75 insertions(+), 25 deletions(-) diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-input.model.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-input.model.ts index 860c481820..d334e3ef19 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-input.model.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-input.model.ts @@ -10,8 +10,10 @@ import { LanguageCode } from '../../models/form-field-language-value.model'; import { AuthorityOptions } from '../../../../../core/integration/models/authority-options.model'; import { hasValue } from '../../../../empty.util'; import { FormFieldMetadataValueObject } from '../../models/form-field-metadata-value.model'; +import { Workspaceitem } from '../../../../../core/submission/models/workspaceitem.model'; export interface DsDynamicInputModelConfig extends DynamicInputModelConfig { + workspaceItem: Workspaceitem; authorityOptions?: AuthorityOptions; languageCodes?: LanguageCode[]; language?: string; @@ -24,12 +26,14 @@ export class DsDynamicInputModel extends DynamicInputModel { @serializable() private _languageCodes: LanguageCode[]; @serializable() private _language: string; @serializable() languageUpdates: Subject; + @serializable() workspaceItem: Workspaceitem; constructor(config: DsDynamicInputModelConfig, layout?: DynamicFormControlLayout) { super(config, layout); this.readOnly = config.readOnly; this.value = config.value; + this.workspaceItem = config.workspaceItem; this.language = config.language; if (!this.language) { // TypeAhead diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/relation-group/dynamic-relation-group.components.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/models/relation-group/dynamic-relation-group.components.ts index fde8d4b7bf..3e0c3b5f09 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/models/relation-group/dynamic-relation-group.components.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/models/relation-group/dynamic-relation-group.components.ts @@ -96,6 +96,7 @@ export class DsDynamicRelationGroupComponent extends DynamicFormControlComponent config, this.model.scopeUUID, {}, + this.model.workspaceItem, this.model.submissionScope, this.model.readOnly); this.initChipsFromModelValue(); diff --git a/src/app/shared/form/builder/form-builder.service.ts b/src/app/shared/form/builder/form-builder.service.ts index 21e702aabb..66048b0883 100644 --- a/src/app/shared/form/builder/form-builder.service.ts +++ b/src/app/shared/form/builder/form-builder.service.ts @@ -29,6 +29,8 @@ import { DynamicRowArrayModel } from './ds-dynamic-form-ui/models/ds-dynamic-row import { DsDynamicInputModel } from './ds-dynamic-form-ui/models/ds-dynamic-input.model'; import { FormFieldMetadataValueObject } from './models/form-field-metadata-value.model'; import { isNgbDateStruct } from '../../date.util'; +import { WorkspaceitemSectionFormObject } from '../../../core/submission/models/workspaceitem-section-form.model'; +import { Workspaceitem } from '../../../core/submission/models/workspaceitem.model'; @Injectable() export class FormBuilderService extends DynamicFormService { @@ -198,13 +200,13 @@ export class FormBuilderService extends DynamicFormService { return result; } - modelFromConfiguration(json: string | SubmissionFormsModel, scopeUUID: string, initFormValues: any = {}, submissionScope?: string, readOnly = false): DynamicFormControlModel[] | never { + modelFromConfiguration(json: string | SubmissionFormsModel, scopeUUID: string, initFormValues: any = {}, wsi: Workspaceitem, submissionScope?: string, readOnly = false): DynamicFormControlModel[] | never { let rows: DynamicFormControlModel[] = []; const rawData = typeof json === 'string' ? JSON.parse(json, JSONUtils.parseReviver) : json; if (rawData.rows && !isEmpty(rawData.rows)) { rawData.rows.forEach((currentRow) => { - const rowParsed = new RowParser(currentRow, scopeUUID, initFormValues, submissionScope, readOnly).parse(); + const rowParsed = new RowParser(currentRow, scopeUUID, initFormValues, wsi, submissionScope, readOnly).parse(); if (isNotNull(rowParsed)) { if (Array.isArray(rowParsed)) { rows = rows.concat(rowParsed); diff --git a/src/app/shared/form/builder/parsers/concat-field-parser.ts b/src/app/shared/form/builder/parsers/concat-field-parser.ts index 135dda5de3..ed8b1eaa8d 100644 --- a/src/app/shared/form/builder/parsers/concat-field-parser.ts +++ b/src/app/shared/form/builder/parsers/concat-field-parser.ts @@ -1,7 +1,11 @@ import { FieldParser } from './field-parser'; import { FormFieldModel } from '../models/form-field.model'; import { FormFieldMetadataValueObject } from '../models/form-field-metadata-value.model'; -import { DynamicFormControlLayout, DynamicInputModel, DynamicInputModelConfig } from '@ng-dynamic-forms/core'; +import { + DynamicFormControlLayout, + DynamicInputModel, + DynamicInputModelConfig +} from '@ng-dynamic-forms/core'; import { CONCAT_FIRST_INPUT_SUFFIX, CONCAT_GROUP_SUFFIX, @@ -11,6 +15,7 @@ import { } from '../ds-dynamic-form-ui/models/ds-dynamic-concat.model'; import { isNotEmpty } from '../../../empty.util'; import { ParserOptions } from './parser-options'; +import { Workspaceitem } from '../../../../core/submission/models/workspaceitem.model'; export class ConcatFieldParser extends FieldParser { @@ -18,9 +23,10 @@ export class ConcatFieldParser extends FieldParser { protected initFormValues, protected parserOptions: ParserOptions, protected separator: string, + protected workspaceItem: Workspaceitem, protected firstPlaceholder: string = null, protected secondPlaceholder: string = null) { - super(configData, initFormValues, parserOptions); + super(configData, initFormValues, parserOptions, workspaceItem); this.separator = separator; this.firstPlaceholder = firstPlaceholder; diff --git a/src/app/shared/form/builder/parsers/field-parser.ts b/src/app/shared/form/builder/parsers/field-parser.ts index 28e3fb8fb5..8550cbae67 100644 --- a/src/app/shared/form/builder/parsers/field-parser.ts +++ b/src/app/shared/form/builder/parsers/field-parser.ts @@ -12,12 +12,12 @@ import { DynamicFormControlLayout } from '@ng-dynamic-forms/core'; import { setLayout } from './parser.utils'; import { AuthorityOptions } from '../../../../core/integration/models/authority-options.model'; import { ParserOptions } from './parser-options'; +import { Workspaceitem } from '../../../../core/submission/models/workspaceitem.model'; export abstract class FieldParser { protected fieldId: string; - - constructor(protected configData: FormFieldModel, protected initFormValues, protected parserOptions: ParserOptions) { + constructor(protected configData: FormFieldModel, protected initFormValues, protected parserOptions: ParserOptions, protected workspaceItem: Workspaceitem) { } public abstract modelFactory(fieldValue?: FormFieldMetadataValueObject, label?: boolean): any; @@ -184,6 +184,7 @@ export abstract class FieldParser { // Set read only option controlModel.readOnly = this.parserOptions.readOnly; controlModel.disabled = this.parserOptions.readOnly; + controlModel.workspaceItem = this.workspaceItem; // Set label this.setLabel(controlModel, label, labelEmpty); diff --git a/src/app/shared/form/builder/parsers/name-field-parser.ts b/src/app/shared/form/builder/parsers/name-field-parser.ts index 896b3cc478..de2b931b17 100644 --- a/src/app/shared/form/builder/parsers/name-field-parser.ts +++ b/src/app/shared/form/builder/parsers/name-field-parser.ts @@ -1,10 +1,11 @@ import { FormFieldModel } from '../models/form-field.model'; import { ConcatFieldParser } from './concat-field-parser'; import { ParserOptions } from './parser-options'; +import { Workspaceitem } from '../../../../core/submission/models/workspaceitem.model'; export class NameFieldParser extends ConcatFieldParser { - constructor(protected configData: FormFieldModel, protected initFormValues, protected parserOptions: ParserOptions) { - super(configData, initFormValues, parserOptions, ',', 'form.last-name', 'form.first-name'); + constructor(protected configData: FormFieldModel, protected initFormValues, protected parserOptions: ParserOptions, wsi: Workspaceitem) { + super(configData, initFormValues, parserOptions, ',', wsi, 'form.last-name', 'form.first-name'); } } diff --git a/src/app/shared/form/builder/parsers/row-parser.ts b/src/app/shared/form/builder/parsers/row-parser.ts index 0bb8a0e89a..ec533eb390 100644 --- a/src/app/shared/form/builder/parsers/row-parser.ts +++ b/src/app/shared/form/builder/parsers/row-parser.ts @@ -10,6 +10,7 @@ import { FormFieldModel } from '../models/form-field.model'; import { ParserType } from './parser-type'; import { ParserOptions } from './parser-options'; import { ParserFactory } from './parser-factory'; +import { Workspaceitem } from '../../../../core/submission/models/workspaceitem.model'; export const ROW_ID_PREFIX = 'df-row-group-config-'; @@ -19,6 +20,7 @@ export class RowParser { constructor(protected rowData, protected scopeUUID, protected initFormValues: any, + protected wsi: Workspaceitem, protected submissionScope, protected readOnly: boolean) { this.authorityOptions = new IntegrationSearchOptions(scopeUUID); @@ -49,7 +51,7 @@ export class RowParser { const layoutFieldClass = (fieldData.style || layoutDefaultGridClass) + layoutClass; const parserCo = ParserFactory.getConstructor(fieldData.input.type as ParserType); if (parserCo) { - fieldModel = new parserCo(fieldData, this.initFormValues, parserOptions).parse(); + fieldModel = new parserCo(fieldData, this.initFormValues, parserOptions, this.wsi).parse(); } else { throw new Error(`unknown form control model type "${fieldData.input.type}" defined for Input field with label "${fieldData.label}".`, ); } diff --git a/src/app/shared/form/builder/parsers/series-field-parser.ts b/src/app/shared/form/builder/parsers/series-field-parser.ts index 9857b4e993..fbe2716341 100644 --- a/src/app/shared/form/builder/parsers/series-field-parser.ts +++ b/src/app/shared/form/builder/parsers/series-field-parser.ts @@ -1,10 +1,11 @@ import { FormFieldModel } from '../models/form-field.model'; import { ConcatFieldParser } from './concat-field-parser'; import { ParserOptions } from './parser-options'; +import { Workspaceitem } from '../../../../core/submission/models/workspaceitem.model'; export class SeriesFieldParser extends ConcatFieldParser { - constructor(protected configData: FormFieldModel, protected initFormValues, protected parserOptions: ParserOptions) { - super(configData, initFormValues, parserOptions, ';'); + constructor(protected configData: FormFieldModel, protected initFormValues, protected parserOptions: ParserOptions, wsi: Workspaceitem) { + super(configData, initFormValues, parserOptions, ';', wsi); } } diff --git a/src/app/shared/mocks/mock-form-models.ts b/src/app/shared/mocks/mock-form-models.ts index 5851da94be..a17d2e81af 100644 --- a/src/app/shared/mocks/mock-form-models.ts +++ b/src/app/shared/mocks/mock-form-models.ts @@ -13,6 +13,7 @@ import { AuthorityOptions } from '../../core/integration/models/authority-option import { AuthorityValue } from '../../core/integration/models/authority.value'; import { FormFieldMetadataValueObject } from '../form/builder/models/form-field-metadata-value.model'; import { DynamicRowGroupModel } from '../form/builder/ds-dynamic-form-ui/models/ds-dynamic-row-group-model'; +import { Workspaceitem } from '../../core/submission/models/workspaceitem.model'; export const qualdropSelectConfig = { name: 'dc.identifier_QUALDROP_METADATA', @@ -55,7 +56,8 @@ export const qualdropInputConfig = { id: 'dc_identifier_QUALDROP_VALUE', readOnly: false, disabled: false, - value: 'test' + value: 'test', + workspaceItem: new Workspaceitem() }; export const mockQualdropSelectModel = new DynamicSelectModel(qualdropSelectConfig); @@ -130,7 +132,8 @@ const relationGroupConfig = { 'issue test 1', 'issue test 2' ], - } + }, + workspaceItem: new Workspaceitem() }; export const MockRelationModel: DynamicRelationGroupModel = new DynamicRelationGroupModel(relationGroupConfig); @@ -156,7 +159,8 @@ export const inputWithLanguageAndAuthorityConfig = { value: 'testWithLanguageAndAuthority', display: 'testWithLanguageAndAuthority', id: 'testWithLanguageAndAuthority', - } + }, + workspaceItem: new Workspaceitem() }; export const mockInputWithLanguageAndAuthorityModel = new DsDynamicInputModel(inputWithLanguageAndAuthorityConfig); @@ -177,7 +181,8 @@ export const inputWithLanguageConfig = { id: 'testWithLanguage', readOnly: false, disabled: false, - value: 'testWithLanguage' + value: 'testWithLanguage', + workspaceItem: new Workspaceitem() }; export const mockInputWithLanguageModel = new DsDynamicInputModel(inputWithLanguageConfig); @@ -203,7 +208,8 @@ export const inputWithLanguageAndAuthorityArrayConfig = { value: 'testLanguageAndAuthorityArray', display: 'testLanguageAndAuthorityArray', id: 'testLanguageAndAuthorityArray', - }] + }], + workspaceItem: new Workspaceitem() }; export const mockInputWithLanguageAndAuthorityArrayModel = new DsDynamicInputModel(inputWithLanguageAndAuthorityArrayConfig); @@ -213,7 +219,8 @@ export const inputWithFormFieldValueConfig = { id: 'testWithFormField', readOnly: false, disabled: false, - value: new FormFieldMetadataValueObject('testWithFormFieldValue') + value: new FormFieldMetadataValueObject('testWithFormFieldValue'), + workspaceItem: new Workspaceitem() }; export const mockInputWithFormFieldValueModel = new DsDynamicInputModel(inputWithFormFieldValueConfig); @@ -223,7 +230,8 @@ export const inputWithAuthorityValueConfig = { id: 'testWithAuthorityField', readOnly: false, disabled: false, - value: Object.assign({}, new AuthorityValue(), { value: 'testWithAuthorityValue', id: 'testWithAuthorityValue', display: 'testWithAuthorityValue' }) + value: Object.assign({}, new AuthorityValue(), { value: 'testWithAuthorityValue', id: 'testWithAuthorityValue', display: 'testWithAuthorityValue' }), + workspaceItem: new Workspaceitem() }; export const mockInputWithAuthorityValueModel = new DsDynamicInputModel(inputWithAuthorityValueConfig); @@ -233,7 +241,8 @@ export const inputWithObjectValueConfig = { id: 'testWithObjectValue', readOnly: false, disabled: false, - value: { value: 'testWithObjectValue', id: 'testWithObjectValue', display: 'testWithObjectValue' } + value: { value: 'testWithObjectValue', id: 'testWithObjectValue', display: 'testWithObjectValue' }, + workspaceItem: new Workspaceitem() }; export const mockInputWithObjectValueModel = new DsDynamicInputModel(inputWithObjectValueConfig); @@ -248,6 +257,7 @@ export const fileFormEditInputConfig = { id: 'dc_title', readOnly: false, disabled: false, + workspaceItem: new Workspaceitem() }; export const mockFileFormEditInputModel = new DsDynamicInputModel(fileFormEditInputConfig); diff --git a/src/app/submission/sections/form/section-form.component.spec.ts b/src/app/submission/sections/form/section-form.component.spec.ts index 477d42a0a1..71dc207cfd 100644 --- a/src/app/submission/sections/form/section-form.component.spec.ts +++ b/src/app/submission/sections/form/section-form.component.spec.ts @@ -43,6 +43,10 @@ import { DsDynamicInputModel } from '../../../shared/form/builder/ds-dynamic-for import { SubmissionSectionError } from '../../objects/submission-objects.reducer'; import { DynamicFormControlEvent, DynamicFormControlEventType } from '@ng-dynamic-forms/core'; import { JsonPatchOperationPathCombiner } from '../../../core/json-patch/builder/json-patch-operation-path-combiner'; +import { WorkspaceitemDataService } from '../../../core/submission/workspaceitem-data.service'; +import { RemoteData } from '../../../core/data/remote-data'; +import { PaginatedList } from '../../../core/data/paginated-list'; +import { Workspaceitem } from '../../../core/submission/models/workspaceitem.model'; function getMockSubmissionFormsConfigService(): SubmissionFormsConfigService { return jasmine.createSpyObj('FormOperationsService', { @@ -179,6 +183,7 @@ describe('SubmissionSectionformComponent test suite', () => { { provide: 'collectionIdProvider', useValue: collectionId }, { provide: 'sectionDataProvider', useValue: sectionObject }, { provide: 'submissionIdProvider', useValue: submissionId }, + { provide: WorkspaceitemDataService, useValue: {getById: observableOf(new RemoteData(false, false, true, null, new Workspaceitem()))}}, ChangeDetectorRef, SubmissionSectionformComponent ], diff --git a/src/app/submission/sections/form/section-form.component.ts b/src/app/submission/sections/form/section-form.component.ts index ef817a7568..49cc62566a 100644 --- a/src/app/submission/sections/form/section-form.component.ts +++ b/src/app/submission/sections/form/section-form.component.ts @@ -15,7 +15,10 @@ import { hasValue, isNotEmpty, isUndefined } from '../../../shared/empty.util'; import { ConfigData } from '../../../core/config/config-data'; import { JsonPatchOperationPathCombiner } from '../../../core/json-patch/builder/json-patch-operation-path-combiner'; import { SubmissionFormsModel } from '../../../core/config/models/config-submission-forms.model'; -import { SubmissionSectionError, SubmissionSectionObject } from '../../objects/submission-objects.reducer'; +import { + SubmissionSectionError, + SubmissionSectionObject +} from '../../objects/submission-objects.reducer'; import { FormFieldPreviousValueObject } from '../../../shared/form/builder/models/form-field-previous-value-object'; import { GLOBAL_CONFIG } from '../../../../config'; import { GlobalConfig } from '../../../../config/global-config.interface'; @@ -28,6 +31,11 @@ import { NotificationsService } from '../../../shared/notifications/notification import { SectionsService } from '../sections.service'; import { difference } from '../../../shared/object.util'; import { WorkspaceitemSectionFormObject } from '../../../core/submission/models/workspaceitem-section-form.model'; +import { Workspaceitem } from '../../../core/submission/models/workspaceitem.model'; +import { WorkspaceitemDataService } from '../../../core/submission/workspaceitem-data.service'; +import { combineLatest as combineLatestObservable } from 'rxjs'; +import { getSucceededRemoteData } from '../../../core/shared/operators'; +import { RemoteData } from '../../../core/data/remote-data'; /** * This component represents a section that contains a Form. @@ -94,6 +102,7 @@ export class SubmissionSectionformComponent extends SectionModelComponent { */ protected subs: Subscription[] = []; + protected workspaceItem; /** * The FormComponent reference */ @@ -125,6 +134,7 @@ export class SubmissionSectionformComponent extends SectionModelComponent { protected sectionService: SectionsService, protected submissionService: SubmissionService, protected translate: TranslateService, + protected workspaceItemDataService: WorkspaceitemDataService, @Inject(GLOBAL_CONFIG) protected EnvConfig: GlobalConfig, @Inject('collectionIdProvider') public injectedCollectionId: string, @Inject('sectionDataProvider') public injectedSectionData: SectionDataObject, @@ -138,16 +148,20 @@ export class SubmissionSectionformComponent extends SectionModelComponent { onSectionInit() { this.pathCombiner = new JsonPatchOperationPathCombiner('sections', this.sectionData.id); this.formId = this.formService.getUniqueId(this.sectionData.id); - this.formConfigService.getConfigByHref(this.sectionData.config).pipe( map((configData: ConfigData) => configData.payload), tap((config: SubmissionFormsModel) => this.formConfig = config), - flatMap(() => this.sectionService.getSectionData(this.submissionId, this.sectionData.id)), + flatMap(() => + combineLatestObservable( + this.sectionService.getSectionData(this.submissionId, this.sectionData.id), + this.workspaceItemDataService.findById(this.submissionId).pipe(getSucceededRemoteData()) + )), take(1)) - .subscribe((sectionData: WorkspaceitemSectionFormObject) => { + .subscribe(([sectionData, workspaceItem]: [WorkspaceitemSectionFormObject, RemoteData]) => { if (isUndefined(this.formModel)) { this.sectionData.errors = []; // Is the first loading so init form + this.workspaceItem = workspaceItem; this.initForm(sectionData); this.sectionData.data = sectionData; this.subscriptions(); @@ -212,7 +226,9 @@ export class SubmissionSectionformComponent extends SectionModelComponent { this.formConfig, this.collectionId, sectionData, - this.submissionService.getSubmissionScope()); + this.workspaceItem, + this.submissionService.getSubmissionScope() + ); } catch (e) { const msg: string = this.translate.instant('error.submission.sections.init-form-error') + e.toString(); const sectionError: SubmissionSectionError = { diff --git a/src/app/submission/sections/sections.service.ts b/src/app/submission/sections/sections.service.ts index aed83143a5..f6ad5ef0cf 100644 --- a/src/app/submission/sections/sections.service.ts +++ b/src/app/submission/sections/sections.service.ts @@ -84,7 +84,7 @@ export class SectionsService { } else if (!isEqual(currentErrors, prevErrors)) { // compare previous error list with the current one const dispatchedErrors = []; - // Itereate over the current error list + // Iterate over the current error list currentErrors.forEach((error: SubmissionSectionError) => { const errorPaths: SectionErrorPath[] = parseSectionErrorPaths(error.path); diff --git a/src/app/submission/sections/upload/file/edit/section-upload-file-edit.component.ts b/src/app/submission/sections/upload/file/edit/section-upload-file-edit.component.ts index b2575d1d58..8040b7d818 100644 --- a/src/app/submission/sections/upload/file/edit/section-upload-file-edit.component.ts +++ b/src/app/submission/sections/upload/file/edit/section-upload-file-edit.component.ts @@ -169,6 +169,7 @@ export class SubmissionSectionUploadFileEditComponent implements OnChanges { configForm, this.collectionId, this.fileData.metadata, + undefined, this.submissionService.getSubmissionScope() ); formModel.push(new DynamicFormGroupModel(metadataGroupModelConfig, BITSTREAM_METADATA_FORM_GROUP_LAYOUT));