예제 #1
0
        public override FieldError ValidateValue(StringBuilder data)
        {
            FieldError error = null;
            string     str   = data.ToString();

            if (minLength >= 0 && data.Length < minLength)
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeDataElementTooShortCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeDataElementTooShortCode), str);
            }

            if (maxLength >= 0 && data.Length > maxLength)
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeDataElementTooLongCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeDataElementTooLongCode), str);
            }

            if (mAllowedValues != null && mAllowedValues.Count > 0 && !mAllowedValues.ContainsKey(str))
            {
                //error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidCodeValueCode,
                //  X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidCodeValueCode), str);

                StringBuilder allowedList = new StringBuilder();
                Dictionary <string, string> .KeyCollection keyColl = mAllowedValues.Keys;
                allowedList.Append('{');
                foreach (string key in keyColl)
                {
                    if (allowedList.Length > 1)
                    {
                        allowedList.Append(", ");
                    }

                    allowedList.Append(key);
                }
                allowedList.Append('}');

                string errorDescription = "Value {0} not found in list of possible values {1}";
                errorDescription = string.Format(errorDescription, str, allowedList.ToString());

                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidCodeValueCode,
                                                          errorDescription, str);
            }
            else
            {
                CharSet charSet = CharSetFactory.GetCharSetClass(CharSetFactory.ExtendedCharSetIndex);

                for (int i = 0; i < data.Length; i++)
                {
                    if (!charSet.IsMember(data[i]))
                    {
                        error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidCharacterInDataElementCode,
                                                                  X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidCharacterInDataElementCode), data.ToString());
                        break;
                    }
                }
            }

            return(error);
        }
예제 #2
0
        /*
         * All characters are allowed, however length restriction needs to be obeyed
         * Leading spaces are allowed only for min length restriction
         */
        public override FieldError ValidateValue(StringBuilder data)
        {
            FieldError error           = null;
            string     str             = data.ToString();
            bool       isLeadingSpace  = data.Length > 0 && data[0] == ' ';
            bool       isTrailingSpace = data.Length > 0 && data[data.Length - 1] == ' ' && data.ToString().Trim().Length != 0;

            if (minLength >= 0 && data.Length < minLength)
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeDataElementTooShortCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeDataElementTooShortCode), str);
            }

            else if (maxLength >= 0 && data.Length > maxLength)
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeDataElementTooLongCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeDataElementTooLongCode), str);
            }

            else if ((isTrailingSpace || isLeadingSpace) && minLength >= 0 && data.Length > minLength &&
                     false)
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidCharacterInDataElementCode,
                                                          //X12ErrorCode.X12DataElementLeadingOrTrailingSpaceFoundDescription,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidCharacterInDataElementCode),
                                                          data.ToString());
            }

            else //no min max error
            {
                //todo Get this from the environment context
                CharSet charSet = CharSetFactory.GetCharSetClass(CharSetFactory.ExtendedCharSetIndex);

                for (int i = 0; i < data.Length; i++)
                {
                    if (!charSet.IsMember(data[i]))
                    {
                        error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidCharacterInDataElementCode,
                                                                  X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidCharacterInDataElementCode),
                                                                  data.ToString());
                        break;
                    }
                }
            }

            return(error);
        }
