Skip to content

Commit

Permalink
SetDisabled method tweaked to also update LastModifiedDate
Browse files Browse the repository at this point in the history
  • Loading branch information
smbadiwe committed Apr 28, 2017
1 parent dff0d0e commit 4469cf2
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion MultiTenancyFramework.Core/Data/ICoreDAO.General.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public interface ICoreGeneralDAO
void RunDirectQuery(string query, bool clearSession = false);

/// <summary>
/// Run a query using ADO.NET
/// Run a query using ADO.NET.
/// </summary>
/// <param name="query">The query to run.</param>
void RunDirectQueryADODotNET(string query, bool closeConnection = false);
Expand Down
9 changes: 9 additions & 0 deletions MultiTenancyFramework.Core/Entities/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ public abstract class Entity<idT> : IEntity<idT> where idT : IEquatable<idT>

public virtual bool IsEnabled { get { return !IsDisabled; } }

/// <summary>
/// This default implementation returns the plural of the type name using the string extension method .ToPlural() in MultiTenancyFramework namespace
/// </summary>
/// <returns></returns>
public virtual string GetTableName()
{
return GetType().Name.ToPlural();
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
Expand Down
6 changes: 6 additions & 0 deletions MultiTenancyFramework.Core/Entities/IEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,11 @@ public interface IEntity<idT> where idT : IEquatable<idT>
/// The user ID of the user that saved the entity
/// </summary>
idT CreatedBy { get; set; }

/// <summary>
/// The default implementation returns the plural of the type name using the string extension method .ToPlural() in MultiTenancyFramework namespace
/// </summary>
/// <returns></returns>
string GetTableName();
}
}
5 changes: 5 additions & 0 deletions MultiTenancyFramework.Core/Entities/UserClaim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,10 @@ public UserClaim(string type, string value) : base(type, value)
public virtual bool IsEnabled { get { return !IsDisabled; } }

public virtual long CreatedBy { get; set; }

public virtual string GetTableName()
{
return GetType().Name.ToPlural();
}
}
}
4 changes: 4 additions & 0 deletions MultiTenancyFramework.Core/Exceptions/GeneralException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public override string Message
return base.Message;
}
}
public override Exception GetBaseException()
{
return this;
}

public ExceptionType ExceptionType { get; set; }
public GeneralException(string message, ExceptionType exceptionType = ExceptionType.InvalidUserActionOrInput) : base(message)
Expand Down
2 changes: 2 additions & 0 deletions MultiTenancyFramework.Core/Extensions/EntityExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using MultiTenancyFramework.Data;
using MultiTenancyFramework.Entities;
using System;
using System.Linq;

namespace MultiTenancyFramework
Expand Down
27 changes: 27 additions & 0 deletions MultiTenancyFramework.Core/Logic/CoreBaseLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,32 @@ public void Refresh(T entity)
_dao.Refresh(entity);
}

/// <summary>
/// This uses direct non-NHibernate query to set an entity to enabled or disabled
/// </summary>
/// <param name="entityId">The entity id.</param>
/// <param name="disabled">If true, entity.IsDisabled will be set to true; otherwise, false</param>
/// <param name="tableName">The table name. If not supplied, the plural of the class name will be used</param>
/// <returns>True if action is successful. Otherwise, false</returns>
public bool SetDisabled(long entityId, bool disabled, string tableName = null)
{
_dao.InstitutionCode = _institutionCode;
_dao.EntityName = EntityName;
if (string.IsNullOrWhiteSpace(tableName))
{
tableName = typeof(T).Name.ToPlural();
}
try
{
_dao.RunDirectQueryADODotNET(string.Format("UPDATE {0} SET IsDisabled = {1}, LastDateModified = CURRENT_TIMESTAMP WHERE Id = {2}", tableName, disabled, entityId));
return true;
}
catch (Exception ex)
{
Utilities.Logger.Log(new GeneralException($"Failed updating record {entityId} in table: {tableName}", ex, ExceptionType.DatabaseRelated));
return false;
}
}

}
}
6 changes: 5 additions & 1 deletion MultiTenancyFramework.NHibernate/CoreGeneralDAO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ public void RunDirectQuery(string query, bool clearSession = false)
session.CreateSQLQuery(query);
}

public void RunDirectQueryADODotNET(string query, bool closeConnection = false)
/// <summary>
/// Run a query using ADO.NET. Default implementation supports SQL Server and MySql
/// </summary>
/// <param name="query">The query to run.</param>
public virtual void RunDirectQueryADODotNET(string query, bool closeConnection = false)
{
var session = BuildSession();
var connection = session.Connection;
Expand Down

0 comments on commit 4469cf2

Please sign in to comment.