Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduced calls to Globals.prefs and Globals.entryTypesManager #10177

Merged
merged 11 commits into from
Sep 2, 2023
20 changes: 9 additions & 11 deletions src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,19 @@ public class ArgumentProcessor {
private final Mode startupMode;
private final PreferencesService preferencesService;
private final FileUpdateMonitor fileUpdateMonitor;
private final BibEntryTypesManager entryTypesManager;
private boolean noGUINeeded;

public ArgumentProcessor(String[] args,
Mode startupMode,
PreferencesService preferencesService,
FileUpdateMonitor fileUpdateMonitor) throws org.apache.commons.cli.ParseException {
FileUpdateMonitor fileUpdateMonitor,
BibEntryTypesManager entryTypesManager) throws org.apache.commons.cli.ParseException {
this.cli = new JabRefCLI(args);
this.startupMode = startupMode;
this.preferencesService = preferencesService;
this.fileUpdateMonitor = fileUpdateMonitor;
this.entryTypesManager = entryTypesManager;

this.parserResults = processArguments();
}
Expand Down Expand Up @@ -291,13 +294,12 @@ private void writeMetadataToPdf(List<ParserResult> loaded,
}
ParserResult pr = loaded.get(loaded.size() - 1);
BibDatabaseContext databaseContext = pr.getDatabaseContext();
BibDatabase dataBase = pr.getDatabase();

XmpPdfExporter xmpPdfExporter = new XmpPdfExporter(xmpPreferences);
EmbeddedBibFilePdfExporter embeddedBibFilePdfExporter = new EmbeddedBibFilePdfExporter(databaseMode, entryTypesManager, fieldPreferences);

if ("all".equals(filesAndCitekeys)) {
for (BibEntry entry : dataBase.getEntries()) {
for (BibEntry entry : databaseContext.getEntries()) {
writeMetadataToPDFsOfEntry(
databaseContext,
entry.getCitationKey().orElse("<no cite key defined>"),
Expand All @@ -324,7 +326,6 @@ private void writeMetadataToPdf(List<ParserResult> loaded,

writeMetadataToPdfByCitekey(
databaseContext,
dataBase,
citeKeys,
filePreferences,
xmpPdfExporter,
Expand All @@ -334,7 +335,6 @@ private void writeMetadataToPdf(List<ParserResult> loaded,
embeddBibfile);
writeMetadataToPdfByFileNames(
databaseContext,
dataBase,
pdfs,
filePreferences,
xmpPdfExporter,
Expand Down Expand Up @@ -374,7 +374,6 @@ private void writeMetadataToPDFsOfEntry(BibDatabaseContext databaseContext,
}

private void writeMetadataToPdfByCitekey(BibDatabaseContext databaseContext,
BibDatabase dataBase,
List<String> citeKeys,
FilePreferences filePreferences,
XmpPdfExporter xmpPdfExporter,
Expand All @@ -383,7 +382,7 @@ private void writeMetadataToPdfByCitekey(BibDatabaseContext databaseContext,
boolean writeXMP,
boolean embeddBibfile) {
for (String citeKey : citeKeys) {
List<BibEntry> bibEntryList = dataBase.getEntriesByCitationKey(citeKey);
List<BibEntry> bibEntryList = databaseContext.getDatabase().getEntriesByCitationKey(citeKey);
if (bibEntryList.isEmpty()) {
System.err.printf("Skipped - Cannot find %s in library.%n", citeKey);
continue;
Expand All @@ -395,7 +394,6 @@ private void writeMetadataToPdfByCitekey(BibDatabaseContext databaseContext,
}

private void writeMetadataToPdfByFileNames(BibDatabaseContext databaseContext,
BibDatabase dataBase,
List<String> pdfs,
FilePreferences filePreferences,
XmpPdfExporter xmpPdfExporter,
Expand All @@ -411,14 +409,14 @@ private void writeMetadataToPdfByFileNames(BibDatabaseContext databaseContext,
if (Files.exists(filePath)) {
try {
if (writeXMP) {
if (xmpPdfExporter.exportToFileByPath(databaseContext, dataBase, filePreferences, filePath, abbreviationRepository)) {
if (xmpPdfExporter.exportToFileByPath(databaseContext, filePreferences, filePath, abbreviationRepository)) {
System.out.printf("Successfully written XMP metadata of at least one entry to %s%n", fileName);
} else {
System.out.printf("File %s is not linked to any entry in database.%n", fileName);
}
}
if (embeddBibfile) {
if (embeddedBibFilePdfExporter.exportToFileByPath(databaseContext, dataBase, filePreferences, filePath, abbreviationRepository)) {
if (embeddedBibFilePdfExporter.exportToFileByPath(databaseContext, filePreferences, filePath, abbreviationRepository)) {
System.out.printf("Successfully embedded XMP metadata of at least one entry to %s%n", fileName);
} else {
System.out.printf("File %s is not linked to any entry in database.%n", fileName);
Expand Down Expand Up @@ -607,7 +605,7 @@ private void saveDatabase(BibDatabase newBase, String subName) {
saveConfiguration,
preferencesService.getFieldPreferences(),
preferencesService.getCitationKeyPatternPreferences(),
Globals.entryTypesManager);
entryTypesManager);
databaseWriter.saveDatabase(new BibDatabaseContext(newBase));

// Show just a warning message if encoding did not work for all characters:
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/org/jabref/cli/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.jabref.logic.remote.client.RemoteClient;
import org.jabref.logic.util.OS;
import org.jabref.migrations.PreferencesMigrations;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.util.FileUpdateMonitor;
import org.jabref.preferences.JabRefPreferences;
import org.jabref.preferences.PreferencesService;
Expand Down Expand Up @@ -59,10 +60,13 @@ public static void main(String[] args) {

addLogToDisk();
try {
BibEntryTypesManager entryTypesManager = new BibEntryTypesManager();
Globals.entryTypesManager = entryTypesManager;

// Init preferences
final JabRefPreferences preferences = JabRefPreferences.getInstance();
Globals.prefs = preferences;
PreferencesMigrations.runMigrations(preferences);
PreferencesMigrations.runMigrations(preferences, entryTypesManager);

// Early exit in case another instance is already running
if (!handleMultipleAppInstances(ARGUMENTS, preferences)) {
Expand All @@ -82,7 +86,8 @@ public static void main(String[] args) {
ArgumentProcessor argumentProcessor = new ArgumentProcessor(
ARGUMENTS, ArgumentProcessor.Mode.INITIAL_START,
preferences,
fileUpdateMonitor);
fileUpdateMonitor,
entryTypesManager);
if (argumentProcessor.shouldShutDown()) {
LOGGER.debug("JabRef shut down after processing command line arguments");
return;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/jabref/gui/ClipBoardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.jabref.logic.bibtex.FieldWriter;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.preferences.PreferencesService;

import org.slf4j.Logger;
Expand Down Expand Up @@ -141,9 +142,9 @@ public void setContent(String string) {
setPrimaryClipboardContent(content);
}

public void setContent(List<BibEntry> entries) throws IOException {
public void setContent(List<BibEntry> entries, BibEntryTypesManager entryTypesManager) throws IOException {
final ClipboardContent content = new ClipboardContent();
BibEntryWriter writer = new BibEntryWriter(new FieldWriter(preferencesService.getFieldPreferences()), Globals.entryTypesManager);
BibEntryWriter writer = new BibEntryWriter(new FieldWriter(preferencesService.getFieldPreferences()), entryTypesManager);
String serializedEntries = writer.serializeAll(entries, BibDatabaseMode.BIBTEX);
// BibEntry is not Java serializable. Thus, we need to do the serialization manually
// At reading of the clipboard in JabRef, we parse the plain string in all cases, so we don't need to flag we put BibEntries here
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class Globals {
public static ProtectedTermsLoader protectedTermsLoader;

public static CountingUndoManager undoManager = new CountingUndoManager();
public static BibEntryTypesManager entryTypesManager = new BibEntryTypesManager();
public static BibEntryTypesManager entryTypesManager;

private static ClipBoardManager clipBoardManager = null;
private static KeyBindingRepository keyBindingRepository;
Expand Down Expand Up @@ -123,7 +123,7 @@ public static void startBackgroundTasks() {
} */
RemotePreferences remotePreferences = prefs.getRemotePreferences();
if (remotePreferences.useRemoteServer()) {
Globals.REMOTE_LISTENER.openAndStart(new CLIMessageHandler(prefs, fileUpdateMonitor), remotePreferences.getPort());
Globals.REMOTE_LISTENER.openAndStart(new CLIMessageHandler(prefs, fileUpdateMonitor, entryTypesManager), remotePreferences.getPort());
}
}

Expand Down
Loading