1 package net.sf.bacchus.records;
2
3 import java.util.Collections;
4 import java.util.List;
5 import java.util.Locale;
6
7 import net.sf.bacchus.Addenda;
8 import net.sf.bacchus.RecordUtilities;
9 import net.sf.bacchus.InvalidRecordException;
10
11
12 public class AbstractTerminalDetail extends AbstractDetail {
13
14
15 private static final String DETAIL_FORMAT = "%-22s" + "%-15s" + "%-2s";
16
17
18 public static class OptionalTerminal extends Terminal {
19
20 }
21
22
23 private String identification;
24
25
26 private String name;
27
28
29 private String cardTransaction;
30
31
32 private final OptionalTerminal terminal = new OptionalTerminal();
33
34
35
36
37
38 public OptionalTerminal getTerminal() {
39 return this.terminal;
40 }
41
42
43
44
45
46 public String getIdentification() {
47 return this.identification == null ? "" : this.identification;
48 }
49
50
51
52
53
54 public void setIdentification(final String identification) {
55 this.identification = identification;
56 }
57
58
59
60
61
62 public String getName() {
63 return this.name == null ? "" : this.name;
64 }
65
66
67
68
69
70 public void setName(final String name) {
71 this.name = name;
72 }
73
74
75
76
77
78 public String getCardTransaction() {
79 return this.cardTransaction == null ? "" : this.cardTransaction;
80 }
81
82
83
84
85
86 public void setCardTransaction(final String transaction) {
87 this.cardTransaction = transaction;
88 }
89
90
91
92
93
94
95 @Override
96 public List<Addenda> getAddenda() {
97 if (this.terminal.isActive()) {
98 return Collections.<Addenda> singletonList(this.terminal);
99 } else {
100 return Collections.emptyList();
101 }
102 }
103
104
105
106
107
108 @Override
109 public void load(final String record) {
110 super.load(record);
111 setIdentification(record.substring(39, 61).trim());
112 setName(record.substring(61, 76).trim());
113 setCardTransaction(record.substring(76, 78).trim());
114 }
115
116
117
118
119
120 @Override
121 public void validate() throws InvalidRecordException {
122 super.validate();
123
124 if (this.cardTransaction == null) {
125 throw new InvalidRecordException("Missing card transaction type code");
126 }
127 }
128
129
130
131
132
133
134
135 @Override
136 public void normalize() {
137 super.normalize();
138
139 if (RecordUtilities.empty(this.identification)) {
140 this.identification = null;
141 }
142
143 if (RecordUtilities.empty(this.name)) {
144 this.name = null;
145 }
146
147 if (RecordUtilities.empty(this.cardTransaction)) {
148 this.cardTransaction = null;
149 }
150 }
151
152
153
154
155
156
157
158 @Override
159 protected StringBuilder build() {
160 return super.build().replace(39, 78,
161 String.format(Locale.US, DETAIL_FORMAT, getIdentification(), getName(), getCardTransaction()));
162 }
163 }