/*
* jGnash, a personal finance application
* Copyright (C) 2001-2008 Craig Cavanaugh
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package jgnash.engine.db4o;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import jgnash.engine.Account;
import jgnash.engine.Transaction;
import jgnash.engine.TransactionEntry;
import jgnash.engine.dao.TransactionDAO;
import com.db4o.ObjectContainer;
/** db4o transaction DAO
*
* @author Craig Cavanaugh
*
* $Id: Db4oTransactionDAO.java 197 2008-01-01 08:13:35Z ccavanaugh $
*/
class Db4oTransactionDAO extends AbstractDb4oDAO implements TransactionDAO {
protected Logger logger = Logger.getLogger(Db4oTransactionDAO.class.getName());
Db4oTransactionDAO(ObjectContainer container) {
super(container);
logger.setLevel(Level.ALL);
}
/*
* @see jgnash.engine.TransactionDAOInterface#getTransactions()
*/
public synchronized List<Transaction> getTransactions() {
if (container.ext().setSemaphore(GLOBAL_SEMAPHORE, SEMAPHORE_WAIT_TIME)) {
List<Transaction> list = container.query(Transaction.class);
container.ext().releaseSemaphore(GLOBAL_SEMAPHORE);
List<Transaction> resultList = new ArrayList<Transaction>();
for (Transaction t : list) {
if (!t.isMarkedForRemoval()) {
resultList.add(t);
}
}
return resultList;
}
logger.severe("Could not obtain the global semaphore");
return Collections.emptyList();
}
public void refreshTransaction(Transaction transaction) {
if (container.ext().setSemaphore(GLOBAL_SEMAPHORE, SEMAPHORE_WAIT_TIME)) {
container.ext().refresh(transaction, 4);
container.ext().releaseSemaphore(GLOBAL_SEMAPHORE);
} else {
logger.severe("Could not obtain the global semaphore");
}
}
/*
* @see jgnash.engine.TransactionDAOInterface#addTransaction(jgnash.engine.Transaction)
*/
public synchronized boolean addTransaction(jgnash.engine.Transaction transaction) {
if (container.ext().setSemaphore(GLOBAL_SEMAPHORE, SEMAPHORE_WAIT_TIME)) {
List<TransactionEntry> entries = transaction.getTransactionEntries();
Set<Account> accounts = new HashSet<Account>();
for (TransactionEntry entry : entries) {
accounts.add(entry.getCreditAccount());
accounts.add(entry.getDebitAccount());
container.set(entry);
}
for (Account account : accounts) {
container.set(account);
}
container.set(transaction);
container.commit();
container.ext().releaseSemaphore(GLOBAL_SEMAPHORE);
return true;
}
logger.severe("Could not obtain the global semaphore");
return false;
}
/*
* @see jgnash.engine.TransactionDAOInterface#removeTransaction(jgnash.engine.Transaction)
*/
public synchronized boolean removeTransaction(jgnash.engine.Transaction transaction) {
// look at accounts this transaction impacted and update the accounts
if (container.ext().setSemaphore(GLOBAL_SEMAPHORE, SEMAPHORE_WAIT_TIME)) {
List<TransactionEntry> entries = transaction.getTransactionEntries();
Set<Account> accounts = new HashSet<Account>();
for (TransactionEntry entry : entries) {
accounts.add(entry.getCreditAccount());
accounts.add(entry.getDebitAccount());
}
for (Account account : accounts) {
container.set(account);
}
container.commit();
container.ext().releaseSemaphore(GLOBAL_SEMAPHORE);
return true;
}
logger.severe("Could not obtain the global semaphore");
return false;
}
}
|