Search in sources :

Example 1 with SquidProject

use of org.cirdles.squid.projects.SquidProject in project Squid by CIRDLES.

the class FileHandlerTest method testSelectProjectFile.

// April 2022 example test for Ian Robinson
public void testSelectProjectFile() throws SquidException, IOException {
    SquidProject myProject = new SquidProject(Squid3Constants.TaskTypeEnum.GENERAL);
    SquidSerializer.serializeObjectToFile(myProject, "Test.ser");
    SquidProject myOtherProject = (SquidProject) SquidSerializer.getSerializedObjectFromFile("Test.ser", true);
    assertEquals(myProject.getProjectName(), myOtherProject.getProjectName());
    Files.deleteIfExists((new File("Test.ser")).toPath());
}
Also used : SquidProject(org.cirdles.squid.projects.SquidProject) File(java.io.File)

Example 2 with SquidProject

use of org.cirdles.squid.projects.SquidProject in project Squid by CIRDLES.

the class Squid3Ink method newSquid3GeochronProjectFromPrawnXML.

/**
 * @param prawnXMLFileSourcePath
 * @throws IOException
 * @throws JAXBException
 * @throws SAXException
 * @throws SquidException
 */
@Override
public void newSquid3GeochronProjectFromPrawnXML(Path prawnXMLFileSourcePath) throws IOException, JAXBException, SAXException, SquidException {
    File prawnSourceFile = prawnXMLFileSourcePath.toFile();
    if (prawnSourceFile != null) {
        squid3Project = new SquidProject(GEOCHRON);
        // this updates output folder for reports to current version
        CalamariFileUtilities.initCalamariReportsFolder(squid3Project.getPrawnFileHandler(), prawnXMLFileSourcePath.toFile().getParentFile());
        if (((SquidProject) squid3Project).setupPrawnXMLFile(prawnSourceFile)) {
            ((SquidProject) squid3Project).autoDivideSamples();
            squidPersistentState.updatePrawnFileListMRU(prawnSourceFile);
        } else {
            squid3Project.getTask().setChanged(false);
            SquidProject.setProjectChanged(false);
            throw new SquidException("Squid3 encountered an error while trying to open the selected data file.");
        }
    }
}
Also used : SquidException(org.cirdles.squid.exceptions.SquidException) SquidProject(org.cirdles.squid.projects.SquidProject) ZipUtility.extractZippedFile(org.cirdles.squid.utilities.fileUtilities.ZipUtility.extractZippedFile) PrawnFile(org.cirdles.squid.prawn.PrawnFile) File(java.io.File)

Example 3 with SquidProject

use of org.cirdles.squid.projects.SquidProject in project Squid by CIRDLES.

the class ExpressionGroupXMLTest method testExpressionGroups.

