Skip to content

Commit

Permalink
Partly working
Browse files Browse the repository at this point in the history
  • Loading branch information
Siedlerchr committed Dec 31, 2020
1 parent 9bc1b51 commit ef8c6c6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 48 deletions.
44 changes: 24 additions & 20 deletions src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.jabref.gui.exporter;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.nio.file.Path;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

import javafx.scene.control.ButtonBar;
Expand All @@ -22,8 +22,8 @@
import org.jabref.gui.util.FileDialogConfiguration;
import org.jabref.logic.autosaveandbackup.AutosaveManager;
import org.jabref.logic.autosaveandbackup.BackupManager;
import org.jabref.logic.exporter.AtomicFileWriter;
import org.jabref.logic.exporter.BibtexDatabaseWriter;
import org.jabref.logic.exporter.GlobalSaveManager;
import org.jabref.logic.exporter.GlobalSaveManager.SaveResult;
import org.jabref.logic.exporter.SaveException;
import org.jabref.logic.exporter.SavePreferences;
import org.jabref.logic.l10n.Encodings;
Expand Down Expand Up @@ -55,6 +55,8 @@ public class SaveDatabaseAction {
private final PreferencesService preferences;
private final BibEntryTypesManager entryTypesManager;

private final GlobalSaveManager saveManager;

public enum SaveDatabaseMode {
SILENT, NORMAL
}
Expand All @@ -65,6 +67,7 @@ public SaveDatabaseAction(LibraryTab libraryTab, PreferencesService preferences,
this.dialogService = frame.getDialogService();
this.preferences = preferences;
this.entryTypesManager = entryTypesManager;
this.saveManager = GlobalSaveManager.start(libraryTab.getBibDatabaseContext());
}

public boolean save() {
Expand Down Expand Up @@ -213,27 +216,28 @@ private boolean saveDatabase(Path file, boolean selectedOnly, Charset encoding,
SavePreferences preferences = this.preferences.getSavePreferences()
.withEncoding(encoding)
.withSaveType(saveType);
try (AtomicFileWriter fileWriter = new AtomicFileWriter(file, preferences.getEncoding(), preferences.shouldMakeBackup())) {
BibtexDatabaseWriter databaseWriter = new BibtexDatabaseWriter(fileWriter, preferences, entryTypesManager);

if (selectedOnly) {
databaseWriter.savePartOfDatabase(libraryTab.getBibDatabaseContext(), libraryTab.getSelectedEntries());
} else {
databaseWriter.saveDatabase(libraryTab.getBibDatabaseContext());
}

libraryTab.registerUndoableChanges(databaseWriter.getSaveActionsFieldChanges());
Future<SaveResult> saved = this.saveManager.save(file, selectedOnly, libraryTab.getSelectedEntries(), preferences);

if (fileWriter.hasEncodingProblems()) {
saveWithDifferentEncoding(file, selectedOnly, preferences.getEncoding(), fileWriter.getEncodingProblems(), saveType);
SaveResult result;
try {
if(saved.isCancelled())
{
return false;
}
result = saved.get();
libraryTab.registerUndoableChanges(result.getFieldChanges());

if (!result.getEncodingProblems().isEmpty()) {
saveWithDifferentEncoding(file, selectedOnly, preferences.getEncoding(), result.getEncodingProblems(), saveType);
}
} catch (UnsupportedCharsetException ex) {
throw new SaveException(Localization.lang("Character encoding '%0' is not supported.", encoding.displayName()), ex);
} catch (IOException ex) {
throw new SaveException("Problems saving: " + ex, ex);
return true;
} catch (InterruptedException | ExecutionException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return true;
return false;
}

private void saveWithDifferentEncoding(Path file, boolean selectedOnly, Charset encoding, Set<Character> encodingProblems, SavePreferences.DatabaseSaveType saveType) throws SaveException {
Expand Down
43 changes: 15 additions & 28 deletions src/main/java/org/jabref/logic/exporter/GlobalSaveManager.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jabref.logic.exporter;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.nio.file.Path;
import java.util.ArrayList;
Expand All @@ -10,14 +9,13 @@
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.Future;

import org.jabref.gui.Globals;
import org.jabref.gui.StateManager;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.DelayTaskThrottler;
import org.jabref.model.FieldChange;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.preferences.PreferencesService;
import org.jabref.model.entry.BibEntry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -27,27 +25,20 @@ public class GlobalSaveManager {
private static Set<GlobalSaveManager> runningInstances = new HashSet<>();
private static final Logger LOGGER = LoggerFactory.getLogger(GlobalSaveManager.class);

private final DelayTaskThrottler<SaveResult> throttler = new DelayTaskThrottler<>(1500);
private static DelayTaskThrottler<SaveResult> throttler = new DelayTaskThrottler<>(1500);
private final BibDatabaseContext bibDatabaseContext;

private final StateManager stateManager;

private final PreferencesService preferencesService;

private GlobalSaveManager(StateManager stateManager, PreferencesService preferencesService, BibEntryTypesManager entryTypesManager) {
this.bibDatabaseContext = stateManager.getActiveDatabase().get();
this.stateManager = stateManager;
this.preferencesService = preferencesService;

private GlobalSaveManager(BibDatabaseContext context) {
this.bibDatabaseContext = context;
}

public static void shutdown(BibDatabaseContext context) {
runningInstances.stream().filter(instance -> instance.bibDatabaseContext == context).forEach(GlobalSaveManager::shutdown);
runningInstances.removeIf(instance -> instance.bibDatabaseContext == context);
}

public static GlobalSaveManager start(StateManager stateManager, PreferencesService preferencesService, BibEntryTypesManager entryTypesManager) {
GlobalSaveManager saveAction = new GlobalSaveManager(stateManager, preferencesService, entryTypesManager);
public static GlobalSaveManager start(BibDatabaseContext context) {
GlobalSaveManager saveAction = new GlobalSaveManager(context);

if (runningInstances.contains(saveAction)) {
LOGGER.debug("I have an instance " + saveAction);
Expand All @@ -60,21 +51,17 @@ private void shutdown() {
this.throttler.shutdown();
}

public Future<SaveResult> save(Path file, boolean selectedOnly, Charset encoding, SavePreferences.DatabaseSaveType saveType) {
return throttler.scheduleTask(() -> saveThrottled(file, selectedOnly, encoding, saveType));
public Future<SaveResult> save(Path file, boolean selectedOnly, List<BibEntry> entries, SavePreferences savePrefs) {
return throttler.scheduleTask(() -> saveThrottled(file, selectedOnly, entries, savePrefs));
}

private SaveResult saveThrottled(Path file, boolean selectedOnly, Charset encoding, SavePreferences.DatabaseSaveType saveType) throws SaveException {

SavePreferences savePrefs = this.preferencesService.getSavePreferences()
.withEncoding(encoding)
.withSaveType(saveType);
private SaveResult saveThrottled(Path file, boolean selectedOnly, List<BibEntry> entries, SavePreferences savePrefs) throws SaveException {

try (AtomicFileWriter fileWriter = new AtomicFileWriter(file, savePrefs.getEncoding(), savePrefs.shouldMakeBackup())) {
BibtexDatabaseWriter databaseWriter = new BibtexDatabaseWriter(fileWriter, savePrefs, Globals.entryTypesManager);

if (selectedOnly) {
databaseWriter.savePartOfDatabase(this.bibDatabaseContext, this.stateManager.getSelectedEntries());
databaseWriter.savePartOfDatabase(this.bibDatabaseContext, entries);
} else {
databaseWriter.saveDatabase(this.bibDatabaseContext);
}
Expand All @@ -83,7 +70,7 @@ private SaveResult saveThrottled(Path file, boolean selectedOnly, Charset encodi
return saveResult;

} catch (UnsupportedCharsetException ex) {
throw new SaveException(Localization.lang("Character encoding '%0' is not supported.", encoding.displayName()), ex);
throw new SaveException(Localization.lang("Character encoding '%0' is not supported.", savePrefs.getEncoding().displayName()), ex);
} catch (IOException ex) {
throw new SaveException("Problems saving: " + ex, ex);
}
Expand Down Expand Up @@ -118,16 +105,16 @@ public class SaveResult {
Set<Character> encodingProblems = new HashSet<>();
List<FieldChange> fieldChanges = new ArrayList<>();

public SaveResult(Set<Character> encodingProlems, List<FieldChange> fieldChanges) {
public SaveResult(Set<Character> encodingProblems, List<FieldChange> fieldChanges) {
this.encodingProblems = encodingProblems;
this.fieldChanges = fieldChanges;
}

Set<Character> getEncodingProblems() {
public Set<Character> getEncodingProblems() {
return this.encodingProblems;
}

List<FieldChange> getFieldChanges() {
public List<FieldChange> getFieldChanges() {
return this.fieldChanges;
}

Expand Down

0 comments on commit ef8c6c6

Please sign in to comment.