Commit for development environment setup

This commit is contained in:
2023-06-19 16:12:33 -04:00
parent be72063a3c
commit bbce2ad0a6
2209 changed files with 1171775 additions and 625 deletions

View File

@@ -0,0 +1,220 @@
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
namespace ProjectTracker.Library.Admin
{
[Serializable()]
public class Role : BusinessBase<Role>
{
#region Business Methods
private int _id;
private bool _idSet;
private string _name = String.Empty;
private byte[] _timestamp = new byte[8];
[System.ComponentModel.DataObjectField(true, true)]
public int Id
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty(true);
if (!_idSet)
{
// generate a default id value
_idSet = true;
Roles parent = (Roles)this.Parent;
int max = 0;
foreach (Role item in parent)
{
if (item.Id > max)
max = item.Id;
}
_id = max + 1;
}
return _id;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
set
{
CanWriteProperty(true);
if (!_id.Equals(value))
{
_idSet = true;
_id = value;
PropertyHasChanged();
}
}
}
public string Name
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty(true);
return _name;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
set
{
CanWriteProperty(true);
if (value == null) value = string.Empty;
if (!_name.Equals(value))
{
_name = value;
PropertyHasChanged();
}
}
}
protected override object GetIdValue()
{
return _id;
}
#endregion
#region Validation Rules
protected override void AddBusinessRules()
{
ValidationRules.AddRule(
Csla.Validation.CommonRules.StringRequired, "Name");
}
protected override void AddInstanceBusinessRules()
{
ValidationRules.AddInstanceRule(NoDuplicates, "Id");
}
private bool NoDuplicates(object target, Csla.Validation.RuleArgs e)
{
Roles parent = (Roles)this.Parent;
foreach (Role item in parent)
if (item.Id == _id && !ReferenceEquals(item, this))
{
e.Description = "Role Id must be unique";
return false;
}
return true;
}
#endregion
#region Authorization Rules
protected override void AddAuthorizationRules()
{
AuthorizationRules.AllowWrite(
"Id", "Administrator");
AuthorizationRules.AllowWrite(
"Name", "Administrator");
}
#endregion
#region Factory Methods
internal static Role NewRole()
{
return new Role();
}
internal static Role
GetRole(Csla.Data.SafeDataReader dr)
{
return new Role(dr);
}
private Role()
{
MarkAsChild();
}
#endregion
#region Data Access
private Role(Csla.Data.SafeDataReader dr)
{
MarkAsChild();
_id = dr.GetInt32("id");
_idSet = true;
_name = dr.GetString("name");
dr.GetBytes("lastChanged", 0, _timestamp, 0, 8);
MarkOld();
}
internal void Insert(SqlConnection cn)
{
// if we're not dirty then don't update the database
if (!this.IsDirty) return;
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandText = "addRole";
DoInsertUpdate(cm);
}
}
internal void Update(SqlConnection cn)
{
// if we're not dirty then don't update the database.
if (!this.IsDirty) return;
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandText = "updateRole";
cm.Parameters.AddWithValue("@lastChanged", _timestamp);
DoInsertUpdate(cm);
}
}
void DoInsertUpdate(SqlCommand cm)
{
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.AddWithValue("@id", _id);
cm.Parameters.AddWithValue("@name", _name);
SqlParameter param =
new SqlParameter("@newLastChanged", SqlDbType.Timestamp);
param.Direction = ParameterDirection.Output;
cm.Parameters.Add(param);
cm.ExecuteNonQuery();
_timestamp = (byte[])cm.Parameters["@newLastChanged"].Value;
MarkOld();
}
internal void DeleteSelf(SqlConnection cn)
{
// if we're not dirty then don't update the database
if (!this.IsDirty) return;
// if we're new then don't update the database
if (this.IsNew) return;
DeleteRole(cn, _id);
MarkNew();
}
internal static void DeleteRole(SqlConnection cn, int id)
{
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "deleteRole";
cm.Parameters.AddWithValue("@id", id);
cm.ExecuteNonQuery();
}
}
#endregion
}
}

View File

@@ -0,0 +1,175 @@
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
namespace ProjectTracker.Library.Admin
{
/// <summary>
/// Used to maintain the list of roles
/// in the system.
/// </summary>
[Serializable()]
public class Roles :
BusinessListBase<Roles, Role>
{
#region Business Methods
/// <summary>
/// Remove a role based on the role's
/// id value.
/// </summary>
/// <param name="id">Id value of the role to remove.</param>
public void Remove(int id)
{
foreach (Role item in this)
{
if (item.Id == id)
{
Remove(item);
break;
}
}
}
/// <summary>
/// Get a role based on its id value.
/// </summary>
/// <param name="id">Id valud of the role to return</param>
public Role GetRoleById(int id)
{
foreach (Role item in this)
if (item.Id == id)
return item;
return null;
}
protected override object AddNewCore()
{
Role item = Role.NewRole();
Add(item);
return item;
}
#endregion
#region Authorization Rules
public static bool CanAddObject()
{
return Csla.ApplicationContext.User.IsInRole("Administrator");
}
public static bool CanGetObject()
{
return true;
}
public static bool CanDeleteObject()
{
return Csla.ApplicationContext.User.IsInRole("Administrator");
}
public static bool CanEditObject()
{
return Csla.ApplicationContext.User.IsInRole("Administrator");
}
#endregion
#region Factory Methods
public static Roles GetRoles()
{
return DataPortal.Fetch<Roles>(new Criteria());
}
private Roles()
{
this.AllowNew = true;
}
#endregion
#region Data Access
[Serializable()]
private class Criteria
{ /* no criteria */ }
public override Roles Save()
{
// see if save is allowed
if (!CanEditObject())
throw new System.Security.SecurityException(
"User not authorized to save roles");
// do the save
Roles result;
result = base.Save();
// this runs on the client and invalidates
// the RoleList cache
RoleList.InvalidateCache();
return result;
}
protected override void DataPortal_OnDataPortalInvokeComplete(
DataPortalEventArgs e)
{
if (ApplicationContext.ExecutionLocation ==
ApplicationContext.ExecutionLocations.Server)
{
// this runs on the server and invalidates
// the RoleList cache
RoleList.InvalidateCache();
}
}
private void DataPortal_Fetch(Criteria criteria)
{
RaiseListChangedEvents = false;
using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
{
cn.Open();
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "getRoles";
using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
while (dr.Read())
this.Add(Role.GetRole(dr));
}
}
RaiseListChangedEvents = true;
}
[Transactional(TransactionalTypes.TransactionScope)]
protected override void DataPortal_Update()
{
this.RaiseListChangedEvents = false;
using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
{
cn.Open();
foreach (Role item in DeletedList)
{
item.DeleteSelf(cn);
}
DeletedList.Clear();
foreach (Role item in this)
{
if (item.IsNew)
item.Insert(cn);
else
item.Update(cn);
}
}
this.RaiseListChangedEvents = true;
}
#endregion
}
}

