Files
ayanova7/source/bizobjects/AyaLib/GZTW.AyaNova.BLL/ObjectCustomField.cs
2018-06-29 19:47:36 +00:00

411 lines
8.7 KiB
C#

///////////////////////////////////////////////////////////
// ObjectCustomField.cs
// Implementation of Class ObjectCustomField
// CSLA type: Editable Child
// Created on: 12-"Rocktober"-2004
// Object design: John
// Coded: John 12-"Rocktober"-2004
///////////////////////////////////////////////////////////
using System;
using System.Data;
using CSLA.Data;
using GZTW.Data;
using CSLA;
using System.Threading;
using CSLA.Security;
namespace GZTW.AyaNova.BLL
{
/// <summary>
/// Used by interface to determine how to display and accept values entered in custom fields
///
/// </summary>
[Serializable]
public class ObjectCustomField : BusinessBase
{
#region Attributes
private SmartDate mCreated;
private SmartDate mModified;
private Guid mCreator;
private Guid mModifier;
private string mObjectName="";
private string mFieldName="";
//private string mDisplayName="";
private bool mVisible=false;
private FormFieldDataTypes mFieldType=FormFieldDataTypes.Text;
//FUTURE: Left this out now because it would raise questions
//about why other built-in fields can't be made required,
//perhaps that's coming in future
//if true can't be left empty
//private bool mRequired=false;
#endregion
#region Constructor
/// <summary>
/// Private constructor to prevent direct instantiation
/// </summary>
private ObjectCustomField()
{
//Set as child object
MarkAsChild();
//Set record history to defaults
mCreated = new SmartDate(DBUtil.CurrentWorkingDateTime);
mModified=new SmartDate();
mCreator=Guid.Empty;
mModifier=Guid.Empty;
}
#endregion
#region Business properties
/// <summary>
/// Get created date
///
///
/// </summary>
public string Created
{
get
{
return mCreated.ToString();
}
}
/// <summary>
/// Get modified date
///
///
/// </summary>
public string Modified
{
get
{
return mModified.ToString();
}
}
/// <summary>
/// Get user record ID of person who created this record
///
///
/// </summary>
public Guid Creator
{
get
{
return mCreator;
}
}
/// <summary>
/// Get user ID of person who modified this record
///
///
/// </summary>
public Guid Modifier
{
get
{
return mModifier;
}
}
/// <summary>
/// ObjectName
/// </summary>
public string ObjectName
{
get
{
return mObjectName;
}
set
{
if(mObjectName!=value)
{
mObjectName=value;
MarkDirty();
}
}
}
/// <summary>
/// FieldName
/// </summary>
public string FieldName
{
get
{
return mFieldName;
}
set
{
if(mFieldName!=value)
{
mFieldName=value;
MarkDirty();
}
}
}
// /// <summary>
// /// DisplayName
// /// </summary>
// public string DisplayName
// {
// get
// {
// return mDisplayName;
// }
// set
// {
// if(mDisplayName!=value)
// {
// mDisplayName=value;
// MarkDirty();
// }
// }
// }
/// <summary>
/// Visible
/// </summary>
public bool Visible
{
get
{
return mVisible;
}
set
{
if(mVisible!=value)
{
mVisible=value;
MarkDirty();
}
}
}
/// <summary>
/// FieldType
/// </summary>
public FormFieldDataTypes FieldType
{
get
{
return mFieldType;
}
set
{
if(mFieldType!=value)
{
mFieldType=value;
MarkDirty();
}
}
}
#endregion
#region System.Object overrides
/// <summary>
///
/// </summary>
/// <returns></returns>
public override string ToString()
{
return "ObjectCustomField"+ mObjectName + mFieldName ;
}
/// <summary>
///
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(Object obj)
{
if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false;
ObjectCustomField c=(ObjectCustomField)obj;
return ((mObjectName==c.mObjectName) && (mFieldName==c.mFieldName));
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return ("ObjectCustomField" + mObjectName + mFieldName).GetHashCode();
}
#endregion
#region Static methods
/// <summary>
/// Get new object
/// </summary>
/// <returns></returns>
internal static ObjectCustomField NewItem(string sObjectName, string sFieldName)
{
ObjectCustomField child=new ObjectCustomField();
child.mObjectName=sObjectName;
child.mFieldName=sFieldName;
//child.mDisplayName=sFieldName;
return child;
}
/// <summary>
/// GetItem
/// </summary>
/// <param name="dr"></param>
/// <returns></returns>
internal static ObjectCustomField GetItem(SafeDataReader dr)
{
ObjectCustomField child = new ObjectCustomField();
child.Fetch(dr);
return child;
}
#endregion
#region DAL DATA ACCESS
/// <summary>
/// Fetch
/// </summary>
/// <param name="dr"></param>
private void Fetch(SafeDataReader dr)
{
//ObjectCustomField fields
//Standard fields
mCreated=DBUtil.ToLocal(dr.GetSmartDate("aCreated"));
mModified=DBUtil.ToLocal(dr.GetSmartDate("aModified"));
mCreator=dr.GetGuid("aCreator");
mModifier=dr.GetGuid("aModifier");
mObjectName=dr.GetString("aObjectName");
mFieldName=dr.GetString("aFieldName");
//mDisplayName=dr.GetString("aDisplayName");
mVisible=dr.GetBoolean("aVisible");
mFieldType=(FormFieldDataTypes)dr.GetInt16("aFieldType");
MarkOld();
}
/// <summary>
/// Update
/// </summary>
/// <param name="tr"></param>
internal void Update(IDbTransaction tr)
{
//No need to update if there is nothing changed
if(!this.IsDirty) return;
//DELIBERATELY SKIPPING CONCURRENCY CHECK
//PROBABLY NOT NEEDED AND DIDN"T WANT TO WRITE A CUSTOM ONE
//DUE TO LACK OF GUID ID FIELD (IN FUTURE MUST...MUST...MUST ENSURE EVERY oBJECT HAS A CUSTOM ID FIELD
// If not a new record, check if record was modified
//by another user since original retrieval:
// if(!IsNew)
// DBUtil.CheckSafeToUpdate(this.mModified.Date,this.mID,"ClientNote");
#region Delete
if(IsDeleted)
{
if(!IsNew)
{
//Delete object and child objects
DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL(
"DELETE FROM aObjectCustomField WHERE aObjectName=@ObjectName " +
"AND aFieldName=@FieldName");
cmDelete.AddInParameter("@ObjectName",DbType.String,mObjectName);
cmDelete.AddInParameter("@FieldName",DbType.String,mFieldName);
DBUtil.DB.ExecuteNonQuery(cmDelete, tr);
//-----------------------------
}
MarkNew();
return;
}
#endregion
#region Add / Update
//get modification time temporarily, if update succeeds then
//set to this time
System.DateTime dtModified = DBUtil.CurrentWorkingDateTime;
DBCommandWrapper cm = null;
if(IsNew)//Add or update?
cm=DBUtil.GetCommandFromSQL(
"INSERT INTO aObjectCustomField (aObjectname, aFieldName, " +
"aVisible, aFieldType, aCreated,aModified, aCreator,aModifier) " +
"VALUES (@ObjectName,@FieldName,@Visible,@FieldType, " +
"@Created,@Modified, @CurrentUserID,@CurrentUserID)"
);
else
cm=DBUtil.GetCommandFromSQL(
"UPDATE aObjectCustomField SET aObjectName=@ObjectName, " +
"aFieldName=@FieldName, aVisible=@Visible, aFieldType=@FieldType, " +
"aModifier=@CurrentUserID, aModified=@Modified " +
"WHERE aObjectName=@ObjectName AND " +
"aFieldName=@FieldName"
);
//ObjectCustomFields fields
cm.AddInParameter("@ObjectName",DbType.String,mObjectName);
cm.AddInParameter("@FieldName",DbType.String,mFieldName);
cm.AddInParameter("@Visible",DbType.Boolean,mVisible);
cm.AddInParameter("@FieldType",DbType.Int16,(int)mFieldType);
//Standard fields
cm.AddInParameter("@CurrentUserID",DbType.Guid, CurrentUserID);
cm.AddInParameter("@Created",DbType.DateTime, DBUtil.ToUTC(mCreated.Date));
cm.AddInParameter("@Modified",DbType.DateTime, DBUtil.ToUTC(dtModified));
DBUtil.DB.ExecuteNonQuery(cm, tr);
MarkOld();//db is now synched with object
//Successful update so
//change modification time to match
this.mModified.Date=dtModified;
#endregion
}
#endregion
}//end ObjectCustomField
}//end namespace GZTW.AyaNova.BLL