Ejemplo n.º 1
0
        private ResponseModel StepTwoValidation(ResponseModel rm, BatchHead form)
        {
            List <ValidateDataModel> messages = new List <ValidateDataModel>();
            bool log = true;

            foreach (Models.Files f in _files)
            {
                //Raw text data in lists of srings
                List <string> data       = CSVReader.ReadFormFile(f.File);
                List <string> colheaders = CSVReader.ParseLine(data[0]);


                //remove the col headers from the data;
                data.Remove(data[0]);

                //check the number of cols are a match with what comes back from the DB file properties
                if (!(colheaders.Count() == f.FileProperties.Count()) && f.FileProperties.Count() != 0)
                {
                    MessageValidationManager.Check(ref messages, $"<span class='file-col'>{f.LatestFileName}</span> contains {CSVReader.ParseLine(data[0]).Count()} columns, but {f.FileProperties.Count()} were expected.");
                    log = false;
                }

                //check the col names match with what comes back from the DB file properties
                if (log)
                {
                    int cnt = 1;
                    foreach (string col in colheaders)
                    {
                        if (log)
                        {
                            if (f.FileProperties[cnt - 1].ColumnName.ToLower() != col.ToLower())
                            {
                                MessageValidationManager.Check(ref messages, $"<span class='file-col'>{f.LatestFileName}</span> contains incorrect column headers. They must be {GetColumnList(f.FileProperties)}, and <u>in that order</u>.");
                                log = false;
                            }
                        }
                        if (log)
                        {
                            if (f.FileProperties[cnt - 1].SortOrder != cnt.ToString())
                            {
                                MessageValidationManager.Check(ref messages, $"<span class='file-col'>{f.LatestFileName}</span> contains incorrect order of columns. They must be {GetColumnList(f.FileProperties)}, and <u>in that order</u>.");
                                log = false;
                            }
                        }
                        ++cnt;
                    }
                    log = true;
                }

                log = true;
            }
            rm.messages = messages;
            return(rm);
        }
