public ActionResult CreateSicknessReasons(string reasons) { using (var MasterSession = MasterStore.OpenSession()) { var current = MasterSession.Query<SicknessClassification>().FirstOrDefault(); if (current == null) { current = new SicknessClassification(); current.SicknessReasons = new List<SicknessReason>(); MasterSession.Store(current); MasterSession.SaveChanges(); } var allreasons = current.Flatten(); var re = new Regex(@"^(?<code>[^\t]+)\t(?<parent>[^\t]*)\t(?<name>[^\t]+)$"); foreach (var line in reasons.Split('\n')) { var matches = re.Match(line); if (matches.Success) { var code = matches.Groups["code"].Captures[0].Value.Trim(); var name = matches.Groups["name"].Captures[0].Value.Trim(); var parent = matches.Groups["parent"].Captures[0].Value.Trim(); //check for existing already. var check = allreasons.Where(r => r.Code == code).Count(); if (check == 0) { var newtype = new SicknessReason { Id = IlluminateDatabase.GenerateId<SicknessReason>(), Code = code, Name = name, Description = name, FullName = name, Children = new List<SicknessReason>() }; //get parent. var parenttype = allreasons.Where(r => r.Code == parent).FirstOrDefault(); if (parenttype == null) { //top level. if (current.SicknessReasons == null) current.SicknessReasons = new List<SicknessReason>(); current.SicknessReasons.Add(newtype); } else { if (parenttype.Children == null) parenttype.Children = new List<SicknessReason>(); parenttype.Children.Add(newtype); } allreasons.Add(newtype); } } } MasterSession.SaveChanges(); } return CreateSicknessReasons(); }
public static SicknessReasonSimple Create(SicknessReason reason) { return new SicknessReasonSimple {Id=reason.Id,FullName = reason.FullName,Name = reason.Name}; }
public override void Import(Stream stream) { ErrorLines.Clear(); var reader = new StreamReader(stream); string line; //ignore first line(s). for (var i = 0; i < IgnoreLines; i++) { reader.ReadLine(); } //parse each line var re = new Regex( @"^(?<personnelnumber>[^\t]+)\t(?<startdate>[^\t]+)\t(?<enddate>[^\t]+)\t(?<code>[^\t]+)\t(?<description>[^\t]+)$"); //TODO: more sophisticated regex for dates etc. while ((line = reader.ReadLine()) != null) { var match = re.Match(line); if (match.Success) { using (var Session = Store.OpenSession()) { var personnelnumber = match.Groups["personnelnumber"].Captures[0].Value; var user = Session.Query<User>().FirstOrDefault(u => u.PersonnelNumber == personnelnumber); if (user == null) { ErrorLines.Add(line); continue; } var reasoncode = match.Groups["code"].Captures[0].Value; var reason = Session.Query<SicknessReason>().FirstOrDefault(r => r.Code == reasoncode); if (reason == null) { //get default reason reason = Session.Query<SicknessReason>().FirstOrDefault(r => r.Code == "DEFAULT"); if (reason == null) { //!! reason = new SicknessReason { Code = "DEFAULT", Description = "Default Sickness", FullName = "Default Sickness", Name = "Default Sickness" }; Session.Store(reason); } } //save sickness var s = new Sickness { Id = IlluminateDatabase.GenerateId<Sickness>(), Subject = user.ToSimpleUser(), StartOfProcess = DateTime.Parse(match.Groups["startdate"].Captures[0].Value), EndOfProcess = DateTime.Parse(match.Groups["enddate"].Captures[0].Value), ConsolidatedCompletionDate = DateTime.Parse(match.Groups["enddate"].Captures[0].Value), Title = "Sickness", Summary = match.Groups["description"].Captures[0].Value, SicknessReason = SicknessReasonSimple.Create(reason), }; Session.Store(s); //should we save if there were errors? Session.SaveChanges(); } } else { ErrorLines.Add(line); } } }