1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.plausj.bank.german.util;
18
19 import java.io.BufferedReader;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.Map;
26 import java.util.Set;
27
28 import net.sf.plausj.bank.german.AccountCode;
29 import net.sf.plausj.bank.german.BankAccount;
30 import net.sf.plausj.bank.german.BankCode;
31 import net.sf.plausj.bank.german.CheckDigit;
32 import net.sf.plausj.bank.german.strategy.Strategy;
33
34 /**
35 * Contains all methods used to lookup strategies
36 *
37 * @author ninan
38 *
39 */
40 public class StrategyHelperImpl implements StrategyHelper {
41
42 private static final String CLAZZ_NAME_TEMPLATE = "net.sf.plausj.bank.german.strategy.Strategy";
43
44 private static final Map bankCodeToStrategy = new HashMap();
45
46 static {
47 try {
48 init();
49 } catch (Exception e) {
50 System.err.println(e.getMessage());
51 e.printStackTrace();
52 }
53 }
54
55 private static void init() throws Exception {
56 InputStream is = StrategyHelperImpl.class.getClassLoader()
57 .getResourceAsStream("blz.txt");
58
59 InputStreamReader isr = new InputStreamReader(is);
60 BufferedReader br = new BufferedReader(isr);
61
62 Set strategies = null;
63 String line = null;
64 while ((line = br.readLine()) != null) {
65
66 String bankCode = line.substring(0, 8);
67 String strategyId = line.substring(150, 152);
68
69 if (bankCodeToStrategy.containsKey(bankCode)) {
70 strategies = (Set) bankCodeToStrategy.get(bankCode);
71 strategies.add(strategyId);
72 } else {
73 strategies = new HashSet();
74 strategies.add(strategyId);
75 bankCodeToStrategy.put(bankCode, strategies);
76 }
77 }
78 }
79
80 private Strategy getStrategyInstance(final String strategyId) {
81 String clazzName = CLAZZ_NAME_TEMPLATE + strategyId;
82 Strategy strategy = null;
83
84 try {
85 Class clazz = Class.forName(clazzName);
86 strategy = (Strategy) clazz.newInstance();
87 } catch (ClassNotFoundException e) {
88 strategy = new DefaultStrategy(BankAccount.NOT_IMPLEMENTED);
89 } catch (InstantiationException e) {
90 System.err
91 .println("InstanciationException while loading strategies [strategyId='"
92 + strategyId + "']");
93 throw new InternalError();
94 } catch (IllegalAccessException e) {
95 System.err
96 .println("IllegalAccessException while loading strategies [strategyId='"
97 + strategyId + "']");
98 throw new InternalError();
99 }
100
101 return strategy;
102 }
103
104 public Strategy[] getStrategiesByBankCode(BankCode bankCode) {
105
106 if (!bankCodeToStrategy.containsKey(bankCode.getBankCode())) {
107 return new Strategy[] { new DefaultStrategy(
108 BankAccount.BANK_CODE_NOT_FOUND) };
109 }
110
111 Set strategyIds = (Set) bankCodeToStrategy.get(bankCode.getBankCode());
112 if (null == strategyIds)
113 return new Strategy[] { new DefaultStrategy(BankAccount.NOCHECK) };
114
115 Strategy[] strategies = new Strategy[strategyIds.size()];
116
117 Iterator it = strategyIds.iterator();
118 for (int i = 0; i < strategyIds.size(); ++i) {
119 String strategyId = (String) it.next();
120 strategies[i] = getStrategyInstance(strategyId);
121 }
122
123 return strategies;
124
125 }
126
127 private static class DefaultStrategy implements Strategy {
128
129 private final CheckDigit returnDigit;
130
131 private DefaultStrategy(int returnValue) {
132
133 if (BankAccount.VALID == returnValue) {
134 returnDigit = new CheckDigit(CheckDigit.ALWAYS_VALID, 0);
135 return;
136 }
137
138 if (BankAccount.INVALID == returnValue) {
139 returnDigit = new CheckDigit(CheckDigit.ALWAYS_INVALID, 0);
140 return;
141 }
142
143 if (BankAccount.BANK_CODE_NOT_FOUND == returnValue) {
144 returnDigit = new CheckDigit(CheckDigit.BANK_CODE_NOT_FOUND, 0);
145 return;
146 }
147
148 if (BankAccount.NOT_IMPLEMENTED == returnValue) {
149 returnDigit = new CheckDigit(CheckDigit.NOT_IMPLEMENTED, 0);
150 return;
151 }
152
153 returnDigit = null;
154 }
155
156 /**
157 * @see net.sf.plausj.bank.german.strategy.Strategy#calculateCheckDigit(net.sf.plausj.bank.german.AccountCode)
158 */
159 public CheckDigit calculateCheckDigit(BankCode bankCode,
160 AccountCode accountCode) {
161 return returnDigit;
162 }
163
164 }
165 }