001/*
002 * SonarQube
003 * Copyright (C) 2009-2016 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * This program 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 * This program 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 License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.resources;
021
022import java.util.ArrayList;
023import java.util.Date;
024import java.util.List;
025import javax.annotation.CheckForNull;
026import javax.annotation.Nullable;
027import org.apache.commons.lang.StringUtils;
028import org.apache.commons.lang.builder.ToStringBuilder;
029import org.sonar.api.CoreProperties;
030import org.sonar.api.batch.fs.InputModule;
031import org.sonar.api.component.Component;
032import org.sonar.api.config.Settings;
033
034/**
035 * @since 1.10
036 * @deprecated since 5.6 replaced by {@link InputModule}.
037 */
038@Deprecated
039public class Project extends Resource implements Component {
040
041  /**
042   * Internal use
043   */
044  public static final Language NONE_LANGUAGE = new AbstractLanguage("none", "None") {
045    @Override
046    public String[] getFileSuffixes() {
047      return new String[0];
048    }
049  };
050
051  static final String MAVEN_KEY_FORMAT = "%s:%s";
052  private static final String BRANCH_KEY_FORMAT = "%s:%s";
053
054  public static final String SCOPE = Scopes.PROJECT;
055
056  private String branch;
057  private String name;
058  private String description;
059  private Date analysisDate;
060  private String analysisVersion;
061  private Settings settings;
062
063  // For internal use
064  private java.io.File baseDir;
065
066  // modules tree
067  private Project parent;
068  private List<Project> modules = new ArrayList<>();
069
070  public Project(String key) {
071    setKey(key);
072    setEffectiveKey(key);
073  }
074
075  public Project(String key, String branch, String name) {
076    if (StringUtils.isNotBlank(branch)) {
077      setKey(String.format(BRANCH_KEY_FORMAT, key, branch));
078      this.name = String.format("%s %s", name, branch);
079    } else {
080      setKey(key);
081      this.name = name;
082    }
083    setEffectiveKey(getKey());
084    this.branch = branch;
085  }
086
087  public String getBranch() {
088    return branch;
089  }
090
091  /**
092   * For internal use only.
093   */
094  public Project setBranch(String branch) {
095    this.branch = branch;
096    return this;
097  }
098
099  @Override
100  public String getName() {
101    return name;
102  }
103
104  @Override
105  public String getLongName() {
106    return name;
107  }
108
109  @Override
110  public String getDescription() {
111    return description;
112  }
113
114  /**
115   * For internal use only.
116   */
117  public Project setName(String name) {
118    this.name = name;
119    return this;
120  }
121
122  @Override
123  public Language getLanguage() {
124    return null;
125  }
126
127  /**
128   * For internal use only.
129   */
130  public Project setDescription(String description) {
131    this.description = description;
132    return this;
133  }
134
135  /**
136   * @return whether the current project is root project
137   */
138  public boolean isRoot() {
139    return getParent() == null;
140  }
141
142  public Project getRoot() {
143    return parent == null ? this : parent.getRoot();
144  }
145
146  /**
147   * @return whether the current project is a module
148   */
149  public boolean isModule() {
150    return !isRoot();
151  }
152
153  /**
154   * For internal use only.
155   *
156   * @deprecated in 3.6. It's not possible to analyze a project before the latest known quality snapshot.
157   * See http://jira.sonarsource.com/browse/SONAR-4334
158   */
159  @Deprecated
160  public Project setLatestAnalysis(boolean b) {
161    if (!b) {
162      throw new UnsupportedOperationException("The analysis is always the latest one. " +
163        "Past analysis must be done in a chronological order.");
164    }
165    return this;
166  }
167
168  /**
169     * @return the language key or empty if no language is specified
170     * @deprecated since 4.2 use {@link org.sonar.api.batch.fs.FileSystem#languages()}
171     */
172  @Deprecated
173  public String getLanguageKey() {
174    if (settings == null) {
175      throw new IllegalStateException("Project is not yet initialized");
176    }
177    return StringUtils.defaultIfEmpty(settings.getString(CoreProperties.PROJECT_LANGUAGE_PROPERTY), "");
178  }
179
180  /**
181   * Internal use
182   */
183  public Project setSettings(Settings settings) {
184    this.settings = settings;
185    return this;
186  }
187
188  /**
189   * Internal use for backward compatibility. Settings should be retrieved as an IoC dependency.
190   * @deprecated since 5.0
191   */
192  @Deprecated
193  public Settings getSettings() {
194    return settings;
195  }
196
197  /**
198   * For internal use only.
199   */
200  public Project setAnalysisDate(Date analysisDate) {
201    this.analysisDate = analysisDate;
202    return this;
203  }
204
205  /**
206   * For internal use only.
207   */
208  public Project setAnalysisVersion(String analysisVersion) {
209    this.analysisVersion = analysisVersion;
210    return this;
211  }
212
213  /**
214   * @return the scope of the current object
215   */
216  @Override
217  public String getScope() {
218    return Scopes.PROJECT;
219  }
220
221  /**
222   * @return the qualifier of the current object
223   */
224  @Override
225  public String getQualifier() {
226    return isRoot() ? Qualifiers.PROJECT : Qualifiers.MODULE;
227  }
228
229  @Override
230  public boolean matchFilePattern(String antPattern) {
231    return false;
232  }
233
234  @CheckForNull
235  @Override
236  public Project getParent() {
237    return parent;
238  }
239
240  /**
241   * For internal use only.
242   */
243  public Project setParent(Project parent) {
244    this.parent = parent;
245    if (parent != null) {
246      parent.modules.add(this);
247    }
248    return this;
249  }
250
251  /**
252   * For internal use only.
253   */
254  public void removeFromParent() {
255    if (parent != null) {
256      parent.modules.remove(this);
257    }
258  }
259
260  /**
261   * @return the list of modules
262   */
263  public List<Project> getModules() {
264    return modules;
265  }
266
267  /**
268   * @return the current version of the project
269   */
270  public String getAnalysisVersion() {
271    return analysisVersion;
272  }
273
274  /**
275   * @return the analysis date, i.e. the date that will be used to store the snapshot
276   */
277  public Date getAnalysisDate() {
278    return analysisDate;
279  }
280
281  public static Project createFromMavenIds(String groupId, String artifactId) {
282    return createFromMavenIds(groupId, artifactId, null);
283  }
284
285  public static Project createFromMavenIds(String groupId, String artifactId, @Nullable String branch) {
286    return new Project(String.format(MAVEN_KEY_FORMAT, groupId, artifactId), branch, "");
287  }
288
289  @Override
290  public String toString() {
291    return new ToStringBuilder(this)
292      .append("id", getId())
293      .append("key", getKey())
294      .append("qualifier", getQualifier())
295      .toString();
296  }
297
298  @Override
299  public String key() {
300    return getKey();
301  }
302
303  @Override
304  public String name() {
305    return getName();
306  }
307
308  @Override
309  public String path() {
310    return getPath();
311  }
312
313  @Override
314  public String longName() {
315    return getLongName();
316  }
317
318  @Override
319  public String qualifier() {
320    return getQualifier();
321  }
322
323  // For internal use
324  public void setBaseDir(java.io.File baseDir) {
325    this.baseDir = baseDir;
326  }
327
328  java.io.File getBaseDir() {
329    return baseDir;
330  }
331}