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     */
020    package org.sonar.plugins.dbcleaner;
021    
022    import org.slf4j.Logger;
023    import org.slf4j.LoggerFactory;
024    import org.sonar.api.Properties;
025    import org.sonar.api.Property;
026    import org.sonar.api.config.Settings;
027    import org.sonar.api.resources.Scopes;
028    import org.sonar.core.purge.PurgeDao;
029    import org.sonar.plugins.dbcleaner.api.DbCleanerConstants;
030    import org.sonar.plugins.dbcleaner.api.PurgeTask;
031    import org.sonar.plugins.dbcleaner.period.DefaultPeriodCleaner;
032    
033    /**
034     * @since 2.14
035     */
036    @Properties({
037      @Property(
038        key = DbCleanerConstants.PROPERTY_CLEAN_DIRECTORY,
039        defaultValue = "true",
040        name = "Clean history data of directories/packages")
041    })
042    public class DefaultPurgeTask implements PurgeTask {
043      private static final Logger LOG = LoggerFactory.getLogger(DefaultPurgeTask.class);
044    
045      private PurgeDao purgeDao;
046      private Settings settings;
047      private DefaultPeriodCleaner periodCleaner;
048    
049      public DefaultPurgeTask(PurgeDao purgeDao, Settings settings, DefaultPeriodCleaner periodCleaner) {
050        this.purgeDao = purgeDao;
051        this.settings = settings;
052        this.periodCleaner = periodCleaner;
053      }
054    
055      public PurgeTask delete(long resourceId) {
056        purgeDao.deleteProject(resourceId);
057        return this;
058      }
059    
060      public PurgeTask purge(long resourceId) {
061        cleanHistoricalData(resourceId);
062        doPurge(resourceId);
063        return this;
064      }
065    
066      private void cleanHistoricalData(long resourceId) {
067        try {
068          periodCleaner.clean(resourceId);
069        } catch (Exception e) {
070          // purge errors must no fail the batch
071          LOG.error("Fail to clean historical data [id=" + resourceId + "]", e);
072        }
073      }
074    
075      private String[] getScopesWithoutHistoricalData() {
076        if (settings.getBoolean(DbCleanerConstants.PROPERTY_CLEAN_DIRECTORY)) {
077          return new String[]{Scopes.DIRECTORY, Scopes.FILE};
078        }
079        return new String[]{Scopes.FILE};
080      }
081    
082      private void doPurge(long resourceId) {
083        try {
084          purgeDao.purge(resourceId, getScopesWithoutHistoricalData());
085        } catch (Exception e) {
086          // purge errors must no fail the batch
087          LOG.error("Fail to purge data [id=" + resourceId + "]", e);
088        }
089      }
090    }