예제 #3
0
        public override FieldError ValidateValue(
            StringBuilder data)
        {
            FieldError error = null;
            string     str   = data.ToString();

            if (data.Length < minLength || data.Length == 5)
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidTimeCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidTimeCode), str);
                return(error);
            }

            if (data.Length > maxLength)
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidTimeCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidTimeCode), str);
                return(error);
            }

            //run thru the data to ensure all are digits
            for (int i = 0; i < data.Length; i++)
            {
                if (!DataTypeHelper.IsDigit(data[i]))
                {
                    error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidTimeCode,
                                                              X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidTimeCode), data.ToString());
                    return(error);
                }
            }

            //data is between min and max length, thus it is atleast 4 chars
            //and all are digits
            int hour = 10 * (data[0] - '0') + (data[1] - '0');
            int min  = 10 * (data[2] - '0') + (data[3] - '0');
            int sec  = data.Length == 4 ? 0 : 10 * (data[4] - '0') + (data[5] - '0');

            if (hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 || sec > 59)
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidTimeCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidTimeCode), str);
            }


            return(error);
        }
        /// <summary>
        /// Construct DocumentFragment for leaf node parent and then create and attach leaf nodes to it.
        /// </summary>
        /// <param name="pluglet"></param>
        /// <param name="segmentDetails"></param>
        /// <param name="internalSegment"></param>
        /// <param name="ediDelimiters"></param>
        /// <param name="segmentStartIndex"></param>
        /// <param name="segmentEndIndex"></param>
        /// <param name="ediErrors"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public static DocumentFragment ConstructDocumentFragment(this IPluglet pluglet, string[] segmentDetails, bool internalSegment, Delimiters ediDelimiters,
                                                                 int segmentSeqNumber, long segmentStartIndex, long segmentEndIndex, ref InterchangeErrors ediErrors, out string error)
        {
            if (pluglet == null)
            {
                throw new ArgumentNullException("pluglet");
            }

            if (segmentDetails == null)
            {
                throw new ArgumentNullException("segmentDetails");
            }

            LoggerFactory.Logger.Info("PlugletExtensions.ConstructDocumentFragment", "Constructing {0} instance", pluglet.Tag);

            if (pluglet.Children == null)
            {
                error = string.Format("Pluglet {0} does not have childrens", pluglet.Name);
                return(null);
            }

            // -1 is required since SegmentDetails[0] is the SegmentName
            if (pluglet.Children.Count < segmentDetails.Length - 1)
            {
                error = string.Format("Document has {1} data elements, it is less than segment children count {2} as described in Spec",
                                      pluglet.Name, pluglet.Children.Count, segmentDetails.Length);
                return(null);
            }

            error = string.Empty;
            ++pluglet.CurrentOccurrences;
            DocumentFragment documentFragment = new DocumentFragment()
            {
                Pluglet        = pluglet,
                Children       = new List <IDocumentFragment>(),
                SequenceNumber = segmentSeqNumber,
                StartOffset    = segmentStartIndex,
                EndOffset      = segmentEndIndex,
            };

            int plugletNum = 0;
            DocumentFragment dataFragment;
            IPluglet         plugletChild;
            bool             validateValue;

            long currentSegmentFieldStartIndex = segmentStartIndex;

            //SegmentNum start from 1 since SegmentDetails[0] is Segment name
            int segmentNum = 1;

            // if this is internal segment (composite segment - segment handing data and segment as children) then
            // start segmentDetails from 0
            if (internalSegment == true)
            {
                segmentNum = 0;
            }

            EdiErrorType errorType = pluglet.IsIgnore ? EdiErrorType.Warning : EdiErrorType.Error;

            for (; segmentNum < segmentDetails.Length; segmentNum++, plugletNum++)
            {
                if (segmentNum != 0)
                {
                    currentSegmentFieldStartIndex += segmentDetails[segmentNum - 1].Length + 1;
                }

                validateValue = false;
                plugletChild  = pluglet.Children[plugletNum];

                if (plugletChild.PlugletType == PlugletType.Data)
                {
                    dataFragment = new DocumentFragment()
                    {
                        Parent  = documentFragment,
                        Pluglet = plugletChild,
                        Value   = segmentDetails[segmentNum],

                        /*SequenceNumber = segmentSeqNumber,
                         * StartOffset = currentSegmentFieldStartIndex,
                         * EndOffset = currentSegmentFieldStartIndex + segmentDetails[segmentNum].Length,*/
                    };

                    validateValue = true;
                }
                else if (plugletChild.PlugletType == PlugletType.Segment)
                {
                    if (string.IsNullOrEmpty(segmentDetails[segmentNum]) == false)
                    {
                        string segmentError = string.Empty;

                        string [] internalSegmentDetails = segmentDetails[segmentNum].Split((char)ediDelimiters.ComponentSeperator);

                        dataFragment = plugletChild.ConstructDocumentFragment(internalSegmentDetails, true, ediDelimiters, segmentSeqNumber, currentSegmentFieldStartIndex,
                                                                              currentSegmentFieldStartIndex + segmentDetails[segmentNum].Length, ref ediErrors, out segmentError);

                        if (string.IsNullOrEmpty(segmentError) == false)
                        {
                            if (string.IsNullOrEmpty(error) == true)
                            {
                                error = string.Format("ConstructDocumentSegmentInstance: Pluglet {0}: ", pluglet.Name);
                            }

                            error = string.Format("{0} Error constructing {1}: {2}", error, plugletChild.Name, segmentError);
                        }
                    }
                    else
                    {
                        dataFragment = new DocumentFragment()
                        {
                            Parent  = documentFragment,
                            Pluglet = plugletChild,
                            Value   = segmentDetails[segmentNum],
                        };
                    }
                }
                else
                {
                    error = string.Format("ConstructDocumentSegmentInstance: Error constructing SegmentInstance. Pluglet {0} is of type {1}",
                                          pluglet.Name, plugletChild.Name);
                    return(null);
                }

                if (validateValue == true && plugletChild.DataType != null && string.IsNullOrEmpty(segmentDetails[segmentNum]) == false)
                {
                    FieldError fieldError = plugletChild.DataType.ValidateValue(new StringBuilder(segmentDetails[segmentNum]));
                    if (fieldError != null)
                    {
                        if (string.IsNullOrEmpty(error) == true)
                        {
                            error = string.Format("ConstructDocumentSegmentInstance: Pluglet {0}: ", pluglet.Name);
                        }

                        error = string.Format("{0} Error data validation failed {1}: {2} ({3})", error, plugletChild.Name, fieldError.Description, fieldError.DataValue);

                        ediErrors.AddFieldError(segmentDetails[0], plugletChild.Tag, fieldError.ErrorCode, fieldError.Description, segmentSeqNumber, segmentNum, segmentDetails[segmentNum], currentSegmentFieldStartIndex,
                                                currentSegmentFieldStartIndex + segmentDetails[segmentNum].Length - 1, errorType);
                    }
                }

                //TODO: Add validation (extension method)
                documentFragment.Children.Add(dataFragment);

                if (string.IsNullOrEmpty(segmentDetails[segmentNum]) && plugletChild.IsMandatory == true)
                {
                    if (string.IsNullOrEmpty(error) == true)
                    {
                        error = string.Format("ConstructDocumentSegmentInstance: Pluglet {0}: ", pluglet.Name);
                    }

                    error = string.Format("{0} Child {1} is mandatory but missing", error, plugletChild.Name);

                    ediErrors.AddFieldError(segmentDetails[0], plugletChild.Tag, X12ErrorCode.DeMandatoryDataElementMissingCode,
                                            X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeMandatoryDataElementMissingCode), segmentSeqNumber, segmentNum, string.Empty,
                                            currentSegmentFieldStartIndex, currentSegmentFieldStartIndex, errorType);
                }
            }

            for (; segmentNum <= pluglet.Children.Count; segmentNum++, plugletNum++)
            {
                plugletChild = pluglet.Children[plugletNum];
                if (plugletChild.IsMandatory == true)
                {
                    if (string.IsNullOrEmpty(error) == true)
                    {
                        error = string.Format("ConstructDocumentSegmentInstance: Pluglet {0}: ", pluglet.Name);
                    }

                    error = string.Format("{0} Child {1} is mandatory but missing", error, plugletChild.Name);

                    ediErrors.AddFieldError(segmentDetails[0], plugletChild.Tag, X12ErrorCode.DeMandatoryDataElementMissingCode,
                                            X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeMandatoryDataElementMissingCode), segmentSeqNumber, segmentNum, string.Empty,
                                            currentSegmentFieldStartIndex, currentSegmentFieldStartIndex, errorType);
                }
            }

            return(documentFragment);
        }
