Advertisement
Guest User

Untitled

a guest
Feb 25th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. public ActionResult VariableStructure(VariableSelectionViewModel VariableVM)
  2. {
  3. HttpPostedFileBase upload = Request.Files["structure_file"];
  4.  
  5. List<string> extensions = new List<string>() { ".txt", ".csv" };
  6. string extension = Path.GetExtension(upload.FileName);
  7. if (!extensions.Contains(extension))
  8. {
  9. VariableVM.AllVariables = _db.Variables.ToList();
  10. ViewBag.ErrorMessage = "Improper file type, should be .csv or .txt";
  11. return View(VariableVM);
  12. }
  13.  
  14. if ((upload.ContentLength <= 0) || (VariableVM.SelectedVariableID == null))
  15. {
  16. VariableVM.AllVariables = _db.Variables.ToList();
  17. return View(VariableVM);
  18. }
  19.  
  20. using (var trans = _db.Database.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted))
  21. {
  22. try
  23. {
  24. ParseVariableStructure(VariableVM.SelectedVariableID.Value, upload);
  25. trans.Commit();
  26. }
  27. catch (Exception Exc)
  28. {
  29. trans.Rollback();
  30. ViewBag.ErrorMessage = Exc.Message;
  31. }
  32. }
  33.  
  34. return RedirectToAction("Index");
  35. }
  36.  
  37. /// <summary>
  38. /// Go through the uploaded Variable structure file and create the
  39. /// categories where they do not exist
  40. /// </summary>
  41. /// <param name="a_Variable">The Variable to create the structure for</param>
  42. /// <param name="a_Upload">The uplaoded file with the structure information</param>
  43. /// σταμάτησα εδώ
  44. private void ParseVariableStructure(int a_VariableID, HttpPostedFileBase a_Upload)
  45. {
  46. string user = User.Identity.Name;
  47. Variable Variable = _db.Variables.Where(c => c.Id == a_VariableID).FirstOrDefault();
  48.  
  49. // Loop through the file entires
  50. StreamReader sr = new StreamReader(a_Upload.InputStream);
  51. while (!sr.EndOfStream)
  52. {
  53. string line = sr.ReadLine();
  54. Category parent = null;
  55.  
  56. string[] items = line.Split('\t');
  57. // Level 0 is pre-defined for Family Declaration in order to associate the category
  58. for (int level = 1; level < items.Length; level++)
  59. {
  60. string curName = items[level];
  61. if (string.IsNullOrEmpty(curName))
  62. break;
  63.  
  64. // Check if the category of our variable already exists
  65. VariableClass curVar = Variable.VariableClass.Where(c => c.description == curName).FirstOrDefault();
  66. if (curVar != null)
  67. {
  68. parent = curVar;
  69. continue;
  70. }
  71. else
  72. {
  73. curVar = new VariableClass();
  74. curVar.description = curName;
  75. curVar.dateAdded = DateTime.Now;
  76. curVar.addedBy = user;
  77. curVar.updateBy = user;
  78. curVar.dateLastUpdate = DateTime.Now;
  79.  
  80. if (parent != null)
  81. curVar.parentId = parent.Id;
  82.  
  83. // Check if a related family is specified
  84. if (!string.IsNullOrEmpty(items[0]))
  85. {
  86. string familyName = items[0].Trim();
  87. var family = Variable.VariableFamily.Where(f => f.description == familyName).FirstOrDefault();
  88. if (family != null)
  89. {
  90. curVar.Families = new List<Family>();
  91. curVar.Families.Add(family);
  92. }
  93. }
  94.  
  95. Variable.VariableClass.Add(curVar);
  96. parent = curVar;
  97.  
  98. }
  99. _db.SaveChanges();
  100. }
  101. }
  102.  
  103. }
  104.  
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement