/*******************************************************************************
 * Copyright (c) 2015 IBH SYSTEMS GmbH.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBH SYSTEMS GmbH - initial API and implementation
 *******************************************************************************/
package de.dentrassi.pm.maven.internal;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import de.dentrassi.pm.aspect.aggregate.AggregationContext;
import de.dentrassi.pm.aspect.aggregate.ChannelAggregator;
import de.dentrassi.pm.common.ArtifactInformation;
import de.dentrassi.pm.common.MetaKeys;
import de.dentrassi.pm.maven.ChannelData;
import de.dentrassi.pm.maven.MavenInformation;

public class MavenRepositoryChannelAggregator implements ChannelAggregator
{
    @Override
    public String getId ()
    {
        return MavenRepositoryAspectFactory.ID;
    }

    @Override
    public Map<String, String> aggregateMetaData ( final AggregationContext context ) throws Exception
    {
        final Map<String, String> result = new HashMap<> ();

        final ChannelData cs = new ChannelData ();

        for ( final ArtifactInformation art : context.getArtifacts () )
        {
            final MavenInformation info = makeInfo ( art );
            if ( info != null )
            {
                // add
                cs.add ( info, art );
            }
        }

        String json = cs.toJson ();
        result.put ( "channel", json );

        json = cs.toString ();
        context.createCacheEntry ( "channel", "channel.json", "application/json", new ByteArrayInputStream ( json.getBytes ( StandardCharsets.UTF_8 ) ) );

        return result;
    }

    private MavenInformation makeInfo ( final ArtifactInformation art )
    {
        final MavenInformation info = new MavenInformation ();
        try
        {
            MetaKeys.bind ( info, art.getMetaData () );
        }
        catch ( final Exception e )
        {
            return null;
        }

        if ( info.getGroupId () != null & info.getArtifactId () != null && info.getVersion () != null )
        {
            return info;
        }
        return null;
    }

}

@@ -1286,24 +1286,99 @@
 ption%0A    %7B%0A
+        final Map%3CString, ArtifactInformation%3E map = makeMap ( context );%0A%0A
         fina
@@ -1613,24 +1613,29 @@
 keInfo ( art
+, map
  );%0A        
@@ -2048,24 +2048,364 @@
 ult;%0A    %7D%0A%0A
+    private Map%3CString, ArtifactInformation%3E makeMap ( final AggregationContext context )%0A    %7B%0A        final Map%3CString, ArtifactInformation%3E result = new HashMap%3C%3E ();%0A%0A        for ( final ArtifactInformation art : context.getArtifacts () )%0A        %7B%0A            result.put ( art.getId (), art );%0A        %7D%0A%0A        return result;%0A    %7D%0A%0A
     private 
@@ -2461,16 +2461,60 @@
 tion art
+, final Map%3CString, ArtifactInformation%3E map
  )%0A    %7B
@@ -2573,24 +2573,25 @@
 rmation ();%0A
+%0A
         try%0A
@@ -2878,30 +2878,1555 @@
     
-return info;%0A        %7D
+// found direct meta data%0A            return info;%0A        %7D%0A%0A        final ArtifactInformation pomArt = findChildPom ( art, map );%0A        if ( pomArt != null )%0A        %7B%0A            try%0A            %7B%0A                MetaKeys.bind ( info, pomArt.getMetaData () );%0A            %7D%0A            catch ( final Exception e )%0A            %7B%0A                return null;%0A            %7D%0A%0A            if ( info.getGroupId () != null & info.getArtifactId () != null && info.getVersion () != null )%0A            %7B%0A                // found pom meta data%0A                final String ext = makeExtension ( art.getName () );%0A                if ( ext != null )%0A                %7B%0A                    info.setExtension ( ext );%0A                    return info;%0A                %7D%0A            %7D%0A        %7D%0A%0A        return null;%0A    %7D%0A%0A    private static String makeExtension ( final String name )%0A    %7B%0A        final int idx = name.lastIndexOf ( '.' );%0A        if ( idx %3C 0 )%0A        %7B%0A            return null;%0A        %7D%0A        return name.substring ( idx + 1 );%0A    %7D%0A%0A    private ArtifactInformation findChildPom ( final ArtifactInformation art, final Map%3CString, ArtifactInformation%3E map )%0A    %7B%0A        for ( final String childId : art.getChildIds () )%0A        %7B%0A            final ArtifactInformation child = map.get ( childId );%0A            if ( child == null )%0A            %7B%0A                continue;%0A            %7D%0A            if ( !child.getName ().equals ( %22pom.xml%22 ) )%0A            %7B%0A                continue;%0A            %7D%0A%0A            return child;%0A        %7D%0A
 %0A   

create a maven directory structure for extracted POM files, #71