Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into fileDescr
Browse files Browse the repository at this point in the history
* upstream/master:
  Don't highlight odd rows in file list editor (#3223)
  Fix assertion by removing typo (#3220)
  Update assertion to change of online reference (#3221)
  Put in null return
  Reformatted code, renamed method, added try/catch
  Fix null return
  Changed from Path to Optional<Path>
  Added logic to check whether linked file already exists
  • Loading branch information
Siedlerchr committed Sep 19, 2017
2 parents f767625 + a0e5529 commit c517843
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- We fixed an issue where the arrow keys in the search bar did not work as expected [#3081](https://github.com/JabRef/jabref/issues/3081)
- We fixed wrong hotkey being displayed at "automatically file links" in the entry editor
- We fixed an issue where metadata syncing with local and shared database were unstable. It will also fix syncing groups and sub-groups in database. [#2284](https://github.com/JabRef/jabref/issues/2284)
- We fixed an issue where renaming a linked file would fail silently if a file with the same name existed. Added support for overriding existing file at user discretion. [#3172] (https://github.com/JabRef/jabref/issues/3172)
- We fixed an issue where the "Remove group and subgroups" operation did not remove group information from entries in the group [#3190](https://github.com/JabRef/jabref/issues/3190)
- We fixed an issue where it was possible to leave the entry editor with an imbalance of braces. [#3167](https://github.com/JabRef/jabref/issues/3167)
- Renaming files now truncates the filename to not exceed the limit of 255 chars [#2622](https://github.com/JabRef/jabref/issues/2622)
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/jabref/gui/entryeditor/EntryEditor.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@
-fx-text-fill: text-area-foreground;
-fx-text-origin: baseline;
}

.list-cell:odd {
-fx-background-color: text-area-background;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.jabref.logic.cleanup.MoveFilesCleanup;
import org.jabref.logic.cleanup.RenamePdfCleanup;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.LinkedFile;
Expand Down Expand Up @@ -182,7 +183,8 @@ public void rename() {
Localization.lang("Cancel"));

if (confirm) {
pdfCleanup.cleanup(entry);
Optional<Path> fileConflictCheck = pdfCleanup.findExistingFile(linkedFile, entry);
performRenameWithConflictCheck(file, pdfCleanup, targetFileName, fileConflictCheck);
}
} else {
dialogService.showErrorDialogAndWait(
Expand All @@ -191,6 +193,23 @@ public void rename() {
}
}

private void performRenameWithConflictCheck(Optional<Path> file, RenamePdfCleanup pdfCleanup, String targetFileName, Optional<Path> fileConflictCheck) {
boolean confirm;
if (!fileConflictCheck.isPresent()) {
pdfCleanup.cleanup(entry);
} else {
confirm = dialogService.showConfirmationDialogAndWait(
Localization.lang("File exists"),
Localization.lang("'%0' exists. Overwrite file?", targetFileName),
Localization.lang("Overwrite"),
Localization.lang("Cancel"));
if (confirm) {
FileUtil.renameFile(fileConflictCheck.get(), file.get(), true);
pdfCleanup.cleanup(entry);
}
}
}

public void moveToDefaultDirectory() {
if (linkedFile.isOnlineLink()) {
// Cannot move remote links
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/org/jabref/logic/cleanup/RenamePdfCleanup.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.jabref.logic.layout.LayoutFormatterPreferences;
import org.jabref.logic.util.io.FileUtil;
Expand Down Expand Up @@ -158,4 +159,29 @@ public String getTargetFileName(LinkedFile flEntry, BibEntry entry) {
public int getUnsuccessfulRenames() {
return unsuccessfulRenames;
}

/**
* Check to see if a file already exists in the target directory. Search is not case sensitive.
* @param flEntry
* @param entry
* @return First identified path that matches an existing file. This name can be used in subsequent calls to override the existing file.
*/
public Optional<Path> findExistingFile(LinkedFile flEntry, BibEntry entry) {
String targetFileName = getTargetFileName(flEntry, entry);
// The .get() is legal without check because the method will always return a value.
Path targetFilePath = flEntry.findIn(databaseContext,
fileDirectoryPreferences).get().getParent().resolve(targetFileName);
Path oldFilePath = flEntry.findIn(databaseContext, fileDirectoryPreferences).get();
//Check if file already exists in directory with different case.
//This is necessary because other entries may have such a file.
Optional<Path> matchedByDiffCase = Optional.empty();
try (Stream<Path> stream = Files.list(oldFilePath.getParent())) {
matchedByDiffCase = stream
.filter(name -> name.toString().equalsIgnoreCase(targetFilePath.toString()))
.findFirst();
} catch (IOException e) {
LOGGER.error("Could not get the list of files in target directory", e);
}
return matchedByDiffCase;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void testParse() throws Throwable {

Assert.assertEquals(Optional.of("Heavy Particles from Inflation"), be.getField("title"));
Assert.assertTrue(be.getField("abstract").isPresent());
Assert.assertEquals(Optional.of("23 pages"), be.getField("comments"));
Assert.assertEquals(Optional.of("23 pages"), be.getField("comment"));
Assert.assertEquals(Optional.of("CERN-PH-TH/2004-151"), be.getField("reportno"));
} catch (SAXException e) {
throw e.getException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ public void authorsAreCorrectlyFormatted() throws Exception {
bibEntry.setField("title", "Fundamentals of Business Process Management");
bibEntry.setField("publisher", "Springer");
bibEntry.setField("year", "2015");
bibEntry.setField("author", "Dumas, Marlon and Rosa, Marcello La and Mendling, Jan and Reijers, Hajo");
bibEntry.setField("author", "Dumas, Marlon and Rosa, Marcello La and Mendling, Jan and Reijers, Hajo A.");
bibEntry.setField("date", "2015-04-12");
bibEntry.setField("ean", "9783642434730");
bibEntry.setField("isbn", "3642434738");
bibEntry.setField("pagetotal", "428");
bibEntry.setField("url", "http://www.ebook.de/de/product/23955263/marlon_dumas_marcello_la_rosa_jan_mendling_hajo_reijers_fundamentals_of_business_process_management.html");
bibEntry.setField("url", "http://www.ebook.de/de/product/23955263/marlon_dumas_marcello_la_rosa_jan_mendling_hajo_a_reijers_fundamentals_of_business_process_management.html");

Optional<BibEntry> fetchedEntry = fetcher.performSearchById("3642434738");
assertEquals(Optional.of(bibEntry), fetchedEntry);
Expand Down

0 comments on commit c517843

Please sign in to comment.