Skip to content

Commit

Permalink
init project from filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
krwong committed Jul 15, 2024
1 parent cd9de36 commit 37c13ba
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import edu.unc.lib.boxc.migration.cdm.exceptions.InvalidProjectStateException;
import edu.unc.lib.boxc.migration.cdm.exceptions.MigrationException;
import edu.unc.lib.boxc.migration.cdm.model.CdmEnvironment;
import edu.unc.lib.boxc.migration.cdm.model.CdmFieldInfo;
import edu.unc.lib.boxc.migration.cdm.model.MigrationProject;
import edu.unc.lib.boxc.migration.cdm.services.CdmFieldService;
Expand Down Expand Up @@ -53,6 +54,9 @@ public class InitializeProjectCommand implements Callable<Integer> {
"If not specified, then the project will be initialized in the current directory.",
"If the project name is different from the CDM collection ID, then use -c to specify the ID." })
private String projectName;
@Option(names = {"-s", "--source"},
description = {"Initialize a new project that doesn't need to be linked with a CDM collection."})
private String newProject;

private CdmFieldService fieldService;
private CloseableHttpClient httpClient;
Expand Down Expand Up @@ -87,48 +91,60 @@ public Integer call() throws Exception {
log.error("Unable to read application configuration", e);
return 1;
}
var cdmEnvConfig = config.getCdmEnvironments().get(cdmEnvId);

Path currentPath = parentCommand.getWorkingDirectory();
String projDisplayName = projectName == null ? currentPath.getFileName().toString() : projectName;
String collId = cdmCollectionId == null ? projDisplayName : cdmCollectionId;
String username = System.getProperty("user.name");

