1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.gl.batch.service.impl;
17
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.io.Reader;
21 import java.util.StringTokenizer;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.kuali.ole.gl.GeneralLedgerConstants;
25 import org.kuali.ole.gl.batch.service.ReconciliationParserService;
26 import org.kuali.rice.core.api.util.type.KualiDecimal;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public class ReconciliationParserServiceImpl implements ReconciliationParserService {
64 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(FileEnterpriseFeederHelperServiceImpl.class);
65 private enum ParseState {
66 INIT, TABLE_DEF, COLUMN_DEF, CHECKSUM_DEF;
67 };
68
69
70
71
72
73
74
75
76
77
78
79
80 public ReconciliationBlock parseReconciliationBlock(Reader reader, String tableId) throws IOException {
81 BufferedReader bufReader;
82 if (reader instanceof BufferedReader) {
83 bufReader = (BufferedReader) reader;
84 }
85 else {
86 bufReader = new BufferedReader(reader);
87 }
88
89
90 ReconciliationBlock reconciliationBlock = null;
91
92 int linesInBlock = 0;
93
94
95 String line = bufReader.readLine();
96 while (line != null && reconciliationBlock == null) {
97 line = stripCommentsAndTrim(line);
98 if (StringUtils.isBlank(line)) {
99 line = bufReader.readLine();
100 continue;
101 }
102
103 StringTokenizer strTok = new StringTokenizer(line);
104 if (!strTok.hasMoreTokens()) {
105 LOG.error("Cannot find TABLE_DEF_STRING");
106 throw new RuntimeException();
107 }
108 String command = strTok.nextToken();
109 if (command.equalsIgnoreCase(GeneralLedgerConstants.Reconciliation.TABLE_DEF_STRING)) {
110 if (!strTok.hasMoreTokens()) {
111 LOG.error("Cannot find TABLE_DEF_STRING");
112 throw new RuntimeException();
113 }
114 String parsedTableId = strTok.nextToken();
115 if (parsedTableId.equalsIgnoreCase(tableId)) {
116 if (!strTok.hasMoreTokens()) {
117 LOG.error("Cannot find Parsed Table Id");
118 throw new RuntimeException();
119 }
120 String parsedRowCountStr = StringUtils.removeEnd(strTok.nextToken(), ";");
121 parsedRowCountStr = StringUtils.removeEnd(parsedRowCountStr, ".00");
122 int parsedRowCount = Integer.parseInt(parsedRowCountStr);
123
124 reconciliationBlock = new ReconciliationBlock();
125 reconciliationBlock.setTableId(parsedTableId);
126 reconciliationBlock.setRowCount(parsedRowCount);
127
128 linesInBlock++;
129
130 break;
131 }
132 }
133 line = bufReader.readLine();
134 }
135
136 if (reconciliationBlock == null) {
137 return null;
138 }
139
140 boolean endBlockLineEncountered = false;
141 line = bufReader.readLine();
142 while (line != null && !endBlockLineEncountered) {
143 line = stripCommentsAndTrim(line);
144 if (StringUtils.isBlank(line)) {
145 continue;
146 }
147
148 StringTokenizer strTok = new StringTokenizer(line);
149 if (!strTok.hasMoreTokens()) {
150 LOG.error("Cannot find COLUMN_DEF_STRING");
151 throw new RuntimeException();
152 }
153
154 String command = strTok.nextToken();
155 if (command.equalsIgnoreCase(GeneralLedgerConstants.Reconciliation.COLUMN_DEF_STRING)) {
156 if (!strTok.hasMoreTokens()) {
157 LOG.error("Cannot find COLUMN_DEF_STRING");
158 throw new RuntimeException();
159 }
160 String fieldName = strTok.nextToken();
161 if (!strTok.hasMoreTokens()) {
162 LOG.error("Cannot find COLUMN_DEF_STRING");
163 throw new RuntimeException();
164 }
165 String columnAmountStr = strTok.nextToken();
166 columnAmountStr = StringUtils.removeEnd(columnAmountStr, ";");
167
168 KualiDecimal columnAmount = new KualiDecimal(columnAmountStr);
169 ColumnReconciliation columnReconciliation = new ColumnReconciliation();
170 columnReconciliation.setFieldName(fieldName);
171 columnReconciliation.setDollarAmount(columnAmount);
172 reconciliationBlock.addColumn(columnReconciliation);
173 linesInBlock++;
174 }
175 else if (command.equalsIgnoreCase(GeneralLedgerConstants.Reconciliation.CHECKSUM_DEF_STRING)) {
176 if (!strTok.hasMoreTokens()) {
177 LOG.error("Cannot find CHECKSUM_DEF_STRING");
178 throw new RuntimeException();
179 }
180 String checksumStr = strTok.nextToken();
181 checksumStr = StringUtils.removeEnd(checksumStr, ";");
182
183 int checksum = Integer.parseInt(checksumStr);
184
185 if (checksum != linesInBlock) {
186 LOG.error("Check Sum String is not same as Lines in Block");
187 throw new RuntimeException();
188 }
189 break;
190 }
191 else {
192 LOG.error("Cannot find any fields");
193 throw new RuntimeException();
194 }
195
196 line = bufReader.readLine();
197 }
198 return reconciliationBlock;
199 }
200
201
202
203
204
205
206
207 protected String stripCommentsAndTrim(String line) {
208 int commentIndex = line.indexOf(GeneralLedgerConstants.Reconciliation.COMMENT_STRING);
209 if (commentIndex > -1) {
210
211 line = line.substring(0, commentIndex);
212 }
213
214 line = line.trim();
215 return line;
216 }
217 }