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.batch.components;
021
022 import java.util.Date;
023 import java.util.List;
024
025 import javax.persistence.Query;
026
027 import org.apache.commons.configuration.Configuration;
028 import org.apache.commons.lang.StringUtils;
029 import org.slf4j.LoggerFactory;
030 import org.sonar.api.BatchExtension;
031 import org.sonar.api.CoreProperties;
032 import org.sonar.api.database.DatabaseSession;
033 import org.sonar.api.database.model.Snapshot;
034 import org.sonar.api.resources.Project;
035 import org.sonar.api.resources.Qualifiers;
036 import org.sonar.api.utils.Logs;
037
038 import com.google.common.collect.Lists;
039
040 public class TimeMachineConfiguration implements BatchExtension {
041
042 private static final int NUMBER_OF_VARIATION_SNAPSHOTS = 5;
043
044 private Project project;
045 private final Configuration configuration;
046 private List<PastSnapshot> projectPastSnapshots;
047 private DatabaseSession session;
048
049 public TimeMachineConfiguration(DatabaseSession session, Project project, Configuration configuration,
050 PastSnapshotFinder pastSnapshotFinder) {
051 this.session = session;
052 this.project = project;
053 this.configuration = configuration;
054 initPastSnapshots(pastSnapshotFinder);
055 }
056
057 private void initPastSnapshots(PastSnapshotFinder pastSnapshotFinder) {
058 Snapshot projectSnapshot = buildProjectSnapshot();
059
060 projectPastSnapshots = Lists.newLinkedList();
061 if (projectSnapshot != null) {
062 for (int index = 1; index <= NUMBER_OF_VARIATION_SNAPSHOTS; index++) {
063 PastSnapshot pastSnapshot = pastSnapshotFinder.find(projectSnapshot, configuration, index);
064 if (pastSnapshot != null) {
065 log(pastSnapshot);
066 projectPastSnapshots.add(pastSnapshot);
067 }
068 }
069 }
070 }
071
072 private Snapshot buildProjectSnapshot() {
073 Query query = session
074 .createNativeQuery("select p.id from projects p where p.kee=:resourceKey and p.qualifier<>:lib and p.enabled=:enabled");
075 query.setParameter("resourceKey", project.getKey());
076 query.setParameter("lib", Qualifiers.LIBRARY);
077 query.setParameter("enabled", Boolean.TRUE);
078
079 Snapshot snapshot = null;
080 Number projectId = session.getSingleResult(query, null);
081 if (projectId != null) {
082 snapshot = new Snapshot();
083 snapshot.setResourceId(projectId.intValue());
084 snapshot.setCreatedAt(project.getAnalysisDate());
085 snapshot.setBuildDate(new Date());
086 }
087 return snapshot;
088 }
089
090 private void log(PastSnapshot pastSnapshot) {
091 String qualifier = pastSnapshot.getQualifier();
092 // hack to avoid too many logs when the views plugin is installed
093 if (StringUtils.equals(Qualifiers.VIEW, qualifier) || StringUtils.equals(Qualifiers.SUBVIEW, qualifier)) {
094 LoggerFactory.getLogger(getClass()).debug(pastSnapshot.toString());
095 } else {
096 Logs.INFO.info(pastSnapshot.toString());
097 }
098 }
099
100 public boolean skipTendencies() {
101 return configuration.getBoolean(CoreProperties.SKIP_TENDENCIES_PROPERTY, CoreProperties.SKIP_TENDENCIES_DEFAULT_VALUE);
102 }
103
104 public int getTendencyPeriodInDays() {
105 return configuration.getInt(CoreProperties.CORE_TENDENCY_DEPTH_PROPERTY, CoreProperties.CORE_TENDENCY_DEPTH_DEFAULT_VALUE);
106 }
107
108 public List<PastSnapshot> getProjectPastSnapshots() {
109 return projectPastSnapshots;
110 }
111
112 public boolean isFileVariationEnabled() {
113 return configuration.getBoolean("sonar.enableFileVariation", Boolean.FALSE);
114 }
115 }