package org.apache.maven.artifact.transform;

/*
 * Copyright 2001-2005 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.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.AbstractVersionArtifactMetadata;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.metadata.SnapshotArtifactMetadata;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.wagon.ResourceDoesNotExistException;

import java.util.List;
import java.util.regex.Matcher;

/**
 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 * @author <a href="mailto:mmaczka@interia.pl">Michal Maczka</a>
 * @version $Id: SnapshotTransformation.java,v 1.1 2005/03/03 15:37:25
 *          jvanzyl Exp $
 */
public class SnapshotTransformation
    extends AbstractVersionTransformation
{
    public static final String SNAPSHOT_VERSION = "SNAPSHOT";

    public void transformForResolve( Artifact artifact, List remoteRepositories, ArtifactRepository localRepository )
        throws ArtifactMetadataRetrievalException
    {
        Matcher m = SnapshotArtifactMetadata.VERSION_FILE_PATTERN.matcher( artifact.getBaseVersion() );
        if ( m.matches() )
        {
            // This corrects the base version, but ensure it is not resolved again
            artifact.setBaseVersion( m.group( 1 ) + "-" + SNAPSHOT_VERSION );
        }
        else if ( isSnapshot( artifact ) )
        {
            String version = resolveVersion( artifact, localRepository, remoteRepositories );
            artifact.updateVersion( version, localRepository );
        }
    }

    public void transformForInstall( Artifact artifact, ArtifactRepository localRepository )
        throws ArtifactMetadataRetrievalException
    {
        Matcher m = SnapshotArtifactMetadata.VERSION_FILE_PATTERN.matcher( artifact.getBaseVersion() );
        if ( m.matches() )
        {
            artifact.setBaseVersion( m.group( 1 ) + "-" + SNAPSHOT_VERSION );
        }
        else if ( isSnapshot( artifact ) )
        {
            SnapshotArtifactMetadata metadata = new SnapshotArtifactMetadata( artifact );
            metadata.storeInLocalRepository( localRepository );
        }
    }

    public void transformForDeployment( Artifact artifact, ArtifactRepository remoteRepository )
        throws ArtifactMetadataRetrievalException
    {
        Matcher m = SnapshotArtifactMetadata.VERSION_FILE_PATTERN.matcher( artifact.getBaseVersion() );
        if ( m.matches() )
        {
            // This corrects the base version, but ensure it is not updated again
            artifact.setBaseVersion( m.group( 1 ) + "-" + SNAPSHOT_VERSION );
        }
        else if ( isSnapshot( artifact ) )
        {
            try
            {
                SnapshotArtifactMetadata metadata;
            
                metadata = (SnapshotArtifactMetadata) retrieveFromRemoteRepository( artifact, remoteRepository, null );

                metadata.update();

                artifact.setVersion( metadata.constructVersion() );

                artifact.addMetadata( metadata );
            }
            catch ( ResourceDoesNotExistException e )
            {
                // ignore. We'll be creating this metadata if it doesn't exist...
            }
        }
    }

    private static boolean isSnapshot( Artifact artifact )
    {
        return artifact.getVersion().endsWith( SNAPSHOT_VERSION );
    }

    protected AbstractVersionArtifactMetadata createMetadata( Artifact artifact )
    {
        return new SnapshotArtifactMetadata( artifact );
    }

}
@@ -3319,42 +3319,8 @@
   %7B%0A
-            try%0A            %7B%0A    
     
@@ -3362,16 +3362,17 @@
 tadata;%0A
+%0A
         
@@ -3375,16 +3375,33 @@
         
+try%0A            %7B
 %0A       
@@ -3516,164 +3516,8 @@
 l );
-%0A%0A                metadata.update();%0A%0A                artifact.setVersion( metadata.constructVersion() );%0A%0A                artifact.addMetadata( metadata );
 %0A   
@@ -3681,33 +3681,259 @@
 ...%0A            
-%7D
+    metadata = (SnapshotArtifactMetadata) createMetadata( artifact );%0A            %7D%0A%0A            metadata.update();%0A%0A            artifact.setVersion( metadata.constructVersion() );%0A%0A            artifact.addMetadata( metadata );
 %0A        %7D%0A    %7D

correct behaviour