/*-
 * Copyright (c) 2012-2014 Red Hat, Inc.
 *
 * 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.
 */
package org.fedoraproject.xmvn.connector.aether;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.repository.WorkspaceReader;
import org.eclipse.aether.repository.WorkspaceRepository;

import org.fedoraproject.xmvn.resolver.ResolutionRequest;
import org.fedoraproject.xmvn.resolver.ResolutionResult;
import org.fedoraproject.xmvn.resolver.Resolver;
import org.fedoraproject.xmvn.utils.ArtifactUtils;

/**
 * @author Mikolaj Izdebski
 */
@Named( "ide" )
@Singleton
public class XMvnWorkspaceReader
    implements WorkspaceReader
{
    private final Resolver resolver;

    private static final WorkspaceRepository repository = new WorkspaceRepository();

    private final List<ResolutionListener> listeners = new ArrayList<>();

    @Inject
    public XMvnWorkspaceReader( Resolver resolver )
    {
        this.resolver = resolver;
    }

    public void addResolutionListener( ResolutionListener listener )
    {
        listeners.add( listener );
    }

    @Override
    public File findArtifact( Artifact artifact )
    {
        ResolutionRequest request = new ResolutionRequest( artifact );

        for ( ResolutionListener listener : listeners )
            listener.resolutionRequested( request );

        ResolutionResult result = resolver.resolve( request );

        for ( ResolutionListener listener : listeners )
            listener.resolutionCompleted( request, result );

        return result.getArtifactFile();
    }

    @Override
    public List<String> findVersions( Artifact artifact )
    {
        return Collections.singletonList( ArtifactUtils.DEFAULT_VERSION );
    }

    @Override
    public WorkspaceRepository getRepository()
    {
        return repository;
    }
}

@@ -1778,46 +1778,40 @@
     
-@Override%0A    public File findArtifact
+private ResolutionResult resolve
 ( Ar
@@ -2223,184 +2223,604 @@
 sult
-.getArtifactFile();%0A    %7D%0A%0A    @Override%0A    public List%3CString%3E findVersions( Artifact artifact )%0A    %7B%0A        return Collections.singletonList( ArtifactUtils.DEFAULT_VERSION
+;%0A    %7D%0A%0A    @Override%0A    public File findArtifact( Artifact artifact )%0A    %7B%0A        ResolutionResult result = resolve( artifact );%0A%0A        return result.getArtifactFile();%0A    %7D%0A%0A    @Override%0A    public List%3CString%3E findVersions( Artifact artifact )%0A    %7B%0A        ResolutionResult result = resolve( artifact );%0A%0A        if ( result.getArtifactFile() == null )%0A            return Collections.emptyList();%0A%0A        String version = result.getCompatVersion();%0A        if ( version == null )%0A            version = ArtifactUtils.DEFAULT_VERSION;%0A%0A        return Collections.singletonList( version
  );%0A

Improve XMvnWorkspaceReader.findVersions()