final List<MetadataFacetModel> metadataFacetModels = new ArrayList<>( result.get().getCount() );
for ( Row<String, String, String> row : result.get() )
{
ColumnSlice<String, String> columnSlice = row.getColumnSlice();
MetadataFacetModel metadataFacetModel = new MetadataFacetModel();
metadataFacetModel.setFacetId( getStringValue( columnSlice, "facetId" ) );
metadataFacetModel.setName( getStringValue( columnSlice, "name" ) );
metadataFacetModel.setValue( getStringValue( columnSlice, "value" ) );
metadataFacetModel.setKey( getStringValue( columnSlice, "key" ) );
metadataFacetModel.setProjectVersion( getStringValue( columnSlice, "projectVersion" ) );
metadataFacetModels.add( metadataFacetModel );
}
// rebuild MetadataFacet for artifacts
for ( final ArtifactMetadata artifactMetadata : artifactMetadatas )
{
Iterable<MetadataFacetModel> metadataFacetModelIterable =
Iterables.filter( metadataFacetModels, new Predicate<MetadataFacetModel>()
{
@Override
public boolean apply( MetadataFacetModel metadataFacetModel )
{
if ( metadataFacetModel != null )
{
return StringUtils.equals( artifactMetadata.getVersion(),
metadataFacetModel.getProjectVersion() );
}
return false;
}
} );
Iterator<MetadataFacetModel> iterator = metadataFacetModelIterable.iterator();
Map<String, List<MetadataFacetModel>> metadataFacetValuesPerFacetId = new HashMap<>();
while ( iterator.hasNext() )
{
MetadataFacetModel metadataFacetModel = iterator.next();
List<MetadataFacetModel> values = metadataFacetValuesPerFacetId.get( metadataFacetModel.getName() );
if ( values == null )
{
values = new ArrayList<>();
metadataFacetValuesPerFacetId.put( metadataFacetModel.getFacetId(), values );
}
values.add( metadataFacetModel );
}
for ( Map.Entry<String, List<MetadataFacetModel>> entry : metadataFacetValuesPerFacetId.entrySet() )
{
MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( entry.getKey() );
if ( metadataFacetFactory != null )
{
List<MetadataFacetModel> facetModels = entry.getValue();
if ( !facetModels.isEmpty() )
{
MetadataFacet metadataFacet = metadataFacetFactory.createMetadataFacet();
Map<String, String> props = new HashMap<>( facetModels.size() );
for ( MetadataFacetModel metadataFacetModel : facetModels )
{
props.put( metadataFacetModel.getKey(), metadataFacetModel.getValue() );
}
metadataFacet.fromProperties( props );
artifactMetadata.addFacet( metadataFacet );
}
}