예제 #5
0
        /*
         * This method does validation of a numeric data type after precision point has been
         * removed during serialization and raw data during parsing. The following rules need
         * to be obeyed
         *
         * 1) 0th char is - or digit, + is not allowed
         * 2) All other chars are digit
         * 3) If length > minLength, then no leading zeroes are allowed
         * 4) Sign is not part of length calculation
         *
         * This method doesn't apply precision point nor does it check for it
         */
        public static bool ValidateRDataType(StringBuilder data, int minL, int maxL, int decimalSeparator, out FieldError error)
        {
            error = null;
            int dataLen = data.Length;

            if (dataLen == 0) //too short error
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeDataElementTooShortCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeDataElementTooShortCode), string.Empty);
                return(true);
            }

            char c         = data[0];
            bool isNegSign = false;
            bool isError   = false;

            if (c == '+')
            {
                isError = true;
            }

            else if (c == '-')
            {
                isNegSign = true;
                if (data.Length == 1)
                {
                    isError = true;                   //data is -, single negative sign which is invalid
                }
            }

            if (isError)
            {
                AppendInvalidCharCode(data);
                return(true);
            }

            //This loop would ensure all are digits or digits + one point
            //otherwise loop would abort and return true
            bool bPointFound  = false;
            int  decimalIndex = -1;

            for (int i = isNegSign ? 1 : 0; i < dataLen; i++)
            {
                if (DataTypeHelper.IsDigit(data[i])) //digit found, no-op
                {
                }

                else if (!bPointFound && data[i] == decimalSeparator)
                {
                    bPointFound  = true;
                    decimalIndex = i;
                    //Edifact data should have atleast one digit after decimal point
                    if (!(i + 1 < dataLen && DataTypeHelper.IsDigit(data[i + 1])))
                    {
                        error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidCharacterInDataElementCode,
                                                                  X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidCharacterInDataElementCode), data.ToString());
                    }
                }

                else //error state
                {
                    AppendInvalidCharCode(data);
                    return(true);
                }
            }


            if (isNegSign)
            {
                dataLen--; //count of digits
                if (dataLen > 0)
                {
                    c = data[1];
                }
                else
                {
                    AppendInvalidCharCode(data);
                    return(true);
                }
            }



            //check for leading zeroes
            //Leading zero is the 1st char being zero, otherwise not
            //leading zero is allowed only when data has minimum length
            //skip the check if minL is not specified, #3454
            if (c == '0' && minL > 0 && false)
            {
                if (decimalIndex < 0) //no decimal found
                {
                    if ((dataLen > minL && minL > 0) || minL < 0)
                    {
                        AppendInvalidCharCode(data);
                        return(true);
                    }
                }

                else //decimal was found, so 0.1, -0.1 are valid but 00.1 or -00.1 are invalid
                {
                    if ((dataLen > minL && minL > 0) || minL < 0)
                    {
                        int offset = isNegSign ? 2 : 1;
                        if (data.Length > offset && data[offset] == decimalSeparator)
                        {
                        }

                        else
                        {
                            AppendInvalidCharCode(data);
                            return(true);
                        }
                    }
                }
            }

            if (decimalIndex >= 0)
            {
                dataLen--;
                if (data[data.Length - 1] == '0' && false &&
                    ((dataLen > minL && minL > 0) || minL < 0))
                {
                    AppendInvalidCharCode(data);
                    return(true);
                }
            }

            if (minL > 0 && dataLen < minL)
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeDataElementTooShortCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeDataElementTooShortCode), data.ToString());

                return(true);
            }

            if (maxL > 0 && dataLen > maxL)
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeDataElementTooLongCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeDataElementTooLongCode), data.ToString());
                return(true);
            }

            return(false);
        }
