Skip to content

Commit

Permalink
lower class scopes; simplify logic; add reafctor TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
short000 committed Mar 17, 2018
1 parent 014beb8 commit 82b9b23
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 85 deletions.
11 changes: 5 additions & 6 deletions src/main/java/org/null0/edi/debatcher/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@
*
*
*/
// TODO: define an interface for Config so that for unit testing we can use it instead without any file I/O dependencies
public class Config {
/** If this environment variable exists it must contain full valid path to properties file */
public static final String ENVAR_NAME = "edi_debatch_config_file";
private static final String ENVAR_NAME = "edi_debatch_config_file";

/** Default expected local properties file name if config_env_name environment variable does not exist */
public static final String FILE_NAME = "debatcher.properties";
private static final String FILE_NAME = "debatcher.properties";

/** Expected name for output directory variable in java properties file */
public static final String OUTDIR_PROP_NAME = "outputDirectory";
private static final String OUTDIR_PROP_NAME = "outputDirectory";

private static final Logger LOG = LoggerFactory.getLogger(Config.class); // Logger
private Properties properties;
Expand Down Expand Up @@ -64,7 +65,7 @@ public Config() {
}

// If for any reason we couldn't fetch a configuration file, use defaults
if(!ok) {
if (!ok) {
setToFailover();
}
}
Expand Down Expand Up @@ -124,6 +125,4 @@ private Path toPath (final String path) throws NotDirectoryException {
}
return p;
}


}
54 changes: 27 additions & 27 deletions src/main/java/org/null0/edi/debatcher/Debatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.null0.edi.debatcher.Delimiters.EdiWrapStyle;
import org.null0.edi.debatcher.EncounterEdiValidator.CLAIM_TYPE;
import org.null0.edi.debatcher.EncounterEdiValidator.ERROR;
import org.null0.edi.debatcher.EncounterEdiValidator.X12_837_ELEMENT;
import org.null0.edi.debatcher.EncounterEdiValidator.X12_ELEMENT;
import org.null0.edi.debatcher.MetadataLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -128,7 +128,7 @@ private void readInterchangeControls() throws Exception {
DebatcherException.ERROR_OR_EXCEPTION.Exception);
}
isaSegment = segment.replaceAll("\\r|\\n", "");
ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.DATA_SEPARATOR, fieldDlm, null);
ediValidator.validate(batchIdMetadata, X12_ELEMENT.DATA_SEPARATOR, fieldDlm, null);
isa13 = readField(segment, 13);
isaCnt++;

Expand All @@ -138,16 +138,16 @@ private void readInterchangeControls() throws Exception {
transactionId = isa06.trim() + transactionId;
transactionIdUpdate = false;
}
ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.ISA06, isa06, null);
ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.ISA07, readField(segment, 7), null);
ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.ISA08, readField(segment, 8), null);
ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.ISA11, readField(segment, 11), null);
ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.ISA12, readField(segment, 12), null);
ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.ISA13, isa13, null);
ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.ISA14, readField(segment, 14), null);
ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.ISA15, readField(segment, 15), null);
ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.ISA16, readField(segment, 16), null);
ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.ISAEnd, segmentDlm, null);
ediValidator.validate(batchIdMetadata, X12_ELEMENT.ISA06, isa06, null);
ediValidator.validate(batchIdMetadata, X12_ELEMENT.ISA07, readField(segment, 7), null);
ediValidator.validate(batchIdMetadata, X12_ELEMENT.ISA08, readField(segment, 8), null);
ediValidator.validate(batchIdMetadata, X12_ELEMENT.ISA11, readField(segment, 11), null);
ediValidator.validate(batchIdMetadata, X12_ELEMENT.ISA12, readField(segment, 12), null);
ediValidator.validate(batchIdMetadata, X12_ELEMENT.ISA13, isa13, null);
ediValidator.validate(batchIdMetadata, X12_ELEMENT.ISA14, readField(segment, 14), null);
ediValidator.validate(batchIdMetadata, X12_ELEMENT.ISA15, readField(segment, 15), null);
ediValidator.validate(batchIdMetadata, X12_ELEMENT.ISA16, readField(segment, 16), null);
ediValidator.validate(batchIdMetadata, X12_ELEMENT.ISAEnd, segmentDlm, null);

