Skip to content

Commit

Permalink
Refactorings, renamed some classes and separated out code into packages.
Browse files Browse the repository at this point in the history
  • Loading branch information
janvanmansum committed Nov 12, 2024
1 parent e636bb9 commit 64b2aa1
Show file tree
Hide file tree
Showing 18 changed files with 224 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import io.dropwizard.core.setup.Bootstrap;
import io.dropwizard.core.setup.Environment;
import nl.knaw.dans.dvingest.config.DdDataverseIngestConfiguration;
import nl.knaw.dans.dvingest.core.DatasetTaskFactory;
import nl.knaw.dans.dvingest.core.DataverseServiceImpl;
import nl.knaw.dans.dvingest.core.datasetversiontask.DatasetVersionTaskFactory;
import nl.knaw.dans.dvingest.core.service.DataverseServiceImpl;
import nl.knaw.dans.dvingest.core.IngestArea;
import nl.knaw.dans.dvingest.core.UtilityServicesImpl;
import nl.knaw.dans.dvingest.core.service.UtilityServicesImpl;
import nl.knaw.dans.dvingest.resources.DefaultApiResource;
import nl.knaw.dans.dvingest.resources.IllegalArgumentExceptionMapper;
import nl.knaw.dans.dvingest.resources.IngestApiResource;
Expand Down Expand Up @@ -60,7 +60,7 @@ public void run(final DdDataverseIngestConfiguration configuration, final Enviro
.executorService(environment.lifecycle().executorService("import").minThreads(1).maxThreads(1).build())
.dataverseService(dataverseService)
.utilityServices(utilityServices)
.datasetTaskFactory(new DatasetTaskFactory(dataverseService, utilityServices))
.datasetVersionTaskFactory(new DatasetVersionTaskFactory(dataverseService, utilityServices))
.inbox(configuration.getIngest().getImportConfig().getInbox())
.outbox(configuration.getIngest().getImportConfig().getOutbox()).build();
environment.jersey().register(new IngestApiResource(importArea));
Expand Down
73 changes: 0 additions & 73 deletions src/main/java/nl/knaw/dans/dvingest/core/CreateNewDatasetTask.java

This file was deleted.

1 change: 1 addition & 0 deletions src/main/java/nl/knaw/dans/dvingest/core/Deposit.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import lombok.NonNull;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import nl.knaw.dans.dvingest.core.model.UpdateInstructions;
import nl.knaw.dans.lib.dataverse.MetadataFieldDeserializer;
import nl.knaw.dans.lib.dataverse.model.dataset.Dataset;
import nl.knaw.dans.lib.dataverse.model.dataset.MetadataField;
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/nl/knaw/dans/dvingest/core/ImportJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import nl.knaw.dans.dvingest.api.ImportCommandDto;
import nl.knaw.dans.dvingest.api.ImportJobStatusDto;
import nl.knaw.dans.dvingest.api.ImportJobStatusDto.StatusEnum;
import nl.knaw.dans.dvingest.core.datasetversiontask.DatasetVersionTaskFactory;
import nl.knaw.dans.dvingest.core.service.DataverseService;
import nl.knaw.dans.dvingest.core.service.UtilityServices;

import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -42,7 +45,7 @@ public class ImportJob implements Runnable {
private final UtilityServices utilityServices;

@NonNull
private final DatasetTaskFactory datasetTaskFactory;
private final DatasetVersionTaskFactory datasetVersionTaskFactory;

@NonNull
private final CompletionHandler completionHandler;
Expand All @@ -54,13 +57,13 @@ private ImportJob(@NonNull ImportCommandDto importCommand,
@NonNull Path outputDir,
@NonNull DataverseService dataverseService,
@NonNull UtilityServices utilityServices,
@NonNull DatasetTaskFactory datasetTaskFactory,
@NonNull DatasetVersionTaskFactory datasetVersionTaskFactory,
@NonNull CompletionHandler completionHandler) {
this.importCommand = importCommand;
this.outputDir = outputDir;
this.dataverseService = dataverseService;
this.utilityServices = utilityServices;
this.datasetTaskFactory = datasetTaskFactory;
this.datasetVersionTaskFactory = datasetVersionTaskFactory;
this.completionHandler = completionHandler;
}

Expand Down Expand Up @@ -90,7 +93,8 @@ public void run() {
// Process deposits
for (Deposit deposit : deposits) {
log.info("START Processing deposit: {}", deposit.getId());
datasetTaskFactory.createIngestTask(deposit, outputDir).run();
var task = datasetVersionTaskFactory.createDatasetVersionTask(deposit, outputDir);
task.run();
log.info("END Processing deposit: {}", deposit.getId());
// TODO: record number of processed/rejected/failed deposits in ImportJob status
}
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/nl/knaw/dans/dvingest/core/IngestArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import lombok.extern.slf4j.Slf4j;
import nl.knaw.dans.dvingest.api.ImportCommandDto;
import nl.knaw.dans.dvingest.api.ImportJobStatusDto;
import nl.knaw.dans.dvingest.core.ImportJob.CompletionHandler;
import nl.knaw.dans.dvingest.core.datasetversiontask.DatasetVersionTaskFactory;
import nl.knaw.dans.dvingest.core.service.DataverseService;
import nl.knaw.dans.dvingest.core.service.UtilityServices;

import java.io.IOException;
import java.nio.file.Path;
Expand All @@ -39,20 +41,20 @@ public class IngestArea {
@NonNull
private final UtilityServices utilityServices;
@NonNull
private final DatasetTaskFactory datasetTaskFactory;
private final DatasetVersionTaskFactory datasetVersionTaskFactory;
@NonNull
private final Path inbox;
@NonNull
private final Path outbox;

private final Map<String, ImportJob> importJobs = new ConcurrentHashMap<>();

private IngestArea(ExecutorService executorService, DataverseService dataverseService, UtilityServices utilityServices, DatasetTaskFactory datasetTaskFactory, Path inbox, Path outbox) {
private IngestArea(ExecutorService executorService, DataverseService dataverseService, UtilityServices utilityServices, DatasetVersionTaskFactory datasetVersionTaskFactory, Path inbox, Path outbox) {
try {
this.executorService = executorService;
this.dataverseService = dataverseService;
this.utilityServices = utilityServices;
this.datasetTaskFactory = datasetTaskFactory;
this.datasetVersionTaskFactory = datasetVersionTaskFactory;
this.inbox = inbox.toAbsolutePath().toRealPath();
this.outbox = outbox.toAbsolutePath().toRealPath();
}
Expand Down Expand Up @@ -100,7 +102,7 @@ private ImportJob createImportJob(ImportCommandDto importCommand) {
.outputDir(outbox.resolve(relativePath))
.dataverseService(dataverseService)
.utilityServices(utilityServices)
.datasetTaskFactory(datasetTaskFactory)
.datasetVersionTaskFactory(datasetVersionTaskFactory)
.completionHandler(job -> importJobs.remove(job.getImportCommand().getPath()))
.build();
}
Expand Down
52 changes: 0 additions & 52 deletions src/main/java/nl/knaw/dans/dvingest/core/UpdateDatasetTask.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (C) 2024 DANS - Data Archiving and Networked Services ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.knaw.dans.dvingest.core.datasetversiontask;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import nl.knaw.dans.dvingest.core.service.DataverseService;
import nl.knaw.dans.dvingest.core.Deposit;
import nl.knaw.dans.dvingest.core.service.UtilityServices;

import java.io.IOException;
import java.nio.file.Path;

/**
* Task that results in a new dataset (version) in Dataverse.
*/
@Slf4j
public abstract class AbstractDatasetVersionTask implements Runnable {
public enum Status {
TODO,
SUCCESS,
REJECTED,
FAILED
}

protected final Deposit deposit;
protected final DataverseService dataverseService;
protected final UtilityServices utilityServices;
protected final Path outputDir;

@Getter
protected Status status = Status.TODO;

public AbstractDatasetVersionTask(Deposit deposit, DataverseService dataverseService, UtilityServices utilityServices, Path outputDir) {
this.deposit = deposit;
this.dataverseService = dataverseService;
this.utilityServices = utilityServices;
this.outputDir = outputDir;
}

@Override
public void run() {
try {
var pid = performAndReturnPid();
status = Status.SUCCESS;
dataverseService.publishDataset(pid);
dataverseService.waitForState(pid, "RELEASED");
deposit.moveTo(outputDir.resolve("processed"));
status = Status.SUCCESS;
}
catch (Exception e) {
try {
log.error("Failed to ingest deposit", e);
deposit.moveTo(outputDir.resolve("failed"));
status = Status.FAILED;
}
catch (IOException ioException) {
log.error("Failed to move deposit to failed directory", ioException);
}
}
}

public abstract String performAndReturnPid() throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2024 DANS - Data Archiving and Networked Services ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.knaw.dans.dvingest.core.datasetversiontask;

import lombok.extern.slf4j.Slf4j;
import nl.knaw.dans.dvingest.core.service.DataverseService;
import nl.knaw.dans.dvingest.core.Deposit;
import nl.knaw.dans.dvingest.core.service.PathIterator;
import nl.knaw.dans.dvingest.core.service.UtilityServices;
import nl.knaw.dans.lib.dataverse.model.file.FileMeta;
import org.apache.commons.io.FileUtils;

import java.nio.file.Files;
import java.nio.file.Path;

/**
* Creates the first version of a new dataset in Dataverse.
*/
@Slf4j
public class CreateNewDataset extends AbstractDatasetVersionTask {
public CreateNewDataset(Deposit deposit, DataverseService dataverseService, UtilityServices utilityServices, Path outputDir) {
super(deposit, dataverseService, utilityServices, outputDir);
}

@Override
public String performAndReturnPid() throws Exception {
var result = dataverseService.createDataset(deposit.getDatasetMetadata());
var pid = result.getData().getPersistentId();
log.debug(result.getEnvelopeAsString());
var iterator = new PathIterator(FileUtils.iterateFiles(deposit.getFilesDir().toFile(), null, true));
while (iterator.hasNext()) {
var tempZipFile = utilityServices.createTempZipFile();
try {
var zipFile = utilityServices.createPathIteratorZipperBuilder()
.rootDir(deposit.getFilesDir())
.sourceIterator(iterator)
.targetZipFile(tempZipFile)
.build()
.zip();
dataverseService.addFile(pid, zipFile, new FileMeta());
log.debug("Uploaded {} files (cumulative)", iterator.getIteratedCount());
}
finally {
Files.deleteIfExists(tempZipFile);
}

}
return pid;
}
}
Loading

0 comments on commit 64b2aa1

Please sign in to comment.