@Test
public void testExpressionGroups() throws SquidException {
    boolean retVal = false;
    ResourceExtractor squidProjectExtractor = new ResourceExtractor(SquidProject.class);
    ResourceExtractor squidTaskXMLSchemaExtractor = new ResourceExtractor(Squid.class);
    File squidFile = squidProjectExtractor.extractResourceAsFile("Z626611PKPERM1.squid");
    File schemaFile = squidTaskXMLSchemaExtractor.extractResourceAsFile("schema/SquidTask_ExpressionXMLSchema.xsd");
    SquidProject project = (SquidProject) SquidSerializer.getSerializedObjectFromFile(squidFile.getAbsolutePath(), false);
    final File folder = new File("testingExpressionsGroups");
    folder.mkdir();
    List<Expression> expressions = project.getTask().getTaskExpressionsOrdered();
    List<Expression> customExpressions = Collections.synchronizedList(new ArrayList<>());
    expressions.parallelStream().forEach(exp -> {
        if (exp.isCustom() && exp.amHealthy()) {
            exp.serializeXMLObject(folder.getAbsolutePath() + File.separator + FileNameFixer.fixFileName(exp.getName()) + ".xml");
            customExpressions.add(exp);
        }
    });
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    File[] files = null;
    try {
        final Schema schema = sf.newSchema(schemaFile);
        files = folder.listFiles(f -> f.getName().endsWith(".xml"));
        for (File file : files) {
            try {
                validateXML(file, schema);
            } catch (SAXException | IOException e) {
            // If any exception is thrown, test will fail
            }
        }
    } catch (SAXException e) {
    }
    if (files != null) {
        List<Expression> convertedExpressions = new ArrayList<>();
        Expression converter = new Expression();
        for (File file : files) {
            convertedExpressions.add((Expression) converter.readXMLObject(file.getAbsolutePath(), false));
        }
        files = folder.listFiles();
        for (File file : files) {
            file.delete();
        }
        Comparator<Expression> expressionComparator = Comparator.comparing(Expression::getName);
        convertedExpressions.sort(expressionComparator);
        customExpressions.sort(expressionComparator);
        retVal = convertedExpressions.equals(customExpressions);
    }
    folder.delete();
    assertTrue(retVal);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) SchemaFactory(javax.xml.validation.SchemaFactory) SquidSerializer(org.cirdles.squid.utilities.stateUtilities.SquidSerializer) FileValidator.validateXML(org.cirdles.squid.utilities.fileUtilities.FileValidator.validateXML) SquidException(org.cirdles.squid.exceptions.SquidException) Test(org.junit.Test) IOException(java.io.IOException) Schema(javax.xml.validation.Schema) File(java.io.File) ArrayList(java.util.ArrayList) List(java.util.List) ResourceExtractor(org.cirdles.commons.util.ResourceExtractor) TestCase.assertTrue(junit.framework.TestCase.assertTrue) SAXException(org.xml.sax.SAXException) SquidProject(org.cirdles.squid.projects.SquidProject) FileNameFixer(org.cirdles.squid.utilities.fileUtilities.FileNameFixer) Expression(org.cirdles.squid.tasks.expressions.Expression) XMLConstants(javax.xml.XMLConstants) Comparator(java.util.Comparator) Collections(java.util.Collections) Squid(org.cirdles.squid.Squid) Schema(javax.xml.validation.Schema) ArrayList(java.util.ArrayList) SquidProject(org.cirdles.squid.projects.SquidProject) IOException(java.io.IOException) ResourceExtractor(org.cirdles.commons.util.ResourceExtractor) SAXException(org.xml.sax.SAXException) Expression(org.cirdles.squid.tasks.expressions.Expression) File(java.io.File) Test(org.junit.Test)

Example 4 with SquidProject

use of org.cirdles.squid.projects.SquidProject in project Squid by CIRDLES.

the class PrawnFileHandlerIT method setUp.

/**
 * @throws java.lang.Exception
 */
