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    
021    package org.sonar.batch.components;
022    
023    import org.sonar.api.BatchComponent;
024    import org.sonar.api.config.Settings;
025    import org.sonar.api.database.DatabaseSession;
026    import org.sonar.api.database.model.Snapshot;
027    import org.sonar.api.resources.Qualifiers;
028    import org.sonar.batch.ProjectTree;
029    
030    import javax.persistence.Query;
031    
032    import java.util.Date;
033    import java.util.List;
034    
035    import static com.google.common.collect.Lists.newLinkedList;
036    
037    public class PeriodsDefinition implements BatchComponent {
038    
039      public static final int CORE_TENDENCY_DEPTH_DEFAULT_VALUE = 30;
040      private static final int NUMBER_OF_VARIATION_SNAPSHOTS = 5;
041    
042      private DatabaseSession session;
043    
044      private ProjectTree projectTree;
045      private final Settings settings;
046    
047      private List<PastSnapshot> projectPastSnapshots;
048    
049      public PeriodsDefinition(DatabaseSession session, ProjectTree projectTree, Settings settings,
050                               PastSnapshotFinder pastSnapshotFinder) {
051        this.session = session;
052        this.projectTree = projectTree;
053        this.settings = settings;
054        initPastSnapshots(pastSnapshotFinder, projectTree.getRootProject().getQualifier());
055      }
056    
057      private void initPastSnapshots(PastSnapshotFinder pastSnapshotFinder, String rootQualifier) {
058        Snapshot projectSnapshot = buildProjectSnapshot();
059        projectPastSnapshots = newLinkedList();
060        if (projectSnapshot != null) {
061          for (int index = 1; index <= NUMBER_OF_VARIATION_SNAPSHOTS; index++) {
062            PastSnapshot pastSnapshot = pastSnapshotFinder.find(projectSnapshot, rootQualifier, settings, index);
063            // SONAR-4700 Add a past snapshot only if it exists
064            if (pastSnapshot != null && pastSnapshot.getProjectSnapshot() != null) {
065              projectPastSnapshots.add(pastSnapshot);
066            }
067          }
068        }
069      }
070    
071      private Snapshot buildProjectSnapshot() {
072        Query query = session
073          .createNativeQuery("select p.id from projects p where p.kee=:resourceKey and p.qualifier<>:lib and p.enabled=:enabled");
074        query.setParameter("resourceKey", projectTree.getRootProject().getKey());
075        query.setParameter("lib", Qualifiers.LIBRARY);
076        query.setParameter("enabled", Boolean.TRUE);
077    
078        Snapshot snapshot = null;
079        Number projectId = session.getSingleResult(query, null);
080        if (projectId != null) {
081          snapshot = new Snapshot();
082          snapshot.setResourceId(projectId.intValue());
083          snapshot.setCreatedAt(projectTree.getRootProject().getAnalysisDate());
084          snapshot.setBuildDate(new Date());
085          snapshot.setVersion(projectTree.getRootProject().getAnalysisVersion());
086        }
087        return snapshot;
088      }
089    
090      /**
091       * @return past snapshots of root project
092       */
093      List<PastSnapshot> getRootProjectPastSnapshots() {
094        return projectPastSnapshots;
095      }
096    
097    }