{
ContinuumStaxReader reader = new ContinuumStaxReader();
FileReader fileReader = new FileReader( new File( backupDirectory, BUILDS_XML ) );
ContinuumDatabase database;
try
{
database = reader.read( fileReader, strict );
}
catch ( XMLStreamException e )
{
throw new DataManagementException( e );
}
finally
{
IOUtil.close( fileReader );
}
// Take control of the JDO instead of using the store, and configure a new persistence factory
// that won't generate new object IDs.
Properties properties = new Properties();
properties.putAll( factory.getProperties() );
properties.setProperty( "org.jpox.metadata.jdoFileExtension", "jdorepl" );
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory( properties );
PlexusJdoUtils.addObject( pmf.getPersistenceManager(), database.getSystemConfiguration() );
Map<Integer, BuildQueue> buildQueues = new HashMap<Integer, BuildQueue>();
for ( BuildQueue buildQueue : (List<BuildQueue>) database.getBuildQueues() )
{
buildQueue = (BuildQueue) PlexusJdoUtils.addObject( pmf.getPersistenceManager(), buildQueue );
buildQueues.put( buildQueue.getId(), buildQueue );
}
Map<Integer, Schedule> schedules = new HashMap<Integer, Schedule>();
for ( Iterator i = database.getSchedules().iterator(); i.hasNext(); )
{
Schedule schedule = (Schedule) i.next();
schedule.setBuildQueues( getBuildQueuesBySchedule( buildQueues, schedule ) );
schedule = (Schedule) PlexusJdoUtils.addObject( pmf.getPersistenceManager(), schedule );
schedules.put( Integer.valueOf( schedule.getId() ), schedule );
}
Map<Integer, Installation> installations = new HashMap<Integer, Installation>();
for ( Iterator i = database.getInstallations().iterator(); i.hasNext(); )
{
Installation installation = (Installation) i.next();
installation = (Installation) PlexusJdoUtils.addObject( pmf.getPersistenceManager(), installation );
installations.put( Integer.valueOf( installation.getInstallationId() ), installation );
}
Map<Integer, Profile> profiles = new HashMap<Integer, Profile>();
for ( Iterator i = database.getProfiles().iterator(); i.hasNext(); )
{
Profile profile = (Profile) i.next();
// process installations
if ( profile.getJdk() != null )
{
profile.setJdk( installations.get( profile.getJdk().getInstallationId() ) );
}
if ( profile.getBuilder() != null )
{
profile.setBuilder( installations.get( profile.getBuilder().getInstallationId() ) );
}
List environmentVariables = new ArrayList();
for ( Iterator envIt = profile.getEnvironmentVariables().listIterator(); envIt.hasNext(); )
{
Installation installation = (Installation) envIt.next();
environmentVariables.add( installations.get( installation.getInstallationId() ) );
envIt.remove();
}
profile.setEnvironmentVariables( environmentVariables );
profile = (Profile) PlexusJdoUtils.addObject( pmf.getPersistenceManager(), profile );
profiles.put( Integer.valueOf( profile.getId() ), profile );
}
Map<Integer, BuildDefinition> buildDefinitions = new HashMap<Integer, BuildDefinition>();
for ( BuildDefinition buildDefinition : (List<BuildDefinition>) database.getBuildDefinitions() )
{
if ( buildDefinition.getSchedule() != null )
{
buildDefinition.setSchedule( schedules.get( Integer.valueOf(
buildDefinition.getSchedule().getId() ) ) );
}
if ( buildDefinition.getProfile() != null )
{
buildDefinition.setProfile( profiles.get( Integer.valueOf( buildDefinition.getProfile().getId() ) ) );
}
buildDefinition = (BuildDefinition) PlexusJdoUtils.addObject( pmf.getPersistenceManager(),
buildDefinition );
buildDefinitions.put( Integer.valueOf( buildDefinition.getId() ), buildDefinition );
}
Map<Integer, LocalRepository> localRepositories = new HashMap<Integer, LocalRepository>();
for ( LocalRepository localRepository : (List<LocalRepository>) database.getLocalRepositories() )
{
localRepository = (LocalRepository) PlexusJdoUtils.addObject( pmf.getPersistenceManager(),
localRepository );
localRepositories.put( Integer.valueOf( localRepository.getId() ), localRepository );
}
Map<Integer, ProjectGroup> projectGroups = new HashMap<Integer, ProjectGroup>();
for ( Iterator i = database.getProjectGroups().iterator(); i.hasNext(); )
{
ProjectGroup projectGroup = (ProjectGroup) i.next();
// first, we must map up any schedules, etc.
projectGroup.setBuildDefinitions( processBuildDefinitions( projectGroup.getBuildDefinitions(), schedules,
profiles, buildDefinitions ) );
for ( Iterator j = projectGroup.getProjects().iterator(); j.hasNext(); )
{
Project project = (Project) j.next();
project.setBuildDefinitions( processBuildDefinitions( project.getBuildDefinitions(), schedules,
profiles, buildDefinitions ) );
}
if ( projectGroup.getLocalRepository() != null )
{
projectGroup.setLocalRepository( localRepositories.get( Integer.valueOf(
projectGroup.getLocalRepository().getId() ) ) );
}
projectGroup = (ProjectGroup) PlexusJdoUtils.addObject( pmf.getPersistenceManager(), projectGroup );
projectGroups.put( Integer.valueOf( projectGroup.getId() ), projectGroup );
}
// create project scm root data (CONTINUUM-2040)
Map<Integer, ProjectScmRoot> projectScmRoots = new HashMap<Integer, ProjectScmRoot>();
Set<Integer> keys = projectGroups.keySet();
int id = 1;
for ( Integer key : keys )
{
ProjectGroup projectGroup = projectGroups.get( key );
String url = " ";
List<Project> projects = ProjectSorter.getSortedProjects( getProjectsByGroupIdWithDependencies( pmf,
projectGroup.getId() ),
log );
for ( Iterator j = projects.iterator(); j.hasNext(); )
{
Project project = (Project) j.next();
if ( !project.getScmUrl().trim().startsWith( url ) )
{
url = project.getScmUrl();
ProjectScmRoot projectScmRoot = new ProjectScmRoot();
projectScmRoot.setId( id );
projectScmRoot.setProjectGroup( projectGroup );
projectScmRoot.setScmRootAddress( url );
projectScmRoot.setState( project.getState() );
projectScmRoot = (ProjectScmRoot) PlexusJdoUtils.addObject( pmf.getPersistenceManager(),
projectScmRoot );
projectScmRoots.put( Integer.valueOf( projectScmRoot.getId() ), projectScmRoot );
id++;
}
}
}
/*
for ( RepositoryPurgeConfiguration repoPurge : (List<RepositoryPurgeConfiguration>) database.getRepositoryPurgeConfigurations() )
{
repoPurge.setRepository( localRepositories.get(
Integer.valueOf( repoPurge.getRepository().getId() ) ) );
if ( repoPurge.getSchedule() != null )
{
repoPurge.setSchedule( schedules.get(
Integer.valueOf( repoPurge.getSchedule().getId() ) ) );
}
repoPurge = (RepositoryPurgeConfiguration) PlexusJdoUtils.addObject( pmf.getPersistenceManager(), repoPurge );
}*/
for ( DirectoryPurgeConfiguration dirPurge : (List<DirectoryPurgeConfiguration>) database.getDirectoryPurgeConfigurations() )
{
if ( dirPurge.getSchedule() != null )
{
dirPurge.setSchedule( schedules.get( Integer.valueOf( dirPurge.getSchedule().getId() ) ) );
}
dirPurge = (DirectoryPurgeConfiguration) PlexusJdoUtils.addObject( pmf.getPersistenceManager(), dirPurge );
}
for ( ContinuumReleaseResult releaseResult : (List<ContinuumReleaseResult>) database.getContinuumReleaseResults() )
{
releaseResult.setProjectGroup( projectGroups.get( Integer.valueOf(
releaseResult.getProjectGroup().getId() ) ) );
ProjectGroup group = releaseResult.getProjectGroup();
for ( Project project : (List<Project>) group.getProjects() )
{
if ( project.getId() == releaseResult.getProject().getId() )
{
try
{
Project proj = (Project) PlexusJdoUtils.getObjectById( pmf.getPersistenceManager(),
Project.class, project.getId(), null );
releaseResult.setProject( proj );
}
catch ( Exception e )
{
throw new DataManagementException( e );
}
}
}
releaseResult = (ContinuumReleaseResult) PlexusJdoUtils.addObject( pmf.getPersistenceManager(),
releaseResult );
}
for ( BuildDefinitionTemplate template : (List<BuildDefinitionTemplate>) database.getBuildDefinitionTemplates() )
{
template.setBuildDefinitions( processBuildDefinitions( template.getBuildDefinitions(), buildDefinitions ) );
template = (BuildDefinitionTemplate) PlexusJdoUtils.addObject( pmf.getPersistenceManager(), template );
}