Skip to content

Commit

Permalink
EW-539 working on file builder
Browse files Browse the repository at this point in the history
  • Loading branch information
psachmann committed Nov 28, 2023
1 parent 7616c93 commit da8eb60
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ describe('CommonCartridgeFileBuilder', () => {
expect(manifest).toContain('<organizations>');
expect(manifest).toContain('</organizations>');
});

it('foobar', () => {
// const builder = new CommonCartridgeFileBuilder(CommonCartridgeVersion.V_1_1);
// const metadataBuilder = builder.withMetadata();
// metadataBuilder
// .setTitle('test-title')
// .setCopyrightOwners(['PS', 'SR'])
// .setCreationDate(new Date('2021-01-01'));
// const organization1Builder = builder.withOrganization();
// const organization2Builder = builder.withOrganization();
// organization1Builder.addOrganization()
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,27 @@ import { CommonCartridgeVersion } from '../common-cartridge.enums';
import { CommonCartridgeElement } from '../interfaces/common-cartridge-element.interface';
import { CommonCartridgeResource } from '../interfaces/common-cartridge-resource.interface';
import { CommonCartridgeManifestResource } from '../resources/common-cartridge-manifest-resource';
import { checkDefined } from '../utils';

export class CommonCartridgeManifestBuilder {
private identifier?: string;

private metadata?: CommonCartridgeElement;

private organizations?: CommonCartridgeElement[];

private resources?: CommonCartridgeResource[];

constructor(private readonly version: CommonCartridgeVersion) {}
type CommonCartridgeManifestBuilderProps = {
version: CommonCartridgeVersion;
identifier: string;
metadata: CommonCartridgeElement;
organizations: CommonCartridgeElement[];
resources: CommonCartridgeResource[];
};

setIdentifier(identifier: string): CommonCartridgeManifestBuilder {
this.identifier = identifier;

return this;
}

setMetadata(metadata: CommonCartridgeElement): CommonCartridgeManifestBuilder {
this.metadata = metadata;

return this;
}

setOrganizations(organizations: CommonCartridgeElement[]): CommonCartridgeManifestBuilder {
this.organizations = organizations;

return this;
}

setResources(resources: CommonCartridgeResource[]): CommonCartridgeManifestBuilder {
this.resources = resources;

return this;
}
export class CommonCartridgeManifestBuilder {
constructor(private readonly props: CommonCartridgeManifestBuilderProps) {}

build(): CommonCartridgeResource {
const identifier = checkDefined(this.identifier, 'Identifier');
const metadata = checkDefined(this.metadata, 'Metadata');
const organizations = checkDefined(this.organizations, 'Organizations');
const resources = checkDefined(this.resources, 'Resources');

return new CommonCartridgeManifestResource({
version: this.version,
identifier,
metadata,
organizations,
resources,
const manifestResource = new CommonCartridgeManifestResource({
version: this.props.version,
identifier: this.props.identifier,
metadata: this.props.metadata,
organizations: this.props.organizations,
resources: this.props.resources,
});

return manifestResource;
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,22 @@
import { CommonCartridgeVersion } from '../common-cartridge.enums';
import { CommonCartridgeElement } from '../interfaces/common-cartridge-element.interface';
import { CommonCartridgeResource } from '../interfaces/common-cartridge-resource.interface';
import { checkDefined } from '../utils';

export class CommonCartridgeOrganizationBuilder {
private title?: string;

private identifier?: string;
type CommonCartridgeOrganizationProps = {
version: CommonCartridgeVersion;
title: string;
identifier: string;
};

export class CommonCartridgeOrganizationBuilder {
private items: CommonCartridgeResource[] = [];

private children: CommonCartridgeElement[] = [];

constructor(private readonly version: CommonCartridgeVersion, private readonly parent?: CommonCartridgeElement) {}

setIdentifier(identifier: string): CommonCartridgeOrganizationBuilder {
this.identifier = identifier;

return this;
}

setTitle(title: string): CommonCartridgeOrganizationBuilder {
this.title = title;

return this;
}

addOrganization(organization: CommonCartridgeElement): CommonCartridgeOrganizationBuilder {
this.items.push(organization);
constructor(private readonly props: CommonCartridgeOrganizationProps) {}

return new CommonCartridgeOrganizationBuilder(this.version);
addOrganization(props: CommonCartridgeOrganizationProps): CommonCartridgeOrganizationBuilder {
return new CommonCartridgeOrganizationBuilder(props);
}

addOrganizationItem(item: CommonCartridgeResource): CommonCartridgeOrganizationBuilder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export interface CommonCartridgeElement {
* This method is used to build the imsmanifest.xml file.
* @returns The XML object representation for the imsmanifest.xml file.
*/
getManifestXml(): Record<string, unknown> | undefined;
getManifestXml(): Record<string, unknown>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class CommonCartridgeManifestResource implements CommonCartridgeResource
return this.xmlBuilder.buildObject(this.getManifestXml());
}

getManifestXml(): Record<string, unknown> | undefined {
getManifestXml(): Record<string, unknown> {
return {
manifest: {
$: this.getXmlNamespacesByVersion(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { CommonCartridgeElement } from '../interfaces/common-cartridge-element.interface';

type CommonCartridgeOrganizationElementProps = {
identifier: string;
title: string;
items: CommonCartridgeElement[];
};

export class CommonCartridgeOrganizationElement implements CommonCartridgeElement {
constructor(private readonly props: CommonCartridgeOrganizationElementProps) {}

getManifestXml(): Record<string, unknown> {
return {
$: {
identifier: this.props.identifier,
},
title: this.props.title,
item: this.props.items.map((item) => item.getManifestXml()),
};
}
}

0 comments on commit da8eb60

Please sign in to comment.