Search in sources :

Example 21 with LinkedFile

use of org.jabref.model.entry.LinkedFile in project jabref by JabRef.

the class MoveFilesCleanup method cleanup.

@Override
public List<FieldChange> cleanup(BibEntry entry) {
    Optional<Path> firstExistingFileDir = databaseContext.getFirstExistingFileDir(fileDirectoryPreferences);
    if (!firstExistingFileDir.isPresent()) {
        return Collections.emptyList();
    }
    List<Path> paths = databaseContext.getFileDirectoriesAsPaths(fileDirectoryPreferences);
    String defaultFileDirectory = firstExistingFileDir.get().toString();
    Optional<Path> targetDirectory = FileHelper.expandFilenameAsPath(defaultFileDirectory, paths);
    if (!targetDirectory.isPresent()) {
        return Collections.emptyList();
    }
    List<LinkedFile> fileList;
    List<LinkedFile> newFileList;
    if (singleFileFieldCleanup != null) {
        fileList = Arrays.asList(singleFileFieldCleanup);
        //Add all other except the current selected file
        newFileList = entry.getFiles().stream().filter(name -> !name.equals(singleFileFieldCleanup)).collect(Collectors.toList());
    } else {
        newFileList = new ArrayList<>();
        fileList = entry.getFiles();
    }
    boolean changed = false;
    for (LinkedFile fileEntry : fileList) {
        String oldFileName = fileEntry.getLink();
        Optional<Path> oldFile = fileEntry.findIn(paths);
        if (!oldFile.isPresent() || !Files.exists(oldFile.get())) {
            newFileList.add(fileEntry);
            continue;
        }
        String targetDirName = "";
        if (!fileDirPattern.isEmpty()) {
            targetDirName = FileUtil.createFileNameFromPattern(databaseContext.getDatabase(), entry, fileDirPattern, layoutPrefs);
        }
        Path newTargetFile = targetDirectory.get().resolve(targetDirName).resolve(oldFile.get().getFileName());
        if (Files.exists(newTargetFile)) {
            // We do not overwrite already existing files
            newFileList.add(fileEntry);
            continue;
        }
        try {
            if (!Files.exists(newTargetFile)) {
                Files.createDirectories(newTargetFile);
            }
        } catch (IOException e) {
            LOGGER.error("Could no create necessary target directoires for renaming", e);
        }
        if (FileUtil.renameFile(oldFile.get(), newTargetFile, true)) {
            changed = true;
            String newEntryFilePath = Paths.get(defaultFileDirectory).relativize(newTargetFile).toString();
            LinkedFile newFileEntry = fileEntry;
            if (!oldFileName.equals(newTargetFile.toString())) {
                newFileEntry = new LinkedFile(fileEntry.getDescription(), newEntryFilePath, fileEntry.getFileType());
                changed = true;
            }
            newFileList.add(newFileEntry);
        }
    }
    if (changed) {
        Optional<FieldChange> change = entry.setFiles(newFileList);
        if (change.isPresent()) {
            return Collections.singletonList(change.get());
        } else {
            return Collections.emptyList();
        }
    }
    return Collections.emptyList();
}
Also used : Path(java.nio.file.Path) LinkedFile(org.jabref.model.entry.LinkedFile) FieldChange(org.jabref.model.FieldChange) IOException(java.io.IOException)

Example 22 with LinkedFile

use of org.jabref.model.entry.LinkedFile in project jabref by JabRef.

the class RelativePathsCleanup method cleanup.

@Override
public List<FieldChange> cleanup(BibEntry entry) {
    List<LinkedFile> fileList = entry.getFiles();
    List<LinkedFile> newFileList = new ArrayList<>();
    boolean changed = false;
    for (LinkedFile fileEntry : fileList) {
        String oldFileName = fileEntry.getLink();
        String newFileName = FileUtil.shortenFileName(Paths.get(oldFileName), databaseContext.getFileDirectoriesAsPaths(fileDirectoryPreferences)).toString();
        LinkedFile newFileEntry = fileEntry;
        if (!oldFileName.equals(newFileName)) {
            newFileEntry = new LinkedFile(fileEntry.getDescription(), newFileName, fileEntry.getFileType());
            changed = true;
        }
        newFileList.add(newFileEntry);
    }
    if (changed) {
        Optional<FieldChange> change = entry.setFiles(newFileList);
        if (change.isPresent()) {
            return Collections.singletonList(change.get());
        } else {
            return Collections.emptyList();
        }
    }
    return Collections.emptyList();
}
Also used : LinkedFile(org.jabref.model.entry.LinkedFile) ArrayList(java.util.ArrayList) FieldChange(org.jabref.model.FieldChange)

Example 23 with LinkedFile

use of org.jabref.model.entry.LinkedFile in project jabref by JabRef.

the class CleanupWorkerTest method cleanupDoesNothingByDefault.

