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;
021
022 import org.apache.commons.configuration.PropertiesConfiguration;
023 import org.apache.commons.lang.StringUtils;
024 import org.apache.maven.project.MavenProject;
025 import org.sonar.api.BatchComponent;
026 import org.sonar.api.CoreProperties;
027 import org.sonar.api.batch.bootstrap.ProjectDefinition;
028 import org.sonar.api.config.Settings;
029 import org.sonar.api.database.DatabaseSession;
030 import org.sonar.api.database.model.ResourceModel;
031 import org.sonar.api.database.model.Snapshot;
032 import org.sonar.api.resources.Project;
033 import org.sonar.api.utils.SonarException;
034
035 import java.util.Date;
036
037 public class ProjectConfigurator implements BatchComponent {
038
039 private DatabaseSession databaseSession;
040 private Settings settings;
041
042 public ProjectConfigurator(DatabaseSession databaseSession, Settings settings) {
043 this.databaseSession = databaseSession;
044 this.settings = settings;
045 }
046
047 public Project create(ProjectDefinition definition) {
048 Project project = new Project(definition.getKey(), loadProjectBranch(), definition.getName());
049
050 // For backward compatibility we must set POM and actual packaging
051 project.setDescription(StringUtils.defaultString(definition.getDescription()));
052 project.setPackaging("jar");
053
054 for (Object component : definition.getContainerExtensions()) {
055 if (component instanceof MavenProject) {
056 MavenProject pom = (MavenProject) component;
057 project.setPom(pom);
058 project.setPackaging(pom.getPackaging());
059 }
060 }
061 return project;
062 }
063
064 String loadProjectBranch() {
065 return settings.getString(CoreProperties.PROJECT_BRANCH_PROPERTY);
066 }
067
068 public ProjectConfigurator configure(Project project) {
069 Date analysisDate = loadAnalysisDate();
070 project
071 .setConfiguration(new PropertiesConfiguration()) // will be populated by ProjectSettings
072 .setAnalysisDate(analysisDate)
073 .setLatestAnalysis(isLatestAnalysis(project.getKey(), analysisDate))
074 .setAnalysisVersion(loadAnalysisVersion())
075 .setAnalysisType(loadAnalysisType());
076 return this;
077 }
078
079 boolean isLatestAnalysis(String projectKey, Date analysisDate) {
080 ResourceModel persistedProject = databaseSession.getSingleResult(ResourceModel.class, "key", projectKey, "enabled", true);
081 if (persistedProject != null) {
082 Snapshot lastSnapshot = databaseSession.getSingleResult(Snapshot.class, "resourceId", persistedProject.getId(), "last", true);
083 return lastSnapshot == null || lastSnapshot.getCreatedAt().before(analysisDate);
084 }
085 return true;
086 }
087
088 Date loadAnalysisDate() {
089 Date date;
090 try {
091 // sonar.projectDate may have been specified as a time
092 date = settings.getDateTime(CoreProperties.PROJECT_DATE_PROPERTY);
093 } catch (SonarException e) {
094 // this is probably just a date
095 date = settings.getDate(CoreProperties.PROJECT_DATE_PROPERTY);
096 }
097 if (date == null) {
098 date = new Date();
099 }
100 return date;
101 }
102
103 Project.AnalysisType loadAnalysisType() {
104 String value = settings.getString(CoreProperties.DYNAMIC_ANALYSIS_PROPERTY);
105 if (value == null) {
106 return Project.AnalysisType.DYNAMIC;
107 }
108 if ("true".equals(value)) {
109 return Project.AnalysisType.DYNAMIC;
110 }
111 if ("reuseReports".equals(value)) {
112 return Project.AnalysisType.REUSE_REPORTS;
113 }
114 return Project.AnalysisType.STATIC;
115 }
116
117 String loadAnalysisVersion() {
118 return settings.getString(CoreProperties.PROJECT_VERSION_PROPERTY);
119 }
120 }