Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public ActionResult VariableStructure(VariableSelectionViewModel VariableVM)
- {
- HttpPostedFileBase upload = Request.Files["structure_file"];
- List<string> extensions = new List<string>() { ".txt", ".csv" };
- string extension = Path.GetExtension(upload.FileName);
- if (!extensions.Contains(extension))
- {
- VariableVM.AllVariables = _db.Variables.ToList();
- ViewBag.ErrorMessage = "Improper file type, should be .csv or .txt";
- return View(VariableVM);
- }
- if ((upload.ContentLength <= 0) || (VariableVM.SelectedVariableID == null))
- {
- VariableVM.AllVariables = _db.Variables.ToList();
- return View(VariableVM);
- }
- using (var trans = _db.Database.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted))
- {
- try
- {
- ParseVariableStructure(VariableVM.SelectedVariableID.Value, upload);
- trans.Commit();
- }
- catch (Exception Exc)
- {
- trans.Rollback();
- ViewBag.ErrorMessage = Exc.Message;
- }
- }
- return RedirectToAction("Index");
- }
- /// <summary>
- /// Go through the uploaded Variable structure file and create the
- /// categories where they do not exist
- /// </summary>
- /// <param name="a_Variable">The Variable to create the structure for</param>
- /// <param name="a_Upload">The uplaoded file with the structure information</param>
- /// σταμάτησα εδώ
- private void ParseVariableStructure(int a_VariableID, HttpPostedFileBase a_Upload)
- {
- string user = User.Identity.Name;
- Variable Variable = _db.Variables.Where(c => c.Id == a_VariableID).FirstOrDefault();
- // Loop through the file entires
- StreamReader sr = new StreamReader(a_Upload.InputStream);
- while (!sr.EndOfStream)
- {
- string line = sr.ReadLine();
- Category parent = null;
- string[] items = line.Split('\t');
- // Level 0 is pre-defined for Family Declaration in order to associate the category
- for (int level = 1; level < items.Length; level++)
- {
- string curName = items[level];
- if (string.IsNullOrEmpty(curName))
- break;
- // Check if the category of our variable already exists
- VariableClass curVar = Variable.VariableClass.Where(c => c.description == curName).FirstOrDefault();
- if (curVar != null)
- {
- parent = curVar;
- continue;
- }
- else
- {
- curVar = new VariableClass();
- curVar.description = curName;
- curVar.dateAdded = DateTime.Now;
- curVar.addedBy = user;
- curVar.updateBy = user;
- curVar.dateLastUpdate = DateTime.Now;
- if (parent != null)
- curVar.parentId = parent.Id;
- // Check if a related family is specified
- if (!string.IsNullOrEmpty(items[0]))
- {
- string familyName = items[0].Trim();
- var family = Variable.VariableFamily.Where(f => f.description == familyName).FirstOrDefault();
- if (family != null)
- {
- curVar.Families = new List<Family>();
- curVar.Families.Add(family);
- }
- }
- Variable.VariableClass.Add(curVar);
- parent = curVar;
- }
- _db.SaveChanges();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement