/*
* 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.batch;
import ch.hortis.sonar.model.JdbcData;
import ch.hortis.sonar.model.MavenProject;
import ch.hortis.sonar.service.MavenProjectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.persistence.FlushModeType;
import javax.persistence.Query;
import java.util.Collection;
import java.util.List;
public class ProjectEraserTask extends DatabaseTask {
private static final Logger LOG = LoggerFactory.getLogger(ProjectEraserTask.class);
public ProjectEraserTask(JdbcData jdbcData) {
super(jdbcData);
}
public void execute() {
Query query = getEntityManager().createNamedQuery(MavenProject.SQL_SELECT_DISABLED_PROJECTS);
query.setFlushMode(FlushModeType.COMMIT);
List<MavenProject> rootProjects = query.getResultList();
for (MavenProject rootProject : rootProjects) {
Integer projectId = rootProject.getId();
try {
if (LOG.isDebugEnabled()) {
LOG.debug("Erasing root project " + rootProject.getId());
}
long startTime = System.currentTimeMillis();
deleteProject(rootProject);
LOG.info("Project " + projectId + " erased 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);
}
getEntityManager().getTransaction().begin();
getEntityManager().remove(project);
getEntityManager().getTransaction().commit();
}
}