Exemple #1
0
        internal override void GetData(IntegrationContext context)
        {
            string sourcePath = context.State[ContextState.SourcePath] as string;

            if (string.IsNullOrEmpty(sourcePath))
            {
                throw new ArgumentNullException(string.Format("argument [{0}] missed!", ContextState.SourcePath));
            }
            if (!File.Exists(sourcePath))
            {
                throw new FileNotFoundException(string.Format("File [{0}] not found!", sourcePath));
            }

            bool skipfirstline = (bool)context.State[ContextState.FlatFileSkipFirstLine];
            bool skiplastline  = (bool)context.State[ContextState.FlatFileSkipLastLine];

            encoding = StringUtil.GetFileEncoding(sourcePath);
            using (
                StreamReader reader =
                    new StreamReader(new FileStream(sourcePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), encoding))
            {
                if (skipfirstline)
                {
                    GetLine(reader);
                }
                string row = null;
                while (!reader.EndOfStream)
                {
                    if (skiplastline)
                    {
                        if (row == null)
                        {
                            row = GetLine(reader);
                            continue;
                        }
                    }
                    else
                    {
                        row = GetLine(reader);
                    }
                    if (row.Trim().Length == 0)
                    {
                        continue;
                    }
                    DataItem item = GetDataItem(row, context.Schema, true);
                    if (item != null)
                    {
                        if (item.IsValid)
                        {
                            context.AddData(item);
                        }
                        else
                        {
                            context.AddSkippedData(item);
                        }
                    }
                    if (skiplastline)
                    {
                        row = GetLine(reader);
                    }
                }
            }
        }
Exemple #2
0
        public virtual DataItem GetDataItem(string row, DataSchema dataSchema, bool isSource)
        {
            string[] items = GetLineColumns(row, dataSchema.GetAnyAttributeValue("delimiter")[0]);

            DataItem dataItem = new DataItem(dataSchema);

            for (int i = 0; i < dataSchema.Fields.Length; i++)
            {
                DataSchemaField schemaField = dataSchema.Fields[i];
                DataItemField   itemfield;
                if (isSource)
                {
                    itemfield = dataItem.GetSourceField(schemaField.Source);
                }
                else
                {
                    itemfield = dataItem.GetDestinationField(schemaField.Destination);
                }

                string replaced     = schemaField.GetAnyAttributeValue("replaced");
                string defaultvalue = schemaField.GetAnyAttributeValue("default");
                if (schemaField.Index >= items.Length)
                {
                    // bad row
                    if (defaultvalue == null)
                    {
                        switch (dataSchema.TransferMode)
                        {
                        case DataFixMode.Skip:
                            StringBuilder sb = new StringBuilder();
                            sb.AppendLine("skipped transfering data row:");
                            Array.ForEach(items, delegate(string item) { sb.AppendFormat("{0}\t", item); });
                            // TODO:
                            //LogEntry log = new LogEntry();
                            //log.Message = sb.ToString();
                            //log.Categories.Add(LogCategory.Trace);
                            //log.Priority = LogPriority.Normal;

                            Logger.Instance.Info(this, sb.ToString());
                            return(null);

                        case DataFixMode.Fix:
                            itemfield.Value = string.Empty;
                            break;

                        case DataFixMode.RaiseError:
                            throw new ArgumentException(
                                      string.Format("row has [{0}] columns while schema require [{1}] fields.{2}row:[{3}]", items.Length,
                                                    dataSchema.Fields.Length, Environment.NewLine, row));

                        default:
                            throw new ArgumentOutOfRangeException(
                                      string.Format("Invalid transfer mode [{0}] in schema, requires one of 'skip,fix,raiseerror'",
                                                    dataSchema.TransferMode));
                        }
                    }
                    else
                    {
                        itemfield.Value = defaultvalue;
                    }
                }
                else
                {
                    itemfield.Value = items[schemaField.Index].Trim();
                }
                // for the value start with 0, in excel which will be escaped with ' starting.
                if (itemfield.Value != null && itemfield.Value.ToString().StartsWith("'0"))
                {
                    itemfield.Value = itemfield.Value.ToString().TrimStart('\'');
                }
                // 如果存在被替换值和默认值,则将被替换值替换为默认值
                if (!string.IsNullOrEmpty(itemfield.Value as string) && !string.IsNullOrEmpty(replaced) && replaced.IndexOf(itemfield.Value.ToString()) != -1 &&
                    defaultvalue != null)
                {
                    itemfield.Value = defaultvalue;
                }
                // 如果该项验证失败, 且为DataFixMode.Fix, 且有默认值, 则以默认值代替
                if (!string.IsNullOrEmpty(dataSchema.TransferMode) && dataSchema.TransferMode.Equals("fix", StringComparison.InvariantCultureIgnoreCase) && defaultvalue != null)
                {
                    ValidationResults vr = new ValidationResults();
                    itemfield.DoValidate(vr);
                    if (!vr.IsValid && itemfield.Value as string != defaultvalue)
                    {
                        itemfield.Value = defaultvalue;
                    }
                }

                try
                {
                    Utilities.AdjustDataItemFieldValue(itemfield);
                }
                catch (System.Exception ex)
                {
                    Logger.Instance.Error(this, ex);
                    return(null);
                }
            }

            return(dataItem);
        }