Search in sources :

Example 21 with FieldChange

use of org.jabref.model.FieldChange in project jabref by JabRef.

the class SpecialFieldsUtilsTest method syncSpecialFieldsFromKeywordCausesNoChangeWhenKeywordsAreEmpty.

@Test
public void syncSpecialFieldsFromKeywordCausesNoChangeWhenKeywordsAreEmpty() {
    BibEntry entry = new BibEntry();
    List<FieldChange> changes = SpecialFieldsUtils.syncSpecialFieldsFromKeywords(entry, ',');
    assertFalse(changes.size() > 0);
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) FieldChange(org.jabref.model.FieldChange) Test(org.junit.Test)

Example 22 with FieldChange

use of org.jabref.model.FieldChange in project jabref by JabRef.

the class SpecialFieldsUtilsTest method syncKeywordsFromSpecialFieldsForEmptyFieldCausesNoChange.

@Test
public void syncKeywordsFromSpecialFieldsForEmptyFieldCausesNoChange() {
    BibEntry entry = new BibEntry();
    List<FieldChange> changes = SpecialFieldsUtils.syncKeywordsFromSpecialFields(entry, ',');
    assertFalse(changes.size() > 0);
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) FieldChange(org.jabref.model.FieldChange) Test(org.junit.Test)

Example 23 with FieldChange

use of org.jabref.model.FieldChange in project jabref by JabRef.

the class BibDatabaseWriter method savePartOfDatabase.

/**
     * Saves the database, including only the specified entries.
     */
public E savePartOfDatabase(BibDatabaseContext bibDatabaseContext, List<BibEntry> entries, SavePreferences preferences) throws SaveException {
    session = saveSessionFactory.createSaveSession(preferences.getEncodingOrDefault(), preferences.getMakeBackup());
    Optional<String> sharedDatabaseIDOptional = bibDatabaseContext.getDatabase().getSharedDatabaseID();
    if (sharedDatabaseIDOptional.isPresent()) {
        writeDatabaseID(sharedDatabaseIDOptional.get());
    }
    // Map to collect entry type definitions that we must save along with entries using them.
    Map<String, EntryType> typesToWrite = new TreeMap<>();
    // Some file formats write something at the start of the file (like the encoding)
    if (preferences.getSaveType() != SavePreferences.DatabaseSaveType.PLAIN_BIBTEX) {
        writePrelogue(bibDatabaseContext, preferences.getEncoding());
    }
    // Write preamble if there is one.
    writePreamble(bibDatabaseContext.getDatabase().getPreamble().orElse(""));
    // Write strings if there are any.
    writeStrings(bibDatabaseContext.getDatabase(), preferences.isReformatFile(), preferences.getLatexFieldFormatterPreferences());
    // Write database entries.
    List<BibEntry> sortedEntries = getSortedEntries(bibDatabaseContext, entries, preferences);
    List<FieldChange> saveActionChanges = applySaveActions(sortedEntries, bibDatabaseContext.getMetaData());
    session.addFieldChanges(saveActionChanges);
    for (BibEntry entry : sortedEntries) {
        // types (*not* all customized standard types) must be written.
        if (!EntryTypes.getStandardType(entry.getType(), bibDatabaseContext.getMode()).isPresent()) {
            // If user-defined entry type, then add it
            // Otherwise (getType returns empty optional) it is a completely unknown entry type, so ignore it
            EntryTypes.getType(entry.getType(), bibDatabaseContext.getMode()).ifPresent(entryType -> typesToWrite.put(entryType.getName(), entryType));
        }
        writeEntry(entry, bibDatabaseContext.getMode(), preferences.isReformatFile(), preferences.getLatexFieldFormatterPreferences());
    }
    if (preferences.getSaveType() != SavePreferences.DatabaseSaveType.PLAIN_BIBTEX) {
        // Write meta data.
        writeMetaData(bibDatabaseContext.getMetaData(), preferences.getGlobalCiteKeyPattern());
        // Write type definitions, if any:
        writeEntryTypeDefinitions(typesToWrite);
    }
    //finally write whatever remains of the file, but at least a concluding newline
    writeEpilogue(bibDatabaseContext.getDatabase().getEpilog());
    try {
        session.getWriter().close();
    } catch (IOException e) {
        throw new SaveException(e);
    }
    return session;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) CustomEntryType(org.jabref.model.entry.CustomEntryType) EntryType(org.jabref.model.entry.EntryType) FieldChange(org.jabref.model.FieldChange) BibtexString(org.jabref.model.entry.BibtexString) IOException(java.io.IOException) TreeMap(java.util.TreeMap)

Example 24 with FieldChange

use of org.jabref.model.FieldChange 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 25 with FieldChange

use of org.jabref.model.FieldChange 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)

Aggregations

FieldChange (org.jabref.model.FieldChange)35 BibEntry (org.jabref.model.entry.BibEntry)17 ArrayList (java.util.ArrayList)13 UndoableFieldChange (org.jabref.gui.undo.UndoableFieldChange)8 NamedCompound (org.jabref.gui.undo.NamedCompound)7 Test (org.junit.Test)7 KeywordList (org.jabref.model.entry.KeywordList)6 LinkedFile (org.jabref.model.entry.LinkedFile)6 IOException (java.io.IOException)3 File (java.io.File)2 Path (java.nio.file.Path)2 Keyword (org.jabref.model.entry.Keyword)2 FieldChangedEvent (org.jabref.model.entry.event.FieldChangedEvent)2 Subscribe (com.google.common.eventbus.Subscribe)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 AbstractUndoableEdit (javax.swing.undo.AbstractUndoableEdit)1 BasePanel (org.jabref.gui.BasePanel)1 CleanupWorker (org.jabref.logic.cleanup.CleanupWorker)1