/*
* 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.Calendar;
import java.util.Iterator;
import java.util.List;
import javax.persistence.Query;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.hortis.sonar.model.File;
import ch.hortis.sonar.model.FileMeasure;
import ch.hortis.sonar.model.Parameter;
import ch.hortis.sonar.model.ProjectMeasure;
import ch.hortis.sonar.model.ProjectTendency;
import ch.hortis.sonar.model.Snapshot;
import ch.hortis.sonar.model.RuleFailure;
import ch.hortis.sonar.model.SnapshotGroup;
public class MeasuresPurgeJob extends BaseJob {
protected static final Logger LOG = LoggerFactory.getLogger( Batch.class );
public final static int RULES_PROJECT_MEASURES_HISTORY_DAYS = 15;
private Calendar calendar;
public MeasuresPurgeJob() {
}
public MeasuresPurgeJob(Calendar calendar) {
this.calendar = calendar;
}
private Calendar getCalendar() {
if ( calendar == null ) {
return Calendar.getInstance();
}
return calendar;
}
public void process( JobExecutionContext context ) throws JobExecutionException {
if ( LOG.isDebugEnabled() ) LOG.debug( "Processing job " + this.getClass().getName() );
purgeMeasures();
purgeRulesProjectMeasures();
}
public final void purgeRulesProjectMeasures() {
Query query = getEntityManager().createNamedQuery( ProjectMeasure.SQL_SELECT_MEASURE_WITH_RULES );
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.size() > 0 ) {
if ( LOG.isDebugEnabled() ) LOG.debug( "Removing " + ruleMeasures.size() + " Rule or RuleCategory project measures" );
getEntityManager().getTransaction().begin();
for (ProjectMeasure measure : ruleMeasures) {
if ( LOG.isDebugEnabled() ) LOG.debug( "Removing Rule ProjectMeasure " + measure.getId() );
getEntityManager().remove(measure);
}
getEntityManager().getTransaction().commit();
}
}
public final void purgeMeasures() {
Query query = getEntityManager().createNamedQuery( SnapshotGroup.SQL_SELECT_GROUPS_TO_PURGE );
List<SnapshotGroup> snapshots = query.getResultList();
if ( snapshots.size() == 0 ) return;
LOG.info( "Purging " + snapshots.size() + " SnapshotGroups metrics" );
for ( SnapshotGroup group : snapshots ) {
if ( LOG.isDebugEnabled() ) LOG.debug( "Processing SnapshotGroup " + group.getId() );
getEntityManager().getTransaction().begin();
for ( Snapshot snapshot : group.getSnapshots() ) {
if ( LOG.isDebugEnabled() ) LOG.debug( "Processing Snapshot " + snapshot.getId() );
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());
if ( LOG.isDebugEnabled() ) LOG.debug( "Removing RuleFailure " + failure.getId() );
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());
if ( LOG.isDebugEnabled() ) LOG.debug( "Removing FileMeasure " + fileMeasure.getId() );
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();
if ( LOG.isDebugEnabled() ) LOG.debug( "Removing ProjectTendency " + tendency.getId() );
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();
if ( LOG.isDebugEnabled() ) LOG.debug( "Removing Parameter " + param.getId() );
getEntityManager().remove( param );
iterParams.remove();
}
}
private void purgeFiles( Snapshot snapshot ) {
List<File> files = snapshot.getFiles();
for ( File file : files ) {
if ( LOG.isDebugEnabled() ) LOG.debug( "Removing File " + file.getId() );
getEntityManager().remove( file );
}
snapshot.getFiles().clear();
}
}