if ( result.getProjectGroups().size() != 1 )
{
throw new ContinuumException( "The project building result has to contain exactly one project group." );
}
ProjectGroup projectGroup = result.getProjectGroups().iterator().next();
boolean projectGroupCreation = false;
try
{
if ( projectGroupId == -1 )
{
try
{
projectGroup = projectGroupDao.getProjectGroupByGroupId( projectGroup.getGroupId() );
projectGroupId = projectGroup.getId();
log.info( "Using existing project group with the group id: '" + projectGroup.getGroupId() + "'." );
}
catch ( ContinuumObjectNotFoundException e )
{
log.info( "Creating project group with the group id: '" + projectGroup.getGroupId() + "'." );
Map<String, Object> pgContext = new HashMap<String, Object>();
AbstractContinuumAction.setWorkingDirectory( pgContext, getWorkingDirectory() );
AbstractContinuumAction.setUnvalidatedProjectGroup( pgContext, projectGroup );
executeAction( "validate-project-group", pgContext );
executeAction( "store-project-group", pgContext );
projectGroupId = AbstractContinuumAction.getProjectGroupId( pgContext );
projectGroupCreation = true;
}
}
projectGroup = projectGroupDao.getProjectGroupWithBuildDetailsByProjectGroupId( projectGroupId );
String url = CreateProjectsFromMetadataAction.getUrl( context );
List<ProjectScmRoot> scmRoots = getProjectScmRootByProjectGroup( projectGroup.getId() );
boolean found = false;
for ( ProjectScmRoot scmRoot : scmRoots )
{
if ( url.startsWith( scmRoot.getScmRootAddress() ) )
{
found = true;
break;
}
}
if ( !found )
{
createProjectScmRoot( projectGroup, url );
}
/* add the project group loaded from database, which has more info, like id */
result.getProjectGroups().remove( 0 );
result.getProjectGroups().add( projectGroup );
}
catch ( ContinuumStoreException e )
{
throw new ContinuumException( "Error while querying for project group.", e );
}
// ----------------------------------------------------------------------
// Save all the projects if recursive mode asked
// TODO: Validate all the projects before saving them
// ----------------------------------------------------------------------
List<Project> projects = result.getProjects();
String scmUserName = null;
String scmPassword = null;
for ( Project project : projects )
{
checkForDuplicateProjectInGroup( projectGroup, project, result );
if ( result.hasErrors() )
{
log.info( result.getErrors().size() + " errors during project add: " );
log.info( result.getErrorsAsString() );
return result;
}
project.setScmUseCache( useCredentialsCache );
// values backup for first checkout
scmUserName = project.getScmUsername();
scmPassword = project.getScmPassword();
// CONTINUUM-1792 : we don't store it
if ( useCredentialsCache )
{
project.setScmUsername( null );
project.setScmPassword( null );
}
projectGroup.addProject( project );
}
try
{
projectGroupDao.updateProjectGroup( projectGroup );
for ( Project project : projects )
{
context = new HashMap<String, Object>();
// CONTINUUM-1953 olamy : attached buildDefs from template here
// if no group creation
if ( !projectGroupCreation && buildDefinitionTemplateId > 0 )
{
buildDefinitionService.addTemplateInProject( buildDefinitionTemplateId,
projectDao.getProject( project.getId() ) );
}
AbstractContinuumAction.setUnvalidatedProject( context, project );
//
// executeAction( "validate-project", context );
//
// executeAction( "store-project", context );
//
AbstractContinuumAction.setProjectId( context, project.getId() );
if ( !StringUtils.isEmpty( scmUserName ) )
{
project.setScmUsername( scmUserName );
CheckoutProjectContinuumAction.setScmUsername( context, scmUserName );
}
if ( !StringUtils.isEmpty( scmPassword ) )
{
project.setScmPassword( scmPassword );
CheckoutProjectContinuumAction.setScmPassword( context, scmPassword );
}
// FIXME
// olamy : read again the project to have values because store.updateProjectGroup( projectGroup );
// remove object data -> we don't display the project name in the build queue
AbstractContinuumAction.setProject( context, projectDao.getProject( project.getId() ) );
BuildDefinition defaultBuildDefinition = null;
if ( projectBuilderId.equals( MavenTwoContinuumProjectBuilder.ID ) )
{
defaultBuildDefinition =
(BuildDefinition) buildDefinitionService.getDefaultMavenTwoBuildDefinitionTemplate().getBuildDefinitions().get(
0 );
}
else if ( projectBuilderId.equals( MavenOneContinuumProjectBuilder.ID ) )
{
defaultBuildDefinition =
(BuildDefinition) buildDefinitionService.getDefaultMavenOneBuildDefinitionTemplate().getBuildDefinitions().get(
0 );
}
// used by BuildManager to determine on which build queue will the project be put
AbstractContinuumAction.setBuildDefinition( context, defaultBuildDefinition );
if ( !configurationService.isDistributedBuildEnabled() )
{
executeAction( "add-project-to-checkout-queue", context );
}
}
}
catch ( BuildDefinitionServiceException e )
{
throw new ContinuumException( "Error attaching buildDefintionTemplate to project ", e );
}
catch ( ContinuumStoreException e )
{
throw new ContinuumException( "Error adding projects from modules", e );
}
AbstractContinuumAction.setProjectGroupId( context, projectGroup.getId() );
// add the relevent security administration roles for this project
if ( addAssignableRoles )
{
executeAction( "add-assignable-roles", context );
}