@Test
public void cleanupDoesNothingByDefault() throws IOException {
    BibEntry entry = new BibEntry();
    entry.setCiteKey("Toot");
    entry.setField("pdf", "aPdfFile");
    entry.setField("some", "1st");
    entry.setField("doi", "http://dx.doi.org/10.1016/0001-8708(80)90035-3");
    entry.setField("month", "01");
    entry.setField("pages", "1-2");
    entry.setField("date", "01/1999");
    entry.setField("pdf", "aPdfFile");
    entry.setField("ps", "aPsFile");
    entry.setField("file", "link::");
    entry.setField("journal", "test");
    entry.setField("title", "<b>hallo</b> units 1 A case AlGaAs and latex $\\alpha$$\\beta$");
    entry.setField("abstract", "Réflexions");
    File tempFile = bibFolder.newFile();
    LinkedFile fileField = new LinkedFile("", tempFile.getAbsolutePath(), "");
    entry.setField("file", FileFieldWriter.getStringRepresentation(fileField));
    List<FieldChange> changes = worker.cleanup(emptyPreset, entry);
    Assert.assertEquals(Collections.emptyList(), changes);
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) LinkedFile(org.jabref.model.entry.LinkedFile) FieldChange(org.jabref.model.FieldChange) LinkedFile(org.jabref.model.entry.LinkedFile) File(java.io.File) Test(org.junit.Test)

Example 24 with LinkedFile

use of org.jabref.model.entry.LinkedFile in project jabref by JabRef.

the class CleanupWorkerTest method cleanupRelativePathsConvertAbsoluteToRelativePath.

@Test
public void cleanupRelativePathsConvertAbsoluteToRelativePath() throws IOException {
    CleanupPreset preset = new CleanupPreset(CleanupPreset.CleanupStep.MAKE_PATHS_RELATIVE);
    File tempFile = bibFolder.newFile();
    BibEntry entry = new BibEntry();
    LinkedFile fileField = new LinkedFile("", tempFile.getAbsolutePath(), "");
    entry.setField("file", FileFieldWriter.getStringRepresentation(fileField));
    worker.cleanup(preset, entry);
    LinkedFile newFileField = new LinkedFile("", tempFile.getName(), "");
    Assert.assertEquals(Optional.of(FileFieldWriter.getStringRepresentation(newFileField)), entry.getField("file"));
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) LinkedFile(org.jabref.model.entry.LinkedFile) LinkedFile(org.jabref.model.entry.LinkedFile) File(java.io.File) Test(org.junit.Test)

Example 25 with LinkedFile

use of org.jabref.model.entry.LinkedFile in project jabref by JabRef.

the class RenamePdfCleanupTest method cleanupRenamePdfRenamesFileStartingWithBibtexKey.

@Test
public void cleanupRenamePdfRenamesFileStartingWithBibtexKey() throws IOException {
    String fileNamePattern = "\\bibtexkey - \\title";
    File tempFile = testFolder.newFile("Toot.tmp");
    LinkedFile fileField = new LinkedFile("", tempFile.getAbsolutePath(), "");
    entry.setField("file", FileFieldWriter.getStringRepresentation(fileField));
    entry.setField("title", "test title");
    RenamePdfCleanup cleanup = new RenamePdfCleanup(false, context, fileNamePattern, mock(LayoutFormatterPreferences.class), fileDirPrefs);
    cleanup.cleanup(entry);
    LinkedFile newFileField = new LinkedFile("", "Toot - test title.tmp", "");
    assertEquals(Optional.of(FileFieldWriter.getStringRepresentation(newFileField)), entry.getField("file"));
}
Also used : LinkedFile(org.jabref.model.entry.LinkedFile) LayoutFormatterPreferences(org.jabref.logic.layout.LayoutFormatterPreferences) File(java.io.File) LinkedFile(org.jabref.model.entry.LinkedFile) Test(org.junit.Test)

Aggregations

LinkedFile (org.jabref.model.entry.LinkedFile)28 Test (org.junit.Test)13 File (java.io.File)11 Path (java.nio.file.Path)8 FieldChange (org.jabref.model.FieldChange)6 ArrayList (java.util.ArrayList)5 BibEntry (org.jabref.model.entry.BibEntry)5 HashMap (java.util.HashMap)3 List (java.util.List)3 ExternalFileType (org.jabref.gui.externalfiletype.ExternalFileType)3 UnknownExternalFileType (org.jabref.gui.externalfiletype.UnknownExternalFileType)3 FileListEntry (org.jabref.gui.filelist.FileListEntry)3 LayoutFormatterPreferences (org.jabref.logic.layout.LayoutFormatterPreferences)3 IOException (java.io.IOException)2 Paths (java.nio.file.Paths)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Log (org.apache.commons.logging.Log)2 LogFactory (org.apache.commons.logging.LogFactory)2 FileFieldParser (org.jabref.model.entry.FileFieldParser)2