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