package org.codehaus.mojo.versions;

/*
 * 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.artifact.Artifact;
import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

import javax.xml.stream.XMLStreamException;
import java.util.Collection;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Attempts to resolve unlocked snapshot dependency versions to the locked timestamp versions used in the build.
 * For example, an unlocked snapshot version like "1.0-SNAPSHOT" could be resolved to "1.0-20090128.202731-1".
 * If a timestamped snapshot is not available, then the version will remained unchanged.  This would be the case
 * if the dependency is only available in the local repository and not in a remote snapshot repository.
 *
 * @author Paul Gier
 * @goal lock-snapshots
 * @requiresProject true
 * @requiresDirectInvocation true
 * @since 1.0-alpha-3
 */
public class LockSnapshotsMojo
    extends AbstractVersionsUpdaterMojo
{

    // ------------------------------ FIELDS ------------------------------

    /**
     * A comma separated list of group ids to update.
     *
     * @parameter expression="${includeGroupIds}"
     * @since 1.0-alpha-3
     */
    private String includeGroupIds = null;

    /**
     * A comma separated list of artifact ids to update.
     *
     * @parameter expression="${includeArtifactIds}"
     * @since 1.0-alpha-3
     */
    private String includeArtifactIds = null;

    /**
     * A comma separated list of group ids to not update.
     *
     * @parameter expression="${excludeGroupIds}"
     * @since 1.0-alpha-3
     */
    private String excludeGroupIds = null;

    /**
     * A comma separated list of artifact ids to not update.
     *
     * @parameter expression="${excludeArtifactIds}"
     * @since 1.0-alpha-3
     */
    private String excludeArtifactIds = null;

    /**
     * Pattern to match a timestamped snapshot version. For example 1.0-20090128.202731-1
     */
    public final Pattern matchSnapshotRegex = Pattern.compile( "-SNAPSHOT" );

    // ------------------------------ METHODS --------------------------

    /**
     * @param pom the pom to update.
     * @throws MojoExecutionException when things go wrong
     * @throws MojoFailureException   when things go wrong in a very bad way
     * @throws XMLStreamException     when things go wrong with XML streaming
     * @see AbstractVersionsUpdaterMojo#update(ModifiedPomXMLEventReader)
     */
    protected void update( ModifiedPomXMLEventReader pom )
        throws MojoExecutionException, MojoFailureException, XMLStreamException
    {
        if ( getProject().getDependencyManagement() != null )
        {
            lockSnapshots( pom, getProject().getDependencyManagement().getDependencies() );
        }
        lockSnapshots( pom, getProject().getDependencies() );
    }

    private void lockSnapshots( ModifiedPomXMLEventReader pom, Collection dependencies )
        throws XMLStreamException, MojoExecutionException
    {
        Iterator iter = dependencies.iterator();

        while ( iter.hasNext() )
        {
            Dependency dep = (Dependency) iter.next();
            try
            {
                if ( !getHelper().isIncluded( getHelper().createDependencyArtifact( dep ), includeGroupIds,
                                              includeArtifactIds, excludeGroupIds, excludeArtifactIds ) )
                {
                    continue;
                }
            }
            catch ( InvalidVersionSpecificationException e )
            {
                throw new MojoExecutionException( e.getMessage(), e );
            }
            String version = dep.getVersion();
            Matcher versionMatcher = matchSnapshotRegex.matcher( version );
            if ( versionMatcher.find() && versionMatcher.end() == version.length() )
            {
                String lockedVersion = resolveSnapshotVersion( dep );
                if ( !version.equals( lockedVersion ) )
                {
                    if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version,
                                                         lockedVersion ) )
                    {
                        getLog().debug( "Version set to " + lockedVersion + " for dependnecy: " + dep );
                    }
                }
            }
        }
    }

    /**
     * Determine the timestamp version of the snapshot dependency used in the build.
     *
     * @param dep
     * @return The timestamp version if exists, otherwise the original snapshot dependency version is returned.
     */
    private String resolveSnapshotVersion( Dependency dep )
    {
        getLog().debug( "Resolving snapshot version for dependency: " + dep );

        String lockedVersion = dep.getVersion();

        Artifact depArtifact =
            artifactFactory.createArtifact( dep.getGroupId(), dep.getArtifactId(), dep.getVersion(), dep.getScope(),
                                            dep.getType() );
        try
        {
            resolver.resolve( depArtifact, getProject().getRemoteArtifactRepositories(), localRepository );
            Iterator metadataIter = depArtifact.getMetadataList().iterator();
            while ( metadataIter.hasNext() )
            {
                Object obj = metadataIter.next();

                if ( obj instanceof SnapshotArtifactRepositoryMetadata )
                {
                    SnapshotArtifactRepositoryMetadata metadata = (SnapshotArtifactRepositoryMetadata) obj;
                    String baseVersion = metadata.getBaseVersion();
                    baseVersion = removeSnapshotQualifier( baseVersion );

                    String timestamp = metadata.getMetadata().getVersioning().getSnapshot().getTimestamp();
                    int buildNumber = metadata.getMetadata().getVersioning().getSnapshot().getBuildNumber();

                    // Only change the version string if the timestamp is available.
                    if ( timestamp != null )
                    {
                        lockedVersion = baseVersion + "-" + timestamp + "-" + buildNumber;
                    }

                }
            }
        }
        catch ( Exception e )
        {
            getLog().error( e );
        }
        return lockedVersion;
    }

    /**
     * Remove the "-SNAPSHOT" qualifier from the version number.  If the given string does not contain
     * "-SNAPSHOT" then the original string is returned.
     *
     * @param version The snapshot version
     * @return The version without the "-SNAPSHOT".
     */
    private String removeSnapshotQualifier( String version )
    {
        int index = version.lastIndexOf( "-SNAPSHOT" );
        if ( index != -1 )
        {
            version = version.substring( 0, index );
        }
        return version;
    }
}

@@ -5468,18 +5468,18 @@
 r depend
-n
 e
+n
 cy: %22 + 

Fix typo.