Search in sources :

Example 1 with StoredObject

use of jgnash.engine.StoredObject in project jgnash by ccavanaugh.

the class JpaEngineDAO method getStoredObjects.

@Override
public List<StoredObject> getStoredObjects() {
    List<StoredObject> list = Collections.emptyList();
    try {
        final Future<List<StoredObject>> future = executorService.submit(() -> {
            emLock.lock();
            try {
                final CriteriaBuilder cb = em.getCriteriaBuilder();
                final CriteriaQuery<StoredObject> cq = cb.createQuery(StoredObject.class);
                final Root<StoredObject> root = cq.from(StoredObject.class);
                cq.select(root);
                final TypedQuery<StoredObject> q = em.createQuery(cq);
                return new ArrayList<>(q.getResultList());
            } finally {
                emLock.unlock();
            }
        });
        list = future.get();
    } catch (final InterruptedException | ExecutionException e) {
        logSevere(JpaEngineDAO.class, e);
    }
    return list;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ArrayList(java.util.ArrayList) StoredObject(jgnash.engine.StoredObject) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with StoredObject

use of jgnash.engine.StoredObject in project jgnash by ccavanaugh.

the class AbstractJpaDataStore method saveAs.

@Override
public void saveAs(final Path path, final Collection<StoredObject> objects) {
    // Remove the existing files so we don't mix entities and cause corruption
    if (Files.exists(path)) {
        deleteDatabase(path.toString());
    }
    if (initEmptyDatabase(path.toString())) {
        final Properties properties = JpaConfiguration.getLocalProperties(getType(), path.toString(), new char[] {}, false);
        EntityManagerFactory factory = null;
        EntityManager em = null;
        try {
            factory = Persistence.createEntityManagerFactory(JpaConfiguration.UNIT_NAME, properties);
            em = factory.createEntityManager();
            em.getTransaction().begin();
            for (StoredObject o : objects) {
                em.persist(o);
            }
            em.getTransaction().commit();
        } catch (Exception e) {
            logger.log(Level.SEVERE, e.getMessage(), e);
        } finally {
            if (em != null) {
                em.close();
            }
            if (factory != null) {
                factory.close();
            }
        }
        waitForLockFileRelease(path.toString(), new char[] {});
    }
}
Also used : EntityManager(javax.persistence.EntityManager) StoredObject(jgnash.engine.StoredObject) EntityManagerFactory(javax.persistence.EntityManagerFactory) Properties(java.util.Properties) IOException(java.io.IOException) SQLException(java.sql.SQLException)

Example 3 with StoredObject

use of jgnash.engine.StoredObject in project jgnash by ccavanaugh.

the class AbstractXStreamContainer method get.

StoredObject get(final String uuid) {
    StoredObject result = null;
    Lock l = readWriteLock.readLock();
    l.lock();
    try {
        for (StoredObject o : objects) {
            if (o.getUuid().equals(uuid)) {
                result = o;
            }
        }
    } finally {
        l.unlock();
    }
    return result;
}
Also used : StoredObject(jgnash.engine.StoredObject) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Example 4 with StoredObject

use of jgnash.engine.StoredObject in project jgnash by ccavanaugh.

the class BinaryContainer method writeBinary.

/**
     * Writes an XML file given a collection of StoredObjects. TrashObjects and
     * objects marked for removal are not written. If the file already exists,
     * it will be overwritten.
     *
     * @param objects Collection of StoredObjects to write
     * @param path    file to write
     */
static synchronized void writeBinary(@NotNull final Collection<StoredObject> objects, @NotNull final Path path) {
    final Logger logger = Logger.getLogger(BinaryContainer.class.getName());
    if (!Files.exists(path.getParent())) {
        try {
            Files.createDirectories(path.getParent());
            logger.info("Created missing directories");
        } catch (final IOException e) {
            logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
        }
    }
    createBackup(path);
    List<StoredObject> list = new ArrayList<>();
    list.addAll(query(objects, Budget.class));
    list.addAll(query(objects, Config.class));
    list.addAll(query(objects, CommodityNode.class));
    list.addAll(query(objects, ExchangeRate.class));
    list.addAll(query(objects, RootAccount.class));
    list.addAll(query(objects, Reminder.class));
    // remove any objects marked for removal
    list.removeIf(StoredObject::isMarkedForRemoval);
    // sort the list
    list.sort(new StoredObjectComparator());
    logger.info("Writing Binary file");
    try (final OutputStream os = new BufferedOutputStream(Files.newOutputStream(path))) {
        final XStream xstream = configureXStream(new XStreamOut(new PureJavaReflectionProvider(), new BinaryStreamDriver()));
        try (final ObjectOutputStream out = xstream.createObjectOutputStream(os)) {
            out.writeObject(list);
            out.flush();
        }
        // forcibly flush before letting go of the resources to help older windows systems write correctly
        os.flush();
    } catch (IOException e) {
        logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
    }
    logger.info("Writing Binary file complete");
}
Also used : ExchangeRate(jgnash.engine.ExchangeRate) Reminder(jgnash.engine.recurring.Reminder) Config(jgnash.engine.Config) XStream(com.thoughtworks.xstream.XStream) BufferedOutputStream(java.io.BufferedOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) CommodityNode(jgnash.engine.CommodityNode) IOException(java.io.IOException) Logger(java.util.logging.Logger) ObjectOutputStream(java.io.ObjectOutputStream) StoredObjectComparator(jgnash.engine.StoredObjectComparator) RootAccount(jgnash.engine.RootAccount) StoredObject(jgnash.engine.StoredObject) Budget(jgnash.engine.budget.Budget) BinaryStreamDriver(com.thoughtworks.xstream.io.binary.BinaryStreamDriver) BufferedOutputStream(java.io.BufferedOutputStream) PureJavaReflectionProvider(com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)

Example 5 with StoredObject

use of jgnash.engine.StoredObject in project jgnash by ccavanaugh.

the class XMLContainer method writeXML.

/**
     * Writes an XML file given a collection of StoredObjects. TrashObjects and
     * objects marked for removal are not written. If the file already exists,
     * it will be overwritten.
     *
     * @param objects Collection of StoredObjects to write
     * @param path    file to write
     */
static synchronized void writeXML(final Collection<StoredObject> objects, final Path path) {
    Logger logger = Logger.getLogger(XMLContainer.class.getName());
    if (!Files.exists(path.getParent())) {
        try {
            Files.createDirectories(path.getParent());
            logger.info("Created missing directories");
        } catch (final IOException e) {
            logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
        }
    }
    createBackup(path);
    List<StoredObject> list = new ArrayList<>();
    list.addAll(query(objects, Budget.class));
    list.addAll(query(objects, Config.class));
    list.addAll(query(objects, CommodityNode.class));
    list.addAll(query(objects, ExchangeRate.class));
    list.addAll(query(objects, RootAccount.class));
    list.addAll(query(objects, Reminder.class));
    // remove any objects marked for removal
    list.removeIf(StoredObject::isMarkedForRemoval);
    // sort the list
    list.sort(new StoredObjectComparator());
    logger.info("Writing XML file");
    try (final Writer writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
        writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        writer.write("<?fileFormat " + Engine.CURRENT_MAJOR_VERSION + "." + Engine.CURRENT_MINOR_VERSION + "?>\n");
        final XStream xstream = configureXStream(new XStreamOut(new PureJavaReflectionProvider(), new KXml2Driver()));
        try (final ObjectOutputStream out = xstream.createObjectOutputStream(new PrettyPrintWriter(writer))) {
            out.writeObject(list);
            // forcibly flush before letting go of the resources to help older windows systems write correctly
            out.flush();
        } catch (final Exception e) {
            logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
        }
    } catch (final IOException e) {
        logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
    }
    logger.info("Writing XML file complete");
}
Also used : ExchangeRate(jgnash.engine.ExchangeRate) Reminder(jgnash.engine.recurring.Reminder) Config(jgnash.engine.Config) XStream(com.thoughtworks.xstream.XStream) ArrayList(java.util.ArrayList) CommodityNode(jgnash.engine.CommodityNode) IOException(java.io.IOException) Logger(java.util.logging.Logger) ObjectOutputStream(java.io.ObjectOutputStream) StoredObjectComparator(jgnash.engine.StoredObjectComparator) IOException(java.io.IOException) RootAccount(jgnash.engine.RootAccount) StoredObject(jgnash.engine.StoredObject) KXml2Driver(com.thoughtworks.xstream.io.xml.KXml2Driver) Budget(jgnash.engine.budget.Budget) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) Writer(java.io.Writer) PureJavaReflectionProvider(com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)

Aggregations

StoredObject (jgnash.engine.StoredObject)9 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ExecutionException (java.util.concurrent.ExecutionException)3 ExchangeRate (jgnash.engine.ExchangeRate)3 Budget (jgnash.engine.budget.Budget)3 XStream (com.thoughtworks.xstream.XStream)2 PureJavaReflectionProvider (com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)2 ObjectOutputStream (java.io.ObjectOutputStream)2 Logger (java.util.logging.Logger)2 CommodityNode (jgnash.engine.CommodityNode)2 Config (jgnash.engine.Config)2 RootAccount (jgnash.engine.RootAccount)2 StoredObjectComparator (jgnash.engine.StoredObjectComparator)2 Reminder (jgnash.engine.recurring.Reminder)2 BinaryStreamDriver (com.thoughtworks.xstream.io.binary.BinaryStreamDriver)1 KXml2Driver (com.thoughtworks.xstream.io.xml.KXml2Driver)1 PrettyPrintWriter (com.thoughtworks.xstream.io.xml.PrettyPrintWriter)1 Dimension (java.awt.Dimension)1 BufferedOutputStream (java.io.BufferedOutputStream)1