001/* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2012 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020package org.sonar.plugins.dbcleaner; 021 022import org.slf4j.Logger; 023import org.slf4j.LoggerFactory; 024import org.sonar.api.Properties; 025import org.sonar.api.Property; 026import org.sonar.api.PropertyType; 027import org.sonar.api.config.Settings; 028import org.sonar.api.resources.Scopes; 029import org.sonar.core.purge.PurgeDao; 030import org.sonar.plugins.dbcleaner.api.DbCleanerConstants; 031import org.sonar.plugins.dbcleaner.api.PurgeTask; 032import org.sonar.plugins.dbcleaner.period.DefaultPeriodCleaner; 033 034/** 035 * @since 2.14 036 */ 037@Properties({ 038 @Property( 039 key = DbCleanerConstants.PROPERTY_CLEAN_DIRECTORY, 040 defaultValue = "true", 041 name = "Clean history data of directories/packages", 042 global = true, 043 project = true, 044 module = false, 045 type = PropertyType.BOOLEAN) 046}) 047public class DefaultPurgeTask implements PurgeTask { 048 private static final Logger LOG = LoggerFactory.getLogger(DefaultPurgeTask.class); 049 050 private PurgeDao purgeDao; 051 private Settings settings; 052 private DefaultPeriodCleaner periodCleaner; 053 054 public DefaultPurgeTask(PurgeDao purgeDao, Settings settings, DefaultPeriodCleaner periodCleaner) { 055 this.purgeDao = purgeDao; 056 this.settings = settings; 057 this.periodCleaner = periodCleaner; 058 } 059 060 public PurgeTask delete(long resourceId) { 061 purgeDao.deleteResourceTree(resourceId); 062 return this; 063 } 064 065 public PurgeTask purge(long resourceId) { 066 cleanHistoricalData(resourceId); 067 doPurge(resourceId); 068 return this; 069 } 070 071 private void cleanHistoricalData(long resourceId) { 072 try { 073 periodCleaner.clean(resourceId); 074 } catch (Exception e) { 075 // purge errors must no fail the batch 076 LOG.error("Fail to clean historical data [id=" + resourceId + "]", e); 077 } 078 } 079 080 private String[] getScopesWithoutHistoricalData() { 081 if (settings.getBoolean(DbCleanerConstants.PROPERTY_CLEAN_DIRECTORY)) { 082 return new String[]{Scopes.DIRECTORY, Scopes.FILE}; 083 } 084 return new String[]{Scopes.FILE}; 085 } 086 087 private void doPurge(long resourceId) { 088 try { 089 purgeDao.purge(resourceId, getScopesWithoutHistoricalData()); 090 } catch (Exception e) { 091 // purge errors must no fail the batch 092 LOG.error("Fail to purge data [id=" + resourceId + "]", e); 093 } 094 } 095}