private boolean shouldCreateProjectBasedOnExisting(final CreateProjectValidationResult result) { return result != null && result.getExistingProjectId().isPresent(); }
@Override public Project createProject(CreateProjectValidationResult result) { Consumer<Project> projectConsumer; if (shouldCreateProjectBasedOnExisting(result)) { projectConsumer = (newProject) -> { final Optional<Project> existing = ofNullable(projectManager.getProjectObj(result.getExistingProjectId().get())); final Project existingProject = existing.orElseThrow(() -> new IllegalStateException("Could not retrieve existing project.")); final Optional<ProjectCategory> projectCategory = ofNullable(existingProject.getProjectCategory()); projectCategory.ifPresent(category -> projectManager.setProjectCategory(newProject, category)); projectSchemeAssociationManager.associateSchemesOfExistingProjectWithNewProject(newProject, existingProject); }; } else { projectConsumer = projectSchemeAssociationManager::associateDefaultSchemesWithProject; } return createProjectInternal(result, projectConsumer); }
private void notifyHandlersOfProjectCreated(ApplicationUser user, Project newProject, CreateProjectValidationResult result) { final ProjectCreatedData.Builder projectCreatedData = new ProjectCreatedData.Builder().withProject(newProject); Optional.ofNullable(result.getProjectCreationData().getProjectTemplateKey()).ifPresent(projectCreatedData::withProjectTemplateKey); result.getExistingProjectId().ifPresent(projectCreatedData::withExistingProjectId); boolean notificationWasSuccessful = projectCreateNotifier.notifyAllHandlers(projectCreatedData.build()); if (!notificationWasSuccessful) { deleteProject(user, new DeleteProjectValidationResult(ErrorCollections.empty(), newProject)); throw new RuntimeException("An error occurred while notifying that a project was created"); } }
private Project createProjectInternal(final CreateProjectValidationResult result, final Consumer<Project> projectConsumer) { if (result == null) { throw new IllegalArgumentException("You can not create a project with a null validation result."); } if (!result.isValid()) { throw new IllegalStateException("You can not create a project with an invalid validation result."); } if (result.getExistingProjectId().isPresent()) { ofNullable(projectManager.getProjectObj(result.getExistingProjectId().get())).orElseThrow(() -> new IllegalStateException("Existing project could not be found.")); } final ApplicationUser user = result.getUser(); final ProjectCreationData projectCreationData = result.getProjectCreationData(); final Project newProject = projectManager.createProject(user, new ProjectCreationData.Builder() .from(projectCreationData) .withKey(projectCreationData.getKey().toUpperCase()) .withLead(projectCreationData.getLead()) .withAssigneeType(effectiveAssigneeType(projectCreationData.getAssigneeType())) .build()); projectConsumer.accept(newProject); // Refresh the workflow cache workflowSchemeManager.clearWorkflowCache(); notifyHandlersOfProjectCreated(user, newProject, result); projectEventManager.dispatchProjectCreated(user, newProject); return newProject; }