001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube 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     * SonarQube 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 License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.batch.components;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.slf4j.Logger;
024    import org.slf4j.LoggerFactory;
025    import org.sonar.api.BatchExtension;
026    import org.sonar.api.database.DatabaseSession;
027    import org.sonar.api.database.model.Snapshot;
028    import org.sonar.api.resources.Project;
029    import org.sonar.api.resources.Qualifiers;
030    
031    import javax.annotation.CheckForNull;
032    
033    import java.util.List;
034    
035    import static com.google.common.collect.Lists.newLinkedList;
036    
037    public class TimeMachineConfiguration implements BatchExtension {
038    
039      private static final Logger LOG = LoggerFactory.getLogger(TimeMachineConfiguration.class);
040    
041      private final DatabaseSession session;
042      private Project project;
043      private final PeriodsDefinition periodsDefinition;
044    
045      private List<Period> periods;
046      private List<PastSnapshot> modulePastSnapshots;
047    
048      public TimeMachineConfiguration(DatabaseSession session, Project project, PeriodsDefinition periodsDefinition) {
049        this.session = session;
050        this.project = project;
051        this.periodsDefinition = periodsDefinition;
052        initModulePastSnapshots();
053      }
054    
055      private void initModulePastSnapshots() {
056        periods = newLinkedList();
057        modulePastSnapshots = newLinkedList();
058        for (PastSnapshot projectPastSnapshot : periodsDefinition.getRootProjectPastSnapshots()) {
059          Snapshot snapshot = findSnapshot(projectPastSnapshot.getProjectSnapshot());
060    
061          PastSnapshot pastSnapshot = projectPastSnapshot.clonePastSnapshot();
062          modulePastSnapshots.add(pastSnapshot);
063          // When no snapshot is found, date of the period is null
064          periods.add(new Period(pastSnapshot.getIndex(), snapshot != null ? snapshot.getCreatedAt() : null));
065          log(pastSnapshot);
066        }
067      }
068    
069      /**
070       * Only used to get the real date of the snapshot on the current period.
071       * The date is used to calculate new_violations measures
072       */
073      @CheckForNull
074      private Snapshot findSnapshot(Snapshot projectSnapshot) {
075        String hql = "from " + Snapshot.class.getSimpleName() + " where resourceId=:resourceId and (rootId=:rootSnapshotId or id=:rootSnapshotId)";
076        List<Snapshot> snapshots = session.createQuery(hql)
077          .setParameter("resourceId", project.getId())
078          .setParameter("rootSnapshotId", projectSnapshot.getId())
079          .setMaxResults(1)
080          .getResultList();
081        return snapshots.isEmpty() ? null : snapshots.get(0);
082      }
083    
084      private void log(PastSnapshot pastSnapshot) {
085        String qualifier = pastSnapshot.getQualifier();
086        // hack to avoid too many logs when the views plugin is installed
087        if (StringUtils.equals(Qualifiers.VIEW, qualifier) || StringUtils.equals(Qualifiers.SUBVIEW, qualifier)) {
088          LOG.debug(pastSnapshot.toString());
089        } else {
090          LOG.info(pastSnapshot.toString());
091        }
092      }
093    
094      public List<Period> periods() {
095        return periods;
096      }
097    
098      public List<PastSnapshot> getProjectPastSnapshots() {
099        return modulePastSnapshots;
100      }
101    }