View Javadoc
1   /*
2    *  jDTAUS Banking RI DTAUS
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.dtaus.ri.zka;
22  
23  import java.io.IOException;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.Map;
27  import org.jdtaus.banking.Textschluessel;
28  import org.jdtaus.banking.TextschluesselVerzeichnis;
29  import org.jdtaus.banking.dtaus.LogicalFile;
30  import org.jdtaus.banking.dtaus.LogicalFileType;
31  import org.jdtaus.banking.dtaus.Transaction;
32  import org.jdtaus.banking.dtaus.spi.IllegalTransactionException;
33  import org.jdtaus.banking.dtaus.spi.TransactionValidator;
34  import org.jdtaus.banking.messages.IllegalAmountMessage;
35  import org.jdtaus.banking.messages.IllegalCurrencyMessage;
36  import org.jdtaus.banking.messages.IllegalDescriptionCountMessage;
37  import org.jdtaus.banking.messages.TextschluesselConstraintMessage;
38  import org.jdtaus.banking.spi.CurrencyMapper;
39  import org.jdtaus.banking.spi.UnsupportedCurrencyException;
40  import org.jdtaus.core.container.ContainerFactory;
41  import org.jdtaus.core.container.PropertyException;
42  import org.jdtaus.core.logging.spi.Logger;
43  import org.jdtaus.core.messages.MandatoryPropertyMessage;
44  import org.jdtaus.core.text.Message;
45  
46  /**
47   * jDTAUS Banking SPI {@code TransactionValidator} implementation.
48   *
49   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
50   * @version $JDTAUS: DefaultTransactionValidator.java 8661 2012-09-27 11:29:58Z schulte $
51   */
52  public final class DefaultTransactionValidator implements TransactionValidator
53  {
54  
55      public IllegalTransactionException assertValidTransaction(
56          final LogicalFile lFile, final Transaction transaction, IllegalTransactionException result )
57          throws IOException
58      {
59          if ( lFile == null )
60          {
61              throw new NullPointerException( "lFile" );
62          }
63          if ( transaction == null )
64          {
65              throw new NullPointerException( "transaction" );
66          }
67  
68          this.assertValidProperties();
69          final Map properties = new HashMap( 20 );
70          final LogicalFileType lFileType = lFile.getHeader().getType();
71          final Textschluessel[] allowedTypes = this.getTextschluesselVerzeichnis().searchTextschluessel(
72              Boolean.valueOf( lFileType.isDebitAllowed() ), Boolean.valueOf( lFileType.isRemittanceAllowed() ),
73              lFile.getHeader().getCreateDate() );
74  
75          if ( transaction.getExecutiveAccount() == null )
76          {
77              properties.put( Transaction.PROP_EXECUTIVEACCOUNT, new MandatoryPropertyMessage() );
78          }
79          if ( transaction.getExecutiveBank() == null )
80          {
81              properties.put( Transaction.PROP_EXECUTIVEBANK, new MandatoryPropertyMessage() );
82          }
83          if ( transaction.getExecutiveName() == null || transaction.getExecutiveName().isEmpty() )
84          {
85              properties.put( Transaction.PROP_EXECUTIVENAME, new MandatoryPropertyMessage() );
86          }
87          if ( transaction.getTargetAccount() == null )
88          {
89              properties.put( Transaction.PROP_TARGETACCOUNT, new MandatoryPropertyMessage() );
90          }
91          if ( transaction.getTargetBank() == null )
92          {
93              properties.put( Transaction.PROP_TARGETBANK, new MandatoryPropertyMessage() );
94          }
95          if ( transaction.getTargetName() == null || transaction.getTargetName().isEmpty() )
96          {
97              properties.put( Transaction.PROP_TARGETNAME, new MandatoryPropertyMessage() );
98          }
99          if ( transaction.getType() == null )
100         {
101             properties.put( Transaction.PROP_TYPE, new MandatoryPropertyMessage() );
102         }
103         if ( transaction.getCurrency() == null )
104         {
105             properties.put( Transaction.PROP_CURRENCY, new MandatoryPropertyMessage() );
106         }
107         if ( transaction.getAmount() == null )
108         {
109             properties.put( Transaction.PROP_AMOUNT, new MandatoryPropertyMessage() );
110         }
111         if ( allowedTypes != null && transaction.getType() != null )
112         {
113             int i;
114             for ( i = allowedTypes.length - 1; i >= 0; i-- )
115             {
116                 if ( allowedTypes[i].equals( transaction.getType() ) )
117                 {
118                     break;
119                 }
120             }
121             if ( i < 0 )
122             {
123                 properties.put( Transaction.PROP_TYPE, new TextschluesselConstraintMessage(
124                     lFileType, transaction.getType() ) );
125 
126             }
127         }
128         else if ( transaction.getType() != null )
129         {
130             properties.put( Transaction.PROP_TYPE, new TextschluesselConstraintMessage(
131                 lFileType, transaction.getType() ) );
132 
133         }
134 
135         if ( transaction.getAmount() != null &&
136              !( transaction.getAmount().longValue() >= this.getMinAmount() &&
137                 transaction.getAmount().longValue() <= this.getMaxAmount() ) )
138         {
139             properties.put( Transaction.PROP_AMOUNT, new IllegalAmountMessage( transaction.getAmount() ) );
140         }
141         if ( !( transaction.getDescriptions().length >= this.getMinDescriptions() &&
142                 transaction.getDescriptions().length <= this.getMaxDescriptions() ) )
143         {
144             properties.put( Transaction.PROP_DESCRIPTIONS, new IllegalDescriptionCountMessage(
145                 this.getMaxDescriptions(), transaction.getDescriptions().length ) );
146 
147         }
148 
149         if ( transaction.getCurrency() != null )
150         {
151             try
152             {
153                 this.getCurrencyMapper().getDtausCode( transaction.getCurrency(), lFile.getHeader().getCreateDate() );
154             }
155             catch ( UnsupportedCurrencyException ex )
156             {
157                 if ( this.getLogger().isDebugEnabled() )
158                 {
159                     this.getLogger().debug( ex.toString() );
160                 }
161 
162                 properties.put( Transaction.PROP_CURRENCY, new IllegalCurrencyMessage(
163                     transaction.getCurrency().getCurrencyCode(), lFile.getHeader().getCreateDate() ) );
164 
165             }
166         }
167 
168         if ( properties.size() > 0 )
169         {
170             if ( result == null )
171             {
172                 result = new IllegalTransactionException();
173             }
174 
175             for ( Iterator it = properties.entrySet().iterator(); it.hasNext(); )
176             {
177                 final Map.Entry entry = (Map.Entry) it.next();
178                 result.addMessage( (String) entry.getKey(), (Message) entry.getValue() );
179             }
180         }
181 
182         return result;
183     }
184 
185     /**
186      * Checks configured properties.
187      *
188      * @throws PropertyException for illegal property values.
189      */
190     private void assertValidProperties()
191     {
192         if ( this.getMinAmount() < 0L )
193         {
194             throw new PropertyException( "minAmount", Long.toString( this.getMinAmount() ) );
195         }
196         if ( this.getMaxAmount() < 0L || this.getMinAmount() > this.getMaxAmount() )
197         {
198             throw new PropertyException( "maxAmount", Long.toString( this.getMaxAmount() ) );
199         }
200         if ( this.getMinDescriptions() < 0 )
201         {
202             throw new PropertyException( "minDescriptions", Integer.toString( this.getMinDescriptions() ) );
203         }
204         if ( this.getMaxDescriptions() < 0 || this.getMinDescriptions() > this.getMaxDescriptions() )
205         {
206             throw new PropertyException( "maxDescriptions", Integer.toString( this.getMaxDescriptions() ) );
207         }
208     }
209 
210     //--Constructors------------------------------------------------------------
211 
212 // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausConstructors
213     // This section is managed by jdtaus-container-mojo.
214 
215     /** Standard implementation constructor <code>org.jdtaus.banking.dtaus.ri.zka.DefaultTransactionValidator</code>. */
216     public DefaultTransactionValidator()
217     {
218         super();
219     }
220 
221 // </editor-fold>//GEN-END:jdtausConstructors
222 
223     //------------------------------------------------------------Constructors--
224     //--Dependencies------------------------------------------------------------
225 
226 // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausDependencies
227     // This section is managed by jdtaus-container-mojo.
228 
229     /**
230      * Gets the configured <code>TextschluesselVerzeichnis</code> implementation.
231      *
232      * @return The configured <code>TextschluesselVerzeichnis</code> implementation.
233      */
234     private TextschluesselVerzeichnis getTextschluesselVerzeichnis()
235     {
236         return (TextschluesselVerzeichnis) ContainerFactory.getContainer().
237             getDependency( this, "TextschluesselVerzeichnis" );
238 
239     }
240 
241     /**
242      * Gets the configured <code>CurrencyMapper</code> implementation.
243      *
244      * @return The configured <code>CurrencyMapper</code> implementation.
245      */
246     private CurrencyMapper getCurrencyMapper()
247     {
248         return (CurrencyMapper) ContainerFactory.getContainer().
249             getDependency( this, "CurrencyMapper" );
250 
251     }
252 
253     /**
254      * Gets the configured <code>Logger</code> implementation.
255      *
256      * @return The configured <code>Logger</code> implementation.
257      */
258     private Logger getLogger()
259     {
260         return (Logger) ContainerFactory.getContainer().
261             getDependency( this, "Logger" );
262 
263     }
264 
265 // </editor-fold>//GEN-END:jdtausDependencies
266 
267     //------------------------------------------------------------Dependencies--
268     //--Properties--------------------------------------------------------------
269 
270 // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausProperties
271     // This section is managed by jdtaus-container-mojo.
272 
273     /**
274      * Gets the value of property <code>minDescriptions</code>.
275      *
276      * @return Minimum number of descriptions any transaction has to specify.
277      */
278     private int getMinDescriptions()
279     {
280         return ( (java.lang.Integer) ContainerFactory.getContainer().
281             getProperty( this, "minDescriptions" ) ).intValue();
282 
283     }
284 
285     /**
286      * Gets the value of property <code>minAmount</code>.
287      *
288      * @return Minimum amount any transaction has to specify.
289      */
290     private long getMinAmount()
291     {
292         return ( (java.lang.Long) ContainerFactory.getContainer().
293             getProperty( this, "minAmount" ) ).longValue();
294 
295     }
296 
297     /**
298      * Gets the value of property <code>maxDescriptions</code>.
299      *
300      * @return Maximum number of descriptions any transaction is allowed to specify.
301      */
302     private int getMaxDescriptions()
303     {
304         return ( (java.lang.Integer) ContainerFactory.getContainer().
305             getProperty( this, "maxDescriptions" ) ).intValue();
306 
307     }
308 
309     /**
310      * Gets the value of property <code>maxAmount</code>.
311      *
312      * @return Maximum amount any transaction is allowed to specify.
313      */
314     private long getMaxAmount()
315     {
316         return ( (java.lang.Long) ContainerFactory.getContainer().
317             getProperty( this, "maxAmount" ) ).longValue();
318 
319     }
320 
321 // </editor-fold>//GEN-END:jdtausProperties
322 
323     //--------------------------------------------------------------Properties--
324 }