/// <summary> /// Lock specified entity identified by Id /// </summary> /// <param name="id">Id of entity</param> /// <param name="tableName">Table name of entity</param> /// <returns></returns> private EntityLockResult LockEntity(Guid id, string tableName) { var userName = userIdentification.UserName; if (string.IsNullOrEmpty(userName)) { return(new EntityLockResult(EntityLockEnum.LockFailNoUser)); } return(contextManager.ExecuteWriter(unitOfWork => { var lockingRep = unitOfWork.CreateRepository <ILockingRepository>(); var entityLock = lockingRep.All().FirstOrDefault(i => i.LockedEntityId == id); if (entityLock == null) { lockingRep.Add(new Model.Models.Locking() { Id = Guid.NewGuid(), LockedEntityId = id, LockedBy = userName, LockedAt = DateTime.UtcNow, TableName = tableName }); unitOfWork.Save(); return new EntityLockResult(EntityLockEnum.LockedForCurrent, userName); } if (entityLock.LockedAt.AddMinutes(entityLockTimeMinutes) < DateTime.UtcNow || entityLock.LockedBy == userName) { entityLock.LockedAt = DateTime.UtcNow; entityLock.LockedBy = userName; unitOfWork.Save(); return new EntityLockResult(EntityLockEnum.LockedForCurrent, userName); } if (entityLock.LockedBy != userName) { return new EntityLockResult(EntityLockEnum.LockedForAnother, entityLock.LockedBy); } return new EntityLockResult(EntityLockEnum.LockedForCurrent, userName); })); }
public void ReAssignMissingRoles(Guid userId, Guid preferedRole) { contextManager.ExecuteWriter(unitOfWork => { var connectionRep = unitOfWork.CreateRepository <IUserOrganizationRepository>(); var wrongConnections = connectionRep.All().Where(u => u.UserId == userId && u.RoleId == Guid.Empty).ToList(); wrongConnections.ForEach(c => c.RoleId = preferedRole); unitOfWork.Save(); }); }
public VmOrganizationHeader DeleteOrganization(Guid?organizationId) { VmOrganizationHeader result = null; contextManager.ExecuteWriter(unitOfWork => { var entity = CascadeDeleteOrganization(unitOfWork, organizationId); unitOfWork.Save(); result = GetOrganizationHeader(organizationId, unitOfWork); }); UnLockOrganization(result.Id.Value); return(result); }
public IVmEntityBase DeleteServiceCollection(Guid serviceCollectionId) { ServiceCollectionVersioned result = null; contextManager.ExecuteWriter(unitOfWork => { result = DeleteServiceCollection(unitOfWork, serviceCollectionId); unitOfWork.Save(); }); return(new VmEntityStatusBase { Id = result.Id, PublishingStatusId = result.PublishingStatusId }); }
public void Delete(string id) { contextManager.ExecuteWriter(unitOfWork => { var formStateRepository = unitOfWork.CreateRepository <IFormStateRepository>(); var formStateToDelete = formStateRepository .All() .Where(formState => formState.Id == new Guid(id)) .FirstOrDefault(); formStateRepository.Remove(formStateToDelete); unitOfWork.Save(); }); }
public void CreateOrUpdateGeneralDescriptions(List<ImportStatutoryServiceGeneralDescription> importedGeneralDescriptions) { contextManager.ExecuteWriter(unitOfWork => { // Dictionary<string, ImportLaw> laws = new Dictionary<string, ImportLaw>(); // foreach (var generalDescription in importedGeneralDescriptions) // { // if (generalDescription.Laws == null) // { // continue; // } // generalDescription.UniqueLaws = generalDescription.Laws.Select(x => // { // ImportLaw law; // if (laws.TryGetValue(x.LawReference, out law)) // { // return law; // } // laws.Add(x.LawReference, x); // return x; // }).ToList(); // } var lawEntities = translationManagerToEntity.TranslateAll<ImportLaw, Law>(importedGeneralDescriptions.SelectMany(i => i.Laws), unitOfWork);//.ToDictionary(x => x.Id); unitOfWork.Save(SaveMode.AllowAnonymous); var generalDescriptions = translationManagerToEntity.TranslateAll<ImportStatutoryServiceGeneralDescription, StatutoryServiceGeneralDescriptionVersioned>( importedGeneralDescriptions, unitOfWork); // foreach (var gd in generalDescriptions) // { // // gd.StatutoryServiceLaws = dataUtils.UpdateCollectionForReferenceTable(unitOfWork, gd.StatutoryServiceLaws, q => q.StatutoryServiceGeneralDescriptionVersionedId == gd.Id, sc => sc.LawId); // gd.ServiceClasses = dataUtils.UpdateCollectionForReferenceTable(unitOfWork, gd.ServiceClasses.Where(x => x != null).ToList(), q => q.StatutoryServiceGeneralDescriptionVersionedId == gd.Id, sc => sc.ServiceClass?.Id ?? sc.ServiceClassId); // gd.IndustrialClasses = dataUtils.UpdateCollectionForReferenceTable(unitOfWork, gd.IndustrialClasses, q => q.StatutoryServiceGeneralDescriptionVersionedId == gd.Id, sc => sc.IndustrialClass?.Id ?? sc.IndustrialClassId); // gd.OntologyTerms = dataUtils.UpdateCollectionForReferenceTable(unitOfWork, gd.OntologyTerms.Where(x => x != null).ToList(), q => q.StatutoryServiceGeneralDescriptionVersionedId == gd.Id, sc => sc.OntologyTerm?.Id ?? sc.OntologyTermId); // gd.LifeEvents = dataUtils.UpdateCollectionForReferenceTable(unitOfWork, gd.LifeEvents.Where(x => x != null).ToList(), q => q.StatutoryServiceGeneralDescriptionVersionedId == gd.Id, sc => sc.LifeEvent?.Id ?? sc.LifeEventId); // gd.TargetGroups = dataUtils.UpdateCollectionForReferenceTable(unitOfWork, gd.TargetGroups.Where(x => x != null).ToList(), q => q.StatutoryServiceGeneralDescriptionVersionedId == gd.Id, sc => sc.TargetGroup?.Id ?? sc.TargetGroupId); // } unitOfWork.Save(SaveMode.AllowAnonymous); }); }
public List <VmBugReport> GetAllBugReports() { var result = new List <VmBugReport>(); contextManager.ExecuteWriter(unitOfWork => { var bugReportRepository = unitOfWork.CreateRepository <IBugReportRepository>(); var resultTemp = bugReportRepository.All() .Select(x => new BugReport() { Id = x.Id, Created = x.Created, Description = x.Description, CreatedBy = x.CreatedBy, Modified = x.Modified, ModifiedBy = x.ModifiedBy, Name = x.Name, Version = x.Version }); result = translationManager.TranslateAll <BugReport, VmBugReport>(resultTemp).ToList(); }); return(result); }
public void UpdateAddress(IEnumerable <Guid> addressIds) { contextManager.ExecuteWriter(unitOfWork => { var coordinateRep = unitOfWork.CreateRepository <ICoordinateRepository>(); coordinateRep.All().Where(x => x.TypeId == typesCache.Get <CoordinateType>(CoordinateTypeEnum.Main.ToString()) && addressIds.Contains(x.AddressId)).ForEach(x => x.CoordinateState = CoordinateStates.Loading.ToString() + Guid.NewGuid().ToString()); unitOfWork.Save(); }); resolveManager.RunInThread(rm => { IReadOnlyList <AddressInfo> result = null; var contextManager = rm.Resolve <IContextManager>(); var tmToVm = rm.Resolve <ITranslationEntity>(); var tmToEntity = rm.Resolve <ITranslationViewModel>(); contextManager.ExecuteReader(unitOfWork => { var addressRepositiory = unitOfWork.CreateRepository <IAddressRepository>(); var addresses = addressRepositiory.All().Where(x => addressIds.Contains(x.Id)); addresses = unitOfWork.ApplyIncludes(addresses, q => q .Include(i => i.AddressStreets).ThenInclude(i => i.StreetNames).ThenInclude(i => i.Localization) .Include(i => i.AddressStreets).ThenInclude(i => i.PostalCode).ThenInclude(i => i.Municipality) .Include(i => i.AddressPostOfficeBoxes).ThenInclude(i => i.PostOfficeBoxNames).ThenInclude(i => i.Localization) .Include(i => i.AddressPostOfficeBoxes).ThenInclude(i => i.PostalCode).ThenInclude(i => i.Municipality) .Include(i => i.AddressForeigns).ThenInclude(i => i.ForeignTextNames).ThenInclude(i => i.Localization) ); result = tmToVm.TranslateAll <Address, AddressInfo>(addresses); }); if (!result.IsNullOrEmpty()) { var coordinates = mapServiceProvider.GetCoordinates(result).Result; if (!coordinates.IsNullOrEmpty()) { contextManager.ExecuteWriter(unitOfWork => { tmToEntity.TranslateAll <AddressInfo, Address>(coordinates, unitOfWork); unitOfWork.Save(); }); } } }); }