/*
* 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.ProjectMeasure;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.persistence.FlushModeType;
import javax.persistence.Query;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
public class PurgeMeasuresTask extends DatabaseTask {
private static final Logger LOG = LoggerFactory.getLogger(PurgeMeasuresTask.class);
public final static int RULES_PROJECT_MEASURES_HISTORY_DAYS = 5;
private Calendar calendar;
public PurgeMeasuresTask(JdbcData jdbcData) {
super(jdbcData);
}
public PurgeMeasuresTask(JdbcData jdbcData, Calendar calendar) {
super(jdbcData);
this.calendar = calendar;
}
public void execute() {
purgeMeasures();
purgeRulesProjectMeasures();
}
private Calendar getCalendar() {
if (calendar == null) {
return Calendar.getInstance();
}
return calendar;
}
public final void purgeRulesProjectMeasures() {
Query query = getEntityManager().createNamedQuery(ProjectMeasure.SQL_SELECT_MEASURE_WITH_RULES);
query.setFlushMode(FlushModeType.COMMIT);
Calendar newCal = Calendar.getInstance();
newCal.setTime(getCalendar().getTime());
newCal.add(Calendar.DAY_OF_MONTH, -RULES_PROJECT_MEASURES_HISTORY_DAYS);
query.setParameter("date", newCal.getTime());
List<ProjectMeasure> ruleMeasures = query.getResultList();
if (!ruleMeasures.isEmpty()) {
getEntityManager().getTransaction().begin();
for (ProjectMeasure measure : ruleMeasures) {
getEntityManager().remove(measure);
}
getEntityManager().getTransaction().commit();
}
}
public final void purgeMeasures() {
Query query = getEntityManager().createNamedQuery(SnapshotGroup.SQL_SELECT_GROUPS_TO_PURGE);
query.setFlushMode(FlushModeType.COMMIT);
List<SnapshotGroup> snapshots = query.getResultList();
if (!snapshots.isEmpty()) {
for (SnapshotGroup group : snapshots) {
getEntityManager().getTransaction().begin();
for (Snapshot snapshot : group.getSnapshots()) {
purgeFileMeasures(snapshot);
purgeRuleFailures(snapshot);
purgeFiles(snapshot);
purgeTendencies(snapshot);
getEntityManager().merge(snapshot);
}
group.setPurged(true);
getEntityManager().merge(group);
getEntityManager().getTransaction().commit();
}
}
}
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();
}
}