// returns asset with AssetId == 0 if no matching asset is found private static Asset FindAsset(string str, AssetManagerContext db) { Asset asset = new Asset(); var usePeriods = db.UsePeriods .Include(u => u.Asset) .Include(u => u.Status) .Include(u => u.UserAccount) ; string[] substrings = str.Split(' '); if (substrings.Length > 1) { string assetType = substrings[0]; string accountFirstName = substrings[1]; usePeriods = usePeriods.Where(u => u.UserAccount.Name.Contains(accountFirstName)) .Where(u => u.EndDate == null || u.EndDate >= DateTime.Now) // current .Where(u => u.Status.UsePeriodStatusId != 4) // not "uit gebruik" ; switch (substrings[0]) { case "D": usePeriods = usePeriods.Where(u => u.Asset is Computer); break; case "T": usePeriods = usePeriods.Where(u => u.Asset is Telephone); break; case "V": usePeriods = usePeriods.Where(u => u.Asset is Telephone); break; case "P": usePeriods = usePeriods.Where(u => u.Asset is Printer); break; default: break; } // var upList = usePeriods.ToList(); if (usePeriods.Count() == 1) { asset = usePeriods.First().Asset; } } return asset; }
public static SelectList GenericSelectList(AssetManagerContext db, Type entityType , string property, object selectedvalue) { var set = db.Set(entityType); var query = set.OrderBy(property) .Select("new(" + property + ")") .Distinct() ; SelectList selectlist = new SelectList(query, property, property, selectedvalue); string selected = selectedvalue.ToStringOrEmpty(); if (!string.IsNullOrWhiteSpace(selected) && !selectlist.Contains(selectedvalue)) { List<SelectListItem> list = selectlist.ToList(); list.Add(new SelectListItem { Text = selected, Value = selected }); list.Sort((x, y) => x.Text.CompareTo(y.Text)); selectlist = new SelectList(list, "Value", "Text", selectedvalue); } return selectlist; }
public static List<PatchPoint> GetPatchPoints(AssetManagerContext db) { List<PatchPoint> patchPoints = new List<PatchPoint>(); string excelFileName = "\\\\vmfile\\users\\sven\\My Documents\\patchpuntjes.xlsx"; Excel.Application xlApp = new Excel.Application(); object misValue = System.Reflection.Missing.Value; Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(excelFileName); Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); int startRow = 2; if (xlWorkSheet != null) { Excel.Range usedRange = xlWorkSheet.UsedRange; PatchPoint pp; for (int rowCount = startRow; rowCount <= usedRange.Rows.Count; rowCount++) { pp = GetPatchPointFromRange(usedRange, rowCount); if (pp.Number > 0) { patchPoints.Add(pp); db.PatchPoints.Add(pp); } } db.SaveChanges(); } xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); return patchPoints; }
public static void InsertCompoundIdsInDb(AssetManagerContext db) { foreach (var c in db.Computers) { c.CompoundId = "C" + c.AssetId; } foreach (var p in db.Printers) { p.CompoundId = "P" + p.AssetId; } foreach (var b in db.Beamers) { b.CompoundId = "B" + b.AssetId; } foreach (var m in db.Monitors) { m.CompoundId = "M" + m.AssetId; } foreach (var t in db.Telephones) { t.CompoundId = "T" + t.AssetId; } foreach (var n in db.Networks) { n.CompoundId = "N" + n.AssetId; } foreach (var a in db.Miscellaneous) { a.CompoundId = "A" + a.AssetId; } db.SaveChanges(); }
public static void RemoveSynonyms(AssetManagerContext db) { try { foreach (Asset a in db.Assets) { CheckRequiredProps(a); } var assetsWithCvbaOwner = db.Assets .Where(a => a.Owner == "cvba") ; foreach (Asset a in assetsWithCvbaOwner) { CheckRequiredProps(a); a.Owner = "cv"; } var assetsWithAltanHP = db.Assets .Where(a => a.Supplier == "Altan/HP") ; foreach (Asset a in assetsWithAltanHP) { a.Supplier = "Altan"; } var computersWin7 = db.Computers .Where(c => c.OperatingSystem == "win 7 pro" || c.OperatingSystem == "windows 7"); foreach (Computer c in computersWin7) { c.OperatingSystem = "Win 7"; } var computersMSE = db.Computers .Where(c => c.AntiVirus == "MS sec" || c.AntiVirus == "mse"); foreach (Computer c in computersMSE) { c.AntiVirus = "MSE"; } db.SaveChanges(); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } }
private static void UpdateUserAccounts(DirectoryEntry entry, out int added, out int updated) { added = 0; updated = 0; using (AssetManagerContext _context = new AssetManagerContext()) { List<Department> Departments = _context.Departments.ToList(); DirectorySearcher mySearcher = new DirectorySearcher(entry); SearchResultCollection src = mySearcher.FindAll(); UserAccount ua; for (int i = 0; i < src.Count; i++) { if (src[i].Properties.Contains("mail")) { string mail = src[i].Properties["mail"][0].ToString(); ua = new UserAccount(); ua.GivenName = PropertyValueIfExists(src[i], "givenname"); ua.Sn = PropertyValueIfExists(src[i], "sn"); ua.Name = PropertyValueIfExists(src[i], "name"); ua.UserPrincipalName = PropertyValueIfExists(src[i], "userprincipalname"); ua.Company = PropertyValueIfExists(src[i], "company"); ua.DepartmentString = PropertyValueIfExists(src[i], "department"); ua.Department = Departments.Find(d => d.LdapName == ua.DepartmentString); ua.Mail = PropertyValueIfExists(src[i], "mail"); if (_context.UserAccounts.Any(o => o.Mail == mail)) { // todo update UserAccount existingAccount = _context.UserAccounts.First(o => o.Mail == mail); existingAccount.GivenName = ua.GivenName; existingAccount.Sn = ua.Sn; existingAccount.Name = ua.Name; existingAccount.UserPrincipalName = ua.UserPrincipalName; existingAccount.Company = ua.Company; existingAccount.DepartmentString = ua.DepartmentString; existingAccount.Department = ua.Department; updated++; } else { _context.UserAccounts.Add(ua); added++; } } } _context.SaveChanges(); } }
public static void AddDepartments(AssetManagerContext db) { List<Department> Departments = db.Departments.ToList(); foreach (UserAccount useraccount in db.UserAccounts) { useraccount.Department = Departments.Find(d => d.LdapName == useraccount.DepartmentString); } db.SaveChanges(); }