blob: 72e63708bd4db5311f84e22727deb211a75d8e74 [file] [log] [blame]
package org.apache.continuum.dao;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.continuum.model.project.BuildResult;
import org.apache.maven.continuum.model.project.Project;
import org.apache.maven.continuum.project.ContinuumProjectState;
import org.apache.maven.continuum.store.ContinuumStoreException;
import org.springframework.stereotype.Repository;
import javax.jdo.Extent;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
* @version $Id$
*/
@Repository("buildResultDao")
public class BuildResultDaoImpl
extends AbstractDao
implements BuildResultDao
{
public void updateBuildResult( BuildResult build )
throws ContinuumStoreException
{
PersistenceManager pm = getPersistenceManager();
Transaction tx = pm.currentTransaction();
Project project = build.getProject();
try
{
tx.begin();
if ( !JDOHelper.isDetached( build ) )
{
throw new ContinuumStoreException( "Not detached: " + build );
}
pm.makePersistent( build );
if ( !JDOHelper.isDetached( project ) )
{
throw new ContinuumStoreException( "Not detached: " + project );
}
project.setState( build.getState() );
pm.makePersistent( project );
tx.commit();
}
finally
{
rollback( tx );
}
}
public void addBuildResult( Project project, BuildResult build )
throws ContinuumStoreException
{
PersistenceManager pm = getPersistenceManager();
Transaction tx = pm.currentTransaction();
try
{
tx.begin();
pm.getFetchPlan().addGroup( PROJECT_WITH_BUILDS_FETCH_GROUP );
Object objectId = pm.newObjectIdInstance( Project.class, project.getId() );
project = (Project) pm.getObjectById( objectId );
build = (BuildResult) makePersistent( pm, build, false );
// TODO: these are in the wrong spot - set them on success (though
// currently some depend on latest build being the one in progress)
project.setLatestBuildId( build.getId() );
project.setState( build.getState() );
project.addBuildResult( build );
tx.commit();
}
finally
{
rollback( tx );
}
}
public BuildResult getLatestBuildResultForProject( int projectId )
{
PersistenceManager pm = getPersistenceManager();
Transaction tx = pm.currentTransaction();
try
{
tx.begin();
Extent extent = pm.getExtent( BuildResult.class, true );
Query query = pm.newQuery( extent );
query.declareParameters( "int projectId" );
query.setFilter( "this.project.id == projectId && this.project.latestBuildId == this.id" );
List<BuildResult> result = (List<BuildResult>) query.execute( projectId );
result = (List<BuildResult>) pm.detachCopyAll( result );
tx.commit();
if ( result != null && !result.isEmpty() )
{
return (BuildResult) result.get( 0 );
}
}
finally
{
rollback( tx );
}
return null;
}
public BuildResult getLatestBuildResultForBuildDefinition( int projectId, int buildDefinitionId )
{
PersistenceManager pm = getPersistenceManager();
Transaction tx = pm.currentTransaction();
try
{
tx.begin();
Extent extent = pm.getExtent( BuildResult.class, true );
Query query = pm.newQuery( extent );
query.declareParameters( "int projectId, int buildDefinitionId" );
query.setFilter( "this.project.id == projectId && this.buildDefinition.id == buildDefinitionId" );
query.setOrdering( "id descending" );
Object[] params = new Object[2];
params[0] = projectId;
params[1] = buildDefinitionId;
List<BuildResult> result = (List<BuildResult>) query.executeWithArray( params );
result = (List<BuildResult>) pm.detachCopyAll( result );
tx.commit();
if ( result != null && !result.isEmpty() )
{
return (BuildResult) result.get( 0 );
}
}
finally
{
rollback( tx );
}
return null;
}
public Map<Integer, BuildResult> getLatestBuildResultsByProjectGroupId( int projectGroupId )
{
PersistenceManager pm = getPersistenceManager();
Transaction tx = pm.currentTransaction();
try
{
tx.begin();
Extent extent = pm.getExtent( BuildResult.class, true );
Query query = pm.newQuery( extent );
String filter = "this.project.latestBuildId == this.id";
if ( projectGroupId > 0 )
{
query.declareParameters( "int projectGroupId" );
filter += " && this.project.projectGroup.id == projectGroupId";
}
query.setFilter( filter );
List<BuildResult> result;
if ( projectGroupId > 0 )
{
result = (List<BuildResult>) query.execute( projectGroupId );
}
else
{
result = (List<BuildResult>) query.execute();
}
result = (List<BuildResult>) pm.detachCopyAll( result );
tx.commit();
if ( result != null && !result.isEmpty() )
{
Map<Integer, BuildResult> builds = new HashMap<Integer, BuildResult>();
for ( BuildResult br : result )
{
builds.put( br.getProject().getId(), br );
}
return builds;
}
}
finally
{
rollback( tx );
}
return null;
}
public Map<Integer, BuildResult> getLatestBuildResults()
{
return getLatestBuildResultsByProjectGroupId( -1 );
}
public void removeBuildResult( BuildResult buildResult )
{
removeObject( buildResult );
}
public List<BuildResult> getAllBuildsForAProjectByDate( int projectId )
{
PersistenceManager pm = getPersistenceManager();
Transaction tx = pm.currentTransaction();
try
{
tx.begin();
Query query = pm.newQuery( "SELECT FROM " + BuildResult.class.getName() +
" WHERE project.id == projectId PARAMETERS int projectId ORDER BY endTime DESC" );
query.declareImports( "import java.lang.Integer" );
query.declareParameters( "Integer projectId" );
List result = (List) query.execute( projectId );
result = (List) pm.detachCopyAll( result );
tx.commit();
return result;
}
finally
{
rollback( tx );
}
}
public BuildResult getBuildResult( int buildId )
throws ContinuumStoreException
{
return (BuildResult) getObjectById( BuildResult.class, buildId, BUILD_RESULT_WITH_DETAILS_FETCH_GROUP );
}
public List<BuildResult> getBuildResultByBuildNumber( int projectId, int buildNumber )
{
PersistenceManager pm = getPersistenceManager();
Transaction tx = pm.currentTransaction();
try
{
tx.begin();
Extent extent = pm.getExtent( BuildResult.class, true );
Query query = pm.newQuery( extent );
query.declareParameters( "int projectId, int buildNumber" );
query.setFilter( "this.project.id == projectId && this.buildNumber == buildNumber" );
List result = (List) query.execute( projectId, buildNumber );
result = (List) pm.detachCopyAll( result );
tx.commit();
return result;
}
finally
{
rollback( tx );
}
}
public List<BuildResult> getBuildResultsByBuildDefinition( int projectId, int buildDefinitionId )
{
return getBuildResultsByBuildDefinition( projectId, buildDefinitionId, -1, -1 );
}
public List<BuildResult> getBuildResultsByBuildDefinition( int projectId, int buildDefinitionId, long startIndex,
long endIndex )
{
PersistenceManager pm = getPersistenceManager();
Transaction tx = pm.currentTransaction();
try
{
tx.begin();
Extent extent = pm.getExtent( BuildResult.class, true );
Query query = pm.newQuery( extent );
if ( startIndex >= 0 && endIndex >= 0 )
{
query.setRange( startIndex, endIndex );
}
query.declareParameters( "int projectId, int buildDefinitionId" );
query.setFilter( "this.project.id == projectId && this.buildDefinition.id == buildDefinitionId" );
query.setOrdering( "this.id descending" );
List<BuildResult> result = (List<BuildResult>) query.execute( projectId, buildDefinitionId );
result = (List<BuildResult>) pm.detachCopyAll( result );
tx.commit();
return result;
}
finally
{
rollback( tx );
}
}
public long getNbBuildResultsForProject( int projectId )
{
PersistenceManager pm = getPersistenceManager();
Transaction tx = pm.currentTransaction();
try
{
tx.begin();
Query query = pm.newQuery( BuildResult.class, "project.id == projectId" );
query.declareParameters( "int projectId" );
query.setResult( "count(this)" );
long result = (Long) query.execute( projectId );
tx.commit();
return result;
}
finally
{
rollback( tx );
}
}
public List<BuildResult> getBuildResultsForProject( int projectId )
{
return getBuildResultsForProject( projectId, -1, -1 );
}
public List<BuildResult> getBuildResultsForProject( int projectId, long startIndex, long endIndex )
{
PersistenceManager pm = getPersistenceManager();
Transaction tx = pm.currentTransaction();
try
{
tx.begin();
Extent extent = pm.getExtent( BuildResult.class, true );
Query query = pm.newQuery( extent );
query.declareParameters( "int projectId" );
query.setFilter( "this.project.id == projectId" );
query.setOrdering( "this.startTime descending" );
if ( startIndex >= 0 )
{
query.setRange( startIndex, endIndex );
}
List result = (List) query.execute( projectId );
result = (List) pm.detachCopyAll( result );
tx.commit();
return result;
}
finally
{
rollback( tx );
}
}
public List<BuildResult> getBuildResultsForProjectFromId( int projectId, long startId )
throws ContinuumStoreException
{
PersistenceManager pm = getPersistenceManager();
Transaction tx = pm.currentTransaction();
pm.getFetchPlan().addGroup( BUILD_RESULT_WITH_DETAILS_FETCH_GROUP );
try
{
tx.begin();
Extent extent = pm.getExtent( BuildResult.class, true );
Query query = pm.newQuery( extent );
query.declareParameters( "int projectId, int buildNumber" );
query.setFilter( "this.project.id == projectId && this.buildNumber >= buildNumber" );
query.setOrdering( "this.startTime descending" );
List result = (List) query.execute( projectId, startId );
result = (List) pm.detachCopyAll( result );
tx.commit();
return result;
}
catch ( Exception e )
{
throw new ContinuumStoreException( e.getMessage(), e );
}
finally
{
rollback( tx );
}
}
public List<BuildResult> getBuildResultsForProject( int projectId, long fromDate )
{
PersistenceManager pm = getPersistenceManager();
Transaction tx = pm.currentTransaction();
pm.getFetchPlan().addGroup( BUILD_RESULT_WITH_DETAILS_FETCH_GROUP );
try
{
tx.begin();
Extent extent = pm.getExtent( BuildResult.class, true );
Query query = pm.newQuery( extent );
query.declareParameters( "int projectId, long fromDate" );
query.setFilter( "this.project.id == projectId && this.startTime > fromDate" );
List result = (List) query.execute( projectId, fromDate );
result = (List) pm.detachCopyAll( result );
tx.commit();
return result;
}
finally
{
rollback( tx );
}
}
public List<BuildResult> getBuildResultsInSuccessForProject( int projectId, long fromDate )
{
List<BuildResult> buildResults = getBuildResultsForProject( projectId, fromDate );
List<BuildResult> results = new ArrayList<BuildResult>();
if ( buildResults != null )
{
for ( BuildResult res : buildResults )
{
if ( res.getState() == ContinuumProjectState.OK )
{
results.add( res );
}
}
}
return results;
}
public Map<Integer, BuildResult> getBuildResultsInSuccessByProjectGroupId( int projectGroupId )
{
PersistenceManager pm = getPersistenceManager();
Transaction tx = pm.currentTransaction();
try
{
tx.begin();
Extent extent = pm.getExtent( BuildResult.class, true );
Query query = pm.newQuery( extent );
String filter = "this.project.buildNumber == this.buildNumber";
if ( projectGroupId > 0 )
{
query.declareParameters( "int projectGroupId" );
filter += " && this.project.projectGroup.id == projectGroupId";
}
query.setFilter( filter );
List<BuildResult> result;
if ( projectGroupId > 0 )
{
result = (List<BuildResult>) query.execute( projectGroupId );
}
else
{
result = (List<BuildResult>) query.execute();
}
result = (List<BuildResult>) pm.detachCopyAll( result );
tx.commit();
if ( result != null && !result.isEmpty() )
{
Map<Integer, BuildResult> builds = new HashMap<Integer, BuildResult>();
for ( BuildResult br : result )
{
builds.put( br.getProject().getId(), br );
}
return builds;
}
}
finally
{
rollback( tx );
}
return null;
}
public Map<Integer, BuildResult> getBuildResultsInSuccess()
{
return getBuildResultsInSuccessByProjectGroupId( -1 );
}
}