Skip to content

Commit

Permalink
refactor: move labels to DatasetVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
MellyGray committed Dec 18, 2023
1 parent 62b69b2 commit 030b794
Show file tree
Hide file tree
Showing 20 changed files with 297 additions and 279 deletions.
172 changes: 102 additions & 70 deletions src/dataset/domain/models/Dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,26 +213,107 @@ export enum DatasetNonNumericVersion {
DRAFT = ':draft'
}

export class DatasetVersionNumber {
constructor(public readonly majorNumber?: number, public readonly minorNumber?: number) {}

toString(): string | DatasetNonNumericVersion {
if (this.majorNumber === undefined || this.minorNumber === undefined) {
return DatasetNonNumericVersion.DRAFT
}
return `${this.majorNumber}.${this.minorNumber}`
}
}

export class DatasetVersion {
constructor(
public readonly id: number,
public readonly title: string,
public readonly number: DatasetVersionNumber,
public readonly publishingStatus: DatasetPublishingStatus,
public readonly citation: string,
public readonly labels: DatasetLabel[],
public readonly isLatest: boolean,
public readonly isInReview: boolean,
public readonly latestVersionStatus: DatasetPublishingStatus,
public readonly citation: string,
// requestedVersion will be set if the user requested a version that did not exist.
public readonly majorNumber?: number,
public readonly minorNumber?: number,
public readonly requestedVersion?: string
public readonly latestVersionPublishingStatus: DatasetPublishingStatus,
public readonly someDatasetVersionHasBeenReleased: boolean
) {}

toString(): string | DatasetNonNumericVersion {
if (this.majorNumber === undefined || this.minorNumber === undefined) {
return DatasetNonNumericVersion.DRAFT
static Builder = class {
public readonly labels: DatasetLabel[] = []

constructor(
public readonly id: number,
public readonly title: string,
public readonly number: DatasetVersionNumber,
public readonly publishingStatus: DatasetPublishingStatus,
public readonly citation: string,
public readonly isLatest: boolean,
public readonly isInReview: boolean,
public readonly latestVersionPublishingStatus: DatasetPublishingStatus,
public readonly someDatasetVersionHasBeenReleased: boolean
) {
this.withLabels()
}

withLabels() {
this.withStatusLabel()
this.withVersionLabel()
}

private withStatusLabel(): void {
if (this.publishingStatus === DatasetPublishingStatus.DRAFT) {
this.labels.push(
new DatasetLabel(DatasetLabelSemanticMeaning.DATASET, DatasetLabelValue.DRAFT)
)
}

if (!this.someDatasetVersionHasBeenReleased) {
this.labels.push(
new DatasetLabel(DatasetLabelSemanticMeaning.WARNING, DatasetLabelValue.UNPUBLISHED)
)
}

if (this.publishingStatus === DatasetPublishingStatus.DEACCESSIONED) {
this.labels.push(
new DatasetLabel(DatasetLabelSemanticMeaning.DANGER, DatasetLabelValue.DEACCESSIONED)
)
}

if (this.publishingStatus === DatasetPublishingStatus.EMBARGOED) {
this.labels.push(
new DatasetLabel(DatasetLabelSemanticMeaning.DATASET, DatasetLabelValue.EMBARGOED)
)
}

if (this.isInReview) {
this.labels.push(
new DatasetLabel(DatasetLabelSemanticMeaning.SUCCESS, DatasetLabelValue.IN_REVIEW)
)
}
}

private withVersionLabel(): void {
if (this.publishingStatus === DatasetPublishingStatus.RELEASED) {
this.labels.push(
new DatasetLabel(DatasetLabelSemanticMeaning.FILE, `Version ${this.number.toString()}`)
)
}
}

build(): DatasetVersion {
return new DatasetVersion(
this.id,
this.title,
this.number,
this.publishingStatus,
this.citation,
this.labels,
this.isLatest,
this.isInReview,
this.latestVersionPublishingStatus,
this.someDatasetVersionHasBeenReleased
)
}
return `${this.majorNumber}.${this.minorNumber}`
}
}

Expand Down Expand Up @@ -275,7 +356,6 @@ export class Dataset {
constructor(
public readonly persistentId: string,
public readonly version: DatasetVersion,
public readonly labels: DatasetLabel[],
public readonly alerts: Alert[],
public readonly summaryFields: DatasetMetadataBlock[],
public readonly license: DatasetLicense,
Expand All @@ -285,11 +365,11 @@ export class Dataset {
public readonly hasValidTermsOfAccess: boolean,
public readonly hasOneTabularFileAtLeast: boolean,
public readonly isValid: boolean,
public readonly isReleased: boolean,
public readonly downloadUrls: DatasetDownloadUrls,
public readonly thumbnail?: string,
public readonly privateUrl?: PrivateUrl,
public readonly fileDownloadSizes?: FileDownloadSize[]
public readonly fileDownloadSizes?: FileDownloadSize[],
public readonly requestedVersion?: string // will be set if the user requested a version that did not exist
) {}

public checkIsLockedFromPublishing(userPersistentId: string): boolean {
Expand Down Expand Up @@ -352,7 +432,6 @@ export class Dataset {
}

static Builder = class {
public readonly labels: DatasetLabel[] = []
public readonly alerts: Alert[] = []

constructor(
Expand All @@ -366,80 +445,34 @@ export class Dataset {
public readonly hasValidTermsOfAccess: boolean,
public readonly hasOneTabularFileAtLeast: boolean,
public readonly isValid: boolean,
public readonly isReleased: boolean,
public readonly downloadUrls: DatasetDownloadUrls,
public readonly thumbnail?: string,
public readonly privateUrl?: PrivateUrl,
public readonly fileDownloadSizes?: FileDownloadSize[]
public readonly fileDownloadSizes?: FileDownloadSize[],
public readonly requestedVersion?: string // will be set if the user requested a version that did not exist
) {
this.withLabels()
this.withAlerts()
}

withLabels() {
this.withStatusLabel()
this.withVersionLabel()
}

private withStatusLabel(): void {
if (this.version.publishingStatus === DatasetPublishingStatus.DRAFT) {
this.labels.push(
new DatasetLabel(DatasetLabelSemanticMeaning.DATASET, DatasetLabelValue.DRAFT)
)
}

if (!this.isReleased) {
this.labels.push(
new DatasetLabel(DatasetLabelSemanticMeaning.WARNING, DatasetLabelValue.UNPUBLISHED)
)
}

if (this.version.publishingStatus === DatasetPublishingStatus.DEACCESSIONED) {
this.labels.push(
new DatasetLabel(DatasetLabelSemanticMeaning.DANGER, DatasetLabelValue.DEACCESSIONED)
)
}

if (this.version.publishingStatus === DatasetPublishingStatus.EMBARGOED) {
this.labels.push(
new DatasetLabel(DatasetLabelSemanticMeaning.DATASET, DatasetLabelValue.EMBARGOED)
)
}

if (this.version.isInReview) {
this.labels.push(
new DatasetLabel(DatasetLabelSemanticMeaning.SUCCESS, DatasetLabelValue.IN_REVIEW)
)
}
}

private withVersionLabel(): void {
if (this.version.publishingStatus === DatasetPublishingStatus.RELEASED) {
this.labels.push(
new DatasetLabel(DatasetLabelSemanticMeaning.FILE, `Version ${this.version.toString()}`)
)
}
}

private withAlerts(): void {
if (
this.version.publishingStatus === DatasetPublishingStatus.DRAFT &&
this.permissions.canPublishDataset
) {
this.alerts.push(new Alert('warning', AlertMessageKey.DRAFT_VERSION))
}
if (this.version.requestedVersion) {
if (this.version.latestVersionStatus == DatasetPublishingStatus.RELEASED) {
if (this.requestedVersion) {
if (this.version.latestVersionPublishingStatus == DatasetPublishingStatus.RELEASED) {
const dynamicFields = {
requestedVersion: this.version.requestedVersion,
returnedVersion: `${this.version.toString()}`
requestedVersion: this.requestedVersion,
returnedVersion: `${this.version.number.toString()}`
}
this.alerts.push(
new Alert('warning', AlertMessageKey.REQUESTED_VERSION_NOT_FOUND, dynamicFields)
)
} else {
const dynamicFields = {
requestedVersion: this.version.requestedVersion
requestedVersion: this.requestedVersion
}
this.alerts.push(
new Alert(
Expand All @@ -466,7 +499,6 @@ export class Dataset {
return new Dataset(
this.persistentId,
this.version,
this.labels,
this.alerts,
this.summaryFields,
this.license,
Expand All @@ -476,11 +508,11 @@ export class Dataset {
this.hasValidTermsOfAccess,
this.hasOneTabularFileAtLeast,
this.isValid,
this.isReleased,
this.downloadUrls,
this.thumbnail,
this.privateUrl,
this.fileDownloadSizes
this.fileDownloadSizes,
this.requestedVersion
)
}
}
Expand Down
43 changes: 26 additions & 17 deletions src/dataset/infrastructure/mappers/JSDatasetMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
DatasetDownloadUrls,
DatasetPermissions,
DatasetLock,
DatasetLockReason
DatasetLockReason,
DatasetVersionNumber
} from '../../domain/models/Dataset'

export class JSDatasetMapper {
Expand All @@ -37,8 +38,7 @@ export class JSDatasetMapper {
jsDataset.versionId,
jsDataset.versionInfo,
jsDataset.metadataBlocks,
jsDatasetCitation,
requestedVersion
jsDatasetCitation
)
return new Dataset.Builder(
jsDataset.persistentId,
Expand All @@ -56,32 +56,41 @@ export class JSDatasetMapper {
true, // TODO Connect with dataset hasValidTermsOfAccess
true, // TODO Connect with dataset hasOneTabularFileAtLeast
true, // TODO Connect with dataset isValid
JSDatasetMapper.toIsReleased(jsDataset.versionInfo),
JSDatasetMapper.toDownloadUrls(jsDataset.persistentId, version),
undefined, // TODO: get dataset thumbnail from Dataverse https://github.com/IQSS/dataverse-frontend/issues/203
privateUrl,
[] // TODO: Connect with file download use case
[], // TODO: Connect with file download use case,
requestedVersion
).build()
}

static toVersion(
jDatasetVersionId: number,
jsDatasetVersionInfo: JSDatasetVersionInfo,
jsDatasetMetadataBlocks: JSDatasetMetadataBlocks,
jsDatasetCitation: string,
requestedVersion?: string
jsDatasetCitation: string
): DatasetVersion {
return new DatasetVersion(
return new DatasetVersion.Builder(
jDatasetVersionId,
jsDatasetMetadataBlocks[0].fields.title,
JSDatasetMapper.toStatus(jsDatasetVersionInfo.state),
true,
false,
JSDatasetMapper.toDatasetTitle(jsDatasetMetadataBlocks),
JSDatasetMapper.toVersionNumber(jsDatasetVersionInfo),
JSDatasetMapper.toStatus(jsDatasetVersionInfo.state),
jsDatasetCitation,
true, // TODO Connect with dataset version isLatest
false, // TODO Connect with dataset version isInReview
JSDatasetMapper.toStatus(jsDatasetVersionInfo.state),
JSDatasetMapper.toSomeDatasetVersionHasBeenReleased(jsDatasetVersionInfo)
)
}

static toDatasetTitle(jsDatasetMetadataBlocks: JSDatasetMetadataBlocks): string {
return jsDatasetMetadataBlocks[0].fields.title
}

static toVersionNumber(jsDatasetVersionInfo: JSDatasetVersionInfo): DatasetVersionNumber {
return new DatasetVersionNumber(
jsDatasetVersionInfo.majorNumber,
jsDatasetVersionInfo.minorNumber,
requestedVersion
jsDatasetVersionInfo.minorNumber
)
}

Expand Down Expand Up @@ -204,11 +213,11 @@ export class JSDatasetMapper {
version: DatasetVersion
): DatasetDownloadUrls {
return {
original: `/api/access/dataset/:persistentId/versions/${version.toString()}?persistentId=${jsDatasetPersistentId}&format=original`,
archival: `/api/access/dataset/:persistentId/versions/${version.toString()}?persistentId=${jsDatasetPersistentId}`
original: `/api/access/dataset/:persistentId/versions/${version.number.toString()}?persistentId=${jsDatasetPersistentId}&format=original`,
archival: `/api/access/dataset/:persistentId/versions/${version.number.toString()}?persistentId=${jsDatasetPersistentId}`
}
}
static toIsReleased(jsDatasetVersionInfo: JSDatasetVersionInfo): boolean {
static toSomeDatasetVersionHasBeenReleased(jsDatasetVersionInfo: JSDatasetVersionInfo): boolean {
return (
jsDatasetVersionInfo.releaseTime !== undefined &&
!isNaN(jsDatasetVersionInfo.releaseTime.getTime())
Expand Down
2 changes: 1 addition & 1 deletion src/sections/dataset/Dataset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function Dataset({ fileRepository }: DatasetProps) {

<header className={styles.header}>
<h1>{dataset.version.title}</h1>
<DatasetLabels labels={dataset.labels} />
<DatasetLabels labels={dataset.version.labels} />
</header>
<div className={styles.container}>
<Row>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ interface DeaccessionDatasetButtonProps {
dataset: Dataset
}
export function DeaccessionDatasetButton({ dataset }: DeaccessionDatasetButtonProps) {
if (!dataset.isReleased || !dataset.permissions.canPublishDataset) {
if (
!dataset.version.someDatasetVersionHasBeenReleased ||
!dataset.permissions.canPublishDataset
) {
return <></>
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface DeleteDatasetButtonProps {
export function DeleteDatasetButton({ dataset }: DeleteDatasetButtonProps) {
if (
!dataset.permissions.canDeleteDataset ||
dataset.version.latestVersionStatus !== DatasetPublishingStatus.DRAFT
dataset.version.latestVersionPublishingStatus !== DatasetPublishingStatus.DRAFT
) {
return <></>
}
Expand All @@ -18,7 +18,7 @@ export function DeleteDatasetButton({ dataset }: DeleteDatasetButtonProps) {
<>
<DropdownSeparator />
<DropdownButtonItem>
{dataset.isReleased
{dataset.version.someDatasetVersionHasBeenReleased
? t('datasetActionButtons.editDataset.delete.draft')
: t('datasetActionButtons.editDataset.delete.released')}
</DropdownButtonItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function LinkDatasetButton({ dataset }: LinkDatasetButtonProps) {

if (
!user ||
!dataset.isReleased ||
!dataset.version.someDatasetVersionHasBeenReleased ||
dataset.version.publishingStatus === DatasetPublishingStatus.DEACCESSIONED
) {
return <></>
Expand Down
Loading

0 comments on commit 030b794

Please sign in to comment.