// first delete link beetwen profile and this installation
// then removing this
//attachAndDelete( installation );
PersistenceManager pm = getPersistenceManager();
Transaction tx = pm.currentTransaction();
try
{
// this must be done in the same transaction
tx.begin();
// first removing linked jdk
Extent extent = pm.getExtent( Profile.class, true );
Query query = pm.newQuery( extent );
query.declareImports( "import java.lang.String" );
query.declareParameters( "String name" );
query.setFilter( "this.jdk.name == name" );
Collection<Profile> result = (Collection) query.execute( installation.getName() );
if ( result.size() != 0 )
{
for ( Profile profile : result )
{
profile.setJdk( null );
pm.makePersistent( profile );
}
}
// removing linked builder
query = pm.newQuery( extent );
query.declareImports( "import java.lang.String" );
query.declareParameters( "String name" );
query.setFilter( "this.builder.name == name" );
result = (Collection) query.execute( installation.getName() );
if ( result.size() != 0 )
{
for ( Profile profile : result )
{
profile.setBuilder( null );
pm.makePersistent( profile );
}
}
// removing linked env Var
query = pm.newQuery( extent );
query.declareImports( "import java.lang.String" );
query.declareImports( "import " + Installation.class.getName() );
query.declareParameters( "Installation installation" );
query.setFilter( "environmentVariables.contains(installation)" );
//query = pm
// .newQuery( "SELECT FROM profile WHERE environmentVariables.contains(installation) && installation.name == name" );
result = (Collection) query.execute( installation );
if ( result.size() != 0 )
{
for ( Profile profile : result )
{
List<Installation> newEnvironmentVariables = new ArrayList<Installation>();
for ( Installation current : (Iterable<Installation>) profile.getEnvironmentVariables() )
{
if ( !StringUtils.equals( current.getName(), installation.getName() ) )
{
newEnvironmentVariables.add( current );
}
}
profile.setEnvironmentVariables( newEnvironmentVariables );
pm.makePersistent( profile );
}
}
pm.deletePersistent( installation );
tx.commit();
}
finally
{
rollback( tx );