diff --git a/src/main/java/edu/unc/lib/boxc/migration/cdm/InitializeProjectCommand.java b/src/main/java/edu/unc/lib/boxc/migration/cdm/InitializeProjectCommand.java index d80dc865..41d1f779 100644 --- a/src/main/java/edu/unc/lib/boxc/migration/cdm/InitializeProjectCommand.java +++ b/src/main/java/edu/unc/lib/boxc/migration/cdm/InitializeProjectCommand.java @@ -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; @@ -53,6 +54,9 @@ public class InitializeProjectCommand implements Callable { "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; @@ -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); diff --git a/src/main/java/edu/unc/lib/boxc/migration/cdm/services/MigrationProjectFactory.java b/src/main/java/edu/unc/lib/boxc/migration/cdm/services/MigrationProjectFactory.java index b7682bd1..0f65f2b2 100644 --- a/src/main/java/edu/unc/lib/boxc/migration/cdm/services/MigrationProjectFactory.java +++ b/src/main/java/edu/unc/lib/boxc/migration/cdm/services/MigrationProjectFactory.java @@ -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); diff --git a/src/test/java/edu/unc/lib/boxc/migration/cdm/InitializeProjectCommandIT.java b/src/test/java/edu/unc/lib/boxc/migration/cdm/InitializeProjectCommandIT.java index 2b7b239d..ed283a99 100644 --- a/src/test/java/edu/unc/lib/boxc/migration/cdm/InitializeProjectCommandIT.java +++ b/src/test/java/edu/unc/lib/boxc/migration/cdm/InitializeProjectCommandIT.java @@ -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, @@ -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 dirStream = Files.newDirectoryStream(baseDir)) { for (Path path : dirStream) { @@ -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 fields = fieldInfo.getFields();