Search in sources :

Example 1 with IndexIsoptopesEnum

use of org.cirdles.squid.constants.Squid3Constants.IndexIsoptopesEnum in project Squid by CIRDLES.

the class SquidReportingService method generateReports.

public Path generateReports(String myProjectName, String myFileName, InputStream prawnFile, InputStream taskFile, boolean useSBM, boolean userLinFits, String refMatFilter, String concRefMatFilter, String preferredIndexIsotopeName) throws IOException, SquidException {
    IndexIsoptopesEnum preferredIndexIsotope = IndexIsoptopesEnum.valueOf(preferredIndexIsotopeName);
    // Posix attributes added to support web service on Linux - ignoring windows for now
    Set<PosixFilePermission> perms = EnumSet.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, GROUP_READ);
    // detect if prawnfile is zipped
    boolean prawnIsZip = false;
    String fileName = "";
    if (myFileName == null) {
        fileName = DEFAULT_PRAWNFILE_NAME;
    } else if (myFileName.toLowerCase().endsWith(".zip")) {
        fileName = FilenameUtils.removeExtension(myFileName);
        prawnIsZip = true;
    } else {
        fileName = myFileName;
    }
    SquidProject squidProject = new SquidProject(myProjectName, GEOCHRON);
    prawnFileHandler = squidProject.getPrawnFileHandler();
    Path reportsZip = null;
    Path reportsFolder = null;
    try {
        Path uploadDirectory = Files.createTempDirectory("upload");
        Path uploadDirectory2 = Files.createTempDirectory("upload2");
        Path prawnFilePath;
        Path taskFilePath;
        if (prawnIsZip) {
            Path prawnFilePathZip = uploadDirectory.resolve("prawn-file.zip");
            Files.copy(prawnFile, prawnFilePathZip);
            prawnFilePath = extractZippedFile(prawnFilePathZip.toFile(), uploadDirectory.toFile());
            // dec 2021 in response to Snyk Path Traversal issue: https://app.snyk.io/org/bowring/project/7dd848fc-362b-4514-a91c-3c04628633ac
            if (!prawnFilePath.normalize().startsWith(uploadDirectory))
                throw new IOException("Zip entry contained path traversal");
        } else {
            prawnFilePath = uploadDirectory.resolve("prawn-file.xml");
            Files.copy(prawnFile, prawnFilePath);
        }
        taskFilePath = uploadDirectory2.resolve("task-file.xls");
        Files.copy(taskFile, taskFilePath);
        File squidTaskFile = taskFilePath.toFile();
        ShrimpDataFileInterface prawnFileData = prawnFileHandler.unmarshallPrawnFileXML(prawnFilePath.toString(), true);
        squidProject.setPrawnFile(prawnFileData);
        squidProject.setDelimiterForUnknownNames("-");
        squidProject.preProcessPrawnSession();
        squidProject.updateFilterForRefMatSpotNames(refMatFilter);
        squidProject.updateFilterForRefMatSpotNames(concRefMatFilter);
        squidProject.replaceCurrentTaskWithImportedSquid25Task(squidTaskFile);
        TaskInterface task = squidProject.getTask();
        // hard-wired for now
        task.setTaskType(Squid3Constants.TaskTypeEnum.GEOCHRON);
        ResourceExtractor extractor = new ResourceExtractor(CommonPbModel.class);
        File commPbModelFile = extractor.extractResourceAsFile("Stacey-Kramers@559.0Ma (z6266) v.1.0.xml");
        CommonPbModel commPbModel = new CommonPbModel();
        commPbModel = (CommonPbModel) commPbModel.readXMLObject(commPbModelFile.getAbsolutePath(), false);
        task.setCommonPbModel(commPbModel);
        extractor = new ResourceExtractor(PhysicalConstantsModel.class);
        File physConstModelFile = extractor.extractResourceAsFile("Squid 2.5 Default Physical Constants Model v.1.0.xml");
        PhysicalConstantsModel physConstModel = new PhysicalConstantsModel();
        physConstModel = (PhysicalConstantsModel) physConstModel.readXMLObject(physConstModelFile.getAbsolutePath(), false);
        task.setPhysicalConstantsModel(physConstModel);
        extractor = new ResourceExtractor(ReferenceMaterialModel.class);
        File refMatModelFile = extractor.extractResourceAsFile("z6266 ID-TIMS (559.0 Ma) v.1.0.xml");
        ReferenceMaterialModel refMatModel = new ReferenceMaterialModel();
        refMatModel = (ReferenceMaterialModel) refMatModel.readXMLObject(refMatModelFile.getAbsolutePath(), false);
        task.setReferenceMaterialModel(refMatModel);
        task.setConcentrationReferenceMaterialModel(refMatModel);
        task.setExtPErrU(0.75);
        task.setExtPErrTh(0.75);
        task.setUseSBM(useSBM);
        task.setUserLinFits(userLinFits);
        task.setSelectedIndexIsotope(preferredIndexIsotope);
        task.setSquidAllowsAutoExclusionOfSpots(true);
        // process task
        task.applyTaskIsotopeLabelsToMassStationsAndUpdateTask();
        Path calamariReportsFolderAliasParent = Files.createTempDirectory("reports-destination");
        Path calamariReportsFolderAlias = calamariReportsFolderAliasParent.resolve("Squid3ReportsFromWebService");
        File reportsDestinationFile = calamariReportsFolderAlias.toFile();
        reportsEngine = prawnFileHandler.getReportsEngine();
        prawnFileHandler.initReportsEngineWithCurrentPrawnFileName(fileName);
        reportsEngine.setFolderToWriteCalamariReports(reportsDestinationFile);
        ((Task) task).initTaskDefaultSquidReportTables(true);
        // for web service, need to set
        prawnFileHandler.setCurrentPrawnSourceFileLocation(fileName);
        if (squidProject.hasReportsFolder()) {
            squidProject.getPrawnFileHandler().getReportsEngine().writeProjectAudit();
            squidProject.getPrawnFileHandler().getReportsEngine().writeTaskAudit();
            squidProject.getPrawnFileHandler().getReportsEngine().writeSummaryReportsForReferenceMaterials();
            squidProject.produceReferenceMaterialPerSquid25CSV(true);
            squidProject.produceSelectedReferenceMaterialReportCSV();
            squidProject.getPrawnFileHandler().getReportsEngine().writeSummaryReportsForUnknowns();
            squidProject.produceUnknownsPerSquid25CSV(true);
            squidProject.produceUnknownsBySampleForETReduxCSV(true);
            squidProject.produceSelectedUnknownsReportCSV();
            squidProject.produceUnknownsWeightedMeanSortingFieldsCSV();
            squidProject.getTask().producePerScanReportsToFiles();
        }
        Files.delete(prawnFilePath);
        reportsFolder = Paths.get(reportsEngine.getFolderToWriteCalamariReports().getParentFile().getPath());
    } catch (IOException | JAXBException | SAXException | SquidException iOException) {
        Path config = Files.createTempFile("SquidWebServiceMessage", "txt", PosixFilePermissions.asFileAttribute(perms));
        try (BufferedWriter writer = Files.newBufferedWriter(config, StandardCharsets.UTF_8)) {
            writer.write("Squid Reporting web service was not able to process supplied files.");
            writer.newLine();
            writer.write(iOException.getMessage());
            writer.newLine();
        }
        File message = config.toFile();
        Path messageDirectory = Files.createTempDirectory("message");
        Path messageFilePath = messageDirectory.resolve("Squid Web Service Message.txt");
        Files.copy(message.toPath(), messageFilePath);
        reportsFolder = messageFilePath.getParent();
    }
    reportsZip = ZipUtility.recursivelyZip(reportsFolder);
    FileUtilities.recursiveDelete(reportsFolder);
    return reportsZip;
}
Also used : Path(java.nio.file.Path) Task(org.cirdles.squid.tasks.Task) PhysicalConstantsModel(org.cirdles.squid.parameters.parameterModels.physicalConstantsModels.PhysicalConstantsModel) JAXBException(javax.xml.bind.JAXBException) ShrimpDataFileInterface(org.cirdles.squid.shrimp.ShrimpDataFileInterface) SquidProject(org.cirdles.squid.projects.SquidProject) IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) CommonPbModel(org.cirdles.squid.parameters.parameterModels.commonPbModels.CommonPbModel) ResourceExtractor(org.cirdles.commons.util.ResourceExtractor) SAXException(org.xml.sax.SAXException) BufferedWriter(java.io.BufferedWriter) ReferenceMaterialModel(org.cirdles.squid.parameters.parameterModels.referenceMaterialModels.ReferenceMaterialModel) SquidException(org.cirdles.squid.exceptions.SquidException) TaskInterface(org.cirdles.squid.tasks.TaskInterface) IndexIsoptopesEnum(org.cirdles.squid.constants.Squid3Constants.IndexIsoptopesEnum) ZipUtility.extractZippedFile(org.cirdles.squid.utilities.fileUtilities.ZipUtility.extractZippedFile) File(java.io.File)

Aggregations

BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)1 JAXBException (javax.xml.bind.JAXBException)1 ResourceExtractor (org.cirdles.commons.util.ResourceExtractor)1 IndexIsoptopesEnum (org.cirdles.squid.constants.Squid3Constants.IndexIsoptopesEnum)1 SquidException (org.cirdles.squid.exceptions.SquidException)1 CommonPbModel (org.cirdles.squid.parameters.parameterModels.commonPbModels.CommonPbModel)1 PhysicalConstantsModel (org.cirdles.squid.parameters.parameterModels.physicalConstantsModels.PhysicalConstantsModel)1 ReferenceMaterialModel (org.cirdles.squid.parameters.parameterModels.referenceMaterialModels.ReferenceMaterialModel)1 SquidProject (org.cirdles.squid.projects.SquidProject)1 ShrimpDataFileInterface (org.cirdles.squid.shrimp.ShrimpDataFileInterface)1 Task (org.cirdles.squid.tasks.Task)1 TaskInterface (org.cirdles.squid.tasks.TaskInterface)1 ZipUtility.extractZippedFile (org.cirdles.squid.utilities.fileUtilities.ZipUtility.extractZippedFile)1 SAXException (org.xml.sax.SAXException)1