// NOTE: FolderItemTab.ParentFolder_Id must have been joined with FolderTab to be able to do this!
//
FolderId parentFolderId = getFolderId(keyValues.get(FolderItem.PARENT_FOLDER_ID));
String stName = keyValues.get(Folder.FOLDER_NAME);
Folder folder = new Folder(folderId, stName, entityType);
m_folderIdFolderMap.put(folderId, folder);
// Assign to InterchangeProject if possible.
IntegrationProject project = null;
String sProjectId = keyValues.getString(IntegrationProject.PROPERTY_INTERCHANGE_PROJECT_ID);
if ( sProjectId != null ) {
IntegrationProjectId projectId = (IntegrationProjectId)EntityType.Project.getIntegrationId(sProjectId);
project = m_integrationProjectMap.get(projectId);
}
if( parentFolderId == null ) {
RootFolder rootFolder = getRootFolder(entityType, project);
if ( rootFolder == null ) {
System.out.println("Failed to find RootFolder for Folder " + folder.getName());
}
else
rootFolder.addSubFolder(folder);
}
else {
Folder parentFolder = m_folderIdFolderMap.get(parentFolderId);
if ( parentFolder == null )
orphanFolders.add(new OrphanFolder(folder, parentFolderId));
else
parentFolder.addSubFolder(folder);
}
}
// Handle all the orphan folders.
int nOrphanedFolders = orphanFolders.size();
while ( nOrphanedFolders > 0 ) {
int currentNumber = nOrphanedFolders;
for( int iFolder = 0; iFolder < nOrphanedFolders; /*iFolder updated in the loop*/) {
Folder orphanFolder = orphanFolders.get(iFolder).getFolder();
FolderId parentFolderId = orphanFolders.get(iFolder).getParentFolderId();
Folder parentFolder = m_folderIdFolderMap.get(parentFolderId);
if ( parentFolder != null ) {
parentFolder.addSubFolder(orphanFolder);
// Remove the current folder from the list of orphaned folders and continue iterating from the same index.
orphanFolders.remove(iFolder);
nOrphanedFolders = orphanFolders.size();
}