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;
}
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[] {});
}
}
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;
}
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");
}
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");
}
Aggregations