Java tutorial
/** * GnucashDatabase.javaTransactionMenuAction.java * created: 06.08.2009 * (c) 2008 by <a href="http://Wolschon.biz">Wolschon Softwaredesign und Beratung</a> * This file is part of jgnucashLib-GPL by Marcus Wolschon <a href="mailto:Marcus@Wolscon.biz">Marcus@Wolscon.biz</a>. * You can purchase support for a sensible hourly rate or * a commercial license of this file (unless modified by others) by contacting him directly. * * jgnucashLib-GPL 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. * * jgnucashLib-GPL 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 jgnucashLib-V1. If not, see <http://www.gnu.org/licenses/>. * *********************************** * Editing this file: * -For consistent code-quality this file should be checked with the * checkstyle-ruleset enclosed in this project. * -After the design of this file has settled it should get it's own * JUnit-Test that shall be executed regularly. It is best to write * the test-case BEFORE writing this class and to run it on every build * as a regression-test. */ package biz.wolschon.finance.jgnucash.mysql.impl; import java.io.File; import java.io.IOException; import java.lang.ref.WeakReference; import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.sql.DataSource; import javax.xml.bind.JAXBException; import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; import biz.wolschon.fileformats.gnucash.GnucashAccount; import biz.wolschon.fileformats.gnucash.GnucashCustomer; import biz.wolschon.fileformats.gnucash.GnucashFile; import biz.wolschon.fileformats.gnucash.GnucashInvoice; import biz.wolschon.fileformats.gnucash.GnucashJob; import biz.wolschon.fileformats.gnucash.GnucashTaxTable; import biz.wolschon.fileformats.gnucash.GnucashTransaction; import biz.wolschon.fileformats.gnucash.GnucashWritableAccount; import biz.wolschon.fileformats.gnucash.GnucashWritableCustomer; import biz.wolschon.fileformats.gnucash.GnucashWritableFile; import biz.wolschon.fileformats.gnucash.GnucashWritableInvoice; import biz.wolschon.fileformats.gnucash.GnucashWritableJob; import biz.wolschon.fileformats.gnucash.GnucashWritableTransaction; import biz.wolschon.fileformats.gnucash.jwsdpimpl.generated.GncV2; import biz.wolschon.finance.ComplexCurrencyTable; import biz.wolschon.numbers.FixedPointNumber; /** * (c) 2009 by <a href="http://Wolschon.biz>Wolschon Softwaredesign und Beratung</a>.<br/> * Project: jgnucashLib-GPL<br/> * GnucashDatabase<br/> * created: 06.08.2009 <br/> *<br/><br/> * <b>GnucashWritableFile using a database generated by Gnucash 2.3 and above.</b> * @author <a href="mailto:Marcus@Wolschon.biz">fox</a> */ public class GnucashDatabase implements GnucashWritableFile { /** * Automatically created logger for debug and error-output. */ private static final Logger LOG = Logger.getLogger(GnucashDatabase.class.getName()); /** * Our database. (Using Sping-DB) */ private final SimpleJdbcTemplate myJDBCTemplate; /** * Cache for {@link #getAccountByID(String)}. */ private final Map<String, WeakReference<GnucashWritableAccount>> myAccountByIDCache = new HashMap<String, WeakReference<GnucashWritableAccount>>(); /** * @param aDataSource the database to connect to. */ public GnucashDatabase(final DataSource aDataSource) { myJDBCTemplate = new SimpleJdbcTemplate(aDataSource); LOG.info("GnucashDatabase -class created"); } /** * @return the jDBCTemplate */ protected SimpleJdbcTemplate getJDBCTemplate() { return myJDBCTemplate; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#addCurrency(java.lang.String, java.lang.String, biz.wolschon.numbers.FixedPointNumber, int, java.lang.String) */ @Override public void addCurrency(final String aCmdtySpace, final String aCmdtyId, final FixedPointNumber aConversionFactor, final int aCmdtyNameFraction, final String aCmdtyName) throws JAXBException { // TODO Auto-generated method stub } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#createWritableAccount() */ @Override public GnucashWritableAccount createWritableAccount() throws JAXBException { // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#createWritableAccount(java.lang.String) */ @Override public GnucashWritableAccount createWritableAccount(String aId) throws JAXBException { // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#createWritableCustomer() */ @Override public GnucashWritableCustomer createWritableCustomer() throws JAXBException { // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#createWritableCustomer(java.lang.String) */ @Override public GnucashWritableCustomer createWritableCustomer(String aId) throws JAXBException { // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#createWritableInvoice(java.lang.String, biz.wolschon.fileformats.gnucash.GnucashJob, biz.wolschon.fileformats.gnucash.GnucashAccount, java.util.Date) */ @Override public GnucashWritableInvoice createWritableInvoice(String aInvoiceNumber, GnucashJob aJob, GnucashAccount aAccountToTransferMoneyTo, Date aDueDate) throws JAXBException { // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#createWritableInvoice(java.lang.String, java.lang.String, biz.wolschon.fileformats.gnucash.GnucashJob, biz.wolschon.fileformats.gnucash.GnucashAccount, java.util.Date) */ @Override public GnucashWritableInvoice createWritableInvoice(String aInternalID, String aInvoiceNumber, GnucashJob aJob, GnucashAccount aAccountToTransferMoneyTo, Date aDueDate) throws JAXBException { // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#createWritableJob(biz.wolschon.fileformats.gnucash.GnucashCustomer) */ @Override public GnucashWritableJob createWritableJob(GnucashCustomer aCustomer) throws JAXBException { // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#createWritableJob(java.lang.String, biz.wolschon.fileformats.gnucash.GnucashCustomer) */ @Override public GnucashWritableJob createWritableJob(String aId, GnucashCustomer aCustomer) throws JAXBException { // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#createWritableTransaction() */ @Override public GnucashWritableTransaction createWritableTransaction() throws JAXBException { // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#createWritableTransaction(java.lang.String) */ @Override public GnucashWritableTransaction createWritableTransaction(String aId) throws JAXBException { // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#getAccountByID(java.lang.String) */ @Override public GnucashWritableAccount getAccountByID(final String aGUID) { GnucashWritableAccount retval = null; WeakReference<GnucashWritableAccount> ref = myAccountByIDCache.get(aGUID); if (ref != null) { retval = ref.get(); } if (retval != null) { return retval; } LOG.info("getAccountByID(aGUID=" + aGUID + ")"); String sql = "select * from accounts where guid = ?"; try { retval = myJDBCTemplate.queryForObject(sql, new AccountRowMapper(this), aGUID); myAccountByIDCache.put(aGUID, new WeakReference<GnucashWritableAccount>(retval)); return retval; } catch (org.springframework.dao.EmptyResultDataAccessException e) { Collection<? extends GnucashWritableAccount> writableRootAccounts = getWritableRootAccounts(); for (GnucashWritableAccount gnucashWritableAccount : writableRootAccounts) { if (gnucashWritableAccount.getId().equals(aGUID)) { retval = gnucashWritableAccount; myAccountByIDCache.put(aGUID, new WeakReference<GnucashWritableAccount>(retval)); return retval; } } throw e; } } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#getAccountByName(java.lang.String) */ @Override public GnucashWritableAccount getAccountByName(final String aName) { LOG.finest("getAccountByName()"); for (GnucashWritableAccount account : getWritableAccounts()) { if (account.getName().equals(aName)) { return account; } if (account.getQualifiedName().equals(aName)) { return account; } } return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#getAccountsByType(java.lang.String) */ @SuppressWarnings("unchecked") @Override public Collection<? extends GnucashWritableAccount> getAccountsByType(final String aType) { LOG.finest("getAccountByType()"); LOG.info("getAccountByID()"); String sql = "select * from accounts where " + AccountRowMapper.COLUMNACCOUNTTYPE + " = ?"; return (Collection<? extends GnucashWritableAccount>) myJDBCTemplate.queryForObject(sql, new AccountRowMapper(this), aType); } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#getCustomerByID(java.lang.String) */ @Override public GnucashWritableCustomer getCustomerByID(String aId) { LOG.finest("getCustomerByID()"); // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#getInvoiceByID(java.lang.String) */ @Override public GnucashWritableInvoice getInvoiceByID(String aId) { LOG.finest("getInvoiceByID()"); // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#getJobByID(java.lang.String) */ @Override public GnucashWritableJob getJobByID(String aJobID) { // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#getJobByNumber(java.lang.String) */ @Override public GnucashWritableJob getJobByNumber(String aJnr) { // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#getLastWriteTime() */ @Override public long getLastWriteTime() { LOG.info("getLastWriteTime()"); // TODO Auto-generated method stub return 0; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#getRootElement() */ @Override public GncV2 getRootElement() { LOG.info("getRootElement()"); throw new IllegalAccessError("getRootElement() is not implemented yet!"); // Collection<? extends GnucashWritableAccount> writableRootAccounts = getWritableRootAccounts(); // if (writableRootAccounts.size() == 1) { // return writableRootAccounts.iterator().next(); // } // LOG.severe("we have " + writableRootAccounts.size() + " root-accounts!"); // return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#getTransactionByID(java.lang.String) */ @Override public GnucashWritableTransaction getTransactionByID(final String aId) { LOG.info("getTransactionByID()"); String sql = "select * from transactions where guid = ?"; return myJDBCTemplate.queryForObject(sql, new TransactionRowMapper(this), aId); } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#getWritableAccounts() */ @Override public Collection<? extends GnucashWritableAccount> getWritableAccounts() { String sql = "select * from accounts"; return myJDBCTemplate.query(sql, new AccountRowMapper(this)); } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#getWritableJobs() */ @Override public Collection<GnucashWritableJob> getWritableJobs() { // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#getWritableRootAccounts() */ @Override public Collection<? extends GnucashWritableAccount> getWritableRootAccounts() { LOG.info("getWritableRootAccounts()"); //TODO: the root-element MAY exist in the "accounts"-table (> v2.3.3=), but it need not (v2.3.3) String sql = "select root_account_guid as guid, " + "NULL as parent_guid, " + "\"Root Account\" as name, " + "\"ROOT\" as account_type, " + "\"\" as commodity_guid," + "0 as commodity_scu, " + "0 as non_std_scu, " + "\"\" as code, " + "\"\" as description " + "from books"; List<GnucashDBAccount> retval = myJDBCTemplate.query(sql, new AccountRowMapper(this)); LOG.info("getWritableRootAccounts() returning " + retval.size() + "accounts"); return retval; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#getWritableTransactions() */ @Override public Collection<? extends GnucashWritableTransaction> getWritableTransactions() { LOG.info("getWritableTransactions()"); String sql = "select * from transactions"; return myJDBCTemplate.query(sql, new TransactionRowMapper(this)); } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#isModified() */ @Override public boolean isModified() { LOG.info("isModified()"); // TODO Auto-generated method stub return false; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#removeAccount(biz.wolschon.fileformats.gnucash.GnucashWritableAccount) */ @Override public void removeAccount(GnucashWritableAccount aImpl) { // TODO Auto-generated method stub } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#removeTransaction(biz.wolschon.fileformats.gnucash.GnucashWritableTransaction) */ @Override public void removeTransaction(GnucashWritableTransaction aImpl) throws JAXBException { // TODO Auto-generated method stub } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#setModified(boolean) */ @Override public void setModified(boolean aPb) { LOG.info("setModified()"); // TODO Auto-generated method stub } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableFile#writeFile(java.io.File) */ @Override public void writeFile(File aFile) throws IOException, JAXBException { // TODO Auto-generated method stub } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashFile#getAccountByIDorName(java.lang.String, java.lang.String) */ @Override public GnucashAccount getAccountByIDorName(final String aId, final String aName) { GnucashAccount retval = getAccountByID(aId); if (retval == null) { retval = getAccountByName(aName); } return retval; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashFile#getAccountByIDorNameEx(java.lang.String, java.lang.String) */ @Override public GnucashAccount getAccountByIDorNameEx(final String aId, final String aName) { GnucashAccount retval = getAccountByID(aId); if (retval == null) { retval = getAccountByNameEx(aName); } return retval; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashFile#getAccountByNameEx(java.lang.String) */ @Override public GnucashAccount getAccountByNameEx(final String aNameRegEx) { LOG.info("getAccountByNameEx()"); GnucashAccount foundAccount = getAccountByName(aNameRegEx); if (foundAccount != null) { return foundAccount; } Pattern pattern = Pattern.compile(aNameRegEx); for (GnucashAccount account : getWritableAccounts()) { Matcher matcher = pattern.matcher(account.getName()); if (matcher.matches()) { return account; } } return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashFile#getAccounts() */ @Override public Collection<? extends GnucashAccount> getAccounts() { LOG.info("getAccounts()"); return getWritableAccounts(); } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashFile#getAccountsByParentID(java.lang.String) */ @Override public Collection<? extends GnucashAccount> getAccountsByParentID(final String aId) { LOG.info("getAccountsByParentID()"); String sql = "select * from accounts where parent_guid = ?"; return getJDBCTemplate().query(sql, new AccountRowMapper(this), aId); } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashFile#getCurrencyTable() */ @Override public ComplexCurrencyTable getCurrencyTable() { LOG.info("getCurrencyTable()"); // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashFile#getCustomerByName(java.lang.String) */ @Override public GnucashCustomer getCustomerByName(String aName) { LOG.info("getCustomerByName()"); // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashFile#getCustomers() */ @Override public Collection<GnucashCustomer> getCustomers() { LOG.info("getCustomers()"); // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashFile#getDefaultCurrencyID() */ @Override public String getDefaultCurrencyID() { LOG.info("getDefaultCurrencyID()"); // TODO Auto-generated method stub return "EUR"; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashFile#getFile() */ @Override public File getFile() { LOG.info("getFile()"); // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashFile#getInvoices() */ @Override public Collection<GnucashInvoice> getInvoices() { LOG.info("getInvoices()"); // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashFile#getJobs() */ @Override public Collection<GnucashJob> getJobs() { LOG.info("getJobs()"); // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashFile#getLatestPrice(java.lang.String, java.lang.String) */ @Override public FixedPointNumber getLatestPrice(String aCmdtySpace, String aCmdtyId) { LOG.info("getLatestPrice()"); // TODO Auto-generated method stub return new FixedPointNumber("1"); } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashFile#getPayedInvoices() */ @Override public Collection<GnucashInvoice> getPayedInvoices() throws JAXBException { // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashFile#getRootAccounts() */ @Override public Collection<? extends GnucashAccount> getRootAccounts() { LOG.info("getRootAccounts()"); return getWritableRootAccounts(); } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashFile#getTaxTableByID(java.lang.String) */ @Override public GnucashTaxTable getTaxTableByID(String aId) { // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashFile#getTaxTables() */ @Override public Collection<GnucashTaxTable> getTaxTables() { LOG.info("getTaxTables()"); // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashFile#getTransactions() */ @Override public Collection<? extends GnucashTransaction> getTransactions() { LOG.info("getTransactions"); // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashFile#getUnpayedInvoices() */ @Override public Collection<GnucashInvoice> getUnpayedInvoices() throws JAXBException { // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashFile#getUnpayedInvoicesForCustomer(biz.wolschon.fileformats.gnucash.GnucashCustomer) */ @Override public Collection<GnucashInvoice> getUnpayedInvoicesForCustomer(GnucashCustomer aCustomer) throws JAXBException { // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.jwsdpimpl.GnucashObject#getGnucashFile() */ @Override public GnucashFile getGnucashFile() { return this; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.jwsdpimpl.GnucashObject#getUserDefinedAttribute(java.lang.String) */ @Override public String getUserDefinedAttribute(String aName) { LOG.info("getUserDefinedAttribute()"); // TODO Auto-generated method stub return null; } /** * @see biz.wolschon.fileformats.gnucash.jwsdpimpl.GnucashObject#getUserDefinedAttributeKeys() */ @Override public Collection<String> getUserDefinedAttributeKeys() { // TODO Auto-generated method stub return null; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableObject#getWritableGnucashFile() */ @Override public GnucashWritableFile getWritableGnucashFile() { return this; } /** * {@inheritDoc} * @see biz.wolschon.fileformats.gnucash.GnucashWritableObject#setUserDefinedAttribute(java.lang.String, java.lang.String) */ @Override public void setUserDefinedAttribute(final String aName, final String aValue) throws JAXBException { LOG.info("getUserDefinedAttribute()"); // TODO Auto-generated method stub } protected GnucashDBCommodity getCommodityByName(final String aNamespace, final String aMnemonic) { LOG.finest("getAccountByType()"); LOG.info("getAccountByID()"); String sql = "select * from " + CurrencyRowMapper.DBTABLE + " where namespace = ? AND mnemonic = ?"; return myJDBCTemplate.queryForObject(sql, new CurrencyRowMapper(this), aNamespace, aMnemonic); } protected GnucashDBCommodity getCommodityByID(final String aGUID) { LOG.finest("getAccountByType()"); LOG.info("getAccountByID()"); String sql = "select * from " + CurrencyRowMapper.DBTABLE + " where guid = ?"; return myJDBCTemplate.queryForObject(sql, new CurrencyRowMapper(this), aGUID); } }