EnVisageOnline/Main/Source/EnVisage/Models/SkillModels.cs

400 lines
12 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using EnVisage.Code;
using EnVisage.Models.Entities;
using System.Web.Mvc;
//using Excel;
namespace EnVisage.Models
{
#region Skill and SkillGroup Models
public class SkillGroupModel : IBaseModel<Skill>, IValidatableObject
{
public Guid Id { get; set; }
[Required]
[MaxLength(512, ErrorMessage = "Name should not exceed 512 characters")]
[Display(Name = "Name")]
public string Name { get; set; }
[Display(Name = "Skill Group has multiple Skills")]
public bool HasChildren { get; set; }
public bool InitialHasChildrenState { get; set; }
public Dictionary<string, SkillModel> Children { get; set; }
public string Timestamp { get; set; }
public Guid? ParentId { get; private set; }
public bool IsNew { get; set; }
public bool IsDeleted { get; set; }
public bool IsChanged { get; set; }
public bool HasS2RData { get; set; }
public bool CopyDataUponDelete { get; set; }
/// <summary>
/// Casts a <see cref="Skill"/> entity to the object of type <see cref="SkillGroupModel"/>.
/// </summary>
/// <param name="obj">A <see cref="Skill"/> object.</param>
/// <returns>A <see cref="SkillGroupModel"/> object filled with data from db.</returns>
public static explicit operator SkillGroupModel(Skill obj)
{
if (obj == null)
return null;
var model = new SkillGroupModel
{
Id = obj.Id,
Name = obj.Name,
HasChildren = obj.HasChildren,
InitialHasChildrenState = obj.HasChildren,
ParentId = obj.ParentId,
Timestamp = obj.EditTimestamp.ByteArrayToString(),
HasS2RData = obj.Skill2Resource.Any()
};
model.TrimStringProperties();
if (model.HasChildren && obj.Skills != null)
{
foreach (var child in obj.Skills.OrderBy(pp => pp.Name))
{
var skill = (SkillModel)child;
model.Children.Add(skill.Id.ToString(), skill);
}
}
model.TrimStringProperties();
return model;
}
/// <summary>
/// Copies data from model to DAL object.
/// </summary>
/// <param name="dbObj">A target DAL object.</param>
public void CopyTo(Skill dbObj)
{
if (dbObj == null)
throw new ArgumentNullException();
dbObj.Name = Name;
dbObj.HasChildren = HasChildren;
}
public SkillGroupModel Clone()
{
return new SkillGroupModel
{
Id = this.Id,
Name = this.Name,
HasChildren = this.HasChildren,
InitialHasChildrenState = this.HasChildren,
Children = this.Children,
HasS2RData = this.HasS2RData,
CopyDataUponDelete = this.CopyDataUponDelete,
IsChanged = this.IsChanged,
IsDeleted = this.IsDeleted,
IsNew = this.IsNew,
ParentId = this.ParentId,
Timestamp = this.Timestamp
};
}
public SkillGroupModel()
{
Children = new Dictionary<string, SkillModel>();
CopyDataUponDelete = true;
}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (HasChildren && Children != null && Children.Count > 0)
{
foreach (var key in Children.Keys)
{
if (string.IsNullOrEmpty(Children[key].Name))
yield return new ValidationResult(string.Format(Constants.ERROR_TEMPLATE_REQUIRED, "Skill Name"), null);
}
}
}
}
public class SkillModel : IBaseModel<Skill>
{
public Guid Id { get; set; }
[Required]
[MaxLength(512, ErrorMessage = "Name should not exceed 512 characters")]
public string Name { get; set; }
public string Timestamp { get; set; }
public Guid? ParentId { get; set; }
public bool IsNew { get; set; }
public bool IsDeleted { get; set; }
public bool IsChanged { get; set; }
public bool HasS2RData { get; set; }
public static explicit operator SkillModel(Skill obj)
{
if (obj == null)
return null;
var model = new SkillModel
{
Id = obj.Id,
Name = obj.Name,
ParentId = obj.ParentId,
Timestamp = obj.EditTimestamp.ByteArrayToString(),
HasS2RData = obj.Skill2Resource.Any()
};
model.TrimStringProperties();
return model;
}
/// <summary>
/// Copies data from model to DAL object.
/// </summary>
/// <param name="dbObj">A target DAL object.</param>
public void CopyTo(Skill dbObj)
{
if (dbObj == null)
throw new ArgumentNullException();
dbObj.Name = Name;
dbObj.HasChildren = false;
dbObj.ParentId = ParentId;
}
public SkillModel Clone()
{
return new SkillModel
{
Id = this.Id,
Name = this.Name,
ParentId = this.ParentId,
Timestamp = this.Timestamp,
HasS2RData = this.HasS2RData,
IsNew = this.IsNew,
IsDeleted = this.IsDeleted,
IsChanged = this.IsChanged
};
}
}
/// <summary>
/// An UI representation of skill to be displayed as list item
/// </summary>
public class EditSkillsModel : Dictionary<string, SkillGroupModel>
{
}
#endregion
#region SkillMatrix Models
public class SkillsMatrixQueryModelBase
{
public long DatePoint { get; set; }
public Skill2ResourceType DataType { get; set; }
public bool ReadOnly { get; set; }
}
/// <summary>
/// Model for managing Skills Matrix display on a page
/// </summary>
public class SkillsMatrixPageLoadModel : SkillsMatrixQueryModelBase
{
public class FilterOptionsModel
{
public List<SelectListItem> Companies = new List<SelectListItem>();
public List<SelectListItem> Teams = new List<SelectListItem>();
public List<SelectListItem> Views = new List<SelectListItem>();
public List<SelectListItem> Skills = new List<SelectListItem>();
public List<SelectListItem> SkillLevels = new List<SelectListItem>();
public List<SelectListItem> SkillInterests = new List<SelectListItem>();
}
public ApplicationDashboards OpenerType;
public string OpenerId;
public FilterOptionsModel FilterOptions;
public string ActivityCalendarUrl;
public SkillsMatrixPageLoadModel()
{
FilterOptions = new FilterOptionsModel();
}
}
public class SkillsMatrixBarChartModel
{
public List<List<object>> Titles { get; set; }
public List<List<object>> GTitles { get; set; }
public string[] Colors { get; set; }
public List<Object> Data { get; set; }
public List<Object> GroupData { get; set; }
public int MaxVal { get; set; }
public int GMaxVal { get; set; }
//public List<int[]>[] Data { get; set; }
}
public class SkillsMatrixBarChartPartModel
{
public Guid Id { get; set; }
public int Count { get; set; }
public int Level0 { get; set; }
public int Level1 { get; set; }
public int Level2 { get; set; }
public int Level3 { get; set; }
public int Level4 { get; set; }
}
public class SkillsMatrixPieChartPartModel
{
public List<Guid> TypeId { get; set; }
public string Label { get; set; }
public decimal Value { get; set; }
public string PresetColor { get; set; }
}
public class SkillsMatrixPieChartModel
{
public List<SkillsMatrixPieChartPartModel> PastData { get; set; }
public List<SkillsMatrixPieChartPartModel> PresentData { get; set; }
public List<SkillsMatrixPieChartPartModel> FutureData { get; set; }
public List<SkillsMatrixPieChartPartModel> PastGroupedData { get; set; }
public List<SkillsMatrixPieChartPartModel> PresentGroupedData { get; set; }
public List<SkillsMatrixPieChartPartModel> FutureGroupedData { get; set; }
}
public class SkillsMatrixPiePanelModel
{
public List<List<SkillsMatrixPieChartModel>> PieCharts { get; set; }
}
/// <summary>
/// Model to query Skills Matrix from server
/// </summary>
public class SkillsMatrixQueryModel : SkillsMatrixQueryModelBase
{
public SkillsMaxtrixFilterModel Filter { get; set; }
}
/// <summary>
/// Filtering Model for Skills matrix
/// </summary>
public class SkillsMaxtrixFilterModel
{
public List<Guid> Teams { get; set; }
public List<Guid> Views { get; set; }
public List<Guid> Resources { get; set; }
public List<Guid> Companies { get; set; }
public List<Guid> Skills { get; set; }
public List<int> SkillLevels { get; set; }
public bool? IncludeInterested { get; set; }
public bool? GroupMode { get; set; }
// Retrive the only skills and groups, that has data
public bool SkillsWithDataOnly { get; set; }
}
/// <summary>
/// Model for transfering Skills Matrix data between client and server
/// </summary>
public class SkillsMatrixSaveModel
{
public class SkillGroupDisplayModel
{
public Guid Id { get; set; }
public string Name { get; set; }
public List<SkillDisplayModel> Skills { get; set; }
public SkillGroupDisplayModel()
{
Skills = new List<SkillDisplayModel>();
}
public SkillsMatrixBarChartModel BarChartData { get; set; }
public SkillsMatrixPiePanelModel PiePanelData { get; set; }
}
public class SkillDisplayModel
{
public Guid Id { get; set; }
public string Name { get; set; }
public Guid? SkillGroupId { get; set; }
public bool IsVirtual { get; set; }
}
public class DataItem
{
public Guid SkillId { get; set; }
public Guid ResourceId { get; set; }
public int? Level { get; set; }
public bool Interested { get; set; }
public bool LevelChanged { get; set; }
public bool InterestChanged { get; set; }
}
public class Team
{
public Guid Id { get; set; }
public string Name { get; set; }
public List<Guid> Resources { get; set; }
public Team()
{
Resources = new List<Guid>();
}
}
public class Resource
{
public Guid Id { get; set; }
public List<Guid> Teams { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string FullName
{
get
{
return String.Format("{0} {1}", FirstName, LastName).Trim();
}
}
public Resource()
{
Teams = new List<Guid>();
}
}
public SkillsMatrixBarChartModel BarGraphData { get; set; }
public SkillsMatrixPieChartModel PieGraphData { get; set; }
public long DatePoint { get; set; }
public Skill2ResourceType DataType { get; set; }
public List<SkillGroupDisplayModel> SkillGroups { get; set; }
public List<Team> Teams { get; set; }
public List<Resource> Resources { get; set; }
public Dictionary<string, DataItem> Values { get; set; } // SA. Key for dictionary values is <ResourceId>#<SkillId>
public SkillsMatrixSaveModel()
{
SkillGroups = new List<SkillGroupDisplayModel>();
Teams = new List<Team>();
Resources = new List<Resource>();
Values = new Dictionary<string, DataItem>();
}
}
#endregion
}
namespace EnVisage
{
/// <summary>
/// Extension for Skill class
/// </summary>
public partial class Skill : ITimestampEntity
{
public void SetUpdatedTimestamp()
{
}
public void SetCreatedTimestamp()
{
this.DateCreated = DateTime.UtcNow;
}
public ulong? GetCurrentVersion()
{
if (this.EditTimestamp != null)
return BitConverter.ToUInt64(this.EditTimestamp.Reverse().ToArray(), 0);
else
return (ulong?)null;
}
}
}