// provides to run setup once
@BeforeClass
public static void setUp() throws Exception {
    // april 2022 to preserve tests due to change in issue #698
    CommonLeadSpecsForSpot.DEFAULT_METHOD = CommonLeadSpecsForSpot.METHOD_COMMON_LEAD_MODEL;
    prawnFileHandler = (new SquidProject(GEOCHRON)).getPrawnFileHandler();
    reportsFolder = temporaryFolder.getRoot();
    prawnFileZ6266 = RESOURCE_EXTRACTOR.extractResourceAsFile(PRAWN_FILE_RESOURCE_Z6266);
    CalamariFileUtilities.initSampleParametersModels();
    squidProjectZ6266 = new SquidProject(GEOCHRON);
    prawnFileDataZ6266 = prawnFileHandler.unmarshallPrawnFileXML(prawnFileZ6266.getAbsolutePath(), true);
    squidProjectZ6266.setPrawnFile(prawnFileDataZ6266);
    // March 2019 remove dependency on Squid25 task for testing of built-ins
    SquidProject.setProjectChanged(true);
    TaskInterface task = new Task("test", prawnFileDataZ6266, prawnFileHandler.getReportsEngine());
    task.updateTaskFromTaskDesign(new TaskDesign11Mass(), false);
    squidProjectZ6266.setTask(task);
    squidProjectZ6266.setDelimiterForUnknownNames("-");
    squidProjectZ6266.getTask().setFilterForRefMatSpotNames("6266");
    squidProjectZ6266.getTask().setFilterForConcRefMatSpotNames("6266");
    squidProjectZ6266.getTask().setTaskType(Squid3Constants.TaskTypeEnum.GEOCHRON);
    squidProjectZ6266.getTask().setUseSBM(true);
    squidProjectZ6266.setUseSBM(true);
    squidProjectZ6266.getTask().setUserLinFits(false);
    squidProjectZ6266.setUserLinFits(false);
    squidProjectZ6266.getTask().setSquidAllowsAutoExclusionOfSpots(true);
    squidProjectZ6266.setSquidAllowsAutoExclusionOfSpots(true);
    squidProjectZ6266.getTask().setExtPErrU(0.75);
    squidProjectZ6266.setExtPErrU(0.75);
    squidProjectZ6266.getTask().setExtPErrTh(0.75);
    squidProjectZ6266.setExtPErrTh(0.75);
    squidProjectZ6266.getTask().setPhysicalConstantsModel(PhysicalConstantsModel.getDefaultModel(SQUID2_DEFAULT_PHYSICAL_CONSTANTS_MODEL_V1, "1.0"));
    squidProjectZ6266.setPhysicalConstantsModel(PhysicalConstantsModel.getDefaultModel(SQUID2_DEFAULT_PHYSICAL_CONSTANTS_MODEL_V1, "1.0"));
    squidProjectZ6266.getTask().setCommonPbModel(CommonPbModel.getDefaultModel("Stacey-Kramers@559.0Ma (z6266)", "1.0"));
    squidProjectZ6266.setCommonPbModel(CommonPbModel.getDefaultModel("Stacey-Kramers@559.0Ma (z6266)", "1.0"));
    // modified sept 202 to accommodate old tests and new models
    ResourceExtractor extractor = new ResourceExtractor(ReferenceMaterialModel.class);
    File testingModelFile = extractor.extractResourceAsFile("GA Accepted BR266 v.1.0.xml");
    ReferenceMaterialModel testingModel = new ReferenceMaterialModel();
    testingModel = (ReferenceMaterialModel) testingModel.readXMLObject(testingModelFile.getAbsolutePath(), false);
    squidProjectZ6266.getTask().setReferenceMaterialModel(testingModel);
    squidProjectZ6266.setReferenceMaterialModel(testingModel);
    squidProjectZ6266.getTask().setConcentrationReferenceMaterialModel(testingModel);
    squidProjectZ6266.setConcentrationReferenceMaterialModel(testingModel);
    squidProjectZ6266.setSelectedIndexIsotope(Squid3Constants.IndexIsoptopesEnum.PB_204);
    squidProjectZ6266.setSelectedIndexIsotope(Squid3Constants.IndexIsoptopesEnum.PB_204);
    // force defaults for testing of builtins
    squidProjectZ6266.getTask().getSpecialSquidFourExpressionsMap().put(PARENT_ELEMENT_CONC_CONST, "[\"238/195.8\"]/[\"254/238\"]^0.66");
    squidProjectZ6266.getTask().getSpecialSquidFourExpressionsMap().put(UNCOR206PB238U_CALIB_CONST, UNCOR206PB238U_CALIB_CONST_DEFAULT_EXPRESSION);
    squidProjectZ6266.getTask().getSpecialSquidFourExpressionsMap().put(UNCOR208PB232TH_CALIB_CONST, UNCOR208PB232TH_CALIB_CONST_DEFAULT_EXPRESSION);
    squidProjectZ6266.getTask().getSpecialSquidFourExpressionsMap().put(TH_U_EXP_DEFAULT, TH_U_EXP_DEFAULT_EXPRESSION);
    squidProjectZ6266.getTask().setSquidAllowsAutoExclusionOfSpots(true);
    squidProjectZ6266.getTask().applyTaskIsotopeLabelsToMassStations();
}
Also used : ReferenceMaterialModel(org.cirdles.squid.parameters.parameterModels.referenceMaterialModels.ReferenceMaterialModel) Task(org.cirdles.squid.tasks.Task) TaskInterface(org.cirdles.squid.tasks.TaskInterface) SquidProject(org.cirdles.squid.projects.SquidProject) TaskDesign11Mass(org.cirdles.squid.tasks.taskDesign.TaskDesign11Mass) File(java.io.File) ResourceExtractor(org.cirdles.commons.util.ResourceExtractor)

Example 5 with SquidProject

use of org.cirdles.squid.projects.SquidProject 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

File (java.io.File)8 SquidProject (org.cirdles.squid.projects.SquidProject)8 SquidException (org.cirdles.squid.exceptions.SquidException)5 ZipUtility.extractZippedFile (org.cirdles.squid.utilities.fileUtilities.ZipUtility.extractZippedFile)5 ResourceExtractor (org.cirdles.commons.util.ResourceExtractor)3 PrawnFile (org.cirdles.squid.prawn.PrawnFile)3 IOException (java.io.IOException)2 ReferenceMaterialModel (org.cirdles.squid.parameters.parameterModels.referenceMaterialModels.ReferenceMaterialModel)2 Task (org.cirdles.squid.tasks.Task)2 TaskInterface (org.cirdles.squid.tasks.TaskInterface)2 SAXException (org.xml.sax.SAXException)2 BufferedWriter (java.io.BufferedWriter)1 Path (java.nio.file.Path)1 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 List (java.util.List)1 XMLConstants (javax.xml.XMLConstants)1 JAXBException (javax.xml.bind.JAXBException)1