public static Hotel MapFromSegment(A16HotelSegment segment) { var amount = segment.A16OD_A16RG.Substring(3); var currency = segment.A16OD_A16RG.Substring(0, 3); return(new Hotel { Currency = currency.Trim(), Name = segment.A16NME.Trim(), Amount = Convert.ToDouble(amount.Trim()), ConfirmationNumber = segment.A16CF.Trim() }); }
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); }