gsCnt = 0;

Expand All @@ -157,10 +157,10 @@ private void readInterchangeControls() throws Exception {

if (!"IEA".equals(readField(segment, 0))) {
// throw new Exception("Missing IEA segment");
ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.IEA01, null, null);
ediValidator.validate(batchIdMetadata, X12_ELEMENT.IEA01, null, null);
}

ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.IEA01, readField(segment, 1), String.valueOf(gsCnt));
ediValidator.validate(batchIdMetadata, X12_ELEMENT.IEA01, readField(segment, 1), String.valueOf(gsCnt));

String iea02 = readField(segment, 2);
if (!isa13.equals(iea02)) {
Expand All @@ -184,7 +184,7 @@ private void readFuntionGroups() throws Exception {

if (!readGsSegment) {
if (!"GS".equals(readField(segment, 0))) {
ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.GS, null, null);
ediValidator.validate(batchIdMetadata, X12_ELEMENT.GS, null, null);
}
readGsSegment = true;
}
Expand All @@ -200,23 +200,23 @@ private void readFuntionGroups() throws Exception {

gsIdMetadata = metadataLogger.logGsData(isaIdMetadata, gs06, gsSegment);

ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.GS01, readField(gsSegment, 1), null);
ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.GS08, readField(gsSegment, 8), null);
ediValidator.validate(batchIdMetadata, X12_ELEMENT.GS01, readField(gsSegment, 1), null);
ediValidator.validate(batchIdMetadata, X12_ELEMENT.GS08, readField(gsSegment, 8), null);

stCnt = 0;
getNextSegment();
readTransactionSets();
if (!"GE".equals(readField(segment, 0))) {
// throw new Exception("Missing GE segment");
ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.GE, null, null);
ediValidator.validate(batchIdMetadata, X12_ELEMENT.GE, null, null);
}
String ge02 = readField(segment, 2);
if (!gs06.equals(ge02)) {
logger.error("GS06 {} & GE02 {} don't match", gs06, ge02);
// throw new Exception("GS06 & GE02 don't match");
ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.GS06, gs06, ge02);
ediValidator.validate(batchIdMetadata, X12_ELEMENT.GS06, gs06, ge02);
}
ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.GE01, readField(segment, 1), String.valueOf(stCnt));
ediValidator.validate(batchIdMetadata, X12_ELEMENT.GE01, readField(segment, 1), String.valueOf(stCnt));

metadataLogger.updateGsData(gsIdMetadata, stCnt);
}
Expand All @@ -240,11 +240,11 @@ private void readTransactionSets() throws Exception {

String st01 = readField(segment, 1);

ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.ST01, st01, null);
ediValidator.validate(batchIdMetadata, X12_ELEMENT.ST01, st01, null);

st02 = readField(segment, 2);
claimType = getClaimType(segment);
ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.ST03, readField(segment, 3), claimType.name());
ediValidator.validate(batchIdMetadata, X12_ELEMENT.ST03, readField(segment, 3), claimType.name());

hlCnt = 0;
stIdMetadata = metadataLogger.logStData(gsIdMetadata, st02, segment.trim());
Expand All @@ -260,17 +260,17 @@ private void readTransactionSets() throws Exception {
}

if (isIeaFound || readField(segment, 0).equals("SE")) {
ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.SE, readField(segment, 0), null);
ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.SE01, readField(segment, 1), String.valueOf(segmentCnt));
ediValidator.validate(batchIdMetadata, X12_ELEMENT.SE, readField(segment, 0), null);
ediValidator.validate(batchIdMetadata, X12_ELEMENT.SE01, readField(segment, 1), String.valueOf(segmentCnt));
} else { // If the SE segment is missing but the IEA segment is also missing, then treat as missing IEA.
ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.IEA01, null, null);
ediValidator.validate(batchIdMetadata, X12_ELEMENT.IEA01, null, null);
}

