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