private Constants InitializeConstants() { return new Constants() { ActionTypes = new ObservableCollection<ActionType>(UserConstants.DefaultActionTypes()), Colors = new ObservableCollection<BuiltSteady.Zaplify.Devices.ClientEntities.Color>(UserConstants.DefaultColors()), Permissions = new ObservableCollection<Permission>(UserConstants.DefaultPermissions()), Priorities = new ObservableCollection<Priority>(UserConstants.DefaultPriorities()), ItemTypes = new ObservableCollection<ItemType>(UserConstants.DefaultItemTypes()) }; }
// update constants in User database to current version defined in EntityConstants public bool VersionConstants(string me) { try { bool updateDB = false; if (Versions.Any(v => v.VersionType == DatabaseVersion.Constants && v.VersionString == UserConstants.ConstantsVersion) == false) { // no database - create and lock the new version entry TraceLog.TraceInfo(String.Format("User database version {0} not found", UserConstants.ConstantsVersion)); // remove an existing database version (there should never be more than one) foreach (var existingVersion in Versions.Where(v => v.VersionType == DatabaseVersion.Constants).ToList()) { Versions.Remove(existingVersion); } SaveChanges(); // create the new version entry DatabaseVersion ver = new DatabaseVersion() { VersionType = DatabaseVersion.Constants, VersionString = UserConstants.ConstantsVersion, Status = me }; Versions.Add(ver); SaveChanges(); updateDB = true; } else { var dbVersion = Versions.Single(v => v.VersionType == DatabaseVersion.Constants && v.VersionString == UserConstants.ConstantsVersion); if (dbVersion.Status == DatabaseVersion.Corrupted) { // try to update the database again - take a lock TraceLog.TraceInfo("User database corrupted"); dbVersion.Status = me; SaveChanges(); updateDB = true; } } if (updateDB == false) { TraceLog.TraceInfo(String.Format("User database version {0} is up to date", UserConstants.ConstantsVersion)); return(true); } } catch (Exception ex) { TraceLog.TraceException("Could not find database version", ex); return(false); } // update the default database values DatabaseVersion version = null; UserStorageContext versionContext = Storage.NewUserContext; try { // verify that this unit of execution owns the update lock for the database version version = versionContext.Versions.Single(v => v.VersionType == DatabaseVersion.Constants && v.VersionString == UserConstants.ConstantsVersion); if (version.Status != me) { return(true); } TraceLog.TraceInfo(String.Format("{0} updating User datatbase to version {1}", me, UserConstants.ConstantsVersion)); // update existing action types, add new action types foreach (var entity in UserConstants.DefaultActionTypes()) { if (ActionTypes.Any(e => e.ActionTypeID == entity.ActionTypeID)) { ActionTypes.Single(e => e.ActionTypeID == entity.ActionTypeID).Copy(entity); } else { ActionTypes.Add(entity); } } SaveChanges(); TraceLog.TraceInfo("Replaced action types"); // update existing colors, add new colors foreach (var entity in UserConstants.DefaultColors()) { if (Colors.Any(e => e.ColorID == entity.ColorID)) { Colors.Single(e => e.ColorID == entity.ColorID).Copy(entity); } else { Colors.Add(entity); } } SaveChanges(); TraceLog.TraceInfo("Replaced colors"); // update existing permissions, add new permissions foreach (var entity in UserConstants.DefaultPermissions()) { if (Permissions.Any(e => e.PermissionID == entity.PermissionID)) { Permissions.Single(e => e.PermissionID == entity.PermissionID).Copy(entity); } else { Permissions.Add(entity); } } SaveChanges(); TraceLog.TraceInfo("Replaced permissions"); // update existing priorities, add new priorities foreach (var entity in UserConstants.DefaultPriorities()) { if (Priorities.Any(e => e.PriorityID == entity.PriorityID)) { Priorities.Single(e => e.PriorityID == entity.PriorityID).Copy(entity); } else { Priorities.Add(entity); } } SaveChanges(); TraceLog.TraceInfo("Replaced priorities"); // update existing or add new built-in users foreach (var user in UserConstants.DefaultUsers()) { if (Users.Any(u => u.ID == user.ID)) { var existing = Users.Single(u => u.ID == user.ID); existing.Name = user.Name; existing.Email = user.Email; existing.CreateDate = user.CreateDate; } else { Users.Add(user); } } SaveChanges(); TraceLog.TraceInfo("Replaced users"); // update existing or add new built-in itemtypes and fields foreach (var itemType in UserConstants.DefaultItemTypes()) { if (ItemTypes.Any(it => it.ID == itemType.ID)) { var existing = ItemTypes.Include("Fields").Single(it => it.ID == itemType.ID); existing.Copy(itemType); if (itemType.Fields == null) { continue; } foreach (var field in itemType.Fields) { if (existing.Fields.Any(f => f.ID == field.ID)) { var existingField = existing.Fields.Single(f => f.ID == field.ID); existingField.Copy(field); } else { existing.Fields.Add(field); } } } else { ItemTypes.Add(itemType); } } SaveChanges(); TraceLog.TraceInfo("Replaced item types and fields"); // save the new version number version.Status = DatabaseVersion.OK; versionContext.SaveChanges(); return(true); } catch (Exception ex) { TraceLog.TraceException("VersionConstants failed", ex); // mark the version as corrupted version.Status = DatabaseVersion.Corrupted; versionContext.SaveChanges(); return(false); } }