String se02 = readField(segment, 2);
if (!st02.equals(se02)) {
logger.error("ST02 {} & SE02 {} don't match", st02, se02);
// throw new Exception ("ST02 & SE02 don't match");
ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.ST02, st02, se02);
ediValidator.validate(batchIdMetadata, X12_ELEMENT.ST02, st02, se02);
}

metadataLogger.updateStData(stIdMetadata, hlCnt);
Expand Down Expand Up @@ -352,7 +352,7 @@ private void readHierarchicalLevels() throws Exception {
// ERROR_LEVEL.Batch, batchIdMetadata, ERROR_OR_EXCEPTION.Exception);
ediValidator.logError(batchIdMetadata, EncounterEdiValidatorDefault.IK3_999_ERROR_MISS_SEG, ERROR.TYPE_999, "Missing CLM segment");
} else {
ediValidator.validate(batchIdMetadata, X12_837_ELEMENT.IEA01, null, null);
ediValidator.validate(batchIdMetadata, X12_ELEMENT.IEA01, null, null);
}
}

Expand Down
14 changes: 6 additions & 8 deletions src/main/java/org/null0/edi/debatcher/DebatcherException.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static String getStackTraceFromException(Throwable e) {
return getStackTraceFromException(e, null);
}

public static String getStackTraceFromException(Throwable e, StringBuffer buf) {
private static String getStackTraceFromException(Throwable e, StringBuffer buf) {
String stackTrace = null;

if (e != null) {
Expand All @@ -130,9 +130,9 @@ public static String getStackTraceFromException(Throwable e, StringBuffer buf) {
}
}

if (e.getCause() != null)
if (e.getCause() != null) {
getStackTraceFromException(e.getCause(), buf);

}
stackTrace = new String(buf);
}

Expand All @@ -141,15 +141,13 @@ public static String getStackTraceFromException(Throwable e, StringBuffer buf) {

public static Throwable getLowestLevelCause(Throwable e) {
Throwable result = null;

// null will be returned unless there ia a cause
// null will be returned unless there is a cause
if (e != null && e.getCause() != null) {
result = e;

while (result.getCause() != null)
while (result.getCause() != null) {
result = result.getCause();
}
}

return result;
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/null0/edi/debatcher/Delimiters.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.null0.edi.debatcher;

public class Delimiters {
class Delimiters {
private char dataElementSeparator;
private char segmentTerminator;
private EdiWrapStyle ediWrap;
private String osNewLine;

public enum EdiWrapStyle {
enum EdiWrapStyle {
Unknown, Unwrapped, Unix, Windows
};

Expand Down
29 changes: 13 additions & 16 deletions src/main/java/org/null0/edi/debatcher/EncounterEdiValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
//import org.null0.edi.debatcher.validation.EncounterEdiValidator.ERROR;

public interface EncounterEdiValidator {

static enum X12_837_ELEMENT {
static enum X12_ELEMENT {
ISA06, ISA07, ISA08, ISA11, ISA12, ISA13, ISA14, ISA15, ISA16, IEA01, IEA02, ISAEnd, DATA_SEPARATOR, GS, GS01, GS06, GS08, GE, GE01, ST01, ST02, ST03, SE, SE01
};

Expand Down Expand Up @@ -41,50 +40,48 @@ static enum X12_837_ELEMENT {
static final String AK9_999_ERROR_GS08 = "AK905-2";
static final String AK9_999_ERROR_GE01 = "AK905-5";
static final String AK9_999_ERROR_GE_MISSING = "AK905-3";

static final String[] validISA12 = { "00501" }; // TODO: specific to 837 claims; decouple
static final String[] validISA15 = { "P", "T" }; // TODO: specific to 837 claims; decouple

// TODO: all these below were hard-coded AND this is NOT the proper way to validate!
// According to EDI standards, the valid values below are defined by fields
// from the special fixed-sized ISA01 segment.
static final String[] validISA11 = { ":", "^" };
static final String[] validISA12 = { "00501" };
static final String[] validISA15 = { "P", "T" };
static final String[] validISAEnd = { "~" }; // { ":", "~", "\n" };
static final String[] validDataElementSeparator = { "*" };
static final String[] validComponentSeparator = { ":", ">" };

// TODO: these below are specific to 837 claims; decouple
static final String[] validSt03ForP = { "005010X222A1", "005010X222A2" };
static final String[] validSt03ForI = { "005010X223A2", "005010X223A3" };

static enum CLAIM_TYPE {
PRO, INS, OTH
};

static final String BRE_ERROR = "BRE-ERROR";

public boolean validate(long batchId, X12_837_ELEMENT elementName, String data, String compareWithData)
throws Exception;

public boolean validate(long batchId, X12_ELEMENT elementName, String data, String compareWithData) throws Exception;
public void logError(long batchId, String errorCode, ERROR errorType, String errorMessage) throws Exception;

public enum ERROR {
TYPE_TA1("TA1"), TYPE_999("999"), TYPE_BRE("BRE");

private static final Map<String, ERROR> stringToEnum = new HashMap<String, ERROR>();

static {
for (ERROR code : values())
for (ERROR code : values()) {
stringToEnum.put(code.getCode(), code);
}
}

public static ERROR fromLegacyCode(String code) {
return stringToEnum.get(code);
}

private final String code;

ERROR(String code) {
this.code = code;
}

public String getCode() {
return code;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.slf4j.LoggerFactory;

public class EncounterEdiValidatorDefault implements EncounterEdiValidator {

private static final Logger logger = LoggerFactory.getLogger(EncounterEdiValidatorDefault.class);
private boolean rejectOnValidationError;
private static final boolean stopAtFirstValidationFailure = true;
Expand All @@ -18,9 +17,7 @@ public EncounterEdiValidatorDefault(boolean rejectOnValidationError) {
this.rejectOnValidationError = rejectOnValidationError;
}

public boolean validate(long batchId, X12_837_ELEMENT elementName, String data, String compareWithData)
throws Exception {

public boolean validate(long batchId, X12_ELEMENT elementName, String data, String compareWithData) throws Exception {
logger.debug("Validating {} for data {} and compareWithData {}", elementName, data, compareWithData);

switch (elementName) {
Expand Down Expand Up @@ -183,7 +180,7 @@ public boolean validate(long batchId, X12_837_ELEMENT elementName, String data,
}

/*
* public boolean validate(long batchId, X12_837_ELEMENT elementName, String
* public boolean validate(long batchId, X12_ELEMENT elementName, String
* data, String compareWithData) throws Exception {
*
* switch (elementName) { case IEA01: if(!equals(data, compareWithData)) {
Expand All @@ -208,7 +205,7 @@ public void logError(long batchId, String errorCode, ERROR errorType, String err

}

public boolean isValidSenderId(String senderId, long batchId) throws Exception {
private boolean isValidSenderId(String senderId, long batchId) throws Exception {
// TODO: replace this if it makes sense
/*
* MetadataServicesImpl services = new MetadataServicesImpl();
Expand All @@ -219,29 +216,16 @@ public boolean isValidSenderId(String senderId, long batchId) throws Exception {
return true;
}

public boolean isEmpty(String data) {
if (data == null || data.trim().length() == 0) {
return true;
}
return false;
private boolean isEmpty(String data) {
return data == null || data.trim().length() == 0;
}

public boolean equals(String str1, String str2) {
if (str1 == null || str2 == null) {
return false;
}
if (!str1.trim().equals(str2.trim())) {
return false;
}
return true;
private boolean equals(String str1, String str2) {
return str1 != null && str2 != null && str1.trim().equals(str2.trim());
}

public boolean isNumeric(String s) {

if (s == null)
return false;

return s.matches("\\d+");
private boolean isNumeric(String s) {
return s == null ? false : s.matches("\\d+");
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.null0.edi.debatcher;

public class HierarchicalLevel {
class HierarchicalLevel {

private int id;
private int parentId;
Expand Down

0 comments on commit 82b9b23

Please sign in to comment.