|
| 1 | +package org.labkey.pmr.etl; |
| 2 | + |
| 3 | +import org.apache.commons.io.FileUtils; |
| 4 | +import org.apache.commons.lang3.StringUtils; |
| 5 | +import org.apache.hc.client5.http.classic.methods.HttpPost; |
| 6 | +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; |
| 7 | +import org.apache.hc.core5.http.HttpResponse; |
| 8 | +import org.apache.hc.core5.http.HttpStatus; |
| 9 | +import org.apache.xmlbeans.XmlException; |
| 10 | +import org.jetbrains.annotations.NotNull; |
| 11 | +import org.labkey.api.collections.CaseInsensitiveHashMap; |
| 12 | +import org.labkey.api.data.Container; |
| 13 | +import org.labkey.api.data.ContainerManager; |
| 14 | +import org.labkey.api.di.DataIntegrationService; |
| 15 | +import org.labkey.api.di.TaskRefTask; |
| 16 | +import org.labkey.api.module.Module; |
| 17 | +import org.labkey.api.module.ModuleLoader; |
| 18 | +import org.labkey.api.module.ModuleProperty; |
| 19 | +import org.labkey.api.pipeline.PipeRoot; |
| 20 | +import org.labkey.api.pipeline.PipelineJob; |
| 21 | +import org.labkey.api.pipeline.PipelineJobException; |
| 22 | +import org.labkey.api.pipeline.PipelineService; |
| 23 | +import org.labkey.api.pipeline.RecordedActionSet; |
| 24 | +import org.labkey.api.writer.ContainerUser; |
| 25 | +import org.labkey.remoteapi.CommandException; |
| 26 | +import org.labkey.remoteapi.CommandResponse; |
| 27 | +import org.labkey.remoteapi.PostCommand; |
| 28 | + |
| 29 | +import java.io.File; |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.Collections; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Map; |
| 35 | + |
| 36 | +public class TriggerRemoteGeneticsImportStep implements TaskRefTask |
| 37 | +{ |
| 38 | + protected final Map<String, String> _settings = new CaseInsensitiveHashMap<>(); |
| 39 | + protected ContainerUser _containerUser; |
| 40 | + |
| 41 | + private enum Settings |
| 42 | + { |
| 43 | + remoteSource() |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public RecordedActionSet run(@NotNull PipelineJob job) throws PipelineJobException |
| 48 | + { |
| 49 | + // First find the last successful pipeline iteration: |
| 50 | + Module ehr = ModuleLoader.getInstance().getModule("ehr"); |
| 51 | + Module geneticsCore = ModuleLoader.getInstance().getModule("GeneticsCore"); |
| 52 | + |
| 53 | + ModuleProperty mp = ehr.getModuleProperties().get("EHRStudyContainer"); |
| 54 | + String ehrContainerPath = StringUtils.trimToNull(mp.getEffectiveValue(_containerUser.getContainer())); |
| 55 | + if (ehrContainerPath == null) |
| 56 | + { |
| 57 | + throw new PipelineJobException("EHRStudyContainer has not been set"); |
| 58 | + } |
| 59 | + |
| 60 | + Container ehrContainer = ContainerManager.getForPath(ehrContainerPath); |
| 61 | + if (ehrContainer == null) |
| 62 | + { |
| 63 | + throw new PipelineJobException("Invalid container: " + ehrContainerPath); |
| 64 | + } |
| 65 | + |
| 66 | + if (!_containerUser.getContainer().equals(ehrContainer)) |
| 67 | + { |
| 68 | + throw new PipelineJobException("This ETL can only be run from the EHRStudyContainer"); |
| 69 | + } |
| 70 | + |
| 71 | + ModuleProperty mp2 = geneticsCore.getModuleProperties().get("KinshipDataPath"); |
| 72 | + String pipeDirPath = StringUtils.trimToNull(mp2.getEffectiveValue(ehrContainer)); |
| 73 | + if (pipeDirPath == null) |
| 74 | + { |
| 75 | + throw new PipelineJobException("Must provide the filepath to import data using the KinshipDataPath module property"); |
| 76 | + } |
| 77 | + |
| 78 | + File targetPipelineDir = new File(pipeDirPath); |
| 79 | + if (!targetPipelineDir.exists()) |
| 80 | + { |
| 81 | + targetPipelineDir.mkdirs(); |
| 82 | + } |
| 83 | + |
| 84 | + // Then copy the file to the expected folder: |
| 85 | + PipeRoot pr = PipelineService.get().getPipelineRootSetting(ehrContainer); |
| 86 | + if (pr == null) |
| 87 | + { |
| 88 | + throw new PipelineJobException("Unable to find pipeline root for: " + ehrContainer); |
| 89 | + } |
| 90 | + |
| 91 | + File sourceDir = new File(pr.getRootPath(), "/kinship/EHR Kinship Calculation"); |
| 92 | + if (!sourceDir.exists()) |
| 93 | + { |
| 94 | + throw new PipelineJobException("Unable to find source pipeline dir: " + sourceDir.getPath()); |
| 95 | + } |
| 96 | + |
| 97 | + copyReplaceFile(sourceDir, targetPipelineDir, "kinship.txt"); |
| 98 | + copyReplaceFile(sourceDir, targetPipelineDir, "inbreeding.txt"); |
| 99 | + |
| 100 | + // Then ping the main server to import this file: |
| 101 | + DataIntegrationService.RemoteConnection rc = DataIntegrationService.get().getRemoteConnection(_settings.get(Settings.remoteSource.name()), _containerUser.getContainer(), job.getLogger()); |
| 102 | + if (rc == null) |
| 103 | + { |
| 104 | + throw new PipelineJobException("Unable to find remote connection: " + _settings.get(Settings.remoteSource.name())); |
| 105 | + } |
| 106 | + |
| 107 | + try |
| 108 | + { |
| 109 | + KinshipCommand command = new KinshipCommand(); |
| 110 | + command.execute(rc.connection, rc.remoteContainer); |
| 111 | + } |
| 112 | + catch (CommandException | IOException e) |
| 113 | + { |
| 114 | + throw new PipelineJobException(e); |
| 115 | + } |
| 116 | + |
| 117 | + return new RecordedActionSet(); |
| 118 | + } |
| 119 | + |
| 120 | + private static class KinshipCommand extends PostCommand<CommandResponse> |
| 121 | + { |
| 122 | + public KinshipCommand() |
| 123 | + { |
| 124 | + super("geneticscore", "importGeneticsData"); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private void copyReplaceFile(File sourceDir, File targetDir, String filename) throws PipelineJobException |
| 129 | + { |
| 130 | + File sourceFile = new File(sourceDir, filename); |
| 131 | + if (!sourceFile.exists()) |
| 132 | + { |
| 133 | + throw new PipelineJobException("File does not exist: " + sourceFile.getPath()); |
| 134 | + } |
| 135 | + |
| 136 | + File destFile = new File(targetDir, filename); |
| 137 | + if (destFile.exists()) |
| 138 | + { |
| 139 | + destFile.delete(); |
| 140 | + } |
| 141 | + |
| 142 | + try |
| 143 | + { |
| 144 | + FileUtils.copyFile(sourceFile, destFile); |
| 145 | + } |
| 146 | + catch (IOException e) |
| 147 | + { |
| 148 | + throw new PipelineJobException(e); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public List<String> getRequiredSettings() |
| 154 | + { |
| 155 | + return Collections.unmodifiableList(Arrays.asList(Settings.remoteSource.name())); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public void setSettings(Map<String, String> settings) throws XmlException |
| 160 | + { |
| 161 | + _settings.putAll(settings); |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public void setContainerUser(ContainerUser containerUser) |
| 166 | + { |
| 167 | + _containerUser = containerUser; |
| 168 | + } |
| 169 | +} |
0 commit comments