Skip to content

Commit

Permalink
BXC-4303 Generate list of collection numbers (#68)
Browse files Browse the repository at this point in the history
* ArchivalDestinationsService with generateCollectionNumbersList method and tests, add fieldName option to DestinationMappingOptions

* remove unused indexService from test
  • Loading branch information
krwong committed Oct 11, 2023
1 parent 2e6cd9c commit fa0a78e
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public class DestinationMappingOptions {
"Can be any value that uniquely identifies objects that belong in this destination.",})
private String defaultCollection;

@Option(names = {"-n", "--field-name"},
description = {"Name of the field in CDM where archival collection numbers should be found.",
"The default will be used during SIP generation for objects if they do not have explicit mappings.",})
private String fieldName;

@Option(names = { "-f", "--force"},
description = "Overwrite destination mapping if one already exists")
private boolean force;
Expand Down Expand Up @@ -64,4 +69,12 @@ public String getCdmId() {
public void setCdmId(String cdmId) {
this.cdmId = cdmId;
}

public String getFieldName() {
return fieldName;
}

public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package edu.unc.lib.boxc.migration.cdm.services;

import edu.unc.lib.boxc.migration.cdm.exceptions.MigrationException;
import edu.unc.lib.boxc.migration.cdm.model.MigrationProject;
import edu.unc.lib.boxc.migration.cdm.options.DestinationMappingOptions;
import org.slf4j.Logger;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import static org.slf4j.LoggerFactory.getLogger;

/**
* Service for destination matching based on archival collection number
* @author krwong
*/
public class ArchivalDestinationsService {
private static final Logger log = getLogger(ArchivalDestinationsService.class);

private MigrationProject project;
private CdmIndexService indexService;

/**
* Generates a unique list of values in the accepted field name
* @param options destination mapping options
* @return A list
*/
public List<String> generateCollectionNumbersList(DestinationMappingOptions options) {
List<String> collectionNumbers = new ArrayList<>();

Connection conn = null;
try {
conn = indexService.openDbConnection();
Statement stmt = conn.createStatement();
// skip over values from children of compound objects, since they must go to the same destination as their parent work
ResultSet rs = stmt.executeQuery("select distinct " + options.getFieldName()
+ " from " + CdmIndexService.TB_NAME
+ " where " + " ("+ CdmIndexService.ENTRY_TYPE_FIELD + " != '"
+ CdmIndexService.ENTRY_TYPE_COMPOUND_CHILD + "'" +
" OR " + CdmIndexService.ENTRY_TYPE_FIELD + " is null)");
while (rs.next()) {
collectionNumbers.add(rs.getString(1));
}
return collectionNumbers;
} catch (SQLException e) {
throw new MigrationException("Error interacting with export index", e);
} finally {
CdmIndexService.closeDbConnection(conn);
}
}

public void setProject(MigrationProject project) {
this.project = project;
}

public void setIndexService(CdmIndexService indexService) {
this.indexService = indexService;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package edu.unc.lib.boxc.migration.cdm.services;

import edu.unc.lib.boxc.migration.cdm.model.MigrationProject;
import edu.unc.lib.boxc.migration.cdm.options.DestinationMappingOptions;
import edu.unc.lib.boxc.migration.cdm.test.BxcEnvironmentHelper;
import edu.unc.lib.boxc.migration.cdm.test.CdmEnvironmentHelper;
import edu.unc.lib.boxc.migration.cdm.test.SipServiceHelper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.nio.file.Path;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertIterableEquals;

public class ArchivalDestinationsServiceTest {
private static final String PROJECT_NAME = "proj";

@TempDir
public Path tmpFolder;

private SipServiceHelper testHelper;
private MigrationProject project;
private ArchivalDestinationsService service;

@BeforeEach
public void setup() throws Exception {
project = MigrationProjectFactory.createMigrationProject(
tmpFolder, PROJECT_NAME, null, "user",
CdmEnvironmentHelper.DEFAULT_ENV_ID, BxcEnvironmentHelper.DEFAULT_ENV_ID);
testHelper = new SipServiceHelper(project, tmpFolder);
service = new ArchivalDestinationsService();
service.setProject(project);
service.setIndexService(testHelper.getIndexService());
}

@Test
public void archivalCollectionNumberTest() throws Exception {
testHelper.indexExportData("mini_keepsakes");

var options = new DestinationMappingOptions();
options.setFieldName("dmrecord");

var result = service.generateCollectionNumbersList(options);
assertIterableEquals(Arrays.asList("216", "604", "607"), result);
}
}

0 comments on commit fa0a78e

Please sign in to comment.