EnVisageOnline/Main-RMO/Source/EnVisage/Models/TeamModel.cs

152 lines
5.2 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using EnVisage.Code;
using System.Linq;
using EnVisage.Code.BLL;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Web.Mvc;
namespace EnVisage.Models
{
public class TeamModel : IBaseModel<Team>
{
public Guid Id { get; set; }
[Required]
[MaxLength(100, ErrorMessage = "Team name should not exceed 100 characters")]
[Display(Name = "Team Name")]
public string Name { get; set; }
[Required]
[Display(Name = "Company")]
public Guid? CompanyId { get; set; }
[Required]
[Display(Name = "Cost Center")]
public Guid? CostCenterId { get; set; }
[Display(Name = "Report To")]
public Guid? ReportToId { get; set; }
public Guid? PlannedCapacityScenarioId { get; set; }
public Guid? ActualCapacityScenarioId { get; set; }
public CompanyModel Company { get; set; }
public CreditDepartmentModel CostCenter { get; set; }
public ContactModel ReportTo { get; set; }
[UIHint("MultipleSelect")]
[Display(Name = "Contributors")]
public IList<Guid> UserId { get; set; }
[Display(Name = "Inherit Capacity From")]
public Guid? CapacityTeamId { get; set; }
[Display(Name = "Capacity Type")]
public bool CopyPlanned { get; set; }
public IEnumerable<AspNetUser> Users
{
get
{
EnVisageEntities DbContext = new EnVisageEntities();
return DbContext.AspNetUsers;
}
}
public String TeamUsers
{
get
{
string result = "";
EnVisageEntities DbContext = new EnVisageEntities();
var ac = new ApplicationDbContext();
var usermanager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(ac));
var users = DbContext.User2Team.Where(x => x.TeamId == Id).ToList();
foreach (var user in users)
{
ApplicationUser u = usermanager.FindById(user.UserId);
if (u != null)
{
if (!string.IsNullOrEmpty(result))
result += ", ";
result += u.UserName;
}
}
return result;
}
}
/// <summary>
/// Casts a <see cref="Client"/> obect to the object of type <see cref="ClientModel"/>.
/// </summary>
/// <param name="obj">A <see cref="Client"/> object.</param>
/// <returns>A <see cref="ClientModel"/> object filled with data from db.</returns>
public static explicit operator TeamModel(Team obj)
{
if (obj == null)
return null;
var model = new TeamModel
{
Id = obj.Id,
Name = obj.Name,
CompanyId = obj.CompanyId,
CostCenterId = obj.CostCenterId,
ReportToId = obj.ReportsTo,
Company = (CompanyModel)obj.Company,
CostCenter = (CreditDepartmentModel)obj.CreditDepartment,
ReportTo = (ContactModel)obj.Contact,
PlannedCapacityScenarioId = obj.PlannedCapacityScenarioId,
ActualCapacityScenarioId = obj.ActualCapacityScenarioId
};
model.TrimStringProperties();
return model;
}
public static explicit operator TeamModel(MixTeamModel mixTeam)
{
if (mixTeam == null)
return null;
var team = new TeamModel()
{
Name = mixTeam.Name,
CompanyId = mixTeam.CompanyId,
CostCenterId = mixTeam.CostCenterId,
UserId = mixTeam.UserId
};
if (!string.IsNullOrWhiteSpace(mixTeam.Id))
{
Guid teamId;
if (Guid.TryParse(mixTeam.Id, out teamId) && !Guid.Empty.Equals(teamId))
team.Id = teamId;
}
return team;
}
/// <summary>
/// Copies data from model to DAL object.
/// </summary>
/// <param name="dbObj">A target DAL object.</param>
public void CopyTo(Team dbObj)
{
if (dbObj == null)
throw new ArgumentNullException();
dbObj.Name = Name;
dbObj.CostCenterId = CostCenterId;
dbObj.CompanyId = CompanyId;
dbObj.ReportsTo = ReportToId;
dbObj.PlannedCapacityScenarioId = PlannedCapacityScenarioId;
dbObj.ActualCapacityScenarioId = ActualCapacityScenarioId;
}
}
public class TeamInfoModel
{
public Guid Id { get; set; }
public string Name { get; set; }
public int Allocation { get; set; }
public bool CanBeDeleted { get; set; }
public Guid? PlannedCapacityScenarioId { get; set; }
public ICollection<PeopleResource> Resources { get; set; }
public bool IsAccessible { get; set; }
}
}