Search in sources :

Example 26 with Journal

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

the class SearchEntryOperation method execute.

@Override
public void execute() {
    Set<JournalPointer> searched = new TreeSet<>();
    try {
        if (firstTime) {
            if (indexer().isMemoryBased()) {
                for (Journal j : db().getAll()) {
                    indexer().add(j);
                }
            }
            firstTime = false;
        }
        Iterable<Long> indexResult = indexer().search(query);
        for (Long id : indexResult) {
            Journal j = db().getEntry(id);
            if (j == null) {
                indexer().delete(id);
                continue;
            }
            searched.add(new JournalPointer(j));
        }
    } finally {
        setResult(searched);
    }
}
Also used : JournalPointer(net.viperfish.journal.framework.JournalPointer) TreeSet(java.util.TreeSet) Journal(net.viperfish.journal.framework.Journal)

Example 27 with Journal

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

the class DatabaseStub method removeEntry.

@Override
public Journal removeEntry(Long id) {
    Journal deleted = getEntry(id);
    backend.remove(id);
    return deleted;
}
Also used : Journal(net.viperfish.journal.framework.Journal)

Example 28 with Journal

use of net.viperfish.journal.framework.Journal 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 29 with Journal

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

the class FileEntryDatabaseTest method testPersistence.

@Test
public void testPersistence() {
    File dir = new File("test");
    FileEntryDatabase database = new FileEntryDatabase(new IOFile(new File(dir, "testPersist"), new TextIOStreamHandler()));
    for (int i = 0; i < 10; ++i) {
        Journal j = new Journal();
        j.setSubject("testPersist");
        j.setContent("testPersist");
        database.addEntry(j);
    }
    database.flush();
    database = new FileEntryDatabase(new IOFile(new File(dir, "testPersist"), new TextIOStreamHandler()));
    database.load();
    int count = 0;
    for (Journal i : database.getAll()) {
        Assert.assertEquals("testPersist", i.getContent());
        Assert.assertEquals("testPersist", i.getSubject());
        count++;
    }
    Assert.assertEquals(10, count);
    CommonFunctions.delete(new File("testPersist"));
}
Also used : TextIOStreamHandler(net.viperfish.framework.file.TextIOStreamHandler) Journal(net.viperfish.journal.framework.Journal) IOFile(net.viperfish.framework.file.IOFile) File(java.io.File) IOFile(net.viperfish.framework.file.IOFile) Test(org.junit.Test) DatabaseTest(net.viperfish.journal.framework.DatabaseTest)

Example 30 with Journal

use of net.viperfish.journal.framework.Journal 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)35 Test (org.junit.Test)13 Date (java.util.Date)8 File (java.io.File)7 JournalPointer (net.viperfish.journal.framework.JournalPointer)6 IOException (java.io.IOException)4 FailToSyncEntryException (net.viperfish.journal.framework.errors.FailToSyncEntryException)4 CipherException (net.viperfish.journal.framework.errors.CipherException)3 OperationErrorException (net.viperfish.journal.framework.errors.OperationErrorException)3 LinkedList (java.util.LinkedList)2 TreeSet (java.util.TreeSet)2 IOFile (net.viperfish.framework.file.IOFile)2 TextIOStreamHandler (net.viperfish.framework.file.TextIOStreamHandler)2 DatabaseTest (net.viperfish.journal.framework.DatabaseTest)2 FailToStoreCredentialException (net.viperfish.journal.framework.errors.FailToStoreCredentialException)2 ByteBuffer (java.nio.ByteBuffer)1 DateFormat (java.text.DateFormat)1 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 JsonGenerator (net.viperfish.framework.serialization.JsonGenerator)1