/*
 * Copyright 2011 Red Hat inc. and third party contributors as noted 
 * by the author tags.
 * 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 com.redhat.ceylon.cmr.impl;

import com.redhat.ceylon.cmr.api.*;

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

/**
 * Abstract, use Jandex to read off Module info.
 *
 * @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a>
 */
public abstract class AbstractCeylonArtifactResult extends AbstractArtifactResult {

    private RepositoryManager manager;

    protected AbstractCeylonArtifactResult(RepositoryManager manager, String name, String version) {
        super(name, version);
        this.manager = manager;
    }

    public ArtifactResultType type() {
        return ArtifactResultType.CEYLON;
    }

    public List<ArtifactResult> dependencies() throws RepositoryException {
        List<ModuleInfo> infos = BytecodeUtils.readModuleInformation(name(), artifact());
        if (infos.isEmpty())
            return Collections.emptyList();

        final List<ArtifactResult> results = new ArrayList<ArtifactResult>();
        for (ModuleInfo mi : infos) {
            final ArtifactContext context = new ArtifactContext(mi.getName(), mi.getVersion());
            context.setThrowErrorIfMissing(mi.isOptional() == false);
            final ArtifactResult result = manager.getArtifactResult(context);
            if (result != null)
                results.add(result);
        }
        return results;
    }

}


@@ -723,16 +723,37 @@
 api.*;%0A%0A
+import java.io.File;%0A
 import j
@@ -1687,24 +1687,640 @@
  : infos) %7B%0A
+            results.add(new LazyArtifactResult(mi.getName(), mi.getVersion(), mi.isOptional() ? ImportType.OPTIONAL : ImportType.UNDEFINED));%0A        %7D%0A        return results;%0A    %7D%0A%0A    private class LazyArtifactResult extends AbstractArtifactResult %7B%0A        private ArtifactResult delegate;%0A        private final ImportType importType;%0A%0A        private LazyArtifactResult(String name, String version, ImportType importType) %7B%0A            super(name, version);%0A            this.importType = importType;%0A        %7D%0A%0A        private synchronized ArtifactResult getDelegate() %7B%0A            if (delegate == null) %7B%0A    
             
@@ -2371,37 +2371,25 @@
 Context(
-mi.getName(), mi.getV
+name(), v
 ersion()
@@ -2387,24 +2387,28 @@
 version());%0A
+    
             
@@ -2442,74 +2442,70 @@
 ing(
-mi.isOptional() == false);%0A            final ArtifactResult result
+importType() != ImportType.OPTIONAL);%0A                delegate
  = m
@@ -2555,62 +2555,348 @@
     
-if (result != null)%0A                results.add(result
+%7D%0A            return delegate;%0A        %7D%0A%0A        @Override%0A        public ImportType importType() %7B%0A            return importType;%0A        %7D%0A%0A        public ArtifactResultType type() %7B%0A            return getDelegate().type();%0A        %7D%0A%0A        public File artifact() throws RepositoryException %7B%0A            return getDelegate().artifact(
 );%0A 
@@ -2900,32 +2900,33 @@
 ;%0A        %7D%0A
+%0A
         
 return resul
@@ -2913,34 +2913,148 @@
         
-return results
+public List%3CArtifactResult%3E dependencies() throws RepositoryException %7B%0A            return getDelegate().dependencies()
 ;%0A    
+    %7D%0A    
 %7D
-%0A
 %0A%7D%0A%0A

Add lazy artifact result; only name, version, import.