Search in sources :

Example 1 with OperationErrorException

use of net.viperfish.journal.framework.errors.OperationErrorException in project vsDiaryWriter by shilongdai.

the class ChangePasswordOperation method execute.

@Override
public void execute() {
    List<Journal> buffer = db().getAll();
    // set the new password
    try {
        auth().setPassword(pass);
        // clear all
        indexer().clear();
        db().clear();
        // re-encrypt all entries
        for (Journal i : buffer) {
            i.setId(null);
            Journal added = db().addEntry(i);
            indexer().add(added);
        }
    } catch (FailToStoreCredentialException e) {
        File userHome = new File(System.getProperty("user.home"));
        File export = new File(userHome, "export.txt");
        OperationErrorException err = new OperationErrorException("Failed to save the new password:" + e.getMessage() + " Exporting all entries to " + export.getAbsolutePath());
        new ExportJournalOperation(export.getAbsolutePath()).execute();
        throw err;
    } catch (FailToSyncEntryException e) {
        File userHome = new File(System.getProperty("user.home"));
        File export = new File(userHome, "export.txt");
        OperationErrorException err = new OperationErrorException("Failed to re-add entries with the new password:" + e.getMessage() + " Exporting all entries to " + export.getAbsolutePath());
        new ExportJournalOperation(export.getAbsolutePath()).execute();
        throw err;
    }
}
Also used : FailToSyncEntryException(net.viperfish.journal.framework.errors.FailToSyncEntryException) FailToStoreCredentialException(net.viperfish.journal.framework.errors.FailToStoreCredentialException) Journal(net.viperfish.journal.framework.Journal) OperationErrorException(net.viperfish.journal.framework.errors.OperationErrorException) File(java.io.File)

Example 2 with OperationErrorException

use of net.viperfish.journal.framework.errors.OperationErrorException in project vsDiaryWriter by shilongdai.

the class ChangeConfigurationOperation method execute.

@Override
public void execute() {
    Map<String, String> old = backUpConfig(config);
    // actually load all units
    this.refresh();
    // save all entries in memory
    List<Journal> result = db().getAll();
    // save the password in memory
    String password = auth().getPassword();
    try {
        // clear all entries
        resetUnits();
        // updates configuration
        for (Entry<String, String> i : config.entrySet()) {
            Configuration.setProperty(i.getKey(), i.getValue());
        }
        // refresh all providers to reflect changes in configuration
        EntryDatabases.INSTANCE.refreshAll();
        Indexers.INSTANCE.refreshAll();
        AuthManagers.INSTANCE.refreshAll();
        JournalTransformers.INSTANCE.refreshAll();
        // re-initialize components
        this.refresh();
        // clear new components
        resetUnits();
        // set the password on the new Auth manager
        auth().setPassword(password);
        // configuration, entry database, and indexer
        for (Journal i : result) {
            i.setId(null);
            db().addEntry(i);
            indexer().add(i);
        }
    } catch (Exception e1) {
        ChangeConfigurationFailException cf = new ChangeConfigurationFailException("Failed to change components from:" + old + " to:" + config + "message:" + e1.getMessage(), e1);
        try {
            revert(result, password, old);
        } catch (Exception e) {
            File userHome = new File(System.getProperty("user.home"));
            File export = new File(userHome, "export.txt");
            OperationErrorException fr = new OperationErrorException("Failed to revert changes, application not in usable status, please clear all data files. Exporting all entries to " + export.getAbsolutePath());
            fr.initCause(cf);
            ExportJournalOperation dump = new ExportJournalOperation(export.getAbsolutePath());
            dump.execute();
            throw fr;
        }
        throw new RuntimeException(cf);
    } finally {
        // save the configuration
        try {
            Configuration.save();
        } catch (ConfigurationException e) {
            ChangeConfigurationFailException cf = new ChangeConfigurationFailException("Failed to save configuration, change not persistent.", e);
            throw new RuntimeException(cf);
        }
    }
}
Also used : ConfigurationException(org.apache.commons.configuration.ConfigurationException) Journal(net.viperfish.journal.framework.Journal) OperationErrorException(net.viperfish.journal.framework.errors.OperationErrorException) File(java.io.File) FailToSyncEntryException(net.viperfish.journal.framework.errors.FailToSyncEntryException) OperationErrorException(net.viperfish.journal.framework.errors.OperationErrorException) FailToStoreCredentialException(net.viperfish.journal.framework.errors.FailToStoreCredentialException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) ChangeConfigurationFailException(net.viperfish.journal.framework.errors.ChangeConfigurationFailException) CannotClearPasswordException(net.viperfish.journal.framework.errors.CannotClearPasswordException) ChangeConfigurationFailException(net.viperfish.journal.framework.errors.ChangeConfigurationFailException)

Example 3 with OperationErrorException

use of net.viperfish.journal.framework.errors.OperationErrorException in project vsDiaryWriter by shilongdai.

the class EditEntryOperation method execute.

@Override
public void execute() {
    Journal e = db().getEntry(id);
    edit(e);
    try {
        db().updateEntry(id, e);
    } catch (FailToSyncEntryException e1) {
        OperationErrorException fail = new OperationErrorException("Cannot update journal " + id + " in the database:" + e1.getMessage());
        fail.initCause(e1);
        throw fail;
    }
    indexer().delete(id);
    indexer().add(e);
}
Also used : FailToSyncEntryException(net.viperfish.journal.framework.errors.FailToSyncEntryException) Journal(net.viperfish.journal.framework.Journal) OperationErrorException(net.viperfish.journal.framework.errors.OperationErrorException)

Aggregations

Journal (net.viperfish.journal.framework.Journal)3 FailToSyncEntryException (net.viperfish.journal.framework.errors.FailToSyncEntryException)3 OperationErrorException (net.viperfish.journal.framework.errors.OperationErrorException)3 File (java.io.File)2 FailToStoreCredentialException (net.viperfish.journal.framework.errors.FailToStoreCredentialException)2 CannotClearPasswordException (net.viperfish.journal.framework.errors.CannotClearPasswordException)1 ChangeConfigurationFailException (net.viperfish.journal.framework.errors.ChangeConfigurationFailException)1 ConfigurationException (org.apache.commons.configuration.ConfigurationException)1