/*
 * Copyright 2011 the original author or authors.
 *
 * 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.gradle.api.internal.artifacts.ivyservice.projectmodule;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import org.gradle.api.artifacts.ModuleVersionIdentifier;
import org.gradle.api.artifacts.component.ComponentIdentifier;
import org.gradle.api.artifacts.component.ProjectComponentIdentifier;
import org.gradle.api.internal.artifacts.DefaultModuleVersionIdentifier;
import org.gradle.api.internal.artifacts.Module;
import org.gradle.api.internal.artifacts.ivyservice.moduleconverter.ConfigurationComponentMetaDataBuilder;
import org.gradle.api.internal.project.ProjectInternal;
import org.gradle.api.internal.project.ProjectRegistry;
import org.gradle.internal.component.local.model.DefaultLocalComponentMetadata;
import org.gradle.internal.component.local.model.DefaultProjectComponentIdentifier;
import org.gradle.internal.component.local.model.LocalComponentArtifactMetadata;
import org.gradle.internal.component.local.model.LocalComponentMetadata;

public class DefaultProjectLocalComponentProvider implements ProjectLocalComponentProvider {
    private final ProjectRegistry<ProjectInternal> projectRegistry;
    private final ConfigurationComponentMetaDataBuilder metaDataBuilder;
    private final ListMultimap<ProjectComponentIdentifier, LocalComponentArtifactMetadata> registeredArtifacts = ArrayListMultimap.create();

    public DefaultProjectLocalComponentProvider(ProjectRegistry<ProjectInternal> projectRegistry, ConfigurationComponentMetaDataBuilder metaDataBuilder) {
        this.projectRegistry = projectRegistry;
        this.metaDataBuilder = metaDataBuilder;
    }

    public LocalComponentMetadata getComponent(ProjectComponentIdentifier projectIdentifier) {
        ProjectInternal project = projectRegistry.getProject(projectIdentifier.getProjectPath());
        if (project == null) {
            return null;
        }
        return getLocalComponentMetaData(project);
    }

    private LocalComponentMetadata getLocalComponentMetaData(ProjectInternal project) {
        Module module = project.getModule();
        ModuleVersionIdentifier moduleVersionIdentifier = DefaultModuleVersionIdentifier.newId(module);
        ComponentIdentifier componentIdentifier = new DefaultProjectComponentIdentifier(project.getPath());
        DefaultLocalComponentMetadata metaData = new DefaultLocalComponentMetadata(moduleVersionIdentifier, componentIdentifier, module.getStatus());
        metaDataBuilder.addConfigurations(metaData, project.getConfigurations());
        return metaData;
    }

    @Override
    public void registerAdditionalArtifact(ProjectComponentIdentifier project, LocalComponentArtifactMetadata artifact) {
        registeredArtifacts.put(project, artifact);
    }

    @Override
    public Iterable<LocalComponentArtifactMetadata> getAdditionalArtifacts(ProjectComponentIdentifier projectIdentifier) {
        if (registeredArtifacts.containsKey(projectIdentifier)) {
            return registeredArtifacts.get(projectIdentifier);
        }
        return null;
    }
}

@@ -2417,16 +2417,35 @@
 Project(
+getLocalIdentifier(
 projectI
@@ -2453,16 +2453,17 @@
 entifier
+)
 .getProj
@@ -3375,23 +3375,43 @@
 cts.put(
+getLocalIdentifier(
 project
+)
 , artifa
@@ -3571,158 +3571,925 @@
     
-if (registeredArtifacts.containsKey(projectIdentifier)) %7B%0A            return registeredArtifacts.get(projectIdentifier);%0A        %7D%0A        return null
+ProjectComponentIdentifier localIdentifier = getLocalIdentifier(projectIdentifier);%0A        if (registeredArtifacts.containsKey(localIdentifier)) %7B%0A            return registeredArtifacts.get(localIdentifier);%0A        %7D%0A        return null;%0A    %7D%0A%0A    private ProjectComponentIdentifier getLocalIdentifier(ProjectComponentIdentifier projectIdentifier) %7B%0A        // TODO:DAZ This is yet another place where we need a better id for project components in composite.%0A        if (projectIdentifier.getProjectPath().contains(%22::%22)) %7B%0A            String%5B%5D parts = projectIdentifier.getProjectPath().split(%22::%22, 2);%0A            String buildName = parts%5B0%5D;%0A            String rootProjectName = projectRegistry.getProject(%22:%22).getName();%0A            if (rootProjectName.equals(buildName)) %7B%0A                return DefaultProjectComponentIdentifier.newId(%22:%22 + parts%5B1%5D);%0A            %7D%0A        %7D%0A        return projectIdentifier
 ;%0A  

Return a local component meta-data for fully-qualified composite project id