View File

@@ -0,0 +1,114 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Validation;
using Csla.Data;
using System.Reflection;
namespace ProjectTracker.Library
{
internal interface IHoldRoles
{
int Role { get; set;}
}
internal static class Assignment
{
#region Business Methods
public static DateTime GetDefaultAssignedDate()
{
return DateTime.Today;
}
#endregion
#region Validation Rules
/// <summary>
/// Ensure the Role property value exists
/// in RoleList
/// </summary>
public static bool ValidRole(object target, RuleArgs e)
{
int role = ((IHoldRoles)target).Role;
if (RoleList.GetList().ContainsKey(role))
return true;
else
{
e.Description = "Role must be in RoleList";
return false;
}
}
#endregion
#region Data Access
public static byte[] AddAssignment(
SqlConnection cn, Guid projectId, int resourceId,
SmartDate assigned, int role)
{
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandText = "addAssignment";
return DoAddUpdate(
cm, projectId, resourceId, assigned, role);
}
}
public static byte[] UpdateAssignment(SqlConnection cn,
Guid projectId, int resourceId, SmartDate assigned,
int newRole, byte[] timestamp)
{
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandText = "updateAssignment";
cm.Parameters.AddWithValue("@lastChanged", timestamp);
return DoAddUpdate(
cm, projectId, resourceId, assigned, newRole);
}
}
private static byte[] DoAddUpdate(SqlCommand cm,
Guid projectId, int resourceId, SmartDate assigned,
int newRole)
{
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.AddWithValue("@projectId", projectId);
cm.Parameters.AddWithValue("@resourceId", resourceId);
cm.Parameters.AddWithValue("@assigned", assigned.DBValue);
cm.Parameters.AddWithValue("@role", newRole);
SqlParameter param =
new SqlParameter("@newLastChanged", SqlDbType.Timestamp);
param.Direction = ParameterDirection.Output;
cm.Parameters.Add(param);
cm.ExecuteNonQuery();
return (byte[])cm.Parameters["@newLastChanged"].Value;
}
public static void RemoveAssignment(
SqlConnection cn, Guid projectId, int resourceId)
{
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "deleteAssignment";
cm.Parameters.AddWithValue("@projectId", projectId);
cm.Parameters.AddWithValue("@resourceId", resourceId);
cm.ExecuteNonQuery();
}
}
#endregion
}
}

View File

@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
namespace ProjectTracker.Library
{
public static class Database
{
public static string PTrackerConnection
{
get
{
return ConfigurationManager.ConnectionStrings
["PTracker"].ConnectionString;
}
}
public static string SecurityConnection
{
get { return System.Configuration.ConfigurationManager.ConnectionStrings["Security"].ConnectionString; }
}
}
}

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<ClassDiagram MajorVersion="1" MinorVersion="1">
<Font Name="Tahoma" Size="8.25" />
<Class Name="ProjectTracker.Library.Project" Collapsed="true">
<Position X="1" Y="2.75" Width="1.5" />
<TypeIdentifier>
<FileName>Project.cs</FileName>
<HashCode>EAACIIwBIgAgRAEEAAjIAgQAAAJAgAECAhgBAAEAmAA=</HashCode>
</TypeIdentifier>
</Class>
<Class Name="Csla.BusinessBase&lt;T&gt;" Collapsed="true">
<Position X="1" Y="1.5" Width="1.5" />
<TypeIdentifier />
</Class>
</ClassDiagram>

View File

@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="utf-8"?>
<ClassDiagram MajorVersion="1" MinorVersion="1">
<Font Name="Tahoma" Size="8.25" />
<Class Name="ProjectTracker.Library.Project" Collapsed="true">
<Position X="2" Y="1.75" Width="2.25" />
<TypeIdentifier>
<FileName>Project.cs</FileName>
<HashCode>EAACIIwBIgAgRAEEAAjIAgQAAAJAgAECAhgBAAEAmAA=</HashCode>
</TypeIdentifier>
</Class>
<Class Name="ProjectTracker.Library.Resource" Collapsed="true">
<Position X="5" Y="1.75" Width="2.25" />
<TypeIdentifier>
<FileName>Resource.cs</FileName>
<HashCode>FAMCgMgAAAAARIAAAAlIAAAAAABAgAECABgJCAEAiAg=</HashCode>
</TypeIdentifier>
</Class>
<Class Name="Csla.BusinessBase&lt;T&gt;" Collapsed="true">
<Position X="4.5" Y="0.5" Width="2.25" />
<TypeIdentifier />
</Class>
<Class Name="ProjectTracker.Library.ProjectResource" Collapsed="true">
<Position X="2" Y="2.75" Width="2.25" />
<TypeIdentifier>
<FileName>ProjectResource.cs</FileName>
<HashCode>EAUAAEAAAgAAgIAAEAEAAAAAAAAAAAoAAAABCEAAigg=</HashCode>
</TypeIdentifier>
</Class>
<Class Name="ProjectTracker.Library.ResourceAssignment" Collapsed="true">
<Position X="5" Y="2.75" Width="2.25" />
<TypeIdentifier>
<FileName>ResourceAssignment.cs</FileName>
<HashCode>EAQAAEAAAAAAAABAGAAAAAAABAAAAAIAAgABAEAIigA=</HashCode>
</TypeIdentifier>
</Class>
<Class Name="ProjectTracker.Library.ProjectResources" Collapsed="true">
<Position X="0.5" Y="5.75" Width="3.25" />
<TypeIdentifier>
<FileName>ProjectResources.cs</FileName>
<HashCode>AAQAAAQAAAAAAGAAAAAABAAAAAAAAAAAAAAAQBAQCAA=</HashCode>
</TypeIdentifier>
</Class>
<Class Name="ProjectTracker.Library.ResourceAssignments" Collapsed="true">
<Position X="4" Y="5.75" Width="3.25" />
<TypeIdentifier>
<FileName>ResourceAssignments.cs</FileName>
<HashCode>AAQAAAAAAAAAAAAACAAABAAAIAAAgAAAEAAAAAAACAA=</HashCode>
</TypeIdentifier>
</Class>
<Class Name="Csla.BusinessListBase&lt;T, C&gt;" Collapsed="true">
<Position X="4.5" Y="4.5" Width="2.25" />
<TypeIdentifier />
<Lollipop Position="0.2" />
</Class>
<Class Name="ProjectTracker.Library.Admin.Roles" Collapsed="true">
<Position X="7.5" Y="5.75" Width="1.75" />
<TypeIdentifier>
<FileName>Admin\Roles.cs</FileName>
<HashCode>AAAAAIAAAAAARAAAEAAAhAACACAAgAECAAgAAAAAAAA=</HashCode>
</TypeIdentifier>
</Class>
<Class Name="ProjectTracker.Library.Admin.Role" Collapsed="true">
<Position X="7.75" Y="1.75" Width="1.5" />
<TypeIdentifier>
<FileName>Admin\Role.cs</FileName>
<HashCode>UAQCIEEAAAAAAAAAgARAAAQAAAAAAAAAAAAAAEAAgAA=</HashCode>
</TypeIdentifier>
</Class>
</ClassDiagram>