예제 #6
0
 private static void AppendInvalidCharCode(StringBuilder data)
 {
     DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidCharacterInDataElementCode,
                                       X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidCharacterInDataElementCode), data.ToString());
 }
예제 #7
0
        public override FieldError ValidateValue(StringBuilder data)
        {
            FieldError error = null;
            string     str   = data.ToString();

            // First try the .NET function to check if date is valid
            // if not we will follow other way (check yyyymmdd)

            DateTime dt;

            if (DateTime.TryParse(str, out dt))
            {
                return(error);
            }

            if (data.Length < minLength || data.Length == 7)
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidDateCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidDateCode), str);
                return(error);
            }

            if (data.Length > maxLength)
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidDateCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidDateCode), str);
                return(error);
            }

            //run thru the data to ensure all are digits
            for (int i = 0; i < data.Length; i++)
            {
                if (!DataTypeHelper.IsDigit(data[i]))
                {
                    error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidDateCode,
                                                              X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidDateCode), data.ToString());
                    return(error);
                }
            }

            //data is between min and max length, thus it is atleast 6 chars
            //and all are digits
            int offset = data.Length == 6 ? 2 : 4;
            int year   = data.Length == 6 ? 10 * (data[0] - '0') + (data[1] - '0')
                    : 1000 * (data[0] - '0') + 100 * (data[1] - '0') + 10 * (data[2] - '0') + (data[3] - '0');

            int month = 10 * (data[offset] - '0') + (data[offset + 1] - '0');
            int day   = 10 * (data[offset + 2] - '0') + (data[offset + 3] - '0');

            if (year == 0)
            {
                year = 2000;
            }

            try
            {
                DateTime dateTime = new DateTime(year, month, day);
            }
            catch (ArgumentOutOfRangeException)
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidDateCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidDateCode), str);
            }

            return(error);
        }
