Exemplo n.º 1
0
 internal static void SetPropertyValue(BaseSegment segmentInstance, Property segmentProperty, string segmentPropValue)
 {
     if (segmentProperty.CanWrite)
     {
         PropertiesService.SetPropertyValue(segmentInstance, segmentProperty.Name, segmentPropValue);
     }
 }
Exemplo n.º 2
0
        private static BaseSegment GenerateGeneralSegment(RawSegment rawStringSegment)
        {
            BaseSegment segmentInstance       = null;
            Type        segmentTypeDefinition = null;
            var         rawSegment            = rawStringSegment.SegmentString;

            switch (rawStringSegment.SegmentType)
            {
            case SegmentType.Header: segmentInstance = new HeaderSegment();
                segmentTypeDefinition = typeof(HeaderFieldDefinition);
                break;

            case SegmentType.CustomerRemarks:
                segmentInstance       = new CustomerRemarkSegment();
                segmentTypeDefinition = typeof(CustomerRemarkFieldDefinition);
                break;

            case SegmentType.Passenger:
                segmentInstance       = new PassengerSegment();
                segmentTypeDefinition = typeof(PassengerFieldDefinition);
                break;

            case SegmentType.FareValue:
                segmentInstance       = new FareValueSegment();
                segmentTypeDefinition = typeof(FareValueFieldDefinition);
                break;

            case SegmentType.A16Hotel:
                segmentInstance       = new A16HotelSegment();
                segmentTypeDefinition = typeof(HotelFieldDefinition);
                break;

            case SegmentType.A16Car:
                segmentInstance       = new A16CarSegment();
                segmentTypeDefinition = typeof(CarFieldDefinition);
                break;

            default: return(null);
            }

            var previousSegmentPropIsNotPresent = false;
            var rawSegmentLastPosition          = 0;
            var segmentProperties        = PropertiesService.GetDynamicProperties(segmentInstance);
            var segmentDMLPropertyValues = PropertiesService.GetDynamicPropertyValues(segmentTypeDefinition);

            foreach (var segmentProperty in segmentProperties)
            {
                if (previousSegmentPropIsNotPresent)
                {
                    previousSegmentPropIsNotPresent = false;
                    continue;
                }

                var parentSegmentPropName = segmentProperty.Name.Contains("_") ? segmentProperty.Name.Split("_").First() : segmentProperty.Name;
                var childSegmentPropName  = segmentProperty.Name.Contains("_") ? segmentProperty.Name.Split("_").Last() : string.Empty;

                var dmlPropValue = segmentDMLPropertyValues.FirstOrDefault(pv => pv.Name.StartsWith(parentSegmentPropName));
                if (dmlPropValue != null)
                {
                    var segmentPropValue = "";
                    var fieldDefinition  = dmlPropValue.Value as FieldDefinition;

                    if (fieldDefinition.HasNestedFields && !string.IsNullOrEmpty(childSegmentPropName))
                    {
                        var childFieldDefinition = fieldDefinition.NestedFields.FirstOrDefault(_ => _.Name == childSegmentPropName);
                        if (childFieldDefinition != null)
                        {
                            fieldDefinition = childFieldDefinition;
                        }
                        else
                        {
                            continue;
                        }
                    }

                    if (fieldDefinition.IsComplexField && fieldDefinition.HasCodeId)
                    {
                        var segmentChunks = rawSegment.Split("|");
                        var complexField  = string.Empty;

                        if (fieldDefinition.IsOptionalContained)
                        {
                            complexField = segmentChunks
                                           .FirstOrDefault(_ => _
                                                           .StartsWith(fieldDefinition.CodeId)
                                                           );

                            if (!complexField.IsNullOrEmpty())
                            {
                                segmentPropValue = complexField
                                                   .Replace(fieldDefinition.CodeId, string.Empty)
                                                   .Substring(fieldDefinition.SegmentPosition);
                            }
                        }
                        else
                        {
                            foreach (var chunk in segmentChunks)
                            {
                                var optionalDelimitedChunks = chunk.Split(fieldDefinition.Delimitator);
                                complexField = optionalDelimitedChunks
                                               .FirstOrDefault(_ => _
                                                               .Contains(fieldDefinition.CodeId)
                                                               );

                                if (!complexField.IsNullOrEmpty())
                                {
                                    complexField = complexField
                                                   .Replace(fieldDefinition.CodeId, string.Empty);

                                    if (fieldDefinition.IsNestedDelimitatorDriven)
                                    {
                                        complexField = complexField.Split(fieldDefinition.NestedDelimitator).First();
                                        complexField = complexField.Substring(0, complexField.Length - fieldDefinition.CropIndex);
                                    }

                                    segmentPropValue = complexField;
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        var rawSegmentStartPosition = GetRawSegmentStartPosition(fieldDefinition, rawSegmentLastPosition);
                        rawSegmentLastPosition = rawSegmentStartPosition + fieldDefinition.Length;

                        if (rawSegment.Length >= rawSegmentLastPosition)
                        {
                            var propValue = rawSegment.Substring(rawSegmentStartPosition, fieldDefinition.Length);
                            if (fieldDefinition.IsOptionalField)
                            {
                                if (fieldDefinition.HasCodeId)
                                {
                                    if (string.IsNullOrEmpty(fieldDefinition.CodeId) || propValue != fieldDefinition.CodeId)
                                    {
                                        rawSegmentLastPosition          = rawSegmentStartPosition;
                                        previousSegmentPropIsNotPresent = true;
                                        continue;
                                    }
                                }
                            }

                            segmentPropValue = propValue;
                        }
                    }

                    SetPropertyValue(segmentInstance, segmentProperty, segmentPropValue);
                }
            }
            return(segmentInstance);
        }