-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BXC-4303 Generate list of collection numbers (#68)
* ArchivalDestinationsService with generateCollectionNumbersList method and tests, add fieldName option to DestinationMappingOptions * remove unused indexService from test
- Loading branch information
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/edu/unc/lib/boxc/migration/cdm/services/ArchivalDestinationsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/test/java/edu/unc/lib/boxc/migration/cdm/services/ArchivalDestinationsServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |