Example usage for java.nio.file Path getName

List of usage examples for java.nio.file Path getName

Introduction

In this page you can find the example usage for java.nio.file Path getName.

Prototype

Path getName(int index);

Source Link

Document

Returns a name element of this path as a Path object.

Usage

From source file:org.sleuthkit.autopsy.experimental.autoingest.SingleUserCaseImporter.java

/**
 * This causes iteration over all .aut files in the baseCaseInput path,
 * calling SingleUserCaseConverter.importCase() for each one.
 *//*from  w ww.  j a v  a  2  s . c o  m*/
public void importCases() throws Exception {
    openLog(baseCaseOutput.toFile());
    log(NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.StartingBatch")
            + baseCaseInput.toString() + " "
            + NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.to") + " "
            + baseCaseOutput.toString()); //NON-NLS

    // iterate for .aut files
    FindDotAutFolders dotAutFolders = new FindDotAutFolders();
    try {
        Path walked = Files.walkFileTree(baseCaseInput, dotAutFolders);
    } catch (IOException ex) {
        log(NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.ErrorFindingAutFiles")
                + " " + ex.getMessage()); //NON-NLS
    }

    ArrayList<ImportCaseData> ableToProcess = new ArrayList<>();
    ArrayList<ImportCaseData> unableToProcess = new ArrayList<>();

    SingleUserCaseConverter scc = new SingleUserCaseConverter();

    // validate we can convert the .aut file, one by one
    for (FoundAutFile f : dotAutFolders.getCandidateList()) {
        this.oldCaseName = f.getPath().getFileName().toString();

        // Test image output folder for uniqueness, find a unique folder for it if we can
        File specificOutputFolder = baseImageOutput.resolve(oldCaseName).toFile();
        String newImageName = oldCaseName;
        if (specificOutputFolder.exists()) {
            // Not unique. add numbers before timestamp to specific image output name
            String timeStamp = TimeStampUtils.getTimeStampOnly(oldCaseName);
            newImageName = TimeStampUtils.removeTimeStamp(oldCaseName);
            int number = 1;
            String temp = ""; //NON-NLS
            while (specificOutputFolder.exists()) {
                if (number == Integer.MAX_VALUE) {
                    // It never became unique, so give up.
                    throw new Exception(NbBundle.getMessage(SingleUserCaseImporter.class,
                            "SingleUserCaseImporter.NonUniqueOutputFolder") + newImageName); //NON-NLS
                }
                temp = newImageName + "_" + Integer.toString(number) + timeStamp; //NON-NLS
                specificOutputFolder = baseImageOutput.resolve(temp).toFile();
                ++number;
            }
            newImageName = temp;
        }
        Path imageOutput = baseImageOutput.resolve(newImageName);
        imageOutput.toFile().mkdirs(); // Create image output folder

        // Test case output folder for uniqueness, find a unique folder for it if we can
        specificOutputFolder = baseCaseOutput.resolve(oldCaseName).toFile();
        newCaseName = oldCaseName;
        if (specificOutputFolder.exists()) {
            // not unique. add numbers before timestamp to specific case output name
            String timeStamp = TimeStampUtils.getTimeStampOnly(oldCaseName); //NON-NLS
            newCaseName = TimeStampUtils.removeTimeStamp(oldCaseName);
            int number = 1;
            String temp = ""; //NON-NLS
            while (specificOutputFolder.exists()) {
                if (number == Integer.MAX_VALUE) {
                    // It never became unique, so give up.
                    throw new Exception(NbBundle.getMessage(SingleUserCaseImporter.class,
                            "SingleUserCaseImporter.NonUniqueOutputFolder") + newCaseName); //NON-NLS
                }
                temp = newCaseName + "_" + Integer.toString(number) + timeStamp; //NON-NLS
                specificOutputFolder = baseCaseOutput.resolve(temp).toFile();
                ++number;
            }
            newCaseName = temp;
        }
        Path caseOutput = baseCaseOutput.resolve(newCaseName);
        caseOutput.toFile().mkdirs(); // Create case output folder

        /**
         * Test if the input path has a corresponding image input folder and
         * no repeated case names in the path. If both of these conditions
         * are true, we can process this case, otherwise not.
         */
        // Check that there is an image folder if they are trying to copy it
        boolean canProcess = true;
        Path imageInput = null;
        String relativeCaseName = TimeStampUtils
                .removeTimeStamp(baseCaseInput.relativize(f.getPath()).toString());
        Path testImageInputsFromOldCase = Paths.get(baseImageInput.toString(), relativeCaseName);
        if (copyImages) {
            if (!testImageInputsFromOldCase.toFile().isDirectory()) {
                // Mark that we are unable to process this item
                canProcess = false;
            } else {
                imageInput = testImageInputsFromOldCase;
            }
            if (imageInput == null) {
                throw new Exception(NbBundle.getMessage(SingleUserCaseImporter.class,
                        "SingleUserCaseImporter.SourceImageMissing") + " " + f.getPath()); //NON-NLS
            }

            // If case name is in the image path, it causes bad things to happen with the parsing. Test for this.
            for (int x = 0; x < imageInput.getNameCount(); ++x) {
                if (oldCaseName.toLowerCase().equals(imageInput.getName(x).toString().toLowerCase())) {
                    // Mark that we are unable to process this item
                    canProcess = false;
                }
            }
        } else {
            imageInput = testImageInputsFromOldCase;
        }

        // Create an Import Case Data object for this case
        SingleUserCaseConverter.ImportCaseData icd = scc.new ImportCaseData(imageInput, f.getPath(),
                imageOutput, caseOutput, oldCaseName, newCaseName, f.getAutFile().toString(),
                f.getFolderName().toString(), copyImages, deleteCase);

        if (canProcess) {
            ableToProcess.add(icd);
        } else {
            unableToProcess.add(icd);
        }
    }

    // Create text to be populated in the confirmation dialog
    StringBuilder casesThatWillBeProcessed = new StringBuilder();
    StringBuilder casesThatWillNotBeProcessed = new StringBuilder();

    casesThatWillBeProcessed
            .append(NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.WillImport"))
            .append(SEP); // NON-NLS
    if (ableToProcess.isEmpty()) {
        casesThatWillBeProcessed
                .append(NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.None"))
                .append(SEP); // NON-NLS
    } else {
        for (ImportCaseData i : ableToProcess) {
            casesThatWillBeProcessed.append(i.getCaseInputFolder().toString()).append(SEP);
        }
    }

    if (!unableToProcess.isEmpty()) {
        casesThatWillNotBeProcessed.append(
                NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.WillNotImport"))
                .append(SEP); // NON-NLS
        for (ImportCaseData i : unableToProcess) {
            casesThatWillNotBeProcessed.append(i.getCaseInputFolder().toString()).append(SEP);
        }
    }

    JTextArea jta = new JTextArea(
            casesThatWillBeProcessed.toString() + SEP + casesThatWillNotBeProcessed.toString());
    jta.setEditable(false);
    JScrollPane jsp = new JScrollPane(jta) {
        private static final long serialVersionUID = 1L;

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(700, 480);
        }
    };

    // Show confirmation dialog
    SwingUtilities.invokeLater(() -> {
        userAnswer = JOptionPane.showConfirmDialog(WindowManager.getDefault().getMainWindow(), jsp,
                NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.ContinueWithImport"), // NON-NLS
                OK_CANCEL_OPTION);
        synchronized (threadWaitNotifyLock) {
            threadWaitNotifyLock.notify();
        }
    });

    // Wait while the user handles the confirmation dialog
    synchronized (threadWaitNotifyLock) {
        try {
            threadWaitNotifyLock.wait();
        } catch (InterruptedException ex) {
            Logger.getLogger(SingleUserCaseImporter.class.getName()).log(Level.SEVERE, "Threading Issue", ex); //NON-NLS
            throw new Exception(ex);
        }
    }

    // If the user wants to proceed, do so.
    if (userAnswer == JOptionPane.OK_OPTION) {
        boolean result = true; // if anything went wrong, result becomes false.
        // Feed .aut files in one by one for processing
        for (ImportCaseData i : ableToProcess) {
            try {
                log(NbBundle.getMessage(SingleUserCaseImporter.class,
                        "SingleUserCaseImporter.StartedProcessing") + i.getCaseInputFolder() + " "
                        + NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.to") + " "
                        + i.getCaseOutputFolder()); //NON-NLS
                SingleUserCaseConverter.importCase(i);
                handleAutoIngestLog(i);
                log(NbBundle.getMessage(SingleUserCaseImporter.class,
                        "SingleUserCaseImporter.FinishedProcessing") + i.getCaseInputFolder() + " "
                        + NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.to") + " "
                        + i.getCaseOutputFolder()); //NON-NLS

            } catch (Exception ex) {
                log(NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.FailedToComplete")
                        + i.getCaseInputFolder() + " "
                        + NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.to") + " "
                        + i.getCaseOutputFolder() + " " + ex.getMessage()); //NON-NLS
                result = false;
            }
        }

        log(NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.CompletedBatch")
                + baseCaseInput.toString() + " "
                + NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.to") + " "
                + baseCaseOutput.toString()); //NON-NLS

        closeLog();
        if (notifyOnComplete != null) {
            notifyOnComplete.importDoneCallback(result, ""); // NON-NLS
        }
    } else {
        // The user clicked cancel. Abort.
        log(NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.AbortingBatch")
                + baseCaseInput.toString() + " "
                + NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.to") + " "
                + baseCaseOutput.toString()); //NON-NLS

        closeLog();
        if (notifyOnComplete != null) {
            notifyOnComplete.importDoneCallback(false,
                    NbBundle.getMessage(SingleUserCaseImporter.class, "SingleUserCaseImporter.Cancelled")); // NON-NLS
        }
    }
}