// Retrieve field information from CDM
CdmFieldInfo fieldInfo;
try {
fieldService.setCdmBaseUri(cdmEnvConfig.getHttpBaseUrl());
fieldInfo = fieldService.retrieveFieldsForCollection(collId);
} catch (IOException | MigrationException e) {
log.error("Failed to retrieve field information for collection in project", e);
outputLogger.info("Failed to retrieve field information for collection {} in project {}:\n{}",
collId, projDisplayName, e.getMessage());
return 1;
}
if (newProject != null && !newProject.isBlank()) {
// Instantiate the project
try {
MigrationProjectFactory.createMigrationProject(
currentPath, projectName, null, username, null, bxcEnvId);

String username = System.getProperty("user.name");
} catch (InvalidProjectStateException e) {
outputLogger.info("Failed to initialize project {}: {}", projDisplayName, e.getMessage());
return 1;
}
} else {
var cdmEnvConfig = config.getCdmEnvironments().get(cdmEnvId);

// Retrieve field information from CDM
CdmFieldInfo fieldInfo;
try {
fieldService.setCdmBaseUri(cdmEnvConfig.getHttpBaseUrl());
fieldInfo = fieldService.retrieveFieldsForCollection(collId);
} catch (IOException | MigrationException e) {
log.error("Failed to retrieve field information for collection in project", e);
outputLogger.info("Failed to retrieve field information for collection {} in project {}:\n{}",
collId, projDisplayName, e.getMessage());
return 1;
}

// Instantiate the project
MigrationProject project = null;
try {
project = MigrationProjectFactory.createMigrationProject(
currentPath, projectName, cdmCollectionId, username, cdmEnvId, bxcEnvId);
// Instantiate the project
MigrationProject project = null;
try {
project = MigrationProjectFactory.createMigrationProject(
currentPath, projectName, cdmCollectionId, username, cdmEnvId, bxcEnvId);

// Persist field info to the project
fieldService.persistFieldsToProject(project, fieldInfo);
} catch (InvalidProjectStateException e) {
outputLogger.info("Failed to initialize project {}: {}", projDisplayName, e.getMessage());
return 1;
}
// Persist field info to the project
fieldService.persistFieldsToProject(project, fieldInfo);
} catch (InvalidProjectStateException e) {
outputLogger.info("Failed to initialize project {}: {}", projDisplayName, e.getMessage());
return 1;
}

//Record collection's finding aid (if available)
try {
findingAidService.setProject(project);
findingAidService.setCdmFieldService(fieldService);
findingAidService.recordFindingAid();
} catch (Exception e) {
log.error("Failed to record finding aid for collection", e);
outputLogger.info("Failed to record finding aid for collection", e);
return 1;
//Record collection's finding aid (if available)
try {
findingAidService.setProject(project);
findingAidService.setCdmFieldService(fieldService);
findingAidService.recordFindingAid();
} catch (Exception e) {
log.error("Failed to record finding aid for collection", e);
outputLogger.info("Failed to record finding aid for collection", e);
return 1;
}
}

outputLogger.info("Initialized project {} in {}s", projDisplayName, (System.nanoTime() - start) / 1e9);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ public static MigrationProject createMigrationProject(Path path, String name,
properties.setCreator(user);
properties.setCreatedDate(Instant.now());
properties.setName(projectName);
properties.setCdmCollectionId(collectionId == null ? projectName : collectionId);
if (cdmEnvId == null) {
properties.setCdmCollectionId(null);
} else {
properties.setCdmCollectionId(collectionId == null ? projectName : collectionId);
}
properties.setCdmEnvironmentId(cdmEnvId);
properties.setBxcEnvironmentId(bxcEnvId);
project.setProjectProperties(properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void initValidProjectUsingCurrentDirTest() throws Exception {
}

@Test
public void initCdmCollectioNotFoundTest() throws Exception {
public void initCdmCollectionNotFoundTest() throws Exception {
String[] initArgs = new String[] {
"-w", baseDir.toString(),
"--env-config", chompbConfigPath,
Expand Down Expand Up @@ -186,6 +186,23 @@ public void initNoEnvMappingPathTest() throws Exception {
assertProjectDirectoryNotCreate();
}

@Test
public void initNewProjectTest() throws Exception {
String[] initArgs = new String[] {
"-w", baseDir.toString(),
"--env-config", chompbConfigPath,
"init",
"-p", "test_file_project",
"-s", "files" };
executeExpectSuccess(initArgs);

MigrationProject project = MigrationProjectFactory.loadMigrationProject(baseDir.resolve("test_file_project"));
MigrationProjectProperties properties = project.getProjectProperties();
assertNewProjectPropertiesSet(properties, "test_file_project", null);

assertTrue(Files.exists(project.getDescriptionsPath()), "Description folder not created");
}

private void assertProjectDirectoryNotCreate() throws IOException {
try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(baseDir)) {
for (Path path : dirStream) {
Expand All @@ -206,6 +223,17 @@ private void assertPropertiesSet(MigrationProjectProperties properties, String e
assertEquals(BxcEnvironmentHelper.DEFAULT_ENV_ID, properties.getBxcEnvironmentId());
}

private void assertNewProjectPropertiesSet(MigrationProjectProperties properties, String expName, String expCollId) {
assertEquals(USERNAME, properties.getCreator());
assertEquals(expName, properties.getName(), "Project name did not match expected value");
assertEquals(expCollId, properties.getCdmCollectionId(), "CDM Collection ID did not match expected value");
assertNotNull(properties.getCreatedDate(), "Created date not set");
assertNull(properties.getHookId());
assertNull(properties.getCollectionNumber());
assertNull(properties.getCdmEnvironmentId());
assertEquals(BxcEnvironmentHelper.DEFAULT_ENV_ID, properties.getBxcEnvironmentId());
}

private void assertCdmFieldsPresent(MigrationProject project) throws IOException {
CdmFieldInfo fieldInfo = fieldService.loadFieldsFromProject(project);
List<CdmFieldEntry> fields = fieldInfo.getFields();
Expand Down

0 comments on commit 37c13ba

Please sign in to comment.