Search in sources :

Example 1 with FailToSyncEntryException

use of net.viperfish.journal.framework.errors.FailToSyncEntryException 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 FailToSyncEntryException

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

the class JavaSerializationEntryDatabase method serialize.

/**
 * serialize a JavaSerializationEntryDatabase into a file
 *
 * @param serializedFile
 *            the file to store the database
 * @param db
 *            the database
 */
public static void serialize(File serializedFile, JavaSerializationEntryDatabase db) {
    try {
        CommonFunctions.initFile(serializedFile);
    } catch (IOException e1) {
        FailToSyncEntryException f = new FailToSyncEntryException("Cannot create file to serialize db:" + e1.getMessage());
        f.initCause(e1);
        throw new RuntimeException(e1);
    }
    try (ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(serializedFile)))) {
        out.writeObject(db);
        out.flush();
    } catch (IOException e) {
        FailToSyncEntryException f = new FailToSyncEntryException("Cannot serialize database:" + e.getMessage());
        f.initCause(e);
        throw new RuntimeException(f);
    }
}
Also used : FailToSyncEntryException(net.viperfish.journal.framework.errors.FailToSyncEntryException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 3 with FailToSyncEntryException

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

the class HibernateEntryDatabase method removeEntry.

@Override
public Journal removeEntry(Long id) throws FailToSyncEntryException {
    Transaction tr = this.getSession().beginTransaction();
    try {
        Journal deleted = getEntry(id);
        this.getSession().delete(getEntry(id));
        return deleted;
    } catch (RuntimeException e) {
        tr.rollback();
        FailToSyncEntryException f = new FailToSyncEntryException("Cannot delete entry from database:" + e.getMessage() + " id:" + id);
        throw f;
    } finally {
        tr.commit();
    }
}
Also used : Transaction(org.hibernate.Transaction) FailToSyncEntryException(net.viperfish.journal.framework.errors.FailToSyncEntryException) Journal(net.viperfish.journal.framework.Journal)

Example 4 with FailToSyncEntryException

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

the class HibernateEntryDatabase method updateEntry.

@Override
public Journal updateEntry(Long id, Journal j) throws FailToSyncEntryException {
    Transaction tr = this.getSession().beginTransaction();
    try {
        j.setId(id);
        this.getSession().merge(j);
        return j;
    } catch (RuntimeException e) {
        tr.rollback();
        FailToSyncEntryException f = new FailToSyncEntryException("Cannot update entry in database:" + e.getMessage() + " id:" + id + " entry:" + j);
        f.initCause(e);
        throw f;
    } finally {
        tr.commit();
    }
}
Also used : Transaction(org.hibernate.Transaction) FailToSyncEntryException(net.viperfish.journal.framework.errors.FailToSyncEntryException)

Example 5 with FailToSyncEntryException

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

the class FileEntryDatabase method load.

/**
 * load the data from the file in JSON format
 */
public synchronized void load() {
    try {
        String buf = file.read(StandardCharsets.UTF_16);
        if (buf.length() == 0) {
            return;
        }
        JsonGenerator generator = new JsonGenerator();
        struct = generator.fromJson(FileMemoryStructure.class, buf);
    } catch (IOException e) {
        FailToSyncEntryException f = new FailToSyncEntryException("Cannot load entries from file:" + file.getFile() + " message:" + e.getMessage());
        f.initCause(e);
        throw new RuntimeException(f);
    }
}
Also used : FailToSyncEntryException(net.viperfish.journal.framework.errors.FailToSyncEntryException) JsonGenerator(net.viperfish.framework.serialization.JsonGenerator) IOException(java.io.IOException)

Aggregations

FailToSyncEntryException (net.viperfish.journal.framework.errors.FailToSyncEntryException)8 IOException (java.io.IOException)3 Journal (net.viperfish.journal.framework.Journal)3 Transaction (org.hibernate.Transaction)3 JsonGenerator (net.viperfish.framework.serialization.JsonGenerator)2 OperationErrorException (net.viperfish.journal.framework.errors.OperationErrorException)2 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 FailToStoreCredentialException (net.viperfish.journal.framework.errors.FailToStoreCredentialException)1