001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info 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.List;
023import java.util.stream.Collectors;
024
025import javax.annotation.CheckForNull;
026
027import org.apache.commons.lang.StringUtils;
028import org.apache.commons.lang.builder.ToStringBuilder;
029import org.sonar.api.batch.bootstrap.ProjectDefinition;
030import org.sonar.api.batch.fs.InputModule;
031import org.sonar.api.component.Component;
032import org.sonar.api.scan.filesystem.PathResolver;
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  private final ProjectDefinition definition;
041
042  public Project(ProjectDefinition definition) {
043    this.definition = definition;
044    this.setKey(definition.getKey());
045    this.setEffectiveKey(definition.getKeyWithBranch());
046  }
047
048  public ProjectDefinition definition() {
049    return definition;
050  }
051
052  @Override
053  public String key() {
054    return definition.getKey();
055  }
056
057  @Override
058  public String path() {
059    ProjectDefinition parent = definition.getParent();
060    if (parent == null) {
061      return null;
062    }
063    return new PathResolver().relativePath(parent.getBaseDir(), definition.getBaseDir());
064  }
065
066  public String getBranch() {
067    return definition.getBranch();
068  }
069
070  @CheckForNull
071  public String getOriginalName() {
072    String name = definition.getOriginalName();
073    if (StringUtils.isNotEmpty(getBranch())) {
074      name = name + " " + getBranch();
075    }
076    return name;
077  }
078
079  java.io.File getBaseDir() {
080    return definition.getBaseDir();
081  }
082
083  @Override
084  public String name() {
085    String name = definition.getName();
086    if (StringUtils.isNotEmpty(getBranch())) {
087      name = name + " " + getBranch();
088    }
089    return name;
090  }
091
092  @Override
093  public String longName() {
094    return definition.getName();
095  }
096
097  @Override
098  public String qualifier() {
099    return getParent() == null ? Qualifiers.PROJECT : Qualifiers.MODULE;
100  }
101
102  @Override
103  public String getName() {
104    return name();
105  }
106
107  public boolean isRoot() {
108    return getParent() == null;
109  }
110
111  public Project getRoot() {
112    return getParent() == null ? this : getParent().getRoot();
113  }
114
115  /**
116   * @return whether the current project is a module
117   */
118  public boolean isModule() {
119    return !isRoot();
120  }
121
122  @Override
123  public String getLongName() {
124    return longName();
125  }
126
127  @Override
128  public String getDescription() {
129    return definition.getDescription();
130  }
131
132  /** 
133   * @deprecated since 4.2 use {@link org.sonar.api.batch.fs.FileSystem#languages()}
134   */
135  @Override
136  public Language getLanguage() {
137    throw new UnsupportedOperationException();
138  }
139
140  @Override
141  public String getScope() {
142    return Scopes.PROJECT;
143  }
144
145  @Override
146  public String getQualifier() {
147    return qualifier();
148  }
149
150  @Override
151  public Project getParent() {
152    ProjectDefinition parent = definition.getParent();
153    if (parent == null) {
154      return null;
155    }
156    return new Project(parent);
157  }
158
159  /**
160   * @return the list of modules
161   */
162  public List<Project> getModules() {
163    return definition.getSubProjects().stream()
164      .map(Project::new)
165      .collect(Collectors.toList());
166  }
167
168  @Override
169  public boolean matchFilePattern(String antPattern) {
170    return false;
171  }
172
173  @Override
174  public String toString() {
175    return new ToStringBuilder(this)
176      .append("id", getId())
177      .append("key", key())
178      .append("qualifier", getQualifier())
179      .toString();
180  }
181
182}