public DataItemField(DataSchemaField field)
 {
     Name        = field.Name;
     Source      = field.Source;
     Destination = field.Destination;
     Index       = field.Index;
     Type        = field.Type;
     Skip        = field.Skip;
     AnyAttr     = field.AnyAttr;
     _value      = null;
 }
Esempio n. 2
0
        internal override void CreateDataSchema(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));
            }

            string delimiterString = context.State[ContextState.FlatFileDelimiter] as string;

            if (string.IsNullOrEmpty(delimiterString))
            {
                throw new ArgumentNullException(string.Format("argument [{0}] missed!", ContextState.FlatFileDelimiter));
            }
            char delimiter = delimiterString[0];

            string transfermode = context.State[ContextState.TransferMode] as string;

            if (string.IsNullOrEmpty(transfermode))
            {
                transfermode = DataFixMode.Fix;
            }

            DataSchema dataSchema = new DataSchema();

            dataSchema.SetAnyAttributeValue("delimiter", delimiter.ToString());
            dataSchema.TransferMode = transfermode;

            encoding = StringUtil.GetFileEncoding(sourcePath);
            using (StreamReader reader = new StreamReader(sourcePath, encoding))
            {
                // maybe this line is included by quote, thus need seek the closed quote
                string line = GetLine(reader);

                string[] columns = GetLineColumns(line, delimiter);

                List <DataSchemaField> fields = new List <DataSchemaField>();
                for (int i = 0; i < columns.Length; i++)
                {
                    // skip the empty column
                    if (columns[i].Trim().Length == 0)
                    {
                        continue;
                    }
                    DataSchemaField field = new DataSchemaField();
                    field.Name  = field.Source = field.Destination = columns[i];
                    field.Type  = typeof(string).ToString();
                    field.Index = i;

                    fields.Add(field);
                }

                dataSchema.Fields = fields.ToArray();

                reader.Close();
            }
            context.Schema = dataSchema;
        }
Esempio n. 3
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);
        }