//        protected string directory = StateManager.Settings["XmlMessageDefinition.Directory"].ToString();
        public MessageDefinition Read(string messageName, string section = null)
        {
            var filename = messageName;

            if (!File.Exists(filename)) throw new FileNotFoundException("Unable to locate message definition file {0}", filename);

            var definition = XDocument.Load(filename);
            var message = definition.Element("message");

            if (message == null) return null;

            var messageDefinition = new MessageDefinition
                                        {
                                            Name = message.GetAttributeOrDefault("name"),
                                            FileName = filename,
                                            RecordSeparator = message.GetAttributeOrDefault("recordSeparator", ","),
                                            HasHeader = message.GetAttributeOrDefault<bool>("hasHeader"),
                                        };

            foreach (XElement record in message.Elements("record"))
            {
                var newRecord = new RecordDefinition
                {
                    Name = record.GetAttributeOrDefault("name"),
                    DomainObjectName = record.GetAttributeOrDefault("domainobjectname"),
                    FieldSeparator = record.GetAttributeOrDefault("fieldSeparator", ","),
                };

                int defaultStartPos = 0;
                foreach (XElement field in record.Elements("field"))
                {
                    var newField = new FieldDefinition
                    {
                        Format = field.GetAttributeOrDefault("format"),
                        IsOptional = field.GetAttributeOrDefault<bool>("optional"),
                        StartPosition = field.GetAttributeOrDefault<int>("position", defaultStartPos),
                        Name = field.GetAttributeOrDefault("name"),
                        Type = field.GetAttributeFromDescriptorOrDefault<FieldDefinitionType>("type"),
                        Label = field.GetAttributeOrDefault("label", field.GetAttributeOrDefault("name")),
                        Table = field.GetAttributeOrDefault("table", newRecord.DomainObjectName),
                        Default = field.GetAttributeOrDefault("default")
                    };
                    defaultStartPos = newField.StartPosition + 1;

                    if (newField.Name.IsNullOrEmpty() || newField.Type.IsNullOrEmpty()) throw new MessagingException("Error reading message definition " + messageDefinition.Name);

                    newRecord.Fields.Add(newField);
                }
                messageDefinition.Records.Add(newRecord);
            }

            return messageDefinition;
        }
Ejemplo n.º 2
0
        protected virtual void ReadRecordField(DictionaryState state, TableDescriber table, FieldDefinition fieldDefinition, string[] fields)
        {
            if (fieldDefinition.StartPosition >= fields.Length)
            {
                if (fieldDefinition.IsOptional) return;
                throw new MessagingException("Message does not match definition. Too few fields in current record.");
            }

            if (fields[fieldDefinition.StartPosition].StartsWith("\"") && fields[fieldDefinition.StartPosition].EndsWith("\"")) // Remove quotes
            {
                fields[fieldDefinition.StartPosition] = fields[fieldDefinition.StartPosition].Substring(1, fields[fieldDefinition.StartPosition].Length - 2);
            }

            var describer = new ColumnDescriber(fieldDefinition.Name, table);

            if (fieldDefinition.Type.IsIn(FieldDefinitionType.DateTime))
            {
                state[describer] = DateTime.ParseExact(fields[fieldDefinition.StartPosition].Trim('"'), fieldDefinition.Format, CultureInfo.InvariantCulture);
            }
            else if (fieldDefinition.Type.IsIn(FieldDefinitionType.Amount, FieldDefinitionType.InvertedAmount, FieldDefinitionType.Decimal))
            {
                object oldvalue;

                decimal oldAmount = state.TryGetValue(describer, out oldvalue) && oldvalue is decimal
                                        ? (decimal) oldvalue
                                        : 0;

                decimal amount = !string.IsNullOrWhiteSpace(fields[fieldDefinition.StartPosition].Trim('"'))
                            ? Decimal.Parse(fields[fieldDefinition.StartPosition].Trim('"'),
                                fieldDefinition.Format.IsNullOrEmpty() ? CultureInfo.InvariantCulture : new CultureInfo(fieldDefinition.Format))
                            : 0;

                if (fieldDefinition.Type == FieldDefinitionType.InvertedAmount) amount *= -1;

                state[describer] = oldAmount + amount;
            }
            else if (fieldDefinition.Type == FieldDefinitionType.AmountSign)
            {
                if (IsNegativeAmountSign(fields[fieldDefinition.StartPosition], fieldDefinition))
                {
                    state[describer] = Decimal.Negate(((decimal)state[describer]));
                }
            }
            else
            {
                object oldvalue;
                state[describer] = state.TryGetValue(describer, out oldvalue)
                                       ? oldvalue + " " + fields[fieldDefinition.StartPosition]
                                       : fields[fieldDefinition.StartPosition];
            }
        }
Ejemplo n.º 3
0
 protected virtual bool IsNegativeAmountSign(string value, FieldDefinition fieldDefinition)
 {
     return fieldDefinition.Format.Equals(value, StringComparison.OrdinalIgnoreCase);
 }
Ejemplo n.º 4
0
        private static string GetValueForField(FieldDefinition fieldDefinition, IInternalState state)
        {
            string format = fieldDefinition.Format;
            var columnDescriber = new ColumnDescriber(fieldDefinition.Name, fieldDefinition.Table);

            if(fieldDefinition.Type.IsIn(FieldDefinitionType.DateTime))
            {
                var dateTime = state.Get<DateTime?>(columnDescriber);

                if (!dateTime.HasValue)
                    return fieldDefinition.Default;

                // DateTime needs a format specifier
                return !format.IsNullOrEmpty() ? dateTime.Value.ToString(format, CultureInfo.InvariantCulture) : dateTime.Value.ToShortDateString();
            }

            if(fieldDefinition.Type.IsIn(FieldDefinitionType.Amount, FieldDefinitionType.InvertedAmount, FieldDefinitionType.Decimal))
            {
                var money = state.Get<Money>(columnDescriber);

                if (money.IsEmpty) return fieldDefinition.Default;
                if (fieldDefinition.Type == FieldDefinitionType.InvertedAmount) money *= -1;

                return ToString(money, format);
            }

            if(fieldDefinition.Type.IsIn(FieldDefinitionType.Int16, FieldDefinitionType.Int32, FieldDefinitionType.Int64))
            {
                var value = state.Get<long?>(columnDescriber);

                return !value.HasValue ? fieldDefinition.Default : ToString(value, format);
            }

            return !state.Get<string>(columnDescriber).IsNullOrEmpty() ? state.Get<string>(columnDescriber) : fieldDefinition.Default;
        }