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 com.google.common.collect.Lists;
023 import org.apache.commons.configuration.Configuration;
024 import org.apache.commons.lang.StringUtils;
025 import org.slf4j.LoggerFactory;
026 import org.sonar.api.BatchExtension;
027 import org.sonar.api.CoreProperties;
028 import org.sonar.api.database.DatabaseSession;
029 import org.sonar.api.database.model.Snapshot;
030 import org.sonar.api.resources.Project;
031 import org.sonar.api.resources.Qualifiers;
032 import org.sonar.api.utils.Logs;
033
034 import javax.persistence.Query;
035
036 import java.util.Date;
037 import java.util.List;
038
039 public class TimeMachineConfiguration implements BatchExtension {
040
041 private static final int NUMBER_OF_VARIATION_SNAPSHOTS = 5;
042
043 private Project project;
044 private final Configuration configuration;
045 private List<PastSnapshot> projectPastSnapshots;
046 private DatabaseSession session;
047
048 public TimeMachineConfiguration(DatabaseSession session, Project project, Configuration configuration,
049 PastSnapshotFinder pastSnapshotFinder) {
050 this.session = session;
051 this.project = project;
052 this.configuration = configuration;
053 initPastSnapshots(pastSnapshotFinder);
054 }
055
056 private void initPastSnapshots(PastSnapshotFinder pastSnapshotFinder) {
057 Snapshot projectSnapshot = buildProjectSnapshot();
058
059 projectPastSnapshots = Lists.newLinkedList();
060 if (projectSnapshot != null) {
061 for (int index = 1; index <= NUMBER_OF_VARIATION_SNAPSHOTS; index++) {
062 PastSnapshot pastSnapshot = pastSnapshotFinder.find(projectSnapshot, configuration, index);
063 if (pastSnapshot != null) {
064 log(pastSnapshot);
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", project.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(project.getAnalysisDate());
084 snapshot.setBuildDate(new Date());
085 snapshot.setVersion(project.getAnalysisVersion());
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 }