package org.jabref.logic.texparser;

import java.nio.file.Path;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.logic.importer.OpenDatabase;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.texparser.TexBibEntriesResolverResult;
import org.jabref.model.texparser.TexParserResult;
import org.jabref.model.util.FileUpdateMonitor;

public class TexBibEntriesResolver {

    private final BibDatabase masterDatabase;
    private final ImportFormatPreferences importFormatPreferences;
    private final FileUpdateMonitor fileMonitor;

    public TexBibEntriesResolver(BibDatabase masterDatabase, ImportFormatPreferences importFormatPreferences, FileUpdateMonitor fileMonitor) {
        this.masterDatabase = masterDatabase;
        this.importFormatPreferences = importFormatPreferences;
        this.fileMonitor = fileMonitor;
    }

    /**
     * Resolve all BibTeX entries and check if they are in the given database.
     */
    public TexBibEntriesResolverResult resolve(TexParserResult texParserResult) {
        TexBibEntriesResolverResult resolverResult = new TexBibEntriesResolverResult(texParserResult);

        // Preload databases from BIB files.
        Map<Path, BibDatabase> bibDatabases = resolverResult.getBibFiles().values().stream().distinct().collect(Collectors.toMap(
                Function.identity(), path -> OpenDatabase.loadDatabase(path.toString(), importFormatPreferences, fileMonitor).getDatabase()));

        // Add all new entries to the newEntries set.
        resolverResult.getNewEntries().addAll(
                // Get all pairs Entry<String entryKey, Citation>.
                texParserResult.getCitations().entries().stream().distinct().flatMap(mapEntry ->
                        // Load the associated BIB file.
                        texParserResult.getBibFiles().get(mapEntry.getValue().getPath()).stream().distinct().flatMap(bibFile ->
                                // Get a specific entry from an entryKey and a BIB file.
                                bibDatabases.get(bibFile).getEntriesByKey(mapEntry.getKey()).stream().distinct()
                                            // Check if there is already an entry with the same key in the given database.
                                            .filter(entry -> !entry.equals(masterDatabase.getEntryByKey(entry.getCiteKeyOptional().orElse("")).orElse(new BibEntry())))
                                            // Add cross-referencing data to the entry (fill empty fields).
                                            .map(entry -> {
                                                bibDatabases.get(bibFile).getReferencedEntry(entry).ifPresent(refEntry ->
                                                        refEntry.getFields().forEach(field -> entry.getFieldMap().putIfAbsent(field, refEntry.getFieldOrAlias(field).orElse(""))));
                                                return entry;
                                            })))
                               .collect(Collectors.toSet()));

        return resolverResult;
    }
}

@@ -79,16 +79,38 @@
 il.Map;%0A
+import java.util.Set;%0A
 import j
@@ -172,16 +172,48 @@
 lectors;
+%0Aimport java.util.stream.Stream;
 %0A%0Aimport
@@ -397,16 +397,60 @@
 bEntry;%0A
+import org.jabref.model.texparser.Citation;%0A
 import o
@@ -1709,117 +1709,8 @@
 );%0A%0A
-        // Add all new entries to the newEntries set.%0A        resolverResult.getNewEntries().addAll(%0A        
     
@@ -1764,39 +1764,85 @@
 ation%3E.%0A        
-       
+Stream%3CMap.Entry%3CString, Citation%3E%3E citationsStream =
  texParserResult
@@ -1890,109 +1890,481 @@
 ct()
-.flatMap(mapEntry -%3E%0A                        // Load the associated BIB file.
+;%0A%0A        Set%3CBibEntry%3E newEntries = citationsStream.flatMap(mapEntry -%3E apply(mapEntry, texParserResult, bibDatabases)).collect(Collectors.toSet());%0A%0A        // Add all new entries to the newEntries set.%0A        resolverResult.getNewEntries().addAll(newEntries);%0A%0A        return resolverResult;
 %0A    
+%7D%0A%0A
     
-               
+private Stream%3C? extends BibEntry%3E apply(Map.Entry%3CString, Citation%3E mapEntry, TexParserResult texParserResult, Map%3CPath, BibDatabase%3E bibDatabases) %7B%0A        return
  tex
@@ -2464,32 +2464,16 @@
 File -%3E%0A
-                
         
@@ -2537,32 +2537,16 @@
 B file.%0A
-                
         
@@ -2662,32 +2662,16 @@
         
-                
 // Check
@@ -2741,32 +2741,16 @@
 tabase.%0A
-                
         
@@ -2921,32 +2921,16 @@
         
-  
-              
 // Add c
@@ -3017,80 +3017,200 @@
     
-                .map(entry -%3E %7B%0A                       
+.map(entry -%3E addCrossReferencingData(entry, bibFile, bibDatabases)));%0A
     
+%7D%0A%0A
     
-         
+private BibEntry addCrossReferencingData(BibEntry entry, Path bibFile, Map%3CPath, BibDatabase%3E bibDatabases) %7B%0A
     
@@ -3291,48 +3291,8 @@
  -%3E%0A
-                                        
     
@@ -3431,53 +3431,17 @@
 ));%0A
-                                     
+%0A
         
    r
@@ -3432,27 +3432,24 @@
 );%0A%0A        
-   
 return entry
@@ -3454,151 +3454,8 @@
 ry;%0A
-                                            %7D)))%0A                               .collect(Collectors.toSet()));%0A%0A        return resolverResult;%0A
     

Update TexBibEntriesResolver for improving readability