Skip to content

Commit

Permalink
Removed more unnecessary classes and interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
janvanmansum committed Dec 7, 2024
1 parent dedae6f commit 363298f
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,11 @@
import lombok.extern.slf4j.Slf4j;
import nl.knaw.dans.dvingest.core.dansbag.deposit.BagDirResolver;
import nl.knaw.dans.dvingest.core.dansbag.deposit.BagDirResolverImpl;
import nl.knaw.dans.dvingest.core.dansbag.deposit.DepositFileLister;
import nl.knaw.dans.dvingest.core.dansbag.deposit.DepositFileListerImpl;
import nl.knaw.dans.dvingest.core.dansbag.deposit.DansBagDeposit;
import nl.knaw.dans.dvingest.core.dansbag.deposit.DepositReader;
import nl.knaw.dans.dvingest.core.dansbag.deposit.DepositReaderImpl;
import nl.knaw.dans.dvingest.core.dansbag.deposit.DansBagDeposit;
import nl.knaw.dans.dvingest.core.dansbag.exception.InvalidDepositException;
import nl.knaw.dans.dvingest.core.dansbag.mapper.DepositToDvDatasetMetadataMapper;
import nl.knaw.dans.dvingest.core.dansbag.service.BagDataManager;
import nl.knaw.dans.dvingest.core.dansbag.service.BagDataManagerImpl;
import nl.knaw.dans.dvingest.core.dansbag.service.ManifestHelper;
import nl.knaw.dans.dvingest.core.dansbag.service.ManifestHelperImpl;
import nl.knaw.dans.dvingest.core.dansbag.service.XmlReader;
Expand Down Expand Up @@ -71,12 +67,10 @@ public DansBagMappingServiceImpl(DepositToDvDatasetMetadataMapper depositToDvDat
this.dataverseService = dataverseService;
BagReader bagReader = new BagReader();
ManifestHelper manifestHelper = new ManifestHelperImpl();
DepositFileLister depositFileLister = new DepositFileListerImpl();
BagDataManager bagDataManager = new BagDataManagerImpl(bagReader);
XmlReader xmlReader = new XmlReaderImpl();
BagDirResolver bagDirResolver = new BagDirResolverImpl();

depositReader = new DepositReaderImpl(xmlReader, bagDirResolver, bagDataManager, depositFileLister, manifestHelper);
depositReader = new DepositReaderImpl(xmlReader, bagDirResolver, bagReader, manifestHelper);
this.supportedLicenses = supportedLicenses;
this.fileExclusionPattern = fileExclusionPattern;
this.embargoExclusions = embargoExclusions;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@
package nl.knaw.dans.dvingest.core.dansbag.deposit;

import gov.loc.repository.bagit.domain.Bag;
import gov.loc.repository.bagit.reader.BagReader;
import nl.knaw.dans.dvingest.core.dansbag.exception.InvalidDepositException;
import nl.knaw.dans.dvingest.core.dansbag.service.BagDataManager;
import nl.knaw.dans.dvingest.core.dansbag.service.ManifestHelper;
import nl.knaw.dans.dvingest.core.dansbag.service.ManifestHelperImpl;
import nl.knaw.dans.dvingest.core.dansbag.service.XPathEvaluator;
import nl.knaw.dans.dvingest.core.dansbag.service.XmlReader;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.FileBasedConfiguration;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
Expand All @@ -32,20 +40,23 @@
import java.time.OffsetDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static nl.knaw.dans.dvingest.core.dansbag.service.XPathConstants.FILES_FILE;

public class DepositReaderImpl implements DepositReader {
private static final String DEPOSIT_PROPERTIES_FILENAME = "deposit.properties";

private final XmlReader xmlReader;
private final BagDirResolver bagDirResolver;
private final BagDataManager bagDataManager;
private final DepositFileLister depositFileLister;
private final BagReader bagReader;

private final ManifestHelper manifestHelper;

public DepositReaderImpl(XmlReader xmlReader, BagDirResolver bagDirResolver, BagDataManager bagDataManager, DepositFileLister depositFileLister, ManifestHelper manifestHelper) {
public DepositReaderImpl(XmlReader xmlReader, BagDirResolver bagDirResolver, BagReader bagReader, ManifestHelper manifestHelper) {
this.xmlReader = xmlReader;
this.bagDirResolver = bagDirResolver;
this.bagDataManager = bagDataManager;
this.depositFileLister = depositFileLister;
this.bagReader = bagReader;
this.manifestHelper = manifestHelper;
}

Expand All @@ -54,18 +65,18 @@ public DansBagDeposit readDeposit(Path depositDir) throws InvalidDepositExceptio
try {
var bagDir = bagDirResolver.getBagDir(depositDir);

var config = bagDataManager.readDepositProperties(depositDir);
var bag = bagDataManager.readBag(bagDir);
var config = readDepositProperties(depositDir);
var bag = bagReader.read(bagDir);
manifestHelper.ensureSha1ManifestPresent(bag);

var deposit = mapToDeposit(depositDir, bagDir, config, bag);

deposit.setBag(bag);
deposit.setDdm(readOptionalXmlFile(deposit.getDdmPath()));
deposit.setFilesXml(readOptionalXmlFile(deposit.getFilesXmlPath()));
deposit.setDdm(readRequiredXmlFile(deposit.getDdmPath()));
deposit.setFilesXml(readRequiredXmlFile(deposit.getFilesXmlPath()));
deposit.setAmd(readOptionalXmlFile(deposit.getAmdPath()));

deposit.setFiles(depositFileLister.getDepositFiles(deposit));
deposit.setFiles(getDepositFiles(deposit));

return deposit;
}
Expand All @@ -74,15 +85,54 @@ public DansBagDeposit readDeposit(Path depositDir) throws InvalidDepositExceptio
}
}

Document readOptionalXmlFile(Path path) throws ParserConfigurationException, IOException, SAXException {

private Configuration readDepositProperties(Path depositDir) throws ConfigurationException {
var propertiesFile = depositDir.resolve(DEPOSIT_PROPERTIES_FILENAME);
var params = new Parameters();
var paramConfig = params.properties()
.setFileName(propertiesFile.toString());

var builder = new FileBasedConfigurationBuilder<FileBasedConfiguration>
(PropertiesConfiguration.class, null, true)
.configure(paramConfig);

return builder.getConfiguration();
}

private List<DepositFile> getDepositFiles(DansBagDeposit dansBagDeposit) throws IOException {
var bag = dansBagDeposit.getBag();
var filePathToSha1 = ManifestHelperImpl.getFilePathToSha1(bag);

return XPathEvaluator.nodes(dansBagDeposit.getFilesXml(), FILES_FILE)
.map(node -> {
var filePath = Optional.ofNullable(node.getAttributes().getNamedItem("filepath"))
.map(Node::getTextContent)
.map(Path::of)
.orElseThrow(() -> new IllegalArgumentException("File element without filepath attribute"));

var sha1 = filePathToSha1.get(filePath);

return new DepositFile(filePath, sha1, node);
})
.collect(Collectors.toList());
}

private Document readRequiredXmlFile(Path path) throws ParserConfigurationException, IOException, SAXException {
if (!Files.exists(path)) {
throw new IllegalArgumentException("Required file not found: " + path);

Check warning on line 122 in src/main/java/nl/knaw/dans/dvingest/core/dansbag/deposit/DepositReaderImpl.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvingest/core/dansbag/deposit/DepositReaderImpl.java#L122

Added line #L122 was not covered by tests
}
return xmlReader.readXmlFile(path);
}

private Document readOptionalXmlFile(Path path) throws ParserConfigurationException, IOException, SAXException {
if (Files.exists(path)) {
return xmlReader.readXmlFile(path);
}

return null;
}

DansBagDeposit mapToDeposit(Path path, Path bagDir, Configuration config, Bag bag) {
private DansBagDeposit mapToDeposit(Path path, Path bagDir, Configuration config, Bag bag) {
var deposit = new DansBagDeposit();
deposit.setBagDir(bagDir);
deposit.setDir(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,6 @@
*/
package nl.knaw.dans.dvingest.core.dansbag.service;

import gov.loc.repository.bagit.domain.Bag;
import gov.loc.repository.bagit.exceptions.InvalidBagitFileFormatException;
import gov.loc.repository.bagit.exceptions.MaliciousPathException;
import gov.loc.repository.bagit.exceptions.UnparsableVersionException;
import gov.loc.repository.bagit.exceptions.UnsupportedAlgorithmException;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.ex.ConfigurationException;

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

public interface BagDataManager {

/**
* Reads the bag on the path that contains bag-info.txt and bagit.txt, among other files
*
* @param bagDir the path that contains bag-info.txt and bagit.txt
* @return The bag object
* @throws UnparsableVersionException
* @throws InvalidBagitFileFormatException
* @throws IOException
* @throws MaliciousPathException
* @throws UnsupportedAlgorithmException
*/
Bag readBag(Path bagDir) throws UnparsableVersionException, InvalidBagitFileFormatException, IOException, MaliciousPathException, UnsupportedAlgorithmException;

/**
* Reads the deposit.properties file found inside the folder provided. The path should NOT reference deposit.properties directly
*
* @param depositDir The directory that contains a deposit.properties file
* @return
* @throws ConfigurationException
*/
Configuration readDepositProperties(Path depositDir) throws ConfigurationException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,7 @@
*/
package nl.knaw.dans.dvingest.core.dansbag.service;

import gov.loc.repository.bagit.domain.Bag;
import gov.loc.repository.bagit.exceptions.InvalidBagitFileFormatException;
import gov.loc.repository.bagit.exceptions.MaliciousPathException;
import gov.loc.repository.bagit.exceptions.UnparsableVersionException;
import gov.loc.repository.bagit.exceptions.UnsupportedAlgorithmException;
import gov.loc.repository.bagit.reader.BagReader;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.FileBasedConfiguration;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.ex.ConfigurationException;

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

public class BagDataManagerImpl implements BagDataManager {
private static final String DEPOSIT_PROPERTIES_FILENAME = "deposit.properties";
Expand All @@ -39,23 +25,4 @@ public BagDataManagerImpl(BagReader bagReader) {
this.bagReader = bagReader;

Check warning on line 25 in src/main/java/nl/knaw/dans/dvingest/core/dansbag/service/BagDataManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvingest/core/dansbag/service/BagDataManagerImpl.java#L24-L25

Added lines #L24 - L25 were not covered by tests
}

@Override
public Bag readBag(Path bagDir) throws UnparsableVersionException, InvalidBagitFileFormatException, IOException, MaliciousPathException, UnsupportedAlgorithmException {
return bagReader.read(bagDir);
}

@Override
public Configuration readDepositProperties(Path depositDir) throws ConfigurationException {
var propertiesFile = depositDir.resolve(DEPOSIT_PROPERTIES_FILENAME);
var params = new Parameters();
var paramConfig = params.properties()
.setFileName(propertiesFile.toString());

var builder = new FileBasedConfigurationBuilder<FileBasedConfiguration>
(PropertiesConfiguration.class, null, true)
.configure(paramConfig);

return builder.getConfiguration();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,16 @@

import gov.loc.repository.bagit.reader.BagReader;
import nl.knaw.dans.dvingest.core.TestDirFixture;
import nl.knaw.dans.dvingest.core.service.DataverseService;
import nl.knaw.dans.dvingest.core.dansbag.deposit.BagDirResolver;
import nl.knaw.dans.dvingest.core.dansbag.deposit.BagDirResolverImpl;
import nl.knaw.dans.dvingest.core.dansbag.deposit.DepositFileLister;
import nl.knaw.dans.dvingest.core.dansbag.deposit.DepositFileListerImpl;
import nl.knaw.dans.dvingest.core.dansbag.deposit.DepositReader;
import nl.knaw.dans.dvingest.core.dansbag.deposit.DepositReaderImpl;
import nl.knaw.dans.dvingest.core.dansbag.service.BagDataManager;
import nl.knaw.dans.dvingest.core.dansbag.service.BagDataManagerImpl;
import nl.knaw.dans.dvingest.core.dansbag.mapper.DepositToDvDatasetMetadataMapper;
import nl.knaw.dans.dvingest.core.dansbag.service.ManifestHelper;
import nl.knaw.dans.dvingest.core.dansbag.service.ManifestHelperImpl;
import nl.knaw.dans.dvingest.core.dansbag.service.XmlReader;
import nl.knaw.dans.dvingest.core.dansbag.service.XmlReaderImpl;
import nl.knaw.dans.dvingest.core.dansbag.mapper.DepositToDvDatasetMetadataMapper;
import nl.knaw.dans.dvingest.core.service.DataverseService;
import nl.knaw.dans.lib.dataverse.model.dataset.License;
import nl.knaw.dans.lib.util.MappingLoader;
import org.apache.commons.io.FileUtils;
Expand All @@ -57,12 +53,10 @@ public void setUp() throws Exception {
super.setUp();
BagReader bagReader = new BagReader();
ManifestHelper manifestHelper = new ManifestHelperImpl();
DepositFileLister depositFileLister = new DepositFileListerImpl();
BagDataManager bagDataManager = new BagDataManagerImpl(bagReader);
XmlReader xmlReader = new XmlReaderImpl();
BagDirResolver bagDirResolver = new BagDirResolverImpl();

depositReader = new DepositReaderImpl(xmlReader, bagDirResolver, bagDataManager, depositFileLister, manifestHelper);
depositReader = new DepositReaderImpl(xmlReader, bagDirResolver, bagReader, manifestHelper);
var defaultConfigDir = Paths.get("src/main/assembly/dist/cfg");
var mapper = new DepositToDvDatasetMetadataMapper(
false,
Expand Down

0 comments on commit 363298f

Please sign in to comment.