View File

@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<ClassDiagram MajorVersion="1" MinorVersion="1">
<Font Name="Tahoma" Size="8.25" />
<Class Name="ProjectTracker.Library.ResourceList" Collapsed="true">
<Position X="4.5" Y="1.75" Width="2.5" />
<TypeIdentifier>
<FileName>ResourceList.cs</FileName>
<HashCode>AAAAAAAAAAAABAAAgAAAAAAAAAAAIAAAAAAAAAAAAAA=</HashCode>
</TypeIdentifier>
</Class>
<Class Name="ProjectTracker.Library.ProjectList" Collapsed="true">
<Position X="1.75" Y="1.75" Width="2.5" />
<TypeIdentifier>
<FileName>ProjectList.cs</FileName>
<HashCode>AAAAAAAAAAAABEAAAAEAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
</TypeIdentifier>
</Class>
<Class Name="Csla.ReadOnlyListBase&lt;T, C&gt;" Collapsed="true">
<Position X="3.25" Y="0.5" Width="2.25" />
<TypeIdentifier />
<Lollipop Position="0.2" />
</Class>
<Class Name="ProjectTracker.Library.RoleList" Collapsed="true">
<Position X="3.5" Y="5" Width="2.25" />
<TypeIdentifier>
<FileName>RoleList.cs</FileName>
<HashCode>AAABAAAAAAAABAAAAAAAAAAAAAAAAABAQAAAAAAAAAI=</HashCode>
</TypeIdentifier>
</Class>
<Class Name="Csla.NameValueListBase&lt;K, V&gt;" Collapsed="true">
<Position X="3.5" Y="3.75" Width="2.25" />
<TypeIdentifier />
<Lollipop Position="0.2" />
</Class>
</ClassDiagram>

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<ClassDiagram MajorVersion="1" MinorVersion="1">
<Font Name="Tahoma" Size="8.25" />
<Class Name="ProjectTracker.Library.Security.PTPrincipal" Collapsed="true">
<Position X="3" Y="2.75" Width="1.75" />
<TypeIdentifier>
<FileName>Security\PTPrincipal.cs</FileName>
<HashCode>AAAAAAAACAAAAAAAAAAAAAIAAAAAAAAAAAAQAAAAAAA=</HashCode>
</TypeIdentifier>
</Class>
<Class Name="ProjectTracker.Library.Security.PTIdentity" Collapsed="true">
<Position X="5.25" Y="2.75" Width="1.75" />
<TypeIdentifier>
<FileName>Security\PTIdentity.cs</FileName>
<HashCode>AAAAIAAACAAABAAACAAAAAQgAAAEAAAAAAABAIAAABA=</HashCode>
</TypeIdentifier>
<Lollipop Position="0.2" />
</Class>
<Class Name="Csla.Security.BusinessPrincipalBase" Collapsed="true">
<Position X="3" Y="1.5" Width="1.75" />
<TypeIdentifier />
<Lollipop Position="0.2" />
</Class>
<Class Name="Csla.ReadOnlyBase&lt;T&gt;" Collapsed="true">
<Position X="5.25" Y="1.5" Width="1.75" />
<TypeIdentifier />
<Lollipop Position="0.2" />
</Class>
</ClassDiagram>

View File