Ejemplo n.º 2
0
        private ResponseModel StepOneValidation(ResponseModel rm, IFormFileCollection files, BatchHead form)
        {
            List <ValidateDataModel> messages = new List <ValidateDataModel>();
            List <ProjectFiles>      pfs      = GetProjectFiles(form.ProjectID);

            try
            {
                foreach (ProjectFiles pf in pfs)
                {
                    foreach (IFormFile f in files)
                    {
                        if (FileNameManager.FileName(f.FileName).Length >= pf.FileID.Length)  //cant slip in a file name with 3 char and substring 10 char from it.
                        {
                            if (FileNameManager.FileName(f.FileName).Substring(0, pf.FileID.Length).ToLower() == pf.FileID.ToLower())
                            {//If the file matches to its FileID then its added to _files for upload
                                _files.Add(new Files {
                                    FileID = pf.FileID, LatestFileName = FileNameManager.FileName(f.FileName), File = f, FileProperties = GetFileProperties(form.ProjectID, pf.FileID)
                                });
                                //if the same file has been added twice then return the error message
                                if (_files.FindAll(x => x.LatestFileName.ToLower().Contains(pf.FileID.ToLower())).Count > 1)
                                {
                                    MessageValidationManager.Check(ref messages, $"Upload cannot contain duplicate {pf.FileID} files.");
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }

            //This checks if any files exist in the uploaded batch but not in _files collection that is valid based on the database FileID values
            foreach (IFormFile f in files)
            {
                if (!FileNameManager.FileName(f.FileName).ToLower().Contains(".csv"))
                {
                    MessageValidationManager.Check(ref messages, $"<span class='file-col'>{FileNameManager.FileName(f.FileName)}</span> is not a valid file format. Must be .csv.");
                }
                else if (!_files.Exists(s => s.LatestFileName == FileNameManager.FileName(f.FileName)))
                {
                    MessageValidationManager.Check(ref messages, $"<span class='file-col'>{FileNameManager.FileName(f.FileName)}</span> has an incorrect file name. It must begin one of the following words: {ConvertToFileListString(pfs)}.");
                }
            }

            rm.messages = messages;

            return(rm);
        }
Ejemplo n.º 3
0
        private ResponseModel StepThreeValidation(ResponseModel rm, BatchHead form, ProjectModel pm)
        {
            List <ValidateDataModel> messages = new List <ValidateDataModel>();

            foreach (Models.Files f in _files)
            {
                FileProperties fcp;

                List <string> data       = CSVReader.ReadFormFile(f.File);
                List <string> colheaders = CSVReader.ParseLine(data[0]);
                int           colcnt     = 0;
                data.Remove(data[0]);
                try
                {
                    foreach (var s in data)
                    {
                        colcnt = 0;
                        var row = CSVReader.ParseLine(s);

                        foreach (string c in row)
                        {
                            //every property of a column from the database
                            fcp = f.FileProperties.Find(x => x.ColumnName.ToLower() == colheaders[colcnt].ToLower());

                            if (fcp != null)
                            {
                                if (fcp.SortOrder != (colcnt + 1).ToString())
                                {
                                    MessageValidationManager.Check(ref messages, $"<span class='file-col'>{f.LatestFileName}</span> contains incorrect column header order.They must be {GetColumnList(f.FileProperties)}.");
                                }

                                if (fcp.ColumnName.ToLower() == "siteid")
                                {
                                    if (c.Contains("_") ? (c.Substring(0, form.SiteID.Length).ToLower() == form.SiteID.ToLower() ? false : true) : (c.ToLower() == form.SiteID.ToLower() ? false : true))
                                    {
                                        MessageValidationManager.Check(ref messages, $"The siteid values in <span class='file-col'>{f.LatestFileName}</span> do not match the Siteid in the form.");
                                    }
                                }

                                //validate no nulls
                                if (c.Trim() == "" || c.Trim().ToLower() == "(null)" || c.Trim().ToLower() == "null" || c.Trim().ToLower() == "na" || c.Trim().ToLower() == "n/a" || c.Trim().ToLower() == "n.a.")
                                {
                                    MessageValidationManager.Check(ref messages, $"<span class='file-col'>{f.LatestFileName}</span> contains missing or null values in column {colheaders[colcnt]}. Use {pm.NullCode} to indicate missing data.");
                                }
                                else
                                {
                                    //validate datatypes are ok
                                    //validate ranges in fields in each datatype test as well.  Max and Min can be
                                    //date, int, etc..
                                    switch (fcp.DataType.ToLower())
                                    {
                                    case "string":
                                        if (fcp.ValueList != null)
                                        {
                                            if (!fcp.ValueList.Split("|").ToList().Exists(x => x == c))
                                            {
                                                MessageValidationManager.Check(ref messages, $"There are invalid values in column {fcp.ColumnName} in <span class='file-col'>{f.LatestFileName}</span>.");
                                            }
                                        }
                                        break;

                                    case "date":
                                        if (!Helpers.DateValidation.IsValidDate(c) == true)
                                        {
                                            MessageValidationManager.Check(ref messages, $"The dates in column {fcp.ColumnName} in <span class='file-col'>{f.LatestFileName }</span> must be in the format YYYY-MM-DD.");
                                        }
                                        else if (!Helpers.DateValidation.DateRange(c, fcp.MaxValue, fcp.MinValue))
                                        {
                                            MessageValidationManager.Check(ref messages, $"There are values in column {fcp.ColumnName} in <span class='file-col'>{f.LatestFileName}</span> that are outside the allowed range.");
                                        }
                                        break;

                                    case "int":
                                        int parsedResult;
                                        if (!int.TryParse(c, out parsedResult))
                                        {
                                            MessageValidationManager.Check(ref messages, $"<span class='file-col'>{f.LatestFileName}</span> contains invalid data in column {fcp.ColumnName}, which should only contain values of type {fcp.DataType}.");
                                        }
                                        else if (!Helpers.RangeValidation.IntRanges(parsedResult, fcp.MaxValue, fcp.MinValue))
                                        {
                                            MessageValidationManager.Check(ref messages, $"There are values in column {fcp.ColumnName} in <span class='file-col'>{f.LatestFileName}</span> that are outside the allowed range.");
                                        }
                                        break;

                                    case "real":
                                        float val;
                                        if (!float.TryParse(c, out val))
                                        {
                                            MessageValidationManager.Check(ref messages, $"<span class='file-col'>{f.LatestFileName}</span> contains invalid data in column {fcp.ColumnName}, which should only contain values of type {fcp.DataType}.");
                                        }
                                        else if (!Helpers.RangeValidation.FloatRanges(val, fcp.MaxValue, fcp.MinValue))
                                        {
                                            MessageValidationManager.Check(ref messages, $"There are values in column {fcp.ColumnName} in <span class='file-col'>{f.LatestFileName}</span> that are outside the allowed range.");
                                        }
                                        break;
                                    }
                                }
                            }
                            colcnt++;
                        }
                    }
                }
                catch (Exception e)
                {
                    MessageValidationManager.Check(ref messages, $"An unexpected error occured in <span class='file-col'>{f.LatestFileName}</span>.");
                    Console.WriteLine(e.Message);
                }
            }

            rm.messages = messages;

            return(rm);
        }