|
| 1 | +package org.labkey.GeneticsCore.etl; |
| 2 | + |
| 3 | +import org.apache.commons.lang3.StringUtils; |
| 4 | +import org.apache.xmlbeans.XmlException; |
| 5 | +import org.jetbrains.annotations.NotNull; |
| 6 | +import org.labkey.GeneticsCore.GeneticsCoreModule; |
| 7 | +import org.labkey.api.data.Container; |
| 8 | +import org.labkey.api.data.ContainerManager; |
| 9 | +import org.labkey.api.di.TaskRefTask; |
| 10 | +import org.labkey.api.ehr.EHRService; |
| 11 | +import org.labkey.api.module.Module; |
| 12 | +import org.labkey.api.module.ModuleLoader; |
| 13 | +import org.labkey.api.module.ModuleProperty; |
| 14 | +import org.labkey.api.pipeline.PipelineJob; |
| 15 | +import org.labkey.api.pipeline.PipelineJobException; |
| 16 | +import org.labkey.api.pipeline.RecordedActionSet; |
| 17 | +import org.labkey.api.security.permissions.UpdatePermission; |
| 18 | +import org.labkey.api.view.UnauthorizedException; |
| 19 | +import org.labkey.api.writer.ContainerUser; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +public class ImportGeneticsCalculationsStep implements TaskRefTask |
| 27 | +{ |
| 28 | + protected ContainerUser _containerUser; |
| 29 | + |
| 30 | + @Override |
| 31 | + public RecordedActionSet run(@NotNull PipelineJob job) throws PipelineJobException |
| 32 | + { |
| 33 | + Module ehr = ModuleLoader.getInstance().getModule("ehr"); |
| 34 | + Module geneticsCore = ModuleLoader.getInstance().getModule(GeneticsCoreModule.class); |
| 35 | + |
| 36 | + ModuleProperty mp = ehr.getModuleProperties().get("EHRStudyContainer"); |
| 37 | + String ehrContainerPath = StringUtils.trimToNull(mp.getEffectiveValue(_containerUser.getContainer())); |
| 38 | + if (ehrContainerPath == null) |
| 39 | + { |
| 40 | + throw new PipelineJobException("EHRStudyContainer has not been set"); |
| 41 | + } |
| 42 | + |
| 43 | + Container ehrContainer = ContainerManager.getForPath(ehrContainerPath); |
| 44 | + if (ehrContainer == null) |
| 45 | + { |
| 46 | + throw new PipelineJobException("Invalid container: " + ehrContainerPath); |
| 47 | + } |
| 48 | + |
| 49 | + if (!_containerUser.getContainer().equals(ehrContainer)) |
| 50 | + { |
| 51 | + throw new PipelineJobException("This ETL can only be run from the EHRStudyContainer"); |
| 52 | + } |
| 53 | + |
| 54 | + // Downstream import events will get additional permissions checks |
| 55 | + if (!ehrContainer.hasPermission(_containerUser.getUser(), UpdatePermission.class)) |
| 56 | + { |
| 57 | + throw new UnauthorizedException(); |
| 58 | + } |
| 59 | + |
| 60 | + ModuleProperty mp2 = geneticsCore.getModuleProperties().get("KinshipDataPath"); |
| 61 | + String pipeDirPath = StringUtils.trimToNull(mp2.getEffectiveValue(ehrContainer)); |
| 62 | + if (pipeDirPath == null) |
| 63 | + { |
| 64 | + throw new PipelineJobException("Must provide the filepath to import data using the KinshipDataPath module property"); |
| 65 | + } |
| 66 | + |
| 67 | + File pipeDir = new File(pipeDirPath); |
| 68 | + if (!pipeDir.exists()) |
| 69 | + { |
| 70 | + throw new PipelineJobException("Path does not exist: " + pipeDir.getPath()); |
| 71 | + } |
| 72 | + |
| 73 | + File kinship = new File(pipeDir, "kinship.txt"); |
| 74 | + if (!kinship.exists()) |
| 75 | + { |
| 76 | + throw new PipelineJobException("File does not exist: " + kinship.getPath()); |
| 77 | + } |
| 78 | + |
| 79 | + File inbreeding = new File(pipeDir, "inbreeding.txt"); |
| 80 | + if (!inbreeding.exists()) |
| 81 | + { |
| 82 | + throw new PipelineJobException("File does not exist: " + inbreeding.getPath()); |
| 83 | + } |
| 84 | + |
| 85 | + EHRService.get().standaloneProcessKinshipAndInbreeding(ehrContainer, _containerUser.getUser(), pipeDir, job.getLogger()); |
| 86 | + |
| 87 | + return new RecordedActionSet(); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public List<String> getRequiredSettings() |
| 92 | + { |
| 93 | + return Collections.emptyList(); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void setSettings(Map<String, String> settings) throws XmlException |
| 98 | + { |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void setContainerUser(ContainerUser containerUser) |
| 104 | + { |
| 105 | + _containerUser = containerUser; |
| 106 | + } |
| 107 | +} |
0 commit comments