package org.fedoraproject.maven.model.building;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.building.DefaultModelBuilder;
import org.apache.maven.model.building.ModelBuilder;
import org.apache.maven.model.building.ModelBuildingException;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.model.building.ModelBuildingResult;
import org.apache.maven.model.validation.ModelValidator;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.sonatype.aether.spi.log.Logger;
import org.sonatype.aether.spi.log.NullLogger;

/**
 * @author <a href="mailto:cdewolf@redhat.com">Carlo de Wolf</a>
 */
@Component(role = ModelBuilder.class)
public class FedoraModelBuilder implements ModelBuilder {
    @Requirement
    private Logger logger = NullLogger.INSTANCE;

    @Requirement
    private DefaultModelBuilder delegate;

    @Requirement(hint = "default")
    private ModelValidator defaultModelValidator;

    @Override
    public ModelBuildingResult build(ModelBuildingRequest request) throws ModelBuildingException {
        // The JPP repository contains broken poms which do not properly specify dependency versions.
        // This is a hack to counter such problems.
        final boolean onePhase;
        if (request.isTwoPhaseBuilding()) {
            // just perform phase 1
            return delegate.build(request);
        }
        final ModelBuildingResult result = delegate.build(request);
        return fixUp(request, result);
    }

    @Override
    public ModelBuildingResult build(ModelBuildingRequest request, ModelBuildingResult result) throws ModelBuildingException {
        result = delegate.build(request, result);
        return fixUp(request, result);
    }

    protected ModelBuildingResult fixUp(ModelBuildingRequest request, ModelBuildingResult result) {
        if (!isFromJPP(request, result))
            return result;
        for (Dependency dependency : result.getEffectiveModel().getDependencies()) {
            if (dependency.getVersion() == null) {
                logger.warn("No version specified for " + dependency + " in " + request.getModelSource().getLocation() + ", defaulting to LATEST");
                dependency.setVersion(Artifact.LATEST_VERSION);
            }
        }
        Model resultModel = result.getEffectiveModel();

        DefaultModelProblemCollector problems = new DefaultModelProblemCollector( result.getProblems() );
        problems.setSource( resultModel );
        problems.setRootModel( resultModel );
        defaultModelValidator.validateEffectiveModel(resultModel,  request, problems);
        return result;
    }

    private boolean isFromJPP(ModelBuildingRequest request, ModelBuildingResult result) {
        // TODO: how to implement this?
        return false;
    }
}

@@ -46,51 +46,8 @@
 g;%0A%0A
-import org.apache.maven.artifact.Artifact;%0A
 impo
@@ -2154,24 +2154,137 @@
  == null) %7B%0A
+                // just to get us past a missing dependency validation error set it to %22%22 and cross your fingers%0A
             
@@ -2409,22 +2409,20 @@
 ting to 
-LATEST
+none
 %22);%0A    
@@ -2459,31 +2459,10 @@
 ion(
-Artifact.LATEST_VERSION
+%22%22
 );%0A 
@@ -3002,12 +3002,11 @@
 urn 
-fals
+tru
 e;%0A 

Make sure we actually do validate every pom