/*
* 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.File;
import ch.hortis.sonar.model.FileMeasure;
import ch.hortis.sonar.model.JdbcData;
import ch.hortis.sonar.model.Parameter;
import ch.hortis.sonar.model.ProjectTendency;
import ch.hortis.sonar.model.RuleFailure;
import ch.hortis.sonar.model.Snapshot;
import ch.hortis.sonar.model.SnapshotGroup;
import javax.persistence.FlushModeType;
import javax.persistence.Query;
import java.util.Iterator;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PurgeMeasuresTask extends DatabaseTask {
private static final Logger LOG = LoggerFactory.getLogger(PurgeMeasuresTask.class);
public PurgeMeasuresTask(JdbcData jdbcData) {
super(jdbcData);
}
public void execute() {
purgeMeasures();
}
public final void purgeMeasures() {
Query query = getEntityManager().createNamedQuery(SnapshotGroup.SQL_SELECT_GROUPS_TO_PURGE);
query.setFlushMode(FlushModeType.COMMIT);
List<SnapshotGroup> groups = query.getResultList();
createNewEntityManager();
for (Iterator<SnapshotGroup> i = groups.iterator(); i.hasNext();) {
SnapshotGroup group = i.next();
for (Iterator<Snapshot> is = group.getSnapshots().iterator(); is.hasNext();) {
Snapshot snapshot = is.next();
LOG.info("purging group=" + group.getId() + ", snapshot=" + snapshot.getId());
snapshot = getEntityManager().find(Snapshot.class, snapshot.getId());
getEntityManager().getTransaction().begin();
purgeFileMeasures(snapshot);
purgeRuleFailures(snapshot);
purgeFiles(snapshot);
purgeTendencies(snapshot);
getEntityManager().merge(snapshot);
getEntityManager().getTransaction().commit();
is.remove();
createNewEntityManager();
}
getEntityManager().getTransaction().begin();
// since SnapshotGroup are now detached must reload them to put them under the entitymanager control
group = getEntityManager().find(SnapshotGroup.class, group.getId());
group.setPurged(true);
getEntityManager().merge(group);
getEntityManager().getTransaction().commit();
i.remove();
createNewEntityManager();
}
}
private void purgeRuleFailures(Snapshot snapshot) {
List<RuleFailure> ruleFailures = snapshot.getRuleFailures();
for (Iterator<RuleFailure> iter = ruleFailures.iterator(); iter.hasNext();) {
RuleFailure failure = iter.next();
deleteParameters(failure.getParameters());
getEntityManager().remove(failure);
iter.remove();
}
}
private void purgeFileMeasures(Snapshot snapshot) {
List<FileMeasure> fileMeasures = snapshot.getFileMeasures();
for (Iterator<FileMeasure> iter = fileMeasures.iterator(); iter.hasNext();) {
FileMeasure fileMeasure = iter.next();
deleteParameters(fileMeasure.getParameters());
getEntityManager().remove(fileMeasure);
iter.remove();
}
}
private void purgeTendencies(Snapshot snapshot) {
List<ProjectTendency> tendencies = snapshot.getTendencies();
for (Iterator<ProjectTendency> iter = tendencies.iterator(); iter.hasNext();) {
ProjectTendency tendency = iter.next();
getEntityManager().remove(tendency);
iter.remove();
}
}
private void deleteParameters(List<Parameter> params) {
// explicitly remove the parameters since it is not working with hibernate
for (Iterator<Parameter> iterParams = params.iterator(); iterParams.hasNext();) {
Parameter param = iterParams.next();
getEntityManager().remove(param);
iterParams.remove();
}
}
private void purgeFiles(Snapshot snapshot) {
List<File> files = snapshot.getFiles();
for (File file : files) {
getEntityManager().remove(file);
}
snapshot.getFiles().clear();
}
}