@@ -0,0 +1,440 @@
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
namespace ProjectTracker.Library
{
[Serializable()]
public class Project : BusinessBase<Project>
{
#region Business Methods
private Guid _id;
private string _name = string.Empty;
private SmartDate _started;
private SmartDate _ended = new SmartDate(false);
private string _description = string.Empty;
private byte[] _timestamp = new byte[8];
private ProjectResources _resources =
ProjectResources.NewProjectResources();
[System.ComponentModel.DataObjectField(true, true)]
public Guid Id
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty(true);
return _id;
}
}
public string Name
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty(true);
return _name;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
set
{
CanWriteProperty(true);
if (value == null) value = string.Empty;
if (_name != value)
{
_name = value;
PropertyHasChanged();
}
}
}
public string Started
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty(true);
return _started.Text;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
set
{
CanWriteProperty(true);
if (value == null) value = string.Empty;
if (_started != value)
{
_started.Text = value;
PropertyHasChanged();
}
}
}
public string Ended
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty(true);
return _ended.Text;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
set
{
CanWriteProperty(true);
if (value == null) value = string.Empty;
if (_ended != value)
{
_ended.Text = value;
PropertyHasChanged();
}
}
}
public string Description
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty(true);
return _description;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
set
{
CanWriteProperty(true);
if (value == null) value = string.Empty;
if (_description != value)
{
_description = value;
PropertyHasChanged();
}
}
}
public ProjectResources Resources
{
get { return _resources; }
}
public override bool IsValid
{
get { return base.IsValid && _resources.IsValid; }
}
public override bool IsDirty
{
get { return base.IsDirty || _resources.IsDirty; }
}
protected override object GetIdValue()
{
return _id;
}
#endregion
#region Validation Rules
protected override void AddBusinessRules()
{
ValidationRules.AddRule(
Csla.Validation.CommonRules.StringRequired, "Name");
ValidationRules.AddRule(
Csla.Validation.CommonRules.StringMaxLength,
new Csla.Validation.CommonRules.MaxLengthRuleArgs("Name", 50));
ValidationRules.AddRule<Project>(
StartDateGTEndDate<Project>, "Started");
ValidationRules.AddRule<Project>(
StartDateGTEndDate<Project>, "Ended");
ValidationRules.AddDependantProperty("Started", "Ended", true);
}
private static bool StartDateGTEndDate<T>(
T target, Csla.Validation.RuleArgs e) where T : Project
{
if (target._started > target._ended)
{
e.Description =
"Start date can't be after end date";
return false;
}
else
return true;
}
#endregion
#region Authorization Rules
protected override void AddAuthorizationRules()
{
AuthorizationRules.AllowWrite(
"Name", "ProjectManager");
AuthorizationRules.AllowWrite(
"Started", "ProjectManager");
AuthorizationRules.AllowWrite(
"Ended", "ProjectManager");
AuthorizationRules.AllowWrite(
"Description", "ProjectManager");
}
public static bool CanAddObject()
{
return Csla.ApplicationContext.User.IsInRole(
"ProjectManager");
}
public static bool CanGetObject()
{
return true;
}
public static bool CanDeleteObject()
{
bool result = false;
if (Csla.ApplicationContext.User.IsInRole(
"ProjectManager"))
result = true;
if (Csla.ApplicationContext.User.IsInRole(
"Administrator"))
result = true;
return result;
}
public static bool CanEditObject()
{
return Csla.ApplicationContext.User.IsInRole("ProjectManager");
}
#endregion
#region Factory Methods
public static Project NewProject()
{
if (!CanAddObject())
throw new System.Security.SecurityException(
"User not authorized to add a project");
return DataPortal.Create<Project>();
}
public static Project GetProject(Guid id)
{
if (!CanGetObject())
throw new System.Security.SecurityException(
"User not authorized to view a project");
return DataPortal.Fetch<Project>(new Criteria(id));
}
public static void DeleteProject(Guid id)
{
if (!CanDeleteObject())
throw new System.Security.SecurityException(
"User not authorized to remove a project");
DataPortal.Delete(new Criteria(id));
}
private Project()
{ /* require use of factory methods */ }
public override Project Save()
{
if (IsDeleted && !CanDeleteObject())
throw new System.Security.SecurityException(
"User not authorized to remove a project");
else if (IsNew && !CanAddObject())
throw new System.Security.SecurityException(
"User not authorized to add a project");
else if (!CanEditObject())
throw new System.Security.SecurityException(
"User not authorized to update a project");
return base.Save();
}
#endregion
#region Data Access
[Serializable()]
private class Criteria
{
private Guid _id;
public Guid Id
{
get { return _id; }
}
public Criteria(Guid id)
{ _id = id; }
}
[RunLocal()]
protected override void DataPortal_Create()
{
_id = Guid.NewGuid();
_started.Date = DateTime.Today;
ValidationRules.CheckRules();
}
private void DataPortal_Fetch(Criteria criteria)
{
using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
{
cn.Open();
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "getProject";
cm.Parameters.AddWithValue("@id", criteria.Id);
using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
{
dr.Read();
_id = dr.GetGuid("Id");
_name = dr.GetString("Name");
_started = dr.GetSmartDate("Started", _started.EmptyIsMin);
_ended = dr.GetSmartDate("Ended", _ended.EmptyIsMin);
_description = dr.GetString("Description");
dr.GetBytes("LastChanged", 0, _timestamp, 0, 8);
// load child objects
dr.NextResult();
_resources = ProjectResources.GetProjectResources(dr);
}
}
}
}
[Transactional(TransactionalTypes.TransactionScope)]
protected override void DataPortal_Insert()
{
using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
{
cn.Open();
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandText = "addProject";
DoInsertUpdate(cm);
}
}
// update child objects
_resources.Update(this);
}
[Transactional(TransactionalTypes.TransactionScope)]
protected override void DataPortal_Update()
{
if (base.IsDirty)
{
using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
{
cn.Open();
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandText = "updateProject";
cm.Parameters.AddWithValue("@lastChanged", _timestamp);
DoInsertUpdate(cm);
}
}
}
// update child objects
_resources.Update(this);
}
private void DoInsertUpdate(SqlCommand cm)
{
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.AddWithValue("@id", _id);
cm.Parameters.AddWithValue("@name", _name);
cm.Parameters.AddWithValue("@started", _started.DBValue);
cm.Parameters.AddWithValue("@ended", _ended.DBValue);
cm.Parameters.AddWithValue("@description", _description);
SqlParameter param =
new SqlParameter("@newLastChanged", SqlDbType.Timestamp);
param.Direction = ParameterDirection.Output;
cm.Parameters.Add(param);
cm.ExecuteNonQuery();
_timestamp = (byte[])cm.Parameters["@newLastChanged"].Value;
}
[Transactional(TransactionalTypes.TransactionScope)]
protected override void DataPortal_DeleteSelf()
{
DataPortal_Delete(new Criteria(_id));
}
[Transactional(TransactionalTypes.TransactionScope)]
private void DataPortal_Delete(Criteria criteria)
{
using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
{
cn.Open();
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "deleteProject";
cm.Parameters.AddWithValue("@id", criteria.Id);
cm.ExecuteNonQuery();
}
}
}
#endregion
#region Exists
public static bool Exists(Guid id)
{
ExistsCommand result;
result = DataPortal.Execute<ExistsCommand>
(new ExistsCommand(id));
return result.Exists;
}
[Serializable()]
private class ExistsCommand : CommandBase
{
private Guid _id;
private bool _exists;
public bool Exists
{
get { return _exists; }
}
public ExistsCommand(Guid id)
{
_id = id;
}
protected override void DataPortal_Execute()
{
using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
{
cn.Open();
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "existsProject";
cm.Parameters.AddWithValue("@id", _id);
int count = (int)cm.ExecuteScalar();
_exists = (count > 0);
}
}
}
}
#endregion
}
}

View File

@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
namespace ProjectTracker.Library
{
[Serializable()]
public class ProjectInfo :
ReadOnlyBase<ProjectInfo>
{
#region Business Methods
private Guid _id;
private string _name;
public Guid Id
{
get { return _id; }
}
public string Name
{
get { return _name; }
}
protected override object GetIdValue()
{
return _id;
}
public override string ToString()
{
return _name;
}
#endregion
#region Constructors
private ProjectInfo()
{ /* require use of factory methods */ }
internal ProjectInfo(Guid id, string name)
{
_id = id;
_name = name;
}
#endregion
}
}

View File

@@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
namespace ProjectTracker.Library
{
[Serializable()]
public class ProjectList :
ReadOnlyListBase<ProjectList, ProjectInfo>
{
#region Factory Methods
/// <summary>
/// Return a list of all projects.
/// </summary>
public static ProjectList GetProjectList()
{
return DataPortal.Fetch<ProjectList>(new Criteria());
}
/// <summary>
/// Return a list of projects filtered
/// by project name.
/// </summary>
public static ProjectList GetProjectList(string name)
{
return DataPortal.Fetch<ProjectList>
(new FilteredCriteria(name));
}
private ProjectList()
{ /* require use of factory methods */ }
#endregion
#region Data Access
[Serializable()]
private class Criteria
{ /* no criteria - retrieve all projects */ }
[Serializable()]
private class FilteredCriteria
{
private string _name;
public string Name
{
get { return _name; }
}
public FilteredCriteria(string name)
{
_name = name;
}
}
private void DataPortal_Fetch(Criteria criteria)
{
// fetch with no filter
Fetch("");
}
private void DataPortal_Fetch(FilteredCriteria criteria)
{
Fetch(criteria.Name);
}
private void Fetch(string nameFilter)
{
this.RaiseListChangedEvents = false;
using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
{
cn.Open();
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "getProjects";
using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
{
IsReadOnly = false;
while (dr.Read())
{
ProjectInfo info = new ProjectInfo(
dr.GetGuid(0),
dr.GetString(1));
// apply filter if necessary
if ((nameFilter.Length == 0) || (info.Name.IndexOf(nameFilter) == 0))
this.Add(info);
}
IsReadOnly = true;
}
}
}
this.RaiseListChangedEvents = true;
}
#endregion
}
}

