public static RequestResult AddPerson(List <PersonModel> people, List <string> selectedList) { RequestResult requestResult = new RequestResult(); try { string[] seperator = new string[] { Define.Seperator }; using (CFContext context = new CFContext()) { foreach (string selected in selectedList) { string[] temp = selected.Split(seperator, StringSplitOptions.None); var organizationId = temp[0]; var pId = temp[1]; if (!string.IsNullOrEmpty(pId)) { if (!people.Any(p => p.PId == pId)) { //var person = context.People.First(p => p.LoginId == pId); people.Add(context.People.Where(p => p.LoginId == pId).Select(p => new PersonModel { PId = pId, PersonName = p.Name, OrganizationName = p.Organization.Name }).First()); } } else { var organizations = OrganizationDataAccessor.GetDownStreamOrganizations(new Guid(organizationId), true); var newPeople = context.People.Where(p => organizations.Contains(p.OrganizationId)).ToList(); foreach (var person in newPeople) { if (!people.Any(p => p.PId == person.LoginId)) { people.Add(new PersonModel { PId = person.LoginId, PersonName = person.Name, OrganizationName = person.Organization.Name }); } } } } } requestResult.ReturnData(people.OrderBy(p => p.OrganizationName).ThenBy(p => p.PId).ToList()); } catch (Exception e) { var error = new Error(MethodInfo.GetCurrentMethod(), e); Logger.Log(error); requestResult.ReturnError(error); //throw; } return(requestResult); }
public static RequestResult Create(CreateFormModel model) { RequestResult requestResult = new RequestResult(); try { using (CFContext context = new CFContext()) { var exists = context.People.FirstOrDefault(p => p.LoginId == model.FormInput.Id); if (exists == null) { bool exceedsLimit = false; var upStreamOrganizationIds = OrganizationDataAccessor.GetUpStreamOrganizationIds(new Guid(model.OrganizationId), true); PopulationLimit populationLimit = null; foreach (var item in Config.PopulationLimits) { if (upStreamOrganizationIds.Contains(item.OrganizationId)) { populationLimit = item; break; } } if (populationLimit != null) { var organization = OrganizationDataAccessor.GetOrganization(populationLimit.OrganizationId); var downStreamOrganizations = OrganizationDataAccessor.GetDownStreamOrganizations(populationLimit.OrganizationId, true); var people = context.People.Where(p => downStreamOrganizations.Contains(p.OrganizationId)).ToList(); if (people.Count + 1 > populationLimit.NumberOfPeople) { exceedsLimit = true; requestResult.ReturnFailedMessage(string.Format(Resources.Resource.ExceedsPopulationLimit, organization.Name, populationLimit.NumberOfPeople)); } else { if (model.FormInput.IsMobilePerson && people.Count(x => x.IsMobilePerson) + 1 > populationLimit.NumberOfMobilePeople) { exceedsLimit = true; requestResult.ReturnFailedMessage(string.Format(Resources.Resource.ExceedsMobilePopulationLimit, organization.Name, populationLimit.NumberOfMobilePeople)); } } } if (!exceedsLimit) { var roles = context.Roles.Where(r => model.FormInput.RoleIdsString.Contains(r.RoleId)).ToList(); context.People.Add(new CF.Models.Person() { OrganizationId = new Guid(model.OrganizationId), LoginId = model.FormInput.Id, Name = model.FormInput.Name, Password = model.FormInput.Id, Title = model.FormInput.Title, Email = model.FormInput.EMail, IsMobilePerson = model.FormInput.IsMobilePerson, LastModifyTime = DateTime.Now, Roles = roles }); context.SaveChanges(); requestResult.ReturnData(model.FormInput.Id, string.Format("{0} {1} {2}", Resources.Resource.Create, Resources.Resource.Person, Resources.Resource.Success)); } } else { requestResult.ReturnFailedMessage(string.Format("{0} {1}", Resources.Resource.PId, Resources.Resource.Exists)); } } } catch (Exception e) { var error = new Error(MethodBase.GetCurrentMethod(), e); Logger.Log(error); requestResult.ReturnError(error); //throw; } return(requestResult); }
public static RequestResult Query(QueryParameters queryParameters, Account account) { RequestResult requestResult = new RequestResult(); try { using (CFContext context = new CFContext()) { var downStreamOrganizations = OrganizationDataAccessor.GetDownStreamOrganizationIds(new Guid(queryParameters.OrganizationId), true); var query = (from p in context.People join o in context.Organizations on p.OrganizationId equals o.OrganizationId where downStreamOrganizations.Contains(p.OrganizationId) && account.QueryableOrganizationIds.Contains(p.OrganizationId) select new { Person = p, OrganizationName = o.Name }).AsQueryable(); if (!string.IsNullOrEmpty(queryParameters.Keyword)) { query = query.Where(x => x.Person.Title.Contains(queryParameters.Keyword) || x.Person.LoginId.Contains(queryParameters.Keyword) || x.Person.Name.Contains(queryParameters.Keyword)); } var organization = OrganizationDataAccessor.GetOrganization(new Guid(queryParameters.OrganizationId)); var items = query.ToList(); var model = new GridViewModel() { Permission = account.OrganizationPermission(new Guid(queryParameters.OrganizationId)), OrganizationId = queryParameters.OrganizationId, OrganizationName = organization.Name, FullOrganizationName = organization.FullName, Items = items.Select(x => new GridItem() { Permission = account.OrganizationPermission(x.Person.OrganizationId), OrganizationName = x.OrganizationName, Title = x.Person.Title, Id = x.Person.LoginId, Name = x.Person.Name, Email = x.Person.Email, IsMobilePerson = x.Person.IsMobilePerson, RoleNameList = x.Person.Roles.Select(r => r.Name).ToList() }).OrderBy(x => x.OrganizationName).ThenBy(x => x.Title).ThenBy(x => x.Id).ToList() }; var ancestorOrganizationId = OrganizationDataAccessor.GetAncestorOrganizationId(new Guid(queryParameters.OrganizationId)); var downStreams = OrganizationDataAccessor.GetDownStreamOrganizations(ancestorOrganizationId, true); foreach (var downStream in downStreams) { if (account.EditableOrganizationIds.Any(x => x == downStream)) { model.MoveToTargets.Add(new MoveToTarget() { Id = downStream, Name = OrganizationDataAccessor.GetOrganizationFullName(downStream), Direction = Define.EnumMoveDirection.Down }); } } model.MoveToTargets = model.MoveToTargets.OrderBy(x => x.Name).ToList(); requestResult.ReturnData(model); } } catch (Exception e) { var error = new Error(MethodBase.GetCurrentMethod(), e); Logger.Log(error); requestResult.ReturnError(error); //throw; } return(requestResult); }
public static RequestResult Edit(EditFormModel editFormModel) { RequestResult requestResult = new RequestResult(); try { using (CFContext context = new CFContext()) { var person = context.People.Include("Roles").First(x => x.LoginId == editFormModel.PId); bool exceedsLimit = false; if (editFormModel.FormInput.IsMobilePerson) { var upStreamOrganizationIds = OrganizationDataAccessor.GetUpStreamOrganizationIds(person.OrganizationId, true); PopulationLimit populationLimit = null; foreach (var item in Config.PopulationLimits) { if (upStreamOrganizationIds.Contains(item.OrganizationId)) { populationLimit = item; break; } } if (populationLimit != null) { var organization = OrganizationDataAccessor.GetOrganization(populationLimit.OrganizationId); var downStreamOrganizations = OrganizationDataAccessor.GetDownStreamOrganizations(populationLimit.OrganizationId, true); var mobilePeople = context.People.Where(x => x.LoginId != person.LoginId && x.IsMobilePerson && downStreamOrganizations.Contains(x.OrganizationId)).ToList(); if (mobilePeople.Count + 1 > populationLimit.NumberOfMobilePeople) { exceedsLimit = true; requestResult.ReturnFailedMessage(string.Format(Resources.Resource.ExceedsMobilePopulationLimit, organization.Name, populationLimit.NumberOfMobilePeople)); } } } if (!exceedsLimit) { #if !DEBUG using (TransactionScope trans = new TransactionScope()) { #endif #region Person person.Name = editFormModel.FormInput.Name; person.Title = editFormModel.FormInput.Title; person.Email = editFormModel.FormInput.EMail; //person.UID = editFormModel.FormInput.UID; person.IsMobilePerson = editFormModel.FormInput.IsMobilePerson; person.LastModifyTime = DateTime.Now; context.SaveChanges(); #endregion #region PersonRoles #region Delete if (person != null) { person.Roles = new List <CF.Models.Role>(); context.SaveChanges(); } #endregion #region Insert foreach (var roleId in editFormModel.FormInput.RoleIds) { var role = context.Roles.First(r => r.RoleId == roleId); person.Roles.Add(role); } context.SaveChanges(); #endregion #endregion #if !DEBUG trans.Complete(); } #endif requestResult.ReturnSuccessMessage(string.Format("{0} {1} {2}", Resources.Resource.Edit, Resources.Resource.Person, Resources.Resource.Success)); } } } catch (Exception e) { var error = new Error(MethodBase.GetCurrentMethod(), e); Logger.Log(error); requestResult.ReturnError(error); //throw; } return(requestResult); }