Skip to content

Commit

Permalink
Remove code to lookup runs in the source container when aligning data…
Browse files Browse the repository at this point in the history
…fileUrls. This should not be required anymore due to LabKey/targetedms#724.

Set filePathRoot on the copied expRun to be the target container's file root.
Log error if the data file path is unexpected, i.e. it does not contain "Run<runid>"
  • Loading branch information
vagisha committed May 25, 2023
1 parent 613d6a1 commit 5d7ab66
Showing 1 changed file with 32 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.labkey.api.admin.ImportException;
import org.labkey.api.admin.SubfolderWriter;
import org.labkey.api.data.Container;
import org.labkey.api.data.ContainerService;
import org.labkey.api.exp.api.ExpData;
import org.labkey.api.exp.api.ExpRun;
import org.labkey.api.exp.api.ExperimentService;
Expand All @@ -25,6 +24,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;

/**
* This importer does a file move instead of copy to the temp directory and creates a symlink in place of the original
Expand Down Expand Up @@ -88,86 +88,60 @@ public void process(@Nullable PipelineJob job, FolderImportContext ctx, VirtualF
log.info("Moving files and creating sym links in folder " + ctx.getContainer().getPath());
PanoramaPublicSymlinkManager.get().moveAndSymLinkDirectory(expJob.getUser(), expJob.getContainer(), sourceFiles, targetFiles, false, log);

String sourceContainerPath = Paths.get(expJob.getExportSourceContainer().getPath(), subProject).toString().replace(File.separator, "/");
Container resolvedSourceContainer = ContainerService.get().getForPath(sourceContainerPath);

if (null == resolvedSourceContainer)
{
throw new ImportException("Invalid source container found for aligning data file urls: " + sourceContainerPath);
}

alignDataFileUrls(expJob.getUser(), ctx.getContainer(), resolvedSourceContainer, log);
alignDataFileUrls(expJob.getUser(), ctx.getContainer(), log);
}
}

private void alignDataFileUrls(User user, Container targetContainer, Container sourceContainer, Logger log) throws BatchValidationException, ImportException
private void alignDataFileUrls(User user, Container targetContainer, Logger log) throws BatchValidationException, ImportException
{
log.info("Aligning data files urls in folder: " + targetContainer.getPath());

FileContentService fcs = FileContentService.get();
if (null == fcs)
return;

ExperimentService expService = ExperimentService.get();
List<? extends ExpRun> runs = expService.getExpRuns(targetContainer, null, null);
boolean errors = false;

// Get all source and target container runs
List<? extends ExpRun> sourceRuns = ExperimentService.get().getExpRuns(sourceContainer, null, null);
for (ExpRun run : ExperimentService.get().getExpRuns(targetContainer, null, null))
Path fileRootPath = fcs.getFileRootPath(targetContainer, FileContentService.ContentType.files);
if(fileRootPath == null || !Files.exists(fileRootPath))
{
// Find matching source run
ExpRun sourceRun = sourceRuns.stream().filter(r -> r.getName().equals(run.getName())).findFirst().orElse(null);
if (null == sourceRun)
{
log.error("Source run not found for run: " + run.getName());
errors = true;
continue;
}

Path targetRootPath = fcs.getFileRootPath(run.getContainer(), FileContentService.ContentType.files);
if(targetRootPath == null || !Files.exists(targetRootPath))
{
throw new ImportException("Target file root path for container " + run.getContainer().getPath() + " does not exist: " + targetRootPath);
}

// Get source path relative to file root and apply to target file root
String sourcePath = sourceRun.getFilePathRootPath().toString();
String relativeSource = sourcePath.substring(sourcePath.lastIndexOf(FileContentService.FILES_LINK) + FileContentService.FILES_LINK.length());
Path targetRunPath = Paths.get(targetRootPath.toString(), relativeSource);
throw new ImportException("File root path for container " + targetContainer.getPath() + " does not exist: " + fileRootPath);
}

// Update run file path
if (targetRunPath.toFile().exists())
{
run.setFilePathRoot(targetRunPath.toFile());
run.save(user);
log.debug("Updated copied run: " + run.getName() + " with file path: " + targetRunPath);
}
else
{
log.error("Run file path not found: " + targetRunPath);
errors = true;
}
for (ExpRun run : runs)
{
run.setFilePathRootPath(fileRootPath);
run.save(user);
log.debug("Setting filePathRoot on copied run: " + run.getName() + " to: " + fileRootPath);

// Update data file urls
for (ExpData data : run.getAllDataUsedByRun())
{
// Find matching source data
ExpData sourceData = sourceRun.getAllDataUsedByRun().stream().filter(d -> d.getName().equals(data.getName())).findFirst().orElse(null);
if (null != sourceData && null != sourceData.getDataFileUrl())
if (null != data.getRun() && data.getDataFileUrl().contains(FileContentService.FILES_LINK))
{
// Get source data path relative to file root and apply to target data file root
String sourceDataName = sourceData.getDataFileUrl().substring(sourceData.getDataFileUrl().lastIndexOf(FileContentService.FILES_LINK) + FileContentService.FILES_LINK.length());
Path targetDataPath = Paths.get(targetRootPath.toString(), sourceDataName);
String[] parts = Objects.requireNonNull(data.getFilePath()).toString().split("Run\\d+");

// Update data file url
if (targetDataPath.toFile().exists())
if (parts.length > 1)
{
data.setDataFileURI(targetDataPath.toUri());
data.save(user);
log.debug("Updated copied data: " + data.getName() + " with file path: " + targetDataPath);
String fileName = parts[1];
Path newDataPath = Paths.get(fileRootPath.toString(), fileName);

if (newDataPath.toFile().exists())
{
data.setDataFileURI(newDataPath.toUri());
data.save(user);
log.debug("Setting dataFileUri on copied data: " + data.getName() + " to: " + newDataPath);
}
else
{
log.error("Data file not found: " + newDataPath.toUri());
errors = true;
}
}
else
{
log.error("Data file url not found: " + sourceData.getDataFileUrl());
log.error("Unexpected data file path. Could not align dataFileUri. " + data.getFilePath().toString());
errors = true;
}
}
Expand Down

0 comments on commit 5d7ab66

Please sign in to comment.