New DataCell Methods and Derived ClassesWe can use most of the DataCell classes and methods we developed in the previous chapters. We need only a few new ones in this chapter to satisfy some of the X12 data type formatting requirements. We'll handle the X12 data types as follows .
DataCellX12N Class (Extends DataCellN)As noted above, this class is derived from the DataCellN class developed in Chapter 8. It uses base class methods for everything but the prepareOutput method, which handles the formatting requirements of the X12 Numeric data type. Logic for the DataCellX12N prepareOutput MethodArguments: None Returns: Status or throws exception NegativeNumber <- false IF first character in Cell Buffer is "-" Cell Buffer <- Shift Cell Buffer left one character to remove Minus sign Negative Number <- true Decrement Buffer Length ENDIF Minimum Length <- Call Grammar Element's getAttribute for "MinLength", converting to integer IF (Buffer Length < Minimum Length) Call justifyFieldRight, passing Minimum Length and zero as Fill Character ENDIF Maximum Length <- Call Grammar Element's getAttribute for "MaxLength", converting to integer IF (Buffer Length > Maximum Length) Return failure ENDIF IF Negative Number = true Cell Buffer <- Shift Cell Buffer right one character to insert Minus sign Increment Buffer Length ENDIF Return success DataCellX12R Class (Extends DataCellReal)As noted above, this class is derived from the DataCellReal class developed in Chapter 8. It uses that class's toXML method but provides its own fromXML and prepareOutput methods. Logic for the DataCellX12R fromXML MethodArguments: None Returns: Error status or throws exception Trim Spaces IF Cell Buffer = -0, INF, -INF, or NaN Return error for unsupported constant in X12 Decimal date type ENDIF Convert to uppercase since XML schema language data types allow a lower case 'e' to indicate the exponent IF first Character in Cell Buffer = "+" Cell Buffer <- Shift Cell Buffer left one character to remove plus sign Decrement Buffer Length ENDIF Return success Logic for the DataCellX12R prepareOutput MethodArguments: None Returns: Status or throws exception NegativeNumber <- false ExponentialNotation <- false IF first character in Cell Buffer is "-" Cell Buffer <- Shift Cell Buffer left one character to remove Minus sign Negative Number <- true Decrement Buffer Length ENDIF Minimum Length <- Call Grammar Element's getAttribute for "MinLength", converting to integer Maximum Length <- Call Grammar Element's getAttribute for "MaxLength", converting to integer IF Cell Buffer contains "E" Increment Minimum Length Increment Maximum Length Exponential Notation <- true ENDIF IF Cell Buffer contains a decimal point Increment Minimum Length Increment Maximum Length ENDIF IF (Buffer Length < Minimum Length) Call justifyFieldRight, passing Minimum Length and zero as Fill Character ENDIF IF (Buffer Length > Maximum Length) Truncate <- Call Grammar Element's getAttribute for "Truncatable" IF Truncate = false OR Exponential Notation = true Return truncation error ENDIF Depending on language features, truncate Cell Buffer contents to specified Length stopping when Length is achieved or decimal point is truncated IF (Buffer Length > Maximum Length) Return failure ENDIF Buffer Length <- Maximum Length ENDIF IF Negative Number = true Cell Buffer <- Shift Cell Buffer right one character to insert Minus sign Increment Buffer Length ENDIF Return success DataCellX12DT Class (Extends DataCellDateYYYYMMDD)This class relies heavily on the base class methods. The only thing we really need to deal with is X12's support of a YYMMDD date format. We know we're using that format if the maximum length of the data element is six digits. Logic for the DataCellX12DT fromXML MethodArguments: None Returns: Error status or throws exception Call base class fromXML method Maximum Length <- Call Grammar Element's getAttribute for "MaxLength" IF Maximum Length = "6" Shift Cell Buffer two characters to the left Buffer Length <- 6 ENDIF Return success Logic for the DataCellX12DT toXML MethodArguments: None Returns: Error status or throws exception Cell Buffer < trim leading and trailing whitespace from Cell Buffer IF Buffer Length = 6 Cell Buffer <- "20" + Cell Buffer Buffer Length <- 8 ENDIF Return call to base class toXML method In converting from YYMMDD to YYYYMMDD by adding a prefix of 20, we're making a somewhat arbitrary assumption that any six-digit dates are from the 21st century. I think this is fairly reasonable since, by now, if anyone cares about using 20th-century dates in an X12 transaction set, they most likely are using YYYYMMDD so as not to leave anything to chance. DataCellX12TM ClassThis class handles conversion to and from the X12 Time data type to the schema language time data type based on ISO 8601. The differences in representation are shown earlier in the chapter in Table 9.6. Logic for the DataCellX12TM fromXML MethodArguments: None Returns: Error status or throws exception IF Buffer Length < 8 Return error ENDIF Hours <- Cell Buffer characters at offsets 0 and 1 Minutes <- Cell Buffer characters at offsets 3 and 4 Seconds <- Cell Buffer characters at offsets 6 and 7 Fractional Seconds <- Cell Buffer characters 9 and beyond, if present Cell Buffer <- Hours + Minutes + Seconds + Fractional Seconds Buffer Length <- String length of Cell Buffer Return success Logic for the DataCellX12TM toXML MethodArguments: None Returns: Error status or throws exception Cell Buffer < trim leading and trailing whitespace from Cell Buffer IF Buffer Length < 4 Return error ENDIF Hours <- Cell Buffer characters at offsets 0 and 1 Minutes <- Cell Buffer characters at offsets 2 and 3 Seconds <- Cell Buffer characters at offsets 4 and 5, if present, otherwise "00" Fractional Seconds <- Cell Buffer characters 6 and beyond if present Cell Buffer <- Hours + ":" + Minutes + ":" + Seconds + "." + Fractional Seconds Buffer Length <- String length of Cell Buffer Return success The prepareOutput method truncates the data if required. We will somewhat arbitrarily assume that if the X12 target data element is not large enough to handle seconds or fractions of a second, users will not mind if we discard them. Logic for the DataCellX12TM prepareOutput MethodArguments: None Returns: Status or throws exception Maximum Length <- Call Grammar Element's getAttribute for "MaxLength" IF Buffer Length > Maximum Length Truncate Cell Buffer from left to Maximum Length Buffer Length <- Maximum Length Return success |