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-4635 init project from filesystem #98

Merged
merged 6 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -53,6 +53,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;
krwong marked this conversation as resolved.
Show resolved Hide resolved

private CdmFieldService fieldService;
private CloseableHttpClient httpClient;
Expand Down Expand Up @@ -87,48 +90,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 {
krwong marked this conversation as resolved.
Show resolved Hide resolved
MigrationProjectFactory.createMigrationProject(
krwong marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -19,8 +19,6 @@
import java.util.List;
import java.util.Optional;

import com.fasterxml.jackson.databind.ObjectMapper;
import edu.unc.lib.boxc.migration.cdm.services.ChompbConfigService;
import edu.unc.lib.boxc.migration.cdm.test.BxcEnvironmentHelper;
import edu.unc.lib.boxc.migration.cdm.test.CdmEnvironmentHelper;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -105,7 +103,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 +184,23 @@ public void initNoEnvMappingPathTest() throws Exception {
assertProjectDirectoryNotCreate();
}

@Test
public void initNewProjectFromFilesystemTest() 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);
krwong marked this conversation as resolved.
Show resolved Hide resolved

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 +221,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
Loading