package org.apache.maven;

/* ====================================================================
 *   Copyright 2001-2004 The Apache Software Foundation.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * ====================================================================
 */
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.maven.project.Dependency;
import org.apache.maven.project.Project;

import java.io.File;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;


/**
 * Retreive descriptions for project's dependencies.
 *
 * @author <a href="mailto:michal.maczka@dimatics.com">Michal Maczka</a>
 * @version $Id$
 */
public class DependencyDescriberBean
{
  //~ Instance fields ------------------------------------------------

  /** Dependencies list. */
  private List dependencies = new ArrayList();

  /** For debug output. */
  private Log log = LogFactory.getLog(DependencyDescriberBean.class);

  /** Project reference. */
  private Project project;

  //~ Methods --------------------------------------------------------

  /**
   * The list of described dependencies.
   *
   * @return list of described dependencies
   *
   * @see DescribedDependency
   */
  public List getDependencies()
  {
    return dependencies;
  }

  /**
   * Project provides context/session related information, e.g where
   * maven repo local is.
   *
   * @param projectValue project to scan
   */
  public void build(Project projectValue)
  {
    this.project = projectValue;

    for (Iterator iter = project.getDependencies().iterator();
        iter.hasNext();)
    {
      Dependency          dependency = (Dependency) iter.next();
      DescribedDependency describedDependency =
        new DescribedDependency(dependency);
      dependencies.add(describedDependency);

      Project resolvedProject = resolveProject(dependency);

      if (resolvedProject != null)
      {
        log.debug("POM URL:" + resolvedProject.getUrl());
        describedDependency.setUrl(resolvedProject.getUrl());
        describedDependency.setDescription(
          resolvedProject.getDescription());
      }
    }

    Collections.sort(dependencies);
  }

  /**
   * Returns the project for a given dependency.
   *
   * @param dependency The dependency for which the project is
   *        searched.
   *
   * @return The project associated to this dependency.
   */
  private Project resolveProject(Dependency dependency)
  {
    File projectFile = null;

    if ("pom".equals(dependency.getType()))
    {
      projectFile =
        new File(
          project.getContext().getMavenRepoLocal(),
          dependency.getArtifact());
    }
    else
    {
      projectFile =
        new File(
          project.getContext().getMavenRepoLocal(),
          dependency.getArtifactDirectory() + "/poms/"
          + dependency.getArtifactId() + "-"
          + dependency.getVersion() + ".pom");
    }

    try
    {
      if (projectFile.exists() && projectFile.canRead())
      {
        return MavenUtils.getProject(
          projectFile,
          null,
          false);
      }
      else
      {
        log.debug("Failed to read POM:" + projectFile);

        return null;
      }
    }
    catch (Exception e)
    {
      log.debug("Failed to read POM:" + projectFile);

      return null;
    }
  }
}
