Search in sources :

Example 1 with NotASharedDatabaseException

use of org.jabref.shared.exception.NotASharedDatabaseException in project jabref by JabRef.

the class OpenDatabaseAction method openTheFile.

/**
     * @param file the file, may be null or not existing
     */
private void openTheFile(Path file, boolean raisePanel) {
    Objects.requireNonNull(file);
    if (Files.exists(file)) {
        Path fileToLoad = file.toAbsolutePath();
        frame.output(Localization.lang("Opening") + ": '" + file + "'");
        String fileName = file.getFileName().toString();
        Globals.prefs.put(JabRefPreferences.WORKING_DIRECTORY, fileToLoad.getParent().toString());
        if (FileBasedLock.hasLockFile(file)) {
            Optional<FileTime> modificationTime = FileBasedLock.getLockFileTimeStamp(file);
            if ((modificationTime.isPresent()) && ((System.currentTimeMillis() - modificationTime.get().toMillis()) > FileBasedLock.LOCKFILE_CRITICAL_AGE)) {
                // The lock file is fairly old, so we can offer to "steal" the file:
                int answer = JOptionPane.showConfirmDialog(null, "<html>" + Localization.lang("Error opening file") + " '" + fileName + "'. " + Localization.lang("File is locked by another JabRef instance.") + "<p>" + Localization.lang("Do you want to override the file lock?"), Localization.lang("File locked"), JOptionPane.YES_NO_OPTION);
                if (answer == JOptionPane.YES_OPTION) {
                    FileBasedLock.deleteLockFile(file);
                } else {
                    return;
                }
            } else if (!FileBasedLock.waitForFileLock(file)) {
                JOptionPane.showMessageDialog(null, Localization.lang("Error opening file") + " '" + fileName + "'. " + Localization.lang("File is locked by another JabRef instance."), Localization.lang("Error"), JOptionPane.ERROR_MESSAGE);
                return;
            }
        }
        if (BackupManager.checkForBackupFile(fileToLoad)) {
            BackupUIManager.showRestoreBackupDialog(frame, fileToLoad);
        }
        ParserResult result;
        result = OpenDatabase.loadDatabase(fileToLoad.toString(), Globals.prefs.getImportFormatPreferences());
        if (result.getDatabase().isShared()) {
            try {
                new SharedDatabaseUIManager(frame).openSharedDatabaseFromParserResult(result);
            } catch (SQLException | DatabaseNotSupportedException | InvalidDBMSConnectionPropertiesException | NotASharedDatabaseException e) {
                // do not open the original file
                result.getDatabaseContext().clearDatabaseFile();
                result.getDatabase().clearSharedDatabaseID();
                LOGGER.error("Connection error", e);
                JOptionPane.showMessageDialog(frame, e.getMessage() + "\n\n" + Localization.lang("A local copy will be opened."), Localization.lang("Connection error"), JOptionPane.WARNING_MESSAGE);
            }
        }
        BasePanel panel = addNewDatabase(result, file, raisePanel);
        // After adding the database, go through our list and see if
        // any post open actions need to be done. For instance, checking
        // if we found new entry types that can be imported, or checking
        // if the database contents should be modified due to new features
        // in this version of JabRef:
        final ParserResult finalReferenceToResult = result;
        SwingUtilities.invokeLater(() -> OpenDatabaseAction.performPostOpenActions(panel, finalReferenceToResult));
    }
}
Also used : Path(java.nio.file.Path) SharedDatabaseUIManager(org.jabref.gui.shared.SharedDatabaseUIManager) NotASharedDatabaseException(org.jabref.shared.exception.NotASharedDatabaseException) BasePanel(org.jabref.gui.BasePanel) SQLException(java.sql.SQLException) InvalidDBMSConnectionPropertiesException(org.jabref.shared.exception.InvalidDBMSConnectionPropertiesException) FileTime(java.nio.file.attribute.FileTime) ParserResult(org.jabref.logic.importer.ParserResult) DatabaseNotSupportedException(org.jabref.shared.exception.DatabaseNotSupportedException)

Example 2 with NotASharedDatabaseException

use of org.jabref.shared.exception.NotASharedDatabaseException in project jabref by JabRef.

the class SharedDatabaseUIManager method openSharedDatabaseFromParserResult.

public void openSharedDatabaseFromParserResult(ParserResult parserResult) throws SQLException, DatabaseNotSupportedException, InvalidDBMSConnectionPropertiesException, NotASharedDatabaseException {
    Optional<String> sharedDatabaseIDOptional = parserResult.getDatabase().getSharedDatabaseID();
    if (!sharedDatabaseIDOptional.isPresent()) {
        throw new NotASharedDatabaseException();
    }
    String sharedDatabaseID = sharedDatabaseIDOptional.get();
    DBMSConnectionProperties dbmsConnectionProperties = new DBMSConnectionProperties(new SharedDatabasePreferences(sharedDatabaseID));
    JabRefFrame frame = JabRefGUI.getMainFrame();
    BibDatabaseMode selectedMode = Globals.prefs.getDefaultBibDatabaseMode();
    BibDatabaseContext bibDatabaseContext = new BibDatabaseContext(new Defaults(selectedMode), DatabaseLocation.SHARED, Globals.prefs.getKeywordDelimiter(), Globals.prefs.getKeyPattern());
    bibDatabaseContext.getDatabase().setSharedDatabaseID(sharedDatabaseID);
    bibDatabaseContext.setDatabaseFile(parserResult.getDatabaseContext().getDatabaseFile().orElse(null));
    dbmsSynchronizer = bibDatabaseContext.getDBMSSynchronizer();
    dbmsSynchronizer.openSharedDatabase(dbmsConnectionProperties);
    dbmsSynchronizer.registerListener(this);
    parserResult.setDatabaseContext(bibDatabaseContext);
    frame.output(Localization.lang("Connection_to_%0_server_established.", dbmsConnectionProperties.getType().toString()));
}
Also used : SharedDatabasePreferences(org.jabref.shared.prefs.SharedDatabasePreferences) JabRefFrame(org.jabref.gui.JabRefFrame) NotASharedDatabaseException(org.jabref.shared.exception.NotASharedDatabaseException) Defaults(org.jabref.model.Defaults) BibDatabaseMode(org.jabref.model.database.BibDatabaseMode) BibDatabaseContext(org.jabref.model.database.BibDatabaseContext) DBMSConnectionProperties(org.jabref.shared.DBMSConnectionProperties)

Aggregations

NotASharedDatabaseException (org.jabref.shared.exception.NotASharedDatabaseException)2 Path (java.nio.file.Path)1 FileTime (java.nio.file.attribute.FileTime)1 SQLException (java.sql.SQLException)1 BasePanel (org.jabref.gui.BasePanel)1 JabRefFrame (org.jabref.gui.JabRefFrame)1 SharedDatabaseUIManager (org.jabref.gui.shared.SharedDatabaseUIManager)1 ParserResult (org.jabref.logic.importer.ParserResult)1 Defaults (org.jabref.model.Defaults)1 BibDatabaseContext (org.jabref.model.database.BibDatabaseContext)1 BibDatabaseMode (org.jabref.model.database.BibDatabaseMode)1 DBMSConnectionProperties (org.jabref.shared.DBMSConnectionProperties)1 DatabaseNotSupportedException (org.jabref.shared.exception.DatabaseNotSupportedException)1 InvalidDBMSConnectionPropertiesException (org.jabref.shared.exception.InvalidDBMSConnectionPropertiesException)1 SharedDatabasePreferences (org.jabref.shared.prefs.SharedDatabasePreferences)1