001/*
002 *  jDTAUS Banking RI Bankleitzahlenverzeichnis
003 *  Copyright (C) 2005 Christian Schulte
004 *  <cs@schulte.it>
005 *
006 *  This library is free software; you can redistribute it and/or
007 *  modify it under the terms of the GNU Lesser General Public
008 *  License as published by the Free Software Foundation; either
009 *  version 2.1 of the License, or any later version.
010 *
011 *  This library is distributed in the hope that it will be useful,
012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 *  Lesser General Public License for more details.
015 *
016 *  You should have received a copy of the GNU Lesser General Public
017 *  License along with this library; if not, write to the Free Software
018 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
019 *
020 */
021package org.jdtaus.banking.ri.blzdirectory;
022
023import java.io.IOException;
024import java.text.NumberFormat;
025import java.text.ParseException;
026import java.text.SimpleDateFormat;
027import java.util.Date;
028import java.util.Properties;
029
030/**
031 * {@code BankfileProvider} implementation backed by a properties file.
032 *
033 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
034 * @version $JDTAUS: AbstractPropertiesBankfileProvider.java 8661 2012-09-27 11:29:58Z schulte $
035 *
036 * @see BankfileBankleitzahlenVerzeichnis
037 */
038public abstract class AbstractPropertiesBankfileProvider implements BankfileProvider
039{
040
041    /** Key of the bankfileCount property. */
042    private static final String BANKFILE_COUNT_PROPERTY = "BankleitzahlenVerzeichnis.bankfileCount";
043
044    /** Prefix of bank file properties. */
045    private static final String BANKFILE_PREFIX = "BankleitzahlenDatei.";
046
047    /** Properties backing the instance. */
048    private Properties properties;
049
050    /** Creates a new {@code AbstractPropertiesBankfileProvider} instance. */
051    public AbstractPropertiesBankfileProvider()
052    {
053        super();
054    }
055
056    public long getLastModifiedMillis() throws IOException
057    {
058        return 0L;
059    }
060
061    public final int getBankfileCount() throws IOException
062    {
063        try
064        {
065            final String value = this.getProperties().getProperty( BANKFILE_COUNT_PROPERTY, Integer.toString( 0 ) );
066            return NumberFormat.getIntegerInstance().parse( value ).intValue();
067        }
068        catch ( final ParseException e )
069        {
070            throw (IOException) new IOException( e.getMessage() ).initCause( e );
071        }
072    }
073
074    /**
075     * Gets a bankfile resource location.
076     *
077     * @param index The index of the bankfile resource location to get.
078     *
079     * @return The bankfile resource location at {@code index}.
080     *
081     * @throws IndexOutOfBoundsException if {@code index} is negative or greater or equal to the value returned by
082     * method {@code getBankfileCount()}.
083     * @throws IOException if getting the bankfile resource location fails.
084     */
085    public String getBankfileLocation( final int index ) throws IOException
086    {
087        if ( index < 0 || index >= this.getBankfileCount() )
088        {
089            throw new IndexOutOfBoundsException( Integer.toString( index ) );
090        }
091
092        final String propertyKey = BANKFILE_PREFIX + index + ".location";
093        final String location = this.getProperties().getProperty( propertyKey );
094        assert location != null : "Property '" + propertyKey + "' not found.";
095        return location;
096    }
097
098    public final Date getDateOfValidity( final int index ) throws IOException
099    {
100        if ( index < 0 || index >= this.getBankfileCount() )
101        {
102            throw new IndexOutOfBoundsException( Integer.toString( index ) );
103        }
104
105        try
106        {
107            final String propertyKey = BANKFILE_PREFIX + index + ".dateOfValidity";
108            final String value = this.getProperties().getProperty( propertyKey );
109            assert value != null : "Property '" + propertyKey + "' not found.";
110            return new SimpleDateFormat( "yyyyMMdd" ).parse( value );
111        }
112        catch ( final ParseException e )
113        {
114            throw (IOException) new IOException( e.getMessage() ).initCause( e );
115        }
116    }
117
118    public final Date getDateOfExpiration( final int index ) throws IOException
119    {
120        if ( index < 0 || index >= this.getBankfileCount() )
121        {
122            throw new IndexOutOfBoundsException( Integer.toString( index ) );
123        }
124
125        try
126        {
127            final String propertyKey = BANKFILE_PREFIX + index + ".dateOfExpiration";
128            final String value = this.getProperties().getProperty( propertyKey );
129            assert value != null : "Property '" + propertyKey + "' not found.";
130            return new SimpleDateFormat( "yyyyMMdd" ).parse( value );
131        }
132        catch ( final ParseException e )
133        {
134            throw (IOException) new IOException( e.getMessage() ).initCause( e );
135        }
136    }
137
138    /**
139     * Gets the properties of the instance.
140     *
141     * @return The properties of the instance.
142     *
143     *  @throws IOException if getting the properties fails.
144     */
145    public Properties getProperties() throws IOException
146    {
147        if ( this.properties == null )
148        {
149            this.properties = new Properties();
150        }
151
152        return this.properties;
153    }
154
155    /**
156     * Sets the properties of the instance.
157     *
158     * @param value The new properties of the instance or {@code null}.
159     */
160    public void setProperties( final Properties value )
161    {
162        this.properties = value;
163    }
164
165}