View File

@@ -0,0 +1,228 @@
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Validation;
namespace ProjectTracker.Library
{
[Serializable()]
public class ProjectResource :
BusinessBase<ProjectResource>, IHoldRoles
{
#region Business Methods
private int _resourceId;
private string _firstName = string.Empty;
private string _lastName = string.Empty;
private SmartDate _assigned;
private int _role;
private byte[] _timestamp = new byte[8];
[System.ComponentModel.DataObjectField(false, true)]
public int ResourceId
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty(true);
return _resourceId;
}
}
public string FirstName
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty(true);
return _firstName;
}
}
public string LastName
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty(true);
return _lastName;
}
}
public string FullName
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
if (CanReadProperty("FirstName") &&
CanReadProperty("LastName"))
return string.Format("{0}, {1}", LastName, FirstName);
else
throw new System.Security.SecurityException(
"Property read not allowed");
}
}
public string Assigned
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty(true);
return _assigned.Text;
}
}
public int Role
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty(true);
return _role;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
set
{
CanWriteProperty(true);
if (!_role.Equals(value))
{
_role = value;
PropertyHasChanged();
}
}
}
public Resource GetResource()
{
return Resource.GetResource(_resourceId);
}
protected override object GetIdValue()
{
return _resourceId;
}
#endregion
#region Validation Rules
protected override void AddBusinessRules()
{
ValidationRules.AddRule(
new Csla.Validation.RuleHandler(
Assignment.ValidRole), "Role");
}
#endregion
#region Authorization Rules
protected override void AddAuthorizationRules()
{
AuthorizationRules.AllowWrite(
"Role", "ProjectManager");
}
#endregion
#region Factory Methods
internal static ProjectResource NewProjectResource(int resourceId)
{
return new ProjectResource(
Resource.GetResource(resourceId),
RoleList.DefaultRole());
}
internal static ProjectResource GetResource(SafeDataReader dr)
{
return new ProjectResource(dr);
}
private ProjectResource()
{
MarkAsChild();
}
private ProjectResource(Resource resource, int role)
{
MarkAsChild();
_resourceId = resource.Id;
_lastName = resource.LastName;
_firstName = resource.FirstName;
_assigned.Date = Assignment.GetDefaultAssignedDate();
_role = role;
}
private ProjectResource(SafeDataReader dr)
{
MarkAsChild();
Fetch(dr);
}
#endregion
#region Data Access
private void Fetch(SafeDataReader dr)
{
_resourceId = dr.GetInt32("ResourceId");
_lastName = dr.GetString("LastName");
_firstName = dr.GetString("FirstName");
_assigned = dr.GetSmartDate("Assigned");
_role = dr.GetInt32("Role");
dr.GetBytes("LastChanged", 0, _timestamp, 0, 8);
MarkOld();
}
internal void Insert(Project project)
{
// if we're not dirty then don't update the database
if (!this.IsDirty) return;
using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
{
cn.Open();
_timestamp = Assignment.AddAssignment(
cn, project.Id, _resourceId, _assigned, _role);
MarkOld();
}
}
internal void Update(Project project)
{
// if we're not dirty then don't update the database
if (!this.IsDirty) return;
using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
{
cn.Open();
_timestamp = Assignment.UpdateAssignment(
cn, project.Id, _resourceId, _assigned, _role, _timestamp);
MarkOld();
}
}
internal void DeleteSelf(Project project)
{
// if we're not dirty then don't update the database
if (!this.IsDirty) return;
// if we're new then don't update the database
if (this.IsNew) return;
using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
{
cn.Open();
Assignment.RemoveAssignment(cn, project.Id, _resourceId);
MarkNew();
}
}
#endregion
}
}

View File

@@ -0,0 +1,125 @@
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
namespace ProjectTracker.Library
{
[Serializable()]
public class ProjectResources :
BusinessListBase<ProjectResources, ProjectResource>
{
#region Business Methods
public ProjectResource GetItem(int resourceId)
{
foreach (ProjectResource res in this)
if (res.ResourceId == resourceId)
return res;
return null;
}
public void Assign(int resourceId)
{
if (!Contains(resourceId))
{
ProjectResource resource =
ProjectResource.NewProjectResource(resourceId);
this.Add(resource);
}
else
throw new InvalidOperationException(
"Resource already assigned to project");
}
public void Remove(int resourceId)
{
foreach (ProjectResource res in this)
{
if (res.ResourceId == resourceId)
{
Remove(res);
break;
}
}
}
public bool Contains(int resourceId)
{
foreach (ProjectResource res in this)
if (res.ResourceId == resourceId)
return true;
return false;
}
public bool ContainsDeleted(int resourceId)
{
foreach (ProjectResource res in DeletedList)
if (res.ResourceId == resourceId)
return true;
return false;
}
#endregion
#region Factory Methods
internal static ProjectResources NewProjectResources()
{
return new ProjectResources();
}
internal static ProjectResources GetProjectResources(SafeDataReader dr)
{
return new ProjectResources(dr);
}
private ProjectResources()
{
MarkAsChild();
}
private ProjectResources(SafeDataReader dr)
{
MarkAsChild();
Fetch(dr);
}
#endregion
#region Data Access
// called to load data from the database
private void Fetch(SafeDataReader dr)
{
this.RaiseListChangedEvents = false;
while (dr.Read())
this.Add(ProjectResource.GetResource(dr));
this.RaiseListChangedEvents = true;
}
internal void Update(Project project)
{
this.RaiseListChangedEvents = false;
// update (thus deleting) any deleted child objects
foreach (ProjectResource obj in DeletedList)
obj.DeleteSelf(project);
// now that they are deleted, remove them from memory too
DeletedList.Clear();
// add/update any current child objects
foreach (ProjectResource obj in this)
{
if (obj.IsNew)
obj.Insert(project);
else
obj.Update(project);
}
this.RaiseListChangedEvents = true;
}
#endregion
}
}

View File

