001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 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.period;
021    
022    import org.apache.commons.configuration.Configuration;
023    import org.slf4j.Logger;
024    import org.slf4j.LoggerFactory;
025    import org.sonar.api.resources.Project;
026    import org.sonar.plugins.dbcleaner.api.DbCleanerConstants;
027    
028    import java.text.SimpleDateFormat;
029    import java.util.Date;
030    import java.util.GregorianCalendar;
031    
032    public final class Periods {
033    
034      Date dateToStartKeepingOneSnapshotByWeek;
035      Date dateToStartKeepingOneSnapshotByMonth;
036      Date dateToStartDeletingAllSnapshots;
037    
038      public Periods(Date dateToStartKeepingOneSnapshotByWeek, Date dateToStartKeepingOneSnapshotByMonth, Date dateToStartDeletingAllSnapshots) {
039        this.dateToStartKeepingOneSnapshotByWeek = dateToStartKeepingOneSnapshotByWeek;
040        this.dateToStartKeepingOneSnapshotByMonth = dateToStartKeepingOneSnapshotByMonth;
041        this.dateToStartDeletingAllSnapshots = dateToStartDeletingAllSnapshots;
042      }
043    
044      public Periods(Project project) {
045        dateToStartKeepingOneSnapshotByWeek = getDate(project.getConfiguration(),
046            DbCleanerConstants.MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_WEEK, DbCleanerConstants.ONE_MONTH);
047        dateToStartKeepingOneSnapshotByMonth = getDate(project.getConfiguration(),
048            DbCleanerConstants.MONTHS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_MONTH, DbCleanerConstants.ONE_YEAR);
049        dateToStartDeletingAllSnapshots = getDate(project.getConfiguration(), DbCleanerConstants.MONTHS_BEFORE_DELETING_ALL_SNAPSHOTS,
050            DbCleanerConstants.FIVE_YEARS);
051      }
052    
053      void log() {
054        Logger logger = LoggerFactory.getLogger(getClass());
055        SimpleDateFormat dateFormat = new SimpleDateFormat();
056        logger.debug("Keep only one snapshot by week after : " + dateFormat.format(dateToStartKeepingOneSnapshotByWeek));
057        logger.debug("Keep only one snapshot by month after : " + dateFormat.format(dateToStartKeepingOneSnapshotByMonth));
058        logger.debug("Delete all snapshots after : " + dateFormat.format(dateToStartDeletingAllSnapshots));
059      }
060    
061      static Date getDate(Configuration conf, String propertyKey, String defaultNumberOfMonths) {
062        int months = conf.getInt(propertyKey, Integer.parseInt(defaultNumberOfMonths));
063        GregorianCalendar calendar = new GregorianCalendar();
064        calendar.add(GregorianCalendar.MONTH, -months);
065        return calendar.getTime();
066      }
067    }