예제 #8
0
        /*
         * This method does validation of a numeric data type after precision point has been
         * removed during serialization and raw data during parsing. The following rules need
         * to be obeyed
         *
         * 1) 0th char is - or digit, + is not allowed
         * 2) All other chars are digit
         * 3) If length > minLength, then no leading zeroes are allowed
         * 4) Sign is not part of length calculation
         *
         * This method doesn't apply precision point nor does it check for it
         */
        private bool ValidateNDataType(StringBuilder data, out FieldError error)
        {
            error = null;
            int dataLen = data.Length;

            if (dataLen == 0) //too short error
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeDataElementTooShortCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeDataElementTooShortCode), string.Empty);
                return(true);
            }

            char c         = data[0];
            bool isNegSign = false;

            if (c == '+')
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidCharacterInDataElementCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidCharacterInDataElementCode), data.ToString());
                return(true);
            }

            else if (c == '-')
            {
                isNegSign = true;
            }

            for (int i = isNegSign ? 1 : 0; i < dataLen; i++)
            {
                // "." is valid character
                if (!(DataTypeHelper.IsDigit(data[i]) || data[i] == '.'))
                {
                    error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeInvalidCharacterInDataElementCode,
                                                              X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeInvalidCharacterInDataElementCode), data.ToString());
                    return(true);
                }
            }


            if (isNegSign)
            {
                dataLen--; //count of digits
                if (dataLen > 0)
                {
                    c = data[1];
                }
                else
                {
                    error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeDataElementTooShortCode,
                                                              X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeDataElementTooShortCode), data.ToString());
                    return(true);
                }
            }



            if (/*(mPrecision > 0 && dataLen < mPrecision) || */ (minLength >= 0 && dataLen < minLength))
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeDataElementTooShortCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeDataElementTooShortCode), data.ToString());
                return(true);
            }

            if (maxLength >= 0 && dataLen > maxLength)
            {
                error = DataTypeHelper.GenerateFieldError(X12ErrorCode.DeDataElementTooLongCode,
                                                          X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeDataElementTooLongCode), data.ToString());
                return(true);
            }

            return(false);
        }