@@ -0,0 +1,74 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{366FD3E8-E0AA-40CF-99ED-BB2966E30C96}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ProjectTracker.Library</RootNamespace>
<AssemblyName>ProjectTracker.Library</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Csla, Version=2.0.0.0, Culture=neutral, PublicKeyToken=93be5fdc093e4c30, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\csla20cs\Csla\bin\Debug\Csla.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Admin\Role.cs" />
<Compile Include="Admin\Roles.cs" />
<Compile Include="Assignment.cs" />
<Compile Include="Database.cs" />
<Compile Include="Project.cs" />
<Compile Include="ProjectInfo.cs" />
<Compile Include="ProjectList.cs" />
<Compile Include="ProjectResource.cs" />
<Compile Include="ProjectResources.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Resource.cs" />
<Compile Include="ResourceAssignment.cs" />
<Compile Include="ResourceAssignments.cs" />
<Compile Include="ResourceInfo.cs" />
<Compile Include="ResourceList.cs" />
<Compile Include="RoleList.cs" />
<Compile Include="Security\PTIdentity.cs" />
<Compile Include="Security\PTPrincipal.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Diagrams\BaseClasses.cd" />
<None Include="Diagrams\EditableObjects.cd" />
<None Include="Diagrams\ReadOnlyObjects.cd" />
<None Include="Diagrams\Security.cd" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ProjectTracker.Library")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Rockford Lhotka")]
[assembly: AssemblyProduct("ProjectTracker")]
[assembly: AssemblyCopyright("Copyright © Rockford Lhotka 2006")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("52a894db-5427-450c-9924-b11ce791853a")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -0,0 +1,395 @@
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Validation;
namespace ProjectTracker.Library
{
[Serializable()]
public class Resource : BusinessBase<Resource>
{
#region Business Methods
private int _id;
private string _lastName = string.Empty;
private string _firstName = string.Empty;
private byte[] _timestamp = new byte[8];
private ResourceAssignments _assignments = ResourceAssignments.NewResourceAssignments();
[System.ComponentModel.DataObjectField(true, true)]
public int Id
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty(true);
return _id;
}
}
public string LastName
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty(true);
return _lastName;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
set
{
CanWriteProperty(true);
if (value == null) value = string.Empty;
if (_lastName != value)
{
_lastName = value;
PropertyHasChanged();
}
}
}
public string FirstName
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty(true);
return _firstName;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
set
{
CanWriteProperty();
if (value == null) value = string.Empty;
if (_firstName != value)
{
_firstName = value;
PropertyHasChanged();
}
}
}
public string FullName
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
if (CanReadProperty("FirstName") && CanReadProperty("LastName"))
return string.Format("{0}, {1}", _lastName, _firstName);
else
throw new System.Security.SecurityException("Property read not allowed");
}
}
public ResourceAssignments Assignments
{
get { return _assignments; }
}
public override bool IsValid
{
get { return base.IsValid && _assignments.IsValid; }
}
public override bool IsDirty
{
get { return base.IsDirty || _assignments.IsDirty; }
}
protected override object GetIdValue()
{
return _id;
}
#endregion
#region Validation Rules
protected override void AddBusinessRules()
{
ValidationRules.AddRule(new RuleHandler(CommonRules.StringRequired), "FirstName");
ValidationRules.AddRule(new RuleHandler(CommonRules.StringMaxLength),
new CommonRules.MaxLengthRuleArgs("FirstName", 50));
ValidationRules.AddRule(new RuleHandler(CommonRules.StringMaxLength),
new CommonRules.MaxLengthRuleArgs("LastName", 50));
}
#endregion
#region Authorization Rules
protected override void AddAuthorizationRules()
{
// add AuthorizationRules here
AuthorizationRules.AllowWrite("LastName", "ProjectManager");
AuthorizationRules.AllowWrite("FirstName", "ProjectManager");
}
public static bool CanAddObject()
{
return Csla.ApplicationContext.User.IsInRole("ProjectManager");
}
public static bool CanGetObject()
{
return true;
}
public static bool CanDeleteObject()
{
bool result = false;
if (Csla.ApplicationContext.User.IsInRole("ProjectManager"))
result = true;
if (Csla.ApplicationContext.User.IsInRole("Administrator"))
result = true;
return result;
}
public static bool CanEditObject()
{
return Csla.ApplicationContext.User.IsInRole("ProjectManager");
}
#endregion
#region Factory Methods
public static Resource NewResource()
{
if (!CanAddObject())
throw new System.Security.SecurityException(
"User not authorized to add a resource");
return DataPortal.Create<Resource>();
}
public static void DeleteResource(int id)
{
if (!CanDeleteObject())
throw new System.Security.SecurityException(
"User not authorized to remove a resource");
DataPortal.Delete(new Criteria(id));
}
public static Resource GetResource(int id)
{
if (!CanGetObject())
throw new System.Security.SecurityException(
"User not authorized to view a resource");
return DataPortal.Fetch<Resource>(new Criteria(id));
}
public override Resource Save()
{
if (IsDeleted && !CanDeleteObject())
throw new System.Security.SecurityException(
"User not authorized to remove a resource");
else if (IsNew && !CanAddObject())
throw new System.Security.SecurityException(
"User not authorized to add a resource");
else if (!CanEditObject())
throw new System.Security.SecurityException(
"User not authorized to update a resource");
return base.Save();
}
private Resource()
{ /* require use of factory methods */ }
#endregion
#region Data Access
[Serializable()]
private class Criteria
{
private int _id;
public int Id
{
get { return _id; }
}
public Criteria(int id)
{ _id = id; }
}
[RunLocal()]
protected override void DataPortal_Create()
{
// nothing to initialize
ValidationRules.CheckRules();
}
private void DataPortal_Fetch(Criteria criteria)
{
using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
{
cn.Open();
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "getResource";
cm.Parameters.AddWithValue("@id", criteria.Id);
using (SafeDataReader dr =
new SafeDataReader(cm.ExecuteReader()))
{
dr.Read();
_id = dr.GetInt32("Id");
_lastName = dr.GetString("LastName");
_firstName = dr.GetString("FirstName");
dr.GetBytes("LastChanged", 0, _timestamp, 0, 8);
// load child objects
dr.NextResult();
_assignments =
ResourceAssignments.GetResourceAssignments(dr);
}
}
}
}
[Transactional(TransactionalTypes.TransactionScope)]
protected override void DataPortal_Insert()
{
using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
{
cn.Open();
ApplicationContext.LocalContext["cn"] = cn;
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "addResource";
cm.Parameters.AddWithValue("@lastName", _lastName);
cm.Parameters.AddWithValue("@firstName", _firstName);
SqlParameter param =
new SqlParameter("@newId",SqlDbType.Int);
param.Direction = ParameterDirection.Output;
cm.Parameters.Add(param);
param = new SqlParameter("@newLastChanged", SqlDbType.Timestamp);
param.Direction = ParameterDirection.Output;
cm.Parameters.Add(param);
cm.ExecuteNonQuery();
_id = (int)cm.Parameters["@newId"].Value;
_timestamp = (byte[])cm.Parameters["@newLastChanged"].Value;
}
// update child objects
_assignments.Update(this);
// removing of item only needed for local data portal
if (ApplicationContext.ExecutionLocation==ApplicationContext.ExecutionLocations.Client)
ApplicationContext.LocalContext.Remove("cn");
}
}
[Transactional(TransactionalTypes.TransactionScope)]
protected override void DataPortal_Update()
{
using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
{
cn.Open();
ApplicationContext.LocalContext["cn"] = cn;
if (base.IsDirty)
{
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "updateResource";
cm.Parameters.AddWithValue("@id", _id);
cm.Parameters.AddWithValue("@lastName", _lastName);
cm.Parameters.AddWithValue("@firstName", _firstName);
cm.Parameters.AddWithValue("@lastChanged", _timestamp);
SqlParameter param =
new SqlParameter("@newLastChanged", SqlDbType.Timestamp);
param.Direction = ParameterDirection.Output;
cm.Parameters.Add(param);
cm.ExecuteNonQuery();
_timestamp = (byte[])cm.Parameters["@newLastChanged"].Value;
}
}
// update child objects
_assignments.Update(this);
// removing of item only needed for local data portal
if (ApplicationContext.ExecutionLocation == ApplicationContext.ExecutionLocations.Client)
ApplicationContext.LocalContext.Remove("cn");
}
}
[Transactional(TransactionalTypes.TransactionScope)]
protected override void DataPortal_DeleteSelf()
{
DataPortal_Delete(new Criteria(_id));
}
[Transactional(TransactionalTypes.TransactionScope)]
private void DataPortal_Delete(Criteria criteria)
{
using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
{
cn.Open();
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "deleteResource";
cm.Parameters.AddWithValue("@id", criteria.Id);
cm.ExecuteNonQuery();
}
}
}
#endregion
#region Exists
public static bool Exists(string id)
{
ExistsCommand result;
result = DataPortal.Execute<ExistsCommand>(new ExistsCommand(id));
return result.Exists;
}
[Serializable()]
private class ExistsCommand : CommandBase
{
private string _id;
private bool _exists;
public bool Exists
{
get { return _exists; }
}
public ExistsCommand(string id)
{
_id = id;
}
protected override void DataPortal_Execute()
{
using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
{
cn.Open();
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "existsResource";
cm.Parameters.AddWithValue("@id", _id);
int count = (int)cm.ExecuteScalar();
_exists = (count > 0);
}
}
}
}
#endregion
}
}

