String rootPartialGroupId = StringUtils.strip( root.getPath().replaceAll( "/", "." ), "." );
folders.put( Type.G + ":" + rootPartialGroupId, root );
IteratorSearchResponse artifacts = getArtifacts( root, request );
try
{
for ( ArtifactInfo ai : artifacts )
{
String versionKey = Type.V + ":" + ai.artifactId + ":" + ai.version;
TreeNode versionResource = folders.get( versionKey );
if ( versionResource == null )
{
String artifactKey = Type.A + ":" + ai.artifactId;
TreeNode artifactResource = folders.get( artifactKey );
if ( artifactResource == null )
{
TreeNode groupParentResource = root;
TreeNode groupResource = root;
// here comes the twist: we have to search for parent G node
String partialGroupId = null;
String[] groupIdElems = ai.groupId.split( "\\." );
for ( String groupIdElem : groupIdElems )
{
if ( partialGroupId == null )
{
partialGroupId = groupIdElem;
}
else
{
partialGroupId = partialGroupId + "." + groupIdElem;
}
String groupKey = Type.G + ":" + partialGroupId;
groupResource = folders.get( groupKey );
// it needs to be created only if not found (is null) and is _below_ groupParentResource
if ( groupResource == null
&& groupParentResource.getPath().length() < getPathForAi( ai, MAVEN.GROUP_ID ).length() )
{
String gNodeName =
partialGroupId.lastIndexOf( '.' ) > -1 ? partialGroupId.substring(
partialGroupId.lastIndexOf( '.' ) + 1, partialGroupId.length() )
: partialGroupId;
groupResource =
request.getFactory().createGNode( this, request,
"/" + partialGroupId.replaceAll( "\\.", "/" ) + "/", gNodeName );
groupParentResource.getChildren().add( groupResource );
folders.put( groupKey, groupResource );
groupParentResource = groupResource;
}
else if ( groupResource != null )
{
// we found it as already existing, break if this is the node we want
if ( groupResource.getPath().equals( getPathForAi( ai, MAVEN.GROUP_ID ) ) )
{
break;
}
groupParentResource = groupResource;
}
}
artifactResource =
request.getFactory().createANode( this, request, ai, getPathForAi( ai, MAVEN.ARTIFACT_ID ) );
groupParentResource.getChildren().add( artifactResource );
folders.put( artifactKey, artifactResource );
}
versionResource =
request.getFactory().createVNode( this, request, ai, getPathForAi( ai, MAVEN.VERSION ) );
artifactResource.getChildren().add( versionResource );
folders.put( versionKey, versionResource );
}
String nodePath = getPathForAi( ai, null );
versionResource.getChildren().add(
request.getFactory().createArtifactNode( this, request, ai, nodePath ) );
}
}
finally
{
artifacts.close();
}
if ( !request.hasFieldHints() )
{
Set<String> groups = getGroups( path, allGroups );