Esempio n. 1
0
        private void ExtractHeaderFooter()
        {
            IdpeKey headerAttribute = DataSource.Key(IdpeKeyTypes.HeaderLine1Attribute);
            IdpeKey footerAttribute = DataSource.Key(IdpeKeyTypes.FooterLine1Attribute);

            if ((headerAttribute == null) &&
                (footerAttribute == null))
            {
                return;
            }

            headerAttribute = null;
            footerAttribute = null;

            try
            {
                string[] allLines = FileContent.ToString().Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                string   line     = string.Empty;

                int howManyFound = 0;
                for (int i = 1; i <= 6; i++)
                {
                    IdpeKeyTypes keyType = (IdpeKeyTypes)Enum.Parse(typeof(IdpeKeyTypes), "HeaderLine" + i + "Attribute");
                    headerAttribute = DataSource.Key(keyType);
                    if (headerAttribute != null)
                    {
                        line = allLines[i - 1];
                        ProcessVariables.AddOrUpdate(headerAttribute.Value, line, (key, oldValue) => line);
                    }
                    else
                    {
                        break;
                    }

                    howManyFound++;
                }

                allLines = allLines.SubArray(howManyFound, allLines.Length - howManyFound);

                howManyFound = 0;
                for (int i = 6; i >= 1; i--)
                {
                    IdpeKeyTypes keyType = (IdpeKeyTypes)Enum.Parse(typeof(IdpeKeyTypes), "FooterLine" + i + "Attribute");
                    footerAttribute = DataSource.Key(keyType);
                    if (footerAttribute == null)
                    {
                        continue;
                    }
                    else
                    {
                        line = allLines[allLines.Length - (howManyFound + 1)];
                        ProcessVariables.AddOrUpdate(footerAttribute.Value, line, (key, oldValue) => line);
                    }

                    howManyFound++;
                }

                allLines = allLines.SubArray(0, allLines.Length - howManyFound);

                FileNameWithoutHeaderAndOrFooter = FileName + ShortGuid.NewGuid().ToString();
                using (StreamWriter sw = new StreamWriter(FileNameWithoutHeaderAndOrFooter))
                {
                    for (int i = 0; i < allLines.Length; i++)
                    {
                        sw.WriteLine(allLines[i]);
                    }
                }

                return;
            }
            catch (Exception ex)
            {
                ExtensionMethods.TraceInformation(ex.ToString());
                this.Errors.Add("File content is invalid! Please check that file has required header(s) and footer(s) and at least 1 valid record!");
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Instantiate new job with a data source(id OR name to be passed)
        /// </summary>
        /// <param name="dataSourceId">The data source id (id OR name is required)</param>
        /// <param name="dataSourceName">The data source name (id OR name is required)</param>
        /// <param name="processingBy">Processing by user name</param>
        /// <param name="fileName">The file name</param>
        public Job(int dataSourceId, string dataSourceName, string processingBy, string fileName = null)
        {
            this.ProcessingBy       = processingBy;
            this.FileName           = fileName;
            this.Rows               = new List <Row>(); //these list are in this case thread OK, because we add from individual splitted job return items
            this.Errors             = new List <string>();
            this.Warnings           = new List <string>();
            this.BadDataInCsvFormat = new List <string>();
            this.StartedAt          = DateTime.Now;
            this.JobIdentifier      = ShortGuid.NewGuid();
            this.ProcessVariables   = new ConcurrentDictionary <string, object>();
            this.CsvRows            = new List <string>();
            this.PerformanceCounter = new PerformanceCounter();
            PerformanceCounter.StartNew(JobIdentifier);
            if ((dataSourceId == 0) && (string.IsNullOrEmpty(dataSourceName)))
            {
                return;//Invalid dummy job;
            }


            this.DataSource = new DataSource(dataSourceId, dataSourceName);
            this.DataSource.ClearAdditionalAttachments();

            if (!this.DataSource.IsValid)
            {
                string errorMessage = string.Format("Could not create job as data source was not valid. Data source id was {0} and name was {1}"
                                                    , dataSourceId, string.IsNullOrEmpty(dataSourceName) ? "<Unknown>" : dataSourceName);
                this.DataSource.TraceError(errorMessage);
                return; //we need not to do anything here as job is automatically invalid
            }

            #region OutputWriter
            object objOutputWriter = null;
            if (DataSource.OutputType == OutputTypes.Xml)
            {
                //set default
                objOutputWriter = new OutputWriterGeneric(this);
            }
            else if (DataSource.OutputType == OutputTypes.Delimited)
            {
                objOutputWriter = new OutputWriterDelimited(this);
            }
            else if (DataSource.OutputType == OutputTypes.FixedLength)
            {
                objOutputWriter = new OutputWriterFixedLength(this);
            }
            else if (DataSource.OutputType == OutputTypes.CSharpCode)
            {
                objOutputWriter = new OutputWriterCSharpCode(this);
            }
            else if (DataSource.OutputType == OutputTypes.Database)
            {
                objOutputWriter = new OutputWriterDatabase(this);
            }
            else if (DataSource.OutputType == OutputTypes.Custom)
            {
                if ((!string.IsNullOrEmpty(this.DataSource.OutputWriterTypeFullName)) &&
                    (Type.GetType(this.DataSource.OutputWriterTypeFullName) != null))
                {
                    objOutputWriter = Activator.CreateInstance(Type.GetType(this.DataSource.OutputWriterTypeFullName), this);
                }
                else
                {
                    objOutputWriter = new OutputWriterGeneric(this);
                }
            }
            else
            {
                objOutputWriter = new OutputWriterGeneric(this);
            }

            this.DataSource.OutputWriter = (OutputWriter)objOutputWriter;
            #endregion OutputWriter

            #region PlugIns

            if ((!string.IsNullOrEmpty(this.DataSource.PlugInsType)) &&
                (Type.GetType(this.DataSource.PlugInsType) != null))
            {
                object objPlugIns = Activator.CreateInstance(Type.GetType(this.DataSource.PlugInsType), this);
                this.DataSource.PlugIns = (PlugIns)objPlugIns;
            }
            else
            {
                //set default
                object objPlugIns = Activator.CreateInstance(Type.GetType("Eyedia.IDPE.Services.PlugInsGeneric"), this);
                this.DataSource.PlugIns = (PlugIns)objPlugIns;
            }

            #endregion PlugIns


            IdpeKey key = DataSource.Key(IdpeKeyTypes.IsFirstRowHeader);
            if (key != null)
            {
                DataSource.IsFirstRowHeader = key.Value.ParseBool();
            }

            if ((DataSource.DataFormatType == DataFormatTypes.Delimited) ||
                (DataSource.DataFormatType == DataFormatTypes.FixedLength))
            {
                ExtractHeaderFooter();
            }


            this.SqlClientManager = new SqlClientManager(this.DefaultConnectionString, this.DefaultConnectionType);
            this.Parameters       = new Parameters(this.DataSource.Id, this.DataSource.Name, this.DataSource.Key(IdpeKeyTypes.GenerateParametersFromDatabase), this.SqlClientManager);

            ExtensionMethods.TraceInformation(Environment.NewLine);
            this.TraceInformation("New job '{0}' created.", this.JobIdentifier);
        }