View Javadoc

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  /** An entry detail record with an optional terminal addenda record. */
12  public class AbstractTerminalDetail extends AbstractDetail {
13  
14      /** Format for the name, identification number and card transaction type. */
15      private static final String DETAIL_FORMAT = "%-22s" + "%-15s" + "%-2s";
16  
17      /** A terminal addenda record. */
18      public static class OptionalTerminal extends Terminal {
19  
20      }
21  
22      /** the individual identification number. */
23      private String identification;
24  
25      /** the individual name. */
26      private String name;
27  
28      /** the card transaction type code. */
29      private String cardTransaction;
30  
31      /** the optional terminal addenda record. */
32      private final OptionalTerminal terminal = new OptionalTerminal();
33  
34      /**
35       * Gets the terminal addenda record. This value will never be {@code null}.
36       * @return the terminal addenda record.
37       */
38      public OptionalTerminal getTerminal() {
39          return this.terminal;
40      }
41  
42      /**
43       * Gets the individual identification number.
44       * @return the individual identification number.
45       */
46      public String getIdentification() {
47          return this.identification == null ? "" : this.identification;
48      }
49  
50      /**
51       * Sets the individual identification number.
52       * @param identification the individual identification number.
53       */
54      public void setIdentification(final String identification) {
55          this.identification = identification;
56      }
57  
58      /**
59       * Gets the individual name.
60       * @return the individual name.
61       */
62      public String getName() {
63          return this.name == null ? "" : this.name;
64      }
65  
66      /**
67       * Sets the individual name.
68       * @param name the individual name.
69       */
70      public void setName(final String name) {
71          this.name = name;
72      }
73  
74      /**
75       * Gets the card transaction type code.
76       * @return the card transaction type code.
77       */
78      public String getCardTransaction() {
79          return this.cardTransaction == null ? "" : this.cardTransaction;
80      }
81  
82      /**
83       * Sets the card transaction type code.
84       * @param transaction the card transaction type code.
85       */
86      public void setCardTransaction(final String transaction) {
87          this.cardTransaction = transaction;
88      }
89  
90      /**
91       * Gets the a singleton containing the {@link Terminal} if there is terminal
92       * info, otherwise an empty list.
93       * @return {@inheritDoc}
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      * {@inheritDoc}
106      * @param record {@inheritDoc}
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      * {@inheritDoc}
118      * @throws {@inheritDoc}
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      * Calls {@link AbstractDetail#normalize() the superclass implementation}
131      * and clears the {@link #getIdentification() identification},
132      * {@link #getName() name} or {@link #getCardTransaction() card transaction
133      * type} if any are empty.
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      * Adds the identification number, name and card transaction type code in
154      * positions 40 through 78 of the value returned by
155      * {@link AbstractDetail#format() the superclass implementation}.
156      * @throws {@inheritDoc}
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 }