package net.sf.jabref.logic.importer;

import java.io.File;
import java.io.IOException;

import net.sf.jabref.logic.importer.fileformat.BibtexImporter;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.logic.util.io.AutoSaveUtil;
import net.sf.jabref.logic.util.io.FileBasedLock;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.specialfields.SpecialFieldsUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class OpenDatabase {

    public static final Log LOGGER = LogFactory.getLog(OpenDatabase.class);


    /**
     * Load database (bib-file) or, if there exists, a newer autosave version, unless the flag is set to ignore the autosave
     *
     * @param name Name of the BIB-file to open
     * @param ignoreAutosave true if autosave version of the file should be ignored
     * @return ParserResult which never is null
     */

    public static ParserResult loadDatabaseOrAutoSave(String name, boolean ignoreAutosave,
            ImportFormatPreferences importFormatPreferences) {
        // String in OpenDatabaseAction.java
        LOGGER.info("Opening: " + name);
        File file = new File(name);
        if (!file.exists()) {
            ParserResult pr = new ParserResult(null, null, null);
            pr.setFile(file);
            pr.setInvalid(true);
            LOGGER.error(Localization.lang("Error") + ": " + Localization.lang("File not found"));
            return pr;

        }
        try {

            if (!ignoreAutosave) {
                boolean autoSaveFound = AutoSaveUtil.newerAutoSaveExists(file);
                if (autoSaveFound) {
                    // We have found a newer autosave. Make a note of this, so it can be
                    // handled after startup:
                    ParserResult postp = new ParserResult(null, null, null);
                    postp.setPostponedAutosaveFound(true);
                    postp.setFile(file);
                    return postp;
                }
            }

            if (!FileBasedLock.waitForFileLock(file.toPath())) {
                LOGGER.error(Localization.lang("Error opening file") + " '" + name + "'. "
                        + "File is locked by another JabRef instance.");
                return ParserResult.getNullResult();
            }

            ParserResult pr = OpenDatabase.loadDatabase(file, importFormatPreferences);
            pr.setFile(file);
            if (pr.hasWarnings()) {
                for (String aWarn : pr.warnings()) {
                    LOGGER.warn(aWarn);
                }
            }
            return pr;
        } catch (IOException ex) {
            ParserResult pr = new ParserResult(null, null, null);
            pr.setFile(file);
            pr.setInvalid(true);
            pr.setErrorMessage(ex.getMessage());
            LOGGER.info("Problem opening .bib-file", ex);
            return pr;
        }

    }

    /**
     * Opens a new database.
     */
    public static ParserResult loadDatabase(File fileToOpen, ImportFormatPreferences importFormatPreferences)
            throws IOException {
        // Open and parse file
        ParserResult result = new BibtexImporter(importFormatPreferences).importDatabase(fileToOpen.toPath(),
                importFormatPreferences.getEncoding());

        if (importFormatPreferences.isKeywordSyncEnabled()) {
            for (BibEntry entry : result.getDatabase().getEntries()) {
                SpecialFieldsUtils.syncSpecialFieldsFromKeywords(entry);
            }
            LOGGER.debug("Synchronized special fields based on keywords");
        }

        return result;
    }

}

@@ -503,17 +503,16 @@
 abase %7B%0A
-%0A
     publ
@@ -580,17 +580,16 @@
 lass);%0A%0A
-%0A
     /**%0A
@@ -908,17 +908,16 @@
      */%0A
-%0A
     publ
@@ -1074,44 +1074,35 @@
     
-// String in OpenDatabaseAction.java
+File file = new File(name);
 %0A   
@@ -1143,43 +1143,8 @@
 e);%0A
-        File file = new File(name);
 %0A   
@@ -1421,17 +1421,16 @@
 urn pr;%0A
-%0A
         
@@ -1423,32 +1423,33 @@
 n pr;%0A        %7D%0A
+%0A
         try %7B%0A%0A 
@@ -1446,17 +1446,16 @@
   try %7B%0A
-%0A
         
@@ -2808,20 +2808,21 @@
  LOGGER.
-info
+error
 (%22Proble
@@ -2877,25 +2877,24 @@
 ;%0A        %7D%0A
-%0A
     %7D%0A%0A    /
@@ -3080,39 +3080,8 @@
 n %7B%0A
-        // Open and parse file%0A
     
@@ -3578,11 +3578,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Debug refactoring