View File

@@ -0,0 +1,192 @@
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
namespace ProjectTracker.Library
{
[Serializable()]
public class ResourceAssignment : BusinessBase<ResourceAssignment>, IHoldRoles
{
#region Business Methods
private Guid _projectId = Guid.Empty;
private string _projectName = string.Empty;
private SmartDate _assigned = new SmartDate(DateTime.Today);
private int _role;
private byte[] _timestamp = new byte[8];
public Guid ProjectId
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty(true);
return _projectId;
}
}
public string ProjectName
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty(true);
return _projectName;
}
}
public string Assigned
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty(true);
return _assigned.Text;
}
}
public int Role
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
get
{
CanReadProperty(true);
return _role;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
set
{
CanWriteProperty(true);
if (!_role.Equals(value))
{
_role = value;
PropertyHasChanged();
}
}
}
public Project GetProject()
{
return Project.GetProject(_projectId);
}
protected override object GetIdValue()
{
return _projectId;
}
#endregion
#region Validation Rules
protected override void AddBusinessRules()
{
ValidationRules.AddRule(
new Csla.Validation.RuleHandler(
Assignment.ValidRole), "Role");
}
#endregion
#region Authorization Rules
protected override void AddAuthorizationRules()
{
AuthorizationRules.AllowWrite(
"Role", "ProjectManager");
}
#endregion
#region Factory Methods
internal static ResourceAssignment NewResourceAssignment(
Guid projectId)
{
return new ResourceAssignment(
Project.GetProject(projectId), RoleList.DefaultRole());
}
internal static ResourceAssignment GetResourceAssignment(
SafeDataReader dr)
{
return new ResourceAssignment(dr);
}
private ResourceAssignment()
{ MarkAsChild(); }
#endregion
#region Data Access
/// <summary>
/// Called to when a new object is created.
/// </summary>
private ResourceAssignment(Project project, int role)
{
MarkAsChild();
_projectId = project.Id;
_projectName = project.Name;
_assigned.Date = Assignment.GetDefaultAssignedDate();
_role = role;
}
/// <summary>
/// Called when loading data from the database.
/// </summary>
private ResourceAssignment(SafeDataReader dr)
{
MarkAsChild();
_projectId = dr.GetGuid("ProjectId");
_projectName = dr.GetString("Name");
_assigned = dr.GetSmartDate("Assigned");
_role = dr.GetInt32("Role");
dr.GetBytes("LastChanged", 0, _timestamp, 0, 8);
MarkOld();
}
internal void Insert(Resource resource)
{
SqlConnection cn = (SqlConnection)ApplicationContext.LocalContext["cn"];
// if we're not dirty then don't update the database
if (!this.IsDirty) return;
_timestamp = Assignment.AddAssignment(
cn, _projectId, resource.Id, _assigned, _role);
MarkOld();
}
internal void Update(Resource resource)
{
SqlConnection cn = (SqlConnection)ApplicationContext.LocalContext["cn"];
// if we're not dirty then don't update the database
if (!this.IsDirty) return;
_timestamp = Assignment.UpdateAssignment(
cn, _projectId, resource.Id, _assigned, _role, _timestamp);
MarkOld();
}
internal void DeleteSelf(Resource resource)
{
SqlConnection cn = (SqlConnection)ApplicationContext.LocalContext["cn"];
// if we're not dirty then don't update the database
if (!this.IsDirty) return;
// if we're new then don't update the database
if (this.IsNew) return;
Assignment.RemoveAssignment(cn, _projectId, resource.Id);
MarkNew();
}
#endregion
}
}

View File

@@ -0,0 +1,123 @@
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
namespace ProjectTracker.Library
{
[Serializable()]
public class ResourceAssignments : BusinessListBase<ResourceAssignments, ResourceAssignment>
{
#region Business Methods
public ResourceAssignment this[Guid projectId]
{
get
{
foreach (ResourceAssignment res in this)
if (res.ProjectId.Equals(projectId))
return res;
return null;
}
}
public void AssignTo(Guid projectId)
{
if (!Contains(projectId))
{
ResourceAssignment project = ResourceAssignment.NewResourceAssignment(projectId);
this.Add(project);
}
else
throw new InvalidOperationException("Resource already assigned to project");
}
public void Remove(Guid projectId)
{
foreach (ResourceAssignment res in this)
{
if (res.ProjectId.Equals(projectId))
{
Remove(res);
break;
}
}
}
public bool Contains(Guid projectId)
{
foreach (ResourceAssignment project in this)
if (project.ProjectId == projectId)
return true;
return false;
}
public bool ContainsDeleted(Guid projectId)
{
foreach (ResourceAssignment project in DeletedList)
if (project.ProjectId == projectId)
return true;
return false;
}
#endregion
#region Factory Methods
internal static ResourceAssignments NewResourceAssignments()
{
return new ResourceAssignments();
}
internal static ResourceAssignments GetResourceAssignments(SafeDataReader dr)
{
return new ResourceAssignments(dr);
}
private ResourceAssignments()
{
MarkAsChild();
}
private ResourceAssignments(SafeDataReader dr)
{
MarkAsChild();
Fetch(dr);
}
#endregion
#region Data Access
private void Fetch(SafeDataReader dr)
{
RaiseListChangedEvents = false;
while (dr.Read())
this.Add(ResourceAssignment.GetResourceAssignment(dr));
RaiseListChangedEvents = true;
}
internal void Update(Resource resource)
{
RaiseListChangedEvents = false;
// update (thus deleting) any deleted child objects
foreach (ResourceAssignment item in DeletedList)
item.DeleteSelf(resource);
// now that they are deleted, remove them from memory too
DeletedList.Clear();
// add/update any current child objects
foreach (ResourceAssignment item in this)
{
if (item.IsNew)
item.Insert(resource);
else
item.Update(resource);
}
RaiseListChangedEvents = true;
}
#endregion
}
}

