1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
48
49
50
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
187
188
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
211
212
213
214
215
216 public DefaultTransactionValidator()
217 {
218 super();
219 }
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234 private TextschluesselVerzeichnis getTextschluesselVerzeichnis()
235 {
236 return (TextschluesselVerzeichnis) ContainerFactory.getContainer().
237 getDependency( this, "TextschluesselVerzeichnis" );
238
239 }
240
241
242
243
244
245
246 private CurrencyMapper getCurrencyMapper()
247 {
248 return (CurrencyMapper) ContainerFactory.getContainer().
249 getDependency( this, "CurrencyMapper" );
250
251 }
252
253
254
255
256
257
258 private Logger getLogger()
259 {
260 return (Logger) ContainerFactory.getContainer().
261 getDependency( this, "Logger" );
262
263 }
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278 private int getMinDescriptions()
279 {
280 return ( (java.lang.Integer) ContainerFactory.getContainer().
281 getProperty( this, "minDescriptions" ) ).intValue();
282
283 }
284
285
286
287
288
289
290 private long getMinAmount()
291 {
292 return ( (java.lang.Long) ContainerFactory.getContainer().
293 getProperty( this, "minAmount" ) ).longValue();
294
295 }
296
297
298
299
300
301
302 private int getMaxDescriptions()
303 {
304 return ( (java.lang.Integer) ContainerFactory.getContainer().
305 getProperty( this, "maxDescriptions" ) ).intValue();
306
307 }
308
309
310
311
312
313
314 private long getMaxAmount()
315 {
316 return ( (java.lang.Long) ContainerFactory.getContainer().
317 getProperty( this, "maxAmount" ) ).longValue();
318
319 }
320
321
322
323
324 }