Ejemplo n.º 1
0
        public ActionResult UploadFile(IEnumerable <HttpPostedFileBase> files)
        {
            StringBuilder status = new StringBuilder();

            if (files != null && files.Count() > 0)
            {
                foreach (HttpPostedFileBase file in files)
                {
                    if (file == null)
                    {
                        continue;
                    }

                    try
                    {
                        CsvFile csvFile = CsvFileDal.AddCsvFile(file, db);

                        if (ModelState.IsValid)
                        {
                            db.SaveChanges();
                        }

                        status.AppendFormat("File [{0}] is uploaded successfully! ", file.FileName);

                        IEnumerable <string> result = DirectedGraphUtil.ShortestPath(csvFile.CsvRecords.ToList());
                        if (result != null)
                        {
                            status.AppendFormat(" The shortest path in the grapth is {0}", string.Join("->", result));
                        }
                    }
                    catch (Exception e)
                    {
                        status.AppendFormat("File [{0}] failed to upload due to {1} ", file.FileName, e.Message);
                    }
                }
            }
            else
            {
                status.Append("File upload failed: Error: no file is chosen to upload.");
            }

            ViewBag.Message = status.ToString();
            return(View(db.CsvFiles));
        }
Ejemplo n.º 2
0
        static string ValidateCsvRecords(IList <CsvRecord> records)
        {
            string result = "";

            bool negativeQuantity = records.Any(r => r.Quantity < 0);

            if (negativeQuantity)
            {
                result = "Error: some record has negative quantity column. ";
            }

            IEnumerable <string> cycle = DirectedGraphUtil.DirectedGraphHasCycle(records);

            if (cycle != null)
            {
                result = result + "Error: the records contain cycle: " + string.Join("->", cycle);
            }

            return(result);
        }
Ejemplo n.º 3
0
        public ActionResult UploadFile(HttpPostedFileBase file)
        {
            try
            {
                IList <string> lines   = new List <string>();
                string         content = "";
                string         result  = "And it doesn't contain cycle. ";

                if (file.ContentLength > 0)
                {
                    string fileName = Path.GetFileName(file.FileName);

                    byte[] bytes = new byte[file.ContentLength];
                    file.InputStream.Read(bytes, 0, file.ContentLength);

                    content = Encoding.UTF8.GetString(bytes);

                    CsvFile    csvFile = new CsvFile(file.FileName, bytes);
                    CsvFileDal dal     = new CsvFileDal();
                    dal.AddCsvFile(csvFile);

                    bool b = DirectedGraphUtil.DirectedGraphHasCycle(content);
                    if (b)
                    {
                        result = "But it contains cycle. ";
                    }
                }

                ViewBag.Message = "File Uploaded Successfully!! " + result + content;
                return(View());
            }
            catch (Exception e)
            {
                ViewBag.Message = "File upload failed: " + e.ToString();
                return(View());
            }
        }