View Javadoc
1   /*
2    *  jDTAUS Banking RI Bankleitzahlenverzeichnis
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19   *
20   */
21  package org.jdtaus.banking.ri.blzdirectory;
22  
23  import java.io.IOException;
24  import java.text.NumberFormat;
25  import java.text.ParseException;
26  import java.text.SimpleDateFormat;
27  import java.util.Date;
28  import java.util.Properties;
29  import org.jdtaus.banking.util.BankleitzahlenDatei;
30  
31  /**
32   * {@code BankfileProvider} implementation backed by a properties file.
33   *
34   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
35   * @version $JDTAUS: AbstractPropertiesBankfileProvider.java 8861 2014-01-10 17:09:50Z schulte $
36   *
37   * @see BankfileBankleitzahlenVerzeichnis
38   */
39  public abstract class AbstractPropertiesBankfileProvider implements BankfileProvider
40  {
41  
42      /** Key of the bankfileCount property. */
43      private static final String BANKFILE_COUNT_PROPERTY = "BankleitzahlenVerzeichnis.bankfileCount";
44  
45      /** Prefix of bank file properties. */
46      private static final String BANKFILE_PREFIX = "BankleitzahlenDatei.";
47  
48      /** Properties backing the instance. */
49      private Properties properties;
50  
51      /** Creates a new {@code AbstractPropertiesBankfileProvider} instance. */
52      public AbstractPropertiesBankfileProvider()
53      {
54          super();
55      }
56  
57      public long getLastModifiedMillis() throws IOException
58      {
59          return 0L;
60      }
61  
62      public final int getBankfileCount() throws IOException
63      {
64          try
65          {
66              final String value = this.getProperties().getProperty( BANKFILE_COUNT_PROPERTY, Integer.toString( 0 ) );
67              return NumberFormat.getIntegerInstance().parse( value ).intValue();
68          }
69          catch ( final ParseException e )
70          {
71              throw (IOException) new IOException( e.getMessage() ).initCause( e );
72          }
73      }
74  
75      /**
76       * Gets a bankfile resource location.
77       *
78       * @param index The index of the bankfile resource location to get.
79       *
80       * @return The bankfile resource location at {@code index}.
81       *
82       * @throws IndexOutOfBoundsException if {@code index} is negative or greater or equal to the value returned by
83       * method {@code getBankfileCount()}.
84       * @throws IOException if getting the bankfile resource location fails.
85       */
86      public String getBankfileLocation( final int index ) throws IOException
87      {
88          if ( index < 0 || index >= this.getBankfileCount() )
89          {
90              throw new IndexOutOfBoundsException( Integer.toString( index ) );
91          }
92  
93          final String propertyKey = BANKFILE_PREFIX + index + ".location";
94          final String location = this.getProperties().getProperty( propertyKey );
95          assert location != null : "Property '" + propertyKey + "' not found.";
96          return location;
97      }
98  
99      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 }