package org.apache.maven.archiva.converter.legacy;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.maven.archiva.configuration.ManagedRepositoryConfiguration;
import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
import org.apache.maven.archiva.consumers.ConsumerException;
import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
import org.apache.maven.archiva.converter.artifact.ArtifactConversionException;
import org.apache.maven.archiva.converter.artifact.ArtifactConverter;
import org.apache.maven.archiva.model.ArtifactReference;
import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayout;
import org.apache.maven.archiva.repository.layout.LayoutException;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;

import java.util.ArrayList;
import java.util.List;

/**
 * LegacyConverterArtifactConsumer - convert artifacts as they are found
 * into the destination repository. 
 *
 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
 * @version $Id$
 * 
 * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
 *     role-hint="artifact-legacy-to-default-converter"
 *     instantiation-strategy="per-lookup"
 */
public class LegacyConverterArtifactConsumer
    extends AbstractMonitoredConsumer
    implements KnownRepositoryContentConsumer
{
    /**
     * @plexus.requirement role-hint="legacy-to-default"
     */
    private ArtifactConverter artifactConverter;

    /**
     * @plexus.requirement
     */
    private ArtifactFactory artifactFactory;

    /**
     * @plexus.requirement role-hint="legacy"
     */
    private BidirectionalRepositoryLayout bidirectionalLayout;

    private ArtifactRepository destinationRepository;

    private List includes;

    private List excludes;

    public LegacyConverterArtifactConsumer()
    {
        includes = new ArrayList();
        includes.add( "**/*.jar" );
        includes.add( "**/*.ear" );
        includes.add( "**/*.war" );
    }

    public void beginScan( ManagedRepositoryConfiguration repository )
        throws ConsumerException
    {

    }

    public void completeScan()
    {

    }

    public List getExcludes()
    {
        return excludes;
    }

    public List getIncludes()
    {
        return includes;
    }

    public void processFile( String path )
        throws ConsumerException
    {
        try
        {
            ArtifactReference reference = bidirectionalLayout.toArtifactReference( path );
            Artifact artifact = artifactFactory.createArtifact( reference.getGroupId(), reference.getArtifactId(),
                                                                reference.getVersion(), reference.getClassifier(),
                                                                reference.getType() );
            artifactConverter.convert( artifact, destinationRepository );
        }
        catch ( LayoutException e )
        {
            getLogger().warn( "Unable to convert artifact: " + path + " : " + e.getMessage(), e );
        }
        catch ( ArtifactConversionException e )
        {
            getLogger().warn( "Unable to convert artifact: " + path + " : " + e.getMessage(), e );
        }
    }

    public String getDescription()
    {
        return "Legacy Artifact to Default Artifact Converter";
    }

    public String getId()
    {
        return "artifact-legacy-to-default-converter";
    }

    public boolean isPermanent()
    {
        return false;
    }

    public void setExcludes( List excludes )
    {
        this.excludes = excludes;
    }

    public void setIncludes( List includes )
    {
        this.includes = includes;
    }

    public ArtifactRepository getDestinationRepository()
    {
        return destinationRepository;
    }

    public void setDestinationRepository( ArtifactRepository destinationRepository )
    {
        this.destinationRepository = destinationRepository;
    }
}

@@ -1389,28 +1389,99 @@
 ory.
-layout.Bidirectional
+ManagedRepositoryContent;%0Aimport org.apache.maven.archiva.repository.content.ManagedDefault
 Repo
@@ -1486,21 +1486,22 @@
 pository
-Layou
+Conten
 t;%0Aimpor
@@ -2532,129 +2532,64 @@
     
-/**%0A     * @plexus.requirement role-hint=%22legacy%22%0A     */%0A    private BidirectionalRepositoryLayout bidirectionalLayout;%0A
+private ManagedRepositoryContent managedRepository;%0A    
 %0A   
@@ -3004,25 +3004,156 @@
 ption%0A    %7B%0A
+        this.managedRepository = new ManagedDefaultRepositoryContent();%0A        this.managedRepository.setRepository( repository );
 %0A
-
     %7D%0A%0A    p
@@ -3478,27 +3478,25 @@
 e = 
-bidirectionalLayout
+managedRepository
 .toA

Correcting path resolution logic.