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;
029import org.jdtaus.banking.util.BankleitzahlenDatei;
030
031/**
032 * {@code BankfileProvider} implementation backed by a properties file.
033 *
034 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
035 * @version $JDTAUS: AbstractPropertiesBankfileProvider.java 8861 2014-01-10 17:09:50Z schulte $
036 *
037 * @see BankfileBankleitzahlenVerzeichnis
038 */
039public abstract class AbstractPropertiesBankfileProvider implements BankfileProvider
040{
041
042    /** Key of the bankfileCount property. */
043    private static final String BANKFILE_COUNT_PROPERTY = "BankleitzahlenVerzeichnis.bankfileCount";
044
045    /** Prefix of bank file properties. */
046    private static final String BANKFILE_PREFIX = "BankleitzahlenDatei.";
047
048    /** Properties backing the instance. */
049    private Properties properties;
050
051    /** Creates a new {@code AbstractPropertiesBankfileProvider} instance. */
052    public AbstractPropertiesBankfileProvider()
053    {
054        super();
055    }
056
057    public long getLastModifiedMillis() throws IOException
058    {
059        return 0L;
060    }
061
062    public final int getBankfileCount() throws IOException
063    {
064        try
065        {
066            final String value = this.getProperties().getProperty( BANKFILE_COUNT_PROPERTY, Integer.toString( 0 ) );
067            return NumberFormat.getIntegerInstance().parse( value ).intValue();
068        }
069        catch ( final ParseException e )
070        {
071            throw (IOException) new IOException( e.getMessage() ).initCause( e );
072        }
073    }
074
075    /**
076     * Gets a bankfile resource location.
077     *
078     * @param index The index of the bankfile resource location to get.
079     *
080     * @return The bankfile resource location at {@code index}.
081     *
082     * @throws IndexOutOfBoundsException if {@code index} is negative or greater or equal to the value returned by
083     * method {@code getBankfileCount()}.
084     * @throws IOException if getting the bankfile resource location fails.
085     */
086    public String getBankfileLocation( final int index ) throws IOException
087    {
088        if ( index < 0 || index >= this.getBankfileCount() )
089        {
090            throw new IndexOutOfBoundsException( Integer.toString( index ) );
091        }
092
093        final String propertyKey = BANKFILE_PREFIX + index + ".location";
094        final String location = this.getProperties().getProperty( propertyKey );
095        assert location != null : "Property '" + propertyKey + "' not found.";
096        return location;
097    }
098
099    public final Date getDateOfValidity( final int index ) throws IOException
100    {
101        if ( index < 0 || index >= this.getBankfileCount() )
102        {
103            throw new IndexOutOfBoundsException( Integer.toString( index ) );
104        }
105
106        try
107        {
108            final String propertyKey = BANKFILE_PREFIX + index + ".dateOfValidity";
109            final String value = this.getProperties().getProperty( propertyKey );
110            assert value != null : "Property '" + propertyKey + "' not found.";
111            return new SimpleDateFormat( "yyyyMMdd" ).parse( value );
112        }
113        catch ( final ParseException e )
114        {
115            throw (IOException) new IOException( e.getMessage() ).initCause( e );
116        }
117    }
118
119    public final Date getDateOfExpiration( final int index ) throws IOException
120    {
121        if ( index < 0 || index >= this.getBankfileCount() )
122        {
123            throw new IndexOutOfBoundsException( Integer.toString( index ) );
124        }
125
126        try
127        {
128            final String propertyKey = BANKFILE_PREFIX + index + ".dateOfExpiration";
129            final String value = this.getProperties().getProperty( propertyKey );
130            assert value != null : "Property '" + propertyKey + "' not found.";
131            return new SimpleDateFormat( "yyyyMMdd" ).parse( value );
132        }
133        catch ( final ParseException e )
134        {
135            throw (IOException) new IOException( e.getMessage() ).initCause( e );
136        }
137    }
138
139    public final int getFormat( final int index ) throws IOException
140    {
141        if ( index < 0 || index >= this.getBankfileCount() )
142        {
143            throw new IndexOutOfBoundsException( Integer.toString( index ) );
144        }
145
146        final String value =
147            this.getProperties().getProperty( BANKFILE_PREFIX + index + ".format" );
148
149        return value == null
150               ? BankleitzahlenDatei.JUNE_2006_FORMAT
151               : Integer.parseInt( value );
152
153    }
154
155    /**
156     * Gets the properties of the instance.
157     *
158     * @return The properties of the instance.
159     *
160     *  @throws IOException if getting the properties fails.
161     */
162    public Properties getProperties() throws IOException
163    {
164        if ( this.properties == null )
165        {
166            this.properties = new Properties();
167        }
168
169        return this.properties;
170    }
171
172    /**
173     * Sets the properties of the instance.
174     *
175     * @param value The new properties of the instance or {@code null}.
176     */
177    public void setProperties( final Properties value )
178    {
179        this.properties = value;
180    }
181
182}