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;
}
}
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);
}
}
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();
}
}
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();
}
}
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);
}
}
Aggregations