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.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 import org.sonar.batch.components.PastSnapshot;
034 import org.sonar.batch.components.PastSnapshotFinder;
035
036 import javax.persistence.Query;
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, PastSnapshotFinder pastSnapshotFinder) {
049 this.session = session;
050 this.project = project;
051 this.configuration = configuration;
052 initPastSnapshots(pastSnapshotFinder);
053 }
054
055 private void initPastSnapshots(PastSnapshotFinder pastSnapshotFinder) {
056 Snapshot projectSnapshot = buildProjectSnapshot();
057
058 projectPastSnapshots = Lists.newLinkedList();
059 if (projectSnapshot != null) {
060 for (int index = 1; index <= NUMBER_OF_VARIATION_SNAPSHOTS; index++) {
061 PastSnapshot pastSnapshot = pastSnapshotFinder.find(projectSnapshot, configuration, index);
062 if (pastSnapshot != null) {
063 log(pastSnapshot);
064 projectPastSnapshots.add(pastSnapshot);
065 }
066 }
067 }
068 }
069
070 private Snapshot buildProjectSnapshot() {
071 Query query = session.createNativeQuery("select p.id from projects p where p.kee=:resourceKey and p.qualifier<>:lib and p.enabled=:enabled");
072 query.setParameter("resourceKey", project.getKey());
073 query.setParameter("lib", Qualifiers.LIBRARY);
074 query.setParameter("enabled", Boolean.TRUE);
075
076 Snapshot snapshot = null;
077 Number projectId = session.getSingleResult(query, null);
078 if (projectId != null) {
079 snapshot = new Snapshot();
080 snapshot.setResourceId(projectId.intValue());
081 snapshot.setCreatedAt(project.getAnalysisDate());
082 }
083 return snapshot;
084 }
085
086 private void log(PastSnapshot pastSnapshot) {
087 String qualifier = pastSnapshot.getQualifier();
088 // hack to avoid too many logs when the views plugin is installed
089 if (StringUtils.equals(Qualifiers.VIEW, qualifier) || StringUtils.equals(Qualifiers.SUBVIEW, qualifier)) {
090 LoggerFactory.getLogger(getClass()).debug(pastSnapshot.toString());
091 } else {
092 Logs.INFO.info(pastSnapshot.toString());
093 }
094 }
095
096 public boolean skipTendencies() {
097 return configuration.getBoolean(CoreProperties.SKIP_TENDENCIES_PROPERTY, CoreProperties.SKIP_TENDENCIES_DEFAULT_VALUE);
098 }
099
100 public int getTendencyPeriodInDays() {
101 return configuration.getInt(CoreProperties.CORE_TENDENCY_DEPTH_PROPERTY, CoreProperties.CORE_TENDENCY_DEPTH_DEFAULT_VALUE);
102 }
103
104 public List<PastSnapshot> getProjectPastSnapshots() {
105 return projectPastSnapshots;
106 }
107
108 public boolean isFileVariationEnabled() {
109 return configuration.getBoolean("sonar.enableFileVariation", Boolean.FALSE);
110 }
111 }