View File

@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
namespace ProjectTracker.Library
{
[Serializable()]
public class ResourceInfo :
ReadOnlyBase<ResourceInfo>
{
#region Business Methods
private int _id;
private string _name;
public int Id
{
get { return _id; }
}
public string Name
{
get { return _name; }
}
protected override object GetIdValue()
{
return _id;
}
public override string ToString()
{
return _name;
}
#endregion
#region Constructors
private ResourceInfo()
{ /* require use of factory methods */ }
internal ResourceInfo(SafeDataReader dr)
{
_id = dr.GetInt32("Id");
_name = string.Format("{0}, {1}",
dr.GetString("LastName"),
dr.GetString("FirstName"));
}
#endregion
}
}

View File

@@ -0,0 +1,61 @@
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
namespace ProjectTracker.Library
{
[Serializable()]
public class ResourceList :
ReadOnlyListBase<ResourceList, ResourceInfo>
{
#region Factory Methods
public static ResourceList GetResourceList()
{
return DataPortal.Fetch<ResourceList>(new Criteria());
}
private ResourceList()
{ /* require use of factory methods */ }
#endregion
#region Data Access
[Serializable()]
private class Criteria
{ /* no criteria - retrieve all resources */ }
private void DataPortal_Fetch(Criteria criteria)
{
this.RaiseListChangedEvents = false;
using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
{
cn.Open();
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "getResources";
using (SafeDataReader dr =
new SafeDataReader(cm.ExecuteReader()))
{
IsReadOnly = false;
while (dr.Read())
{
ResourceInfo info = new ResourceInfo(dr);
this.Add(info);
}
IsReadOnly = true;
}
}
}
this.RaiseListChangedEvents = true;
}
#endregion
}
}

View File

@@ -0,0 +1,88 @@
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
namespace ProjectTracker.Library
{
[Serializable()]
public class RoleList :
NameValueListBase<int, string>
{
#region Business Methods
public static int DefaultRole()
{
RoleList list = GetList();
if (list.Count > 0)
return list.Items[0].Key;
else
throw new NullReferenceException(
"No roles available; default role can not be returned");
}
#endregion
#region Factory Methods
private static RoleList _list;
/// <summary>
/// Returns a list of roles.
/// </summary>
public static RoleList GetList()
{
if (_list == null)
_list = DataPortal.Fetch<RoleList>
(new Criteria(typeof(RoleList)));
return _list;
}
/// <summary>
/// Clears the in-memory RoleList cache
/// so the list of roles is reloaded on
/// next request.
/// </summary>
public static void InvalidateCache()
{
_list = null;
}
private RoleList()
{ /* require use of factory methods */ }
#endregion
#region Data Access
private void DataPortal_Fetch(Criteria criteria)
{
this.RaiseListChangedEvents = false;
using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
{
cn.Open();
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "getRoles";
using (SafeDataReader dr =
new SafeDataReader(cm.ExecuteReader()))
{
IsReadOnly = false;
while (dr.Read())
{
this.Add(new NameValuePair(
dr.GetInt32("id"), dr.GetString("name")));
}
IsReadOnly = true;
}
}
}
this.RaiseListChangedEvents = true;
}
#endregion
}
}

View File

@@ -0,0 +1,128 @@
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Security.Principal;
using Csla;
namespace ProjectTracker.Library.Security
{
[Serializable()]
public class PTIdentity :
ReadOnlyBase<PTIdentity>, IIdentity
{
#region Business Methods
private bool _isAuthenticated;
private string _name = string.Empty;
private List<string> _roles = new List<string>();
public string AuthenticationType
{
get { return "Csla"; }
}
public bool IsAuthenticated
{
get { return _isAuthenticated; }
}
public string Name
{
get { return _name; }
}
protected override object GetIdValue()
{
return _name;
}
internal bool IsInRole(string role)
{
return _roles.Contains(role);
}
#endregion
#region Factory Methods
internal static PTIdentity UnauthenticatedIdentity()
{
return new PTIdentity();
}
internal static PTIdentity GetIdentity(
string username, string password)
{
return DataPortal.Fetch<PTIdentity>
(new Criteria(username, password));
}
private PTIdentity()
{ /* require use of factory methods */ }
#endregion
#region Data Access
[Serializable()]
private class Criteria
{
private string _username;
public string Username
{
get { return _username; }
}
private string _password;
public string Password
{
get { return _password; }
}
public Criteria(string username, string password)
{
_username = username;
_password = password;
}
}
private void DataPortal_Fetch(Criteria criteria)
{
using (SqlConnection cn =
new SqlConnection(Database.SecurityConnection))
{
cn.Open();
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandText = "Login";
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.AddWithValue("@user", criteria.Username);
cm.Parameters.AddWithValue("@pw", criteria.Password);
using (SqlDataReader dr = cm.ExecuteReader())
{
if (dr.Read())
{
_name = criteria.Username;
_isAuthenticated = true;
if (dr.NextResult())
{
while (dr.Read())
{
_roles.Add(dr.GetString(0));
}
}
}
else
{
_name = string.Empty;
_isAuthenticated = false;
_roles.Clear();
}
}
}
}
}
#endregion
}
}

View File

@@ -0,0 +1,39 @@
using System;
using System.Security.Principal;
namespace ProjectTracker.Library.Security
{
[Serializable()]
public class PTPrincipal : Csla.Security.BusinessPrincipalBase
{
private PTPrincipal(IIdentity identity)
: base(identity) { }
public static bool Login(string username, string password)
{
PTIdentity identity =
PTIdentity.GetIdentity(username, password);
if (identity.IsAuthenticated)
{
PTPrincipal principal = new PTPrincipal(identity);
Csla.ApplicationContext.User = principal;
}
return identity.IsAuthenticated;
}
public static void Logout()
{
PTIdentity identity = PTIdentity.UnauthenticatedIdentity();
PTPrincipal principal = new PTPrincipal(identity);
Csla.ApplicationContext.User = principal;
}
public override bool IsInRole(string role)
{
PTIdentity identity = (PTIdentity)this.Identity;
return identity.IsInRole(role);
}
}
}