Skip to content

Commit

Permalink
add archivalCollectionMappings method and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
krwong committed Oct 30, 2023
1 parent 6942ccf commit ed94bc4
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
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 edu.unc.lib.boxc.migration.cdm.validators.DestinationsValidator;
import edu.unc.lib.boxc.search.api.exceptions.SolrRuntimeException;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.slf4j.Logger;

import java.io.BufferedWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand Down Expand Up @@ -98,6 +105,46 @@ public Map<String, String> generateCollectionNumbersToPidMapping(DestinationMapp
return mapCollNumToPid;
}

/**
* Add archival collection mapping(s) to destination mappings file
* @param options
* @throws Exception
*/
public void addArchivalCollectionMappings(DestinationMappingOptions options) throws Exception {
DestinationsValidator.assertValidDestination(options.getDefaultDestination());

Path destinationMappingsPath = project.getDestinationMappingsPath();

try (
BufferedWriter writer = Files.newBufferedWriter(destinationMappingsPath,
StandardOpenOption.APPEND,
StandardOpenOption.CREATE);
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.Builder.create().build());
) {
if (options.getDefaultDestination() != null) {
Map<String, String> mapCollNumToPid = generateCollectionNumbersToPidMapping(options);
for (Map.Entry<String, String> entry : mapCollNumToPid.entrySet()) {
String collNum = entry.getKey();
String pid = entry.getValue();

// destinations.csv columns: id, destination, collection
// if the boxc pid is not null, destination field = the boxc pid and "collection" field is empty
// if the boxc pid is null, destination field is empty and "collection" field = <coll_num>
// the user will need to fill in the "destination" value, which would be a boxc AdminUnit pid
if (pid != null) {
csvPrinter.printRecord(options.getFieldName() + ":" + collNum,
pid,
"");
} else {
csvPrinter.printRecord(options.getFieldName() + ":" + collNum,
"",
collNum);
}
}
}
}
}

public void setProject(MigrationProject project) {
this.project = project;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,43 @@ public void unpopulatedDestWithDefaultTest() throws Exception {
assertOutputMatches(".*New Collections: +0\n.*");
}

@Test
public void archivalCollNumsWithPidTest() throws Exception {
testHelper.indexExportData("mini_gilmer");
writeCsv(mappingBody("aid:40126,bdbd99af-36a5-4bab-9785-e3a802d3737e,"));

statusService.report(Verbosity.VERBOSE);

assertOutputMatches(".*Last Generated: +[0-9\\-T:]+.*");
assertOutputMatches(".*Objects Mapped: +0 \\(0.0%\\).*");
assertOutputMatches(".*Unmapped Objects: +3 \\(100.0%\\).*");
assertOutputMatches(".*Unmapped Objects:.*\n +\\* 25\n.*");
assertOutputMatches(".*Unknown Objects: +1 .*");
assertOutputMatches(".*Destinations Valid: +Yes.*");
assertOutputMatches(".*To Default: +0 \\(0.0%\\).*");
assertOutputMatches(".*Destinations: +1\n.*");
assertOutputMatches(".*Destinations:.*\n +\\* bdbd99af-36a5-4bab-9785-e3a802d3737e.*");
assertOutputMatches(".*New Collections: +0\n.*");
}

@Test
public void archivalCollNumsNullPidTest() throws Exception {
testHelper.indexExportData("mini_gilmer");
writeCsv(mappingBody("aid:40147,,40147"));

statusService.report(Verbosity.VERBOSE);

assertOutputMatches(".*Last Generated: +[0-9\\-T:]+.*");
assertOutputMatches(".*Objects Mapped: +0 \\(0.0%\\).*");
assertOutputMatches(".*Unmapped Objects: +3 \\(100.0%\\).*");
assertOutputMatches(".*Unmapped Objects:.*\n +\\* 25\n.*");
assertOutputMatches(".*Unknown Objects: +0 .*");
assertOutputMatches(".*Destinations Valid: +No.*");
assertOutputMatches(".*To Default: +0 \\(0.0%\\).*");
assertOutputMatches(".*Destinations: +0\n.*");
assertOutputMatches(".*New Collections: +0\n.*");
}

private String mappingBody(String... rows) {
return String.join(",", DestinationsInfo.CSV_HEADERS) + "\n"
+ String.join("\n", rows);
Expand Down

0 comments on commit ed94bc4

Please sign in to comment.