/*
* This program is copyright (c) 2007 Hortis-GRC SA.
*
* This file is part of Sonar.
* Sonar is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package ch.hortis.sonar.core;
import java.util.Collection;
import java.util.List;
import javax.persistence.Query;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import ch.hortis.sonar.model.MavenProject;
import ch.hortis.sonar.service.MavenProjectService;
public class ProjectEreaseJob extends BaseJob {
public void process( JobExecutionContext context ) throws JobExecutionException {
deleteProjects();
}
public void deleteProjects() {
Query query = getEntityManager().createNamedQuery( MavenProject.SQL_SELECT_DISABLED_PROJECTS );
List<MavenProject> rootProjects = query.getResultList();
for ( MavenProject rootProject : rootProjects ) {
Integer projectId = rootProject.getId();
try {
if ( LOG.isDebugEnabled() ) LOG.debug( "Ereasing root project " + rootProject.getId() );
long startTime = System.currentTimeMillis();
deleteProject( rootProject );
LOG.info( "Project " + projectId + " ereased in " + ( System.currentTimeMillis() - startTime ) + " ms" );
} catch ( Throwable t ) {
LOG.error( "Error during project " + projectId + " ereasing ", t );
}
}
}
private void deleteProject( MavenProject project ) {
MavenProjectService projectsService = new MavenProjectService( getEntityManager() );
Collection<MavenProject> childProjects = projectsService.getModules( project, false );
for (MavenProject childProject : childProjects) {
deleteProject( childProject );
}
//project.setParent(null);
if ( LOG.isDebugEnabled() ) LOG.debug( "Ereasing project " + project.getId() );
getEntityManager().getTransaction().begin();
getEntityManager().remove(project);
getEntityManager().getTransaction().commit();
}
}