Skip to content

Commit d511812

Browse files
author
short000
committed
clean Debatcher CTORs; start to enable DebatcherTest
1 parent 25b2eab commit d511812

File tree

6 files changed

+166
-195
lines changed

6 files changed

+166
-195
lines changed

src/main/java/org/null0/edi/debatcher/ConfigDefault.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.nio.file.Paths;
1111
import java.util.Properties;
1212

13+
import org.apache.commons.lang3.BooleanUtils;
1314
import org.apache.commons.lang3.StringUtils;
1415
import org.null0.edi.debatcher.interfaces.Config;
1516
import org.slf4j.Logger;
@@ -32,6 +33,7 @@ public class ConfigDefault implements Config {
3233
// Configuration Values from debatcher.properties
3334
private Path outDir;
3435
private int bufferSize;
36+
private boolean willUpdateTransactionId;
3537
private String[] validSendersISA06;
3638
private String[] validReceiversISA08;
3739

@@ -79,10 +81,16 @@ public String[] getValidSenders() {
7981
public String[] getValidReceivers() {
8082
return validReceiversISA08;
8183
}
84+
85+
@Override
86+
public boolean willUpdateTransactionId() {
87+
return willUpdateTransactionId;
88+
}
8289

8390
private void setProperties() throws Exception {
8491
this.bufferSize = Integer.parseInt(this.properties.getProperty("buffer_size"));
8592
this.outDir = toPath(this.properties.getProperty("output_directory"));
93+
this.willUpdateTransactionId = BooleanUtils.toBoolean(this.properties.getProperty("will_update_transaction_id"));
8694
this.validSendersISA06 = setList("valid_senders_isa06");
8795
this.validReceiversISA08 = setList("valid_receivers_isa08");
8896
}

src/main/java/org/null0/edi/debatcher/Debatcher.java

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -84,42 +84,33 @@ public class Debatcher {
8484
private String segment = null;
8585
private String outputLocation;
8686
private Map<String, Long> claimIdMap = new HashMap<String, Long>();
87-
private boolean transactionIdUpdate = true;
87+
private boolean updateTransactionId = true;
8888

8989
public Debatcher() {
90-
this.config = new ConfigDefault();
91-
this.ediValidator = new EdiValidatorDefault(false);
92-
this.metadataLogger = new MetadataLoggerDefault();
90+
this (new ConfigDefault(), new EdiValidatorDefault(false), new MetadataLoggerDefault());
9391
}
9492

9593
public Debatcher(Config config, EdiValidator ediValidator, MetadataLogger metadataLogger) {
9694
this.config = config;
9795
this.ediValidator = ediValidator;
9896
this.metadataLogger = metadataLogger;
99-
}
100-
101-
public void debatch(String transactionId, InputStream is) throws Exception {
102-
logger.info("debatching started..." + transactionId);
103-
this.transactionId = transactionId;
104-
this.inputStream = is;
10597
this.outputLocation = this.config.getOutputDirectory().toString();
106-
this.batchIdMetadata = metadataLogger.logBatchSubmissionData(transactionId);
98+
this.updateTransactionId = this.config.willUpdateTransactionId();
99+
}
107100

108-
readInterchangeControls();
109-
is.close();
101+
public void debatch(String transactionId, InputStream inputStream) throws Exception {
102+
debatch(transactionId, -1, inputStream);
110103
}
111104

112-
public Map<String, Long> debatch(String transactionId, long batchIdMetadata, InputStream is, String outputLocation, boolean transactionIdUpdate) throws Exception {
105+
public Map<String, Long> debatch(String transactionId, long batchIdMetadata, InputStream inputStream) throws Exception {
113106
logger.info("debatching started..." + transactionId);
114107
this.transactionId = transactionId;
115-
this.inputStream = is;
116-
this.batchIdMetadata = batchIdMetadata;
117-
this.outputLocation = outputLocation;
118-
this.transactionIdUpdate = transactionIdUpdate;
119-
108+
this.inputStream = inputStream;
109+
this.batchIdMetadata = batchIdMetadata < 0 ? this.metadataLogger.logBatchSubmissionData(transactionId) : batchIdMetadata;
110+
120111
readInterchangeControls();
121-
is.close();
122-
112+
113+
inputStream.close(); // TODO: Why are we closing a stream we do not own? Shouldn't that be the responsibility of the caller that passed it in?
123114
return claimIdMap;
124115
}
125116

@@ -130,9 +121,7 @@ private void readInterchangeControls() throws Exception {
130121
continue;
131122
}
132123
if (segment == null || segment.equals("\r\n")) {
133-
throw new DebatcherException ("Invalid Control Structure",
134-
EdiValidatorDefault.TA1_ERROR_ISAIEA,
135-
ERROR.TYPE_TA1, ERROR_LEVEL.Batch, batchIdMetadata);
124+
throw new DebatcherException ("Invalid Control Structure", EdiValidatorDefault.TA1_ERROR_ISAIEA, ERROR.TYPE_TA1, ERROR_LEVEL.Batch, batchIdMetadata);
136125
}
137126
isaSegment = segment.replaceAll("\\r|\\n", "");
138127
ediValidator.validate(batchIdMetadata, X12_ELEMENT.DATA_SEPARATOR, fieldDlm, null);
@@ -141,9 +130,9 @@ private void readInterchangeControls() throws Exception {
141130

142131
isaIdMetadata = metadataLogger.logIsaData(batchIdMetadata, isa13, isaSegment);
143132
String isa06 = readField(segment, 6);
144-
if (transactionIdUpdate) {
133+
if (updateTransactionId) {
145134
transactionId = isa06.trim() + transactionId;
146-
transactionIdUpdate = false;
135+
updateTransactionId = false;
147136
}
148137
ediValidator.validate(batchIdMetadata, X12_ELEMENT.ISA06, isa06, null);
149138
ediValidator.validate(batchIdMetadata, X12_ELEMENT.ISA07, readField(segment, 7), null);

src/main/java/org/null0/edi/debatcher/interfaces/Config.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public enum ConfigurationSource { ENVIRONMENT_VARIABLE, LOCAL_PROPERTIES, EXTERN
77
public ConfigurationSource getConfigurationSource();
88
public Path getOutputDirectory();
99
public int getBufferSize();
10+
public boolean willUpdateTransactionId();
1011
public String[] getValidSenders();
1112
public String[] getValidReceivers();
1213
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
buffer_size=8096
22
output_directory=./
3+
will_update_transaction_id=true
34
valid_senders_isa06=
4-
valid_receivers_isa08=
5+
valid_receivers_isa08=

0 commit comments

Comments
 (0)