private static bool ValidateLongTime(EdiSimpleDataElement el, MapSimpleDataElement definition)
        {
            if (string.IsNullOrEmpty(el.Val))
            {
                return(IsOptional(el.Val, definition));
            }

            switch (el.Val.Length)
            {
            case 4:
                return(ValidateShortTime(el, definition));

            case 5:
                return(el.Val.IsValidDateTimeString("HHmms"));

            case 6:
                return(el.Val.IsValidDateTimeString("HHmmss"));

            case 7:
                return(el.Val.IsValidDateTimeString("HHmmssf"));

            case 8:
                return(el.Val.IsValidDateTimeString("HHmmssff"));

            default:
                throw new NotSupportedException($"Time validation with length {el.Val.Length} is not implemented. {el.Type}");
            }
        }
 public EdiSimpleDataElement(MapSimpleDataElement definition, string val) : base(definition)
 {
     if (definition != null)
     {
         Type = definition.GetType().Name;
     }
     Val = val;
 }
        private static bool ValidateShortDate(EdiSimpleDataElement el, MapSimpleDataElement definition)
        {
            if (string.IsNullOrEmpty(el.Val))
            {
                return(IsOptional(el.Val, definition));
            }

            return(el.Val.IsValidDateTimeString("yyMMdd"));
        }
        private static void ProcessComposite(EdiCompositeDataElement composite, string [] content)
        {
            int i = 0;

            foreach (string val in content)
            {
                MapSimpleDataElement elDef = null;
                if (i < composite.Definition.Content.Count)
                {
                    elDef = composite.Definition.Content[i];
                }

                EdiSimpleDataElement el = new EdiSimpleDataElement(elDef, val);
                composite.Content.Add(el);

                i++;
            }
        }
        public static EdiSegment ProcessSegment(MapBaseEntity definition, string[] content, int rowPos, string compositeSeparator, IValidatedEntity validationScope)
        {
            MapSegment segDef = (MapSegment)definition;
            EdiSegment seg    = new EdiSegment(segDef);

            int i = 0;

            foreach (string val in content.Skip(1))
            {
                MapSimpleDataElement    elDef = null;
                MapCompositeDataElement cDef  = null;
                if (i < segDef.Content.Count)
                {
                    if (segDef.Content[i] is MapSimpleDataElement)
                    {
                        elDef = (MapSimpleDataElement)segDef.Content[i];
                    }
                    else if (segDef.Content[i] is MapCompositeDataElement)
                    {
                        cDef = (MapCompositeDataElement)segDef.Content[i];
                    }
                }

                //if cDef is null - create simple element. Even if elDef is null
                // validation will add error of unknown element later on
                if (cDef == null)
                {
                    EdiSimpleDataElement el = new EdiSimpleDataElement(elDef, val);
                    seg.Content.Add(el);
                }
                else
                {
                    EdiCompositeDataElement composite = new EdiCompositeDataElement(cDef);
                    string[] compositeContent         = val.Split(new[] { compositeSeparator }, StringSplitOptions.None);
                    ProcessComposite(composite, compositeContent);
                    seg.Content.Add(composite);
                }

                i++;
            }

            SegmentValidator.ValidateSegment(seg, rowPos, validationScope);
            return(seg);
        }
        public static bool IsValid(this EdiSimpleDataElement el, MapSimpleDataElement definition)
        {
            //required
            if (definition.ReqDes == RequirementDesignator.Mandatory && string.IsNullOrEmpty(el.Val)) //whitespaces allowed
            {
                return(false);
            }

            //Min max len
            if (!string.IsNullOrEmpty(el.Val) && (el.Val.Length < definition.MinLength || el.Val.Length > definition.MaxLength))
            {
                return(false);
            }

            DateTime dummyDt;

            switch (definition.DataType)
            {
            case DataType.B:
            case DataType.AN:
                return(true);

            case DataType.ID:
                return(definition.AllowedValues.IndexOf(el.Val) >= 0 ||
                       (definition.ReqDes == RequirementDesignator.Optional && string.IsNullOrEmpty(el.Val)));

            case DataType.N0:
            case DataType.N1:
            case DataType.N2:
            case DataType.N4:
            case DataType.N6:
                int dummyInt;
                return(int.TryParse(el.Val, out dummyInt) ||
                       (definition.ReqDes == RequirementDesignator.Optional && string.IsNullOrEmpty(el.Val)));

            case DataType.R:
            case DataType.R2:
            case DataType.R4:
            case DataType.R5:
            case DataType.R6:
            case DataType.R7:
            case DataType.R8:
            case DataType.R9:
                decimal dummyDec;
                return(decimal.TryParse(el.Val, out dummyDec) ||
                       (definition.ReqDes == RequirementDesignator.Optional && string.IsNullOrEmpty(el.Val)));

            case DataType.DT:
                switch (definition.MaxLength)
                {
                case 6:
                    return(DateTime.TryParseExact(el.Val, "yyMMdd", CultureInfo.InvariantCulture,
                                                  DateTimeStyles.None, out dummyDt) ||
                           (definition.ReqDes == RequirementDesignator.Optional && string.IsNullOrEmpty(el.Val)));

                case 8:
                    return(DateTime.TryParseExact(el.Val, "yyyyMMdd", CultureInfo.InvariantCulture,
                                                  DateTimeStyles.None, out dummyDt) ||
                           (definition.ReqDes == RequirementDesignator.Optional && string.IsNullOrEmpty(el.Val)));

                default:
                    throw new NotSupportedException($"Date validation with length {definition.MaxLength} is not implemented. {el.Type}");
                }

            case DataType.TM:
                return(DateTime.TryParseExact(el.Val, "HHmm", CultureInfo.InvariantCulture, DateTimeStyles.None, out dummyDt) ||
                       (definition.ReqDes == RequirementDesignator.Optional && string.IsNullOrEmpty(el.Val)));

            default:
                throw new NotSupportedException($"{definition.DataType} validation is not implemented. {el.Type}");
            }
        }
 private static bool IsOptional(string value, MapSimpleDataElement definition)
 {
     return(definition.ReqDes == RequirementDesignator.Optional && string.IsNullOrEmpty(value));
 }
        public static bool IsValid(this EdiSimpleDataElement el, MapSimpleDataElement definition)
        {
            //required
            if (definition.ReqDes == RequirementDesignator.Mandatory && string.IsNullOrEmpty(el.Val)) //whitespaces allowed
            {
                return(false);
            }

            //Min max len
            if (!string.IsNullOrEmpty(el.Val) && (el.Val.Length < definition.MinLength || el.Val.Length > definition.MaxLength))
            {
                return(false);
            }

            switch (definition.DataType)
            {
            case DataType.B:
            case DataType.AN:
                return(true);

            case DataType.ID:
                return(definition.AllowedValues.IndexOf(el.Val) >= 0 || IsOptional(el.Val, definition));

            case DataType.N0:
            case DataType.N1:
            case DataType.N2:
            case DataType.N4:
            case DataType.N6:
                int dummyInt;
                return(int.TryParse(el.Val, out dummyInt) || IsOptional(el.Val, definition));

            case DataType.R:
            case DataType.R2:
            case DataType.R4:
            case DataType.R5:
            case DataType.R6:
            case DataType.R7:
            case DataType.R8:
            case DataType.R9:
                decimal dummyDec;
                return(decimal.TryParse(el.Val, out dummyDec) || IsOptional(el.Val, definition));

            case DataType.DT:
                switch (definition.MaxLength)
                {
                case 6:
                    return(ValidateShortDate(el, definition));

                case 8:
                    return(ValidateLongDate(el, definition));

                default:
                    throw new NotSupportedException($"Date validation with length {definition.MaxLength} is not implemented. {el.Type}");
                }

            case DataType.TM:
                switch (definition.MaxLength)
                {
                case 4:
                    return(ValidateShortTime(el, definition));

                case 8:
                    return(ValidateLongTime(el, definition));

                default:
                    throw new NotSupportedException($"Time validation with length {definition.MaxLength} is not implemented. {el.Type}");
                }

            default:
                throw new NotSupportedException($"{definition.DataType} validation is not implemented. {el.Type}");
            }
        }