package org.jboss.wolf.validator.impl.bom;

import static org.jboss.wolf.validator.internal.Utils.gav;
import static org.jboss.wolf.validator.internal.ValidatorSupport.listPomFiles;

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

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

import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.jboss.wolf.validator.Validator;
import org.jboss.wolf.validator.ValidatorContext;
import org.jboss.wolf.validator.internal.ValidatorSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Named
public class BomVersionPropertyValidator implements Validator {
    
    private static final Logger logger = LoggerFactory.getLogger(BomVersionPropertyValidator.class);
    
    @Inject @Named("bomVersionPropertyValidatorFilter")
    private IOFileFilter fileFilter;
    @Inject
    private BomFilter bomFilter;
    @Inject
    private ValidatorSupport validatorSupport;

    @Override
    public void validate(ValidatorContext ctx) {
        Collection<File> pomFiles = listPomFiles(ctx.getValidatedRepository(), fileFilter);
        for (File pomFile : pomFiles) {
            if (!ctx.getErrors(pomFile).isEmpty()) {
                logger.debug("skipping `{}`, because already contains exceptions", pomFile);
                continue;
            }
            
            Model model = validatorSupport.buildModel(pomFile).getRawModel();
            if (model.getPackaging().equals("pom")) {
                if (bomFilter.isBom(model)) {
                    validateBomDependenciesVersionProperty(ctx, pomFile, model);
                }
            }        
        }
    }

    private void validateBomDependenciesVersionProperty(ValidatorContext ctx, File pomFile, Model model) {
        String bomGav = gav(model);
        
        List<String> bomDependenciesWithoutVersionProperty = new ArrayList<String>();
        for (Dependency bomDependency : model.getDependencyManagement().getDependencies()) {
            if (!(bomDependency.getVersion() != null && bomDependency.getVersion().startsWith("${") && bomDependency.getVersion().endsWith("}"))) {
                String bomDependencyGav = gav(bomDependency);
                bomDependenciesWithoutVersionProperty.add(bomDependencyGav);
            }                        
        }
        
        if( !bomDependenciesWithoutVersionProperty.isEmpty() ) {
            ctx.addError(this, pomFile, new BomVersionPropertyException(bomGav, bomDependenciesWithoutVersionProperty.toArray(new String[]{})));
        }
    }

}
@@ -467,16 +467,76 @@
 .Model;%0A
+import org.apache.maven.model.building.ModelBuildingResult;%0A
 import o
@@ -1528,22 +1528,37 @@
    Model
- model
+BuildingResult result
  = valid
@@ -1592,41 +1592,150 @@
 ile)
-.getRawModel();%0A            if (m
+;%0A            Model rawModel = result.getRawModel();%0A            Model effectiveModel = result.getEffectiveModel();%0A            if (effectiveM
 odel
@@ -1803,17 +1803,26 @@
 r.isBom(
-m
+effectiveM
 odel)) %7B
@@ -1895,17 +1895,36 @@
 omFile, 
-m
+rawModel, effectiveM
 odel);%0A 
@@ -2077,17 +2077,42 @@
 , Model 
-m
+rawModel, Model effectiveM
 odel) %7B%0A
@@ -2139,17 +2139,26 @@
 v = gav(
-m
+effectiveM
 odel);%0A 
@@ -2291,17 +2291,20 @@
 dency : 
-m
+rawM
 odel.get

fix bom gav when version defined in parent