Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BXC-4648 - Don't create empty SIPs #101

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ public int generate(@Mixin SipGenerationOptions options) throws Exception {

List<MigrationSip> sips = sipService.generateSips(options);
for (MigrationSip sip : sips) {
outputLogger.info("Generated SIP for deposit with ID {}", sip.getDepositPid().getId());
if (sip.getWorksCount() == 0) {
outputLogger.info("Skipped SIP for destination {}, it contained no works.", sip.getDestinationId());
continue;
}
outputLogger.info("Generated SIP for deposit with ID {} (containing {} works)",
sip.getDepositPid().getId(), sip.getWorksCount());
outputLogger.info(" * SIP path: {}", sip.getSipPath());
if (sip.getNewCollectionPid() != null) {
outputLogger.info(" * Added new collection {} with box-c id {}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
import static edu.unc.lib.boxc.auth.api.AccessPrincipalConstants.PUBLIC_PRINC;
import static org.slf4j.LoggerFactory.getLogger;

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

import edu.unc.lib.boxc.migration.cdm.exceptions.MigrationException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jena.rdf.model.Bag;
import org.apache.jena.rdf.model.Model;
Expand Down Expand Up @@ -36,6 +40,7 @@ public class DestinationSipEntry {
DepositModelManager depositModelManager;
DepositDirectoryManager depositDirManager;
private Model writeModel;
private Path tdbPath;

public DestinationSipEntry(PID depositPid, DestinationMapping mapping, Path sipPath, PIDMinter pidMinter) {
this.depositPid = depositPid;
Expand Down Expand Up @@ -66,7 +71,15 @@ public void initializeDepositModel() {
}

public Path getTdbPath() {
return depositDirManager.getDepositDir().resolve(SipService.SIP_TDB_PATH);
if (tdbPath == null) {
try {
tdbPath = Files.createTempDirectory("sip-tdb");
tdbPath.toFile().deleteOnExit();
} catch (IOException e) {
throw new MigrationException(e);
}
}
return tdbPath;
}

public Model getWriteModel() {
Expand All @@ -83,6 +96,11 @@ public void commitModel() {

public void close() {
depositModelManager.close();
try {
FileUtils.deleteDirectory(getTdbPath().toFile());
} catch (IOException e) {
log.warn("Failed to cleanup Jena model directory", e.getMessage());
}
}

public Bag getDestinationBag() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class MigrationSip {
private PID newCollectionPid;
private Path sipPath;
private PID destinationPid;
private int worksCount;

public MigrationSip() {
}
Expand Down Expand Up @@ -122,4 +123,15 @@ public String getDestinationId() {
public void setDestinationId(String destinationId) {
this.destinationPid = PIDs.get(destinationId);
}

/**
* @return The number of works contained within this SIP
*/
public int getWorksCount() {
return worksCount;
}

public void setWorksCount(int worksCount) {
this.worksCount = worksCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import edu.unc.lib.boxc.migration.cdm.validators.DestinationsValidator;
import edu.unc.lib.boxc.model.api.ids.PID;
import edu.unc.lib.boxc.model.api.ids.PIDMinter;
import edu.unc.lib.boxc.model.api.rdf.Cdr;
import edu.unc.lib.boxc.operations.api.events.PremisLoggerFactory;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.vocabulary.RDF;
import org.slf4j.Logger;

import java.io.IOException;
Expand Down Expand Up @@ -53,7 +55,6 @@
*/
public class SipService {
private static final Logger log = getLogger(SipService.class);
public static final String SIP_TDB_PATH = "tdb_model";
public static final String MODEL_EXPORT_NAME = "model.n3";
public static final String SIP_INFO_NAME = "sip_info.json";
private static final ObjectReader SIP_INFO_READER = new ObjectMapper().readerFor(MigrationSip.class);
Expand Down Expand Up @@ -171,21 +172,19 @@ public List<MigrationSip> generateSips(SipGenerationOptions options) {
// Finalize all the SIPs by closing and exporting their models
List<MigrationSip> sips = new ArrayList<>();
for (DestinationSipEntry entry : destEntries) {
entry.commitModel();
exportDepositModel(entry);
var worksInSipCount = countWorksInSip(entry);
MigrationSip sip = new MigrationSip(entry);
sip.setWorksCount(worksInSipCount);
sips.add(sip);
// Cleanup the SIP directory if no works were added
if (worksInSipCount == 0) {
cleanupSip(sip);
} else {
persistSip(entry, sip);
}
// update progress bar
destinationCount++;
DisplayProgressUtil.displayProgress(destinationCount, destinationTotal);
// Serialize the SIP info out to file
SIP_INFO_WRITER.writeValue(sip.getSipPath().resolve(SIP_INFO_NAME).toFile(), sip);
// Cleanup the TDB directory not that it has been exported
try {
FileUtils.deleteDirectory(entry.getTdbPath().toFile());
} catch (IOException e) {
log.warn("Failed to cleanup TDB directory", e);
}
}
DisplayProgressUtil.finishProgress();
if (!options.isSuppressCollectionRedirect()) {
Expand All @@ -205,6 +204,31 @@ public List<MigrationSip> generateSips(SipGenerationOptions options) {
}
}

private void cleanupSip(MigrationSip sip) {
try {
FileUtils.deleteDirectory(sip.getSipPath().toFile());
} catch (IOException e) {
log.warn("Failed to cleanup SIP directory", e);
}
}

private void persistSip(DestinationSipEntry entry, MigrationSip sip) throws IOException {
entry.commitModel();
exportDepositModel(entry);
// Serialize the SIP info out to file
SIP_INFO_WRITER.writeValue(sip.getSipPath().resolve(SIP_INFO_NAME).toFile(), sip);
}

private int countWorksInSip(DestinationSipEntry entry) {
var worksCount = 0;
var it = entry.getWriteModel().listResourcesWithProperty(RDF.type, Cdr.Work);
while (it.hasNext()) {
it.next();
worksCount++;
}
return worksCount;
}

private void exportDepositModel(DestinationSipEntry entry) throws IOException {
Model model = entry.getDepositModelManager().getReadModel(entry.getDepositPid());
Path modelExportPath = entry.getDepositDirManager().getDepositDir().resolve(MODEL_EXPORT_NAME);
Expand Down
55 changes: 55 additions & 0 deletions src/test/java/edu/unc/lib/boxc/migration/cdm/SipsCommandIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.stream.Stream;

import static edu.unc.lib.boxc.migration.cdm.services.sips.WorkGenerator.STREAMING_TYPE;
import static edu.unc.lib.boxc.migration.cdm.services.sips.WorkGenerator.STREAMING_URL;
Expand Down Expand Up @@ -276,6 +278,59 @@ public void generateStreamingFilesOnlyTest() throws Exception {
assertTrue(workResc3FileObj.hasProperty(STREAMING_TYPE, "sound"));
}

@Test
public void generateOneDestinationHasNoWorksTest() throws Exception {
testHelper.indexExportData("mini_gilmer");
// Default destination will have no works mapped to it
testHelper.generateDefaultDestinationsMapping(DEST_UUID, null);
var archivalDestUuid = "96ecf2e0-5a7d-465f-ad1c-b1fcec1f58c5";
var destsPath = project.getDestinationMappingsPath();
Files.write(destsPath, ("dcmi:Image," + archivalDestUuid + ",").getBytes(), StandardOpenOption.APPEND);

testHelper.populateDescriptions("gilmer_mods1.xml");
List<Path> stagingLocs = testHelper.populateSourceFiles("276_182_E.tif", "276_183_E.tif", "276_203_E.tif");

String[] args = new String[] {
"-w", project.getProjectPath().toString(),
"sips", "generate" };
executeExpectSuccess(args);

assertOutputContains("Skipped SIP for destination " + DEST_UUID + ", it contained no works");
try (Stream<Path> stream = Files.list(project.getSipsPath())) {
assertEquals(1, stream.count(), "There can only be one sip directory");
}

// The other sip should have 3 works listed in the output
assertOutputContains("containing 3 works");
// And should be populated normally
MigrationSip sip = extractSipFromOutput();

DepositDirectoryManager dirManager = testHelper.createDepositDirectoryManager(sip);
Model model = testHelper.getSipModel(sip);

Bag depBag = model.getBag(sip.getDepositPid().getRepositoryPath());
List<RDFNode> depBagChildren = depBag.iterator().toList();
assertEquals(3, depBagChildren.size());

Resource workResc1 = testHelper.getResourceByCreateTime(depBagChildren, "2005-11-23");
testHelper.assertObjectPopulatedInSip(workResc1, dirManager, model, stagingLocs.get(0), null, "25");
Resource workResc2 = testHelper.getResourceByCreateTime(depBagChildren, "2005-11-24");
testHelper.assertObjectPopulatedInSip(workResc2, dirManager, model, stagingLocs.get(1), null, "26");
Resource workResc3 = testHelper.getResourceByCreateTime(depBagChildren, "2005-12-08");
testHelper.assertObjectPopulatedInSip(workResc3, dirManager, model, stagingLocs.get(2), null, "27");

String[] argsList = new String[] {
"-w", project.getProjectPath().toString(),
"sips", "list" };
executeExpectSuccess(argsList);

assertOutputContains("SIP/Deposit ID: " + sip.getDepositId());
assertOutputContains(" Path: " + sip.getSipPath());
// Should only be one sip listed
assertEquals(1, getOutput().lines().filter(l -> l.contains("SIP/Deposit ID")).count());

}

private void assertChildFileModsPopulated(DepositDirectoryManager dirManager, Resource workResc,
String expectedCdmId) throws Exception {
var workChildPid = retrieveOnlyWorkChildPid(workResc);
Expand Down
Loading