512 lines
12 KiB
C#
512 lines
12 KiB
C#
///////////////////////////////////////////////////////////
|
|
// UnitModelCategory.cs
|
|
// Implementation of Class UnitModelCategory
|
|
// CSLA type: Editable Child
|
|
// Created on: 07-Jun-2004 8:41:39 AM
|
|
// Object design: Joyce
|
|
// Coded: John 04-Nov-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>
|
|
/// Category which a <see cref="UnitModel"/> can be tagged with for filtering, sorting and reporting purposes
|
|
/// </summary>
|
|
[Serializable]
|
|
public class UnitModelCategory : BusinessBase {
|
|
|
|
#region Attributes
|
|
private bool bReadOnly;
|
|
private Guid mID;
|
|
private string mName=null;
|
|
private SmartDate mCreated;
|
|
private SmartDate mModified;
|
|
private bool mActive;
|
|
private Guid mCreator;
|
|
private Guid mModifier;
|
|
private string mDescription="";
|
|
|
|
|
|
#endregion
|
|
|
|
#region Constructor
|
|
|
|
/// <summary>
|
|
/// Private constructor to prevent direct instantiation
|
|
/// </summary>
|
|
private UnitModelCategory()
|
|
{
|
|
MarkAsChild();
|
|
//Set to read / write initially so that properties
|
|
//can be set
|
|
bReadOnly=false;
|
|
|
|
//New ID
|
|
mID = Guid.NewGuid();
|
|
//pre-break the rule
|
|
Name="";
|
|
Active=true;
|
|
|
|
//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 internal id number Read only property because it's set internally, not
|
|
/// externally
|
|
/// </summary>
|
|
public Guid ID
|
|
{
|
|
get
|
|
{
|
|
return mID;
|
|
}
|
|
}
|
|
|
|
/// <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>
|
|
/// Set/get client group name
|
|
/// </summary>
|
|
public string Name
|
|
{
|
|
get
|
|
{
|
|
return mName;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if(mName!=value)
|
|
{
|
|
mName = value;
|
|
|
|
BrokenRules.Assert("NameRequired","Error.Object.RequiredFieldEmpty,UnitModelCategory.Label.Name","Name",value.Length==0);
|
|
|
|
BrokenRules.Assert("NameLength",
|
|
"Error.Object.FieldLengthExceeded255,UnitModelCategory.Label.Name","Name",value.Length>255);
|
|
|
|
MarkDirty();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get /set active status of client group
|
|
/// If active = true then UnitModelCategory is selectable for workorders etc
|
|
/// If active = false then UnitModelCategory in not selectable, but history can be
|
|
/// viewed
|
|
/// </summary>
|
|
public bool Active
|
|
{
|
|
get
|
|
{
|
|
return mActive;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if(mActive!=value)
|
|
{
|
|
mActive = value;
|
|
MarkDirty();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string Description
|
|
{
|
|
get
|
|
{
|
|
return mDescription;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if(mDescription!=value)
|
|
{
|
|
mDescription = value;
|
|
MarkDirty();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Throw an error when a read only user
|
|
/// tries to set a property
|
|
/// (this should normally never be called unless someone is using the developer api since the UI
|
|
/// should prevent it from happening initially)
|
|
/// </summary>
|
|
private void ThrowSetError()
|
|
{
|
|
throw new System.Security.SecurityException
|
|
(
|
|
string.Format
|
|
(
|
|
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToChange"),
|
|
LocalizedTextTable.GetLocalizedTextDirect("O.UnitModelCategory")
|
|
)
|
|
);
|
|
}
|
|
#endregion
|
|
|
|
#region System.Object overrides
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public override string ToString()
|
|
{
|
|
return "UnitModelCategory" + mID.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
public override bool Equals(Object obj)
|
|
{
|
|
if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false;
|
|
UnitModelCategory c=(UnitModelCategory)obj;
|
|
return mID==c.mID;
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public override int GetHashCode()
|
|
{
|
|
return ("UnitModelCategory"+mID).GetHashCode();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Searching
|
|
|
|
/// <summary>
|
|
/// Returns a search result object based on search terms
|
|
/// for the ID specified
|
|
/// </summary>
|
|
/// <param name="ID"></param>
|
|
/// <param name="searchTerms"></param>
|
|
/// <returns></returns>
|
|
public static SearchResult GetSearchResult(Guid ID, string[]searchTerms)
|
|
{
|
|
|
|
|
|
if(AyaBizUtils.Right("Object.UnitModel")<(int)SecurityLevelTypes.ReadOnly)
|
|
return new SearchResult();
|
|
|
|
SearchResult sr=new SearchResult();
|
|
System.Text.StringBuilder sb = new System.Text.StringBuilder();
|
|
|
|
SafeDataReader dr = null;
|
|
try
|
|
{
|
|
dr=DBUtil.GetReaderFromSQLString(
|
|
|
|
"SELECT aCreated, aModified, aCreator, aModifier, aName, " +
|
|
" aDescription FROM aUnitModelCategory WHERE (aID " +
|
|
"= @ID)"
|
|
,ID);
|
|
|
|
if(!dr.Read())
|
|
return new SearchResult();//DBUtil.ThrowFetchError("SearchResult for UnitModelCategoryID: " + ID.ToString());
|
|
|
|
|
|
sr.Description=dr.GetString("aName");
|
|
sb.Append(sr.Description);
|
|
|
|
sb.Append(" ");
|
|
sb.Append(dr.GetString("aDescription"));
|
|
|
|
sr.Created=DBUtil.ToLocal(dr.GetSmartDate("aCreated"));
|
|
sr.Modified=DBUtil.ToLocal(dr.GetSmartDate("aModified"));
|
|
sr.Creator=dr.GetGuid("aCreator");
|
|
sr.Modifier=dr.GetGuid("aModifier");
|
|
|
|
|
|
|
|
|
|
}
|
|
finally
|
|
{
|
|
if(dr!=null) dr.Close();
|
|
}
|
|
|
|
|
|
|
|
|
|
//Formulate results
|
|
ExtractAndRank er = new ExtractAndRank();
|
|
er.Process(sb.ToString().Trim(),searchTerms);
|
|
sr.Extract=er.Extract;
|
|
sr.Rank=er.Ranking;
|
|
sr.AncestorRootObjectID=ID;
|
|
sr.AncestorRootObjectType=RootObjectTypes.UnitModelCategory;
|
|
|
|
return sr;
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Static methods
|
|
/// <summary>
|
|
/// Create new UnitModelCategory
|
|
/// </summary>
|
|
internal static UnitModelCategory NewItem()
|
|
{
|
|
|
|
|
|
if(AyaBizUtils.Right("Object.UnitModel")>(int)SecurityLevelTypes.ReadOnly)
|
|
{
|
|
UnitModelCategory c = new UnitModelCategory();
|
|
return c;
|
|
}
|
|
else
|
|
throw new System.Security.SecurityException(
|
|
string.Format(
|
|
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToCreate"),
|
|
LocalizedTextTable.GetLocalizedTextDirect("O.UnitModelCategory")));
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fetch existing UnitModelCategory
|
|
/// </summary>
|
|
/// <param name="dr">Datareader</param>
|
|
internal static UnitModelCategory GetItem(SafeDataReader dr)
|
|
{
|
|
|
|
if(AyaBizUtils.Right("Object.UnitModel")>(int)SecurityLevelTypes.NoAccess)
|
|
{
|
|
UnitModelCategory child = new UnitModelCategory();
|
|
child.Fetch(dr);
|
|
return child;
|
|
}
|
|
else
|
|
throw new System.Security.SecurityException(
|
|
string.Format(
|
|
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToRetrieve"),
|
|
LocalizedTextTable.GetLocalizedTextDirect("O.UnitModelCategory")));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve internal ID from name.
|
|
///
|
|
/// </summary>
|
|
/// <param name="Name">Text value</param>
|
|
/// <returns>Guid ID value or Guid.Empty if no match</returns>
|
|
public static Guid GetIDFromName(string Name)
|
|
{
|
|
return GuidFetcher.GetItem("AUNITMODELCATEGORY", "ANAME", Name);
|
|
}
|
|
#endregion
|
|
|
|
#region DAL DATA ACCESS
|
|
|
|
/// <summary>
|
|
/// Populate this object from the values in the datareader passed to it
|
|
/// </summary>
|
|
/// <param name="dr"></param>
|
|
private void Fetch(SafeDataReader dr)
|
|
{
|
|
|
|
//Standard fields
|
|
mID=dr.GetGuid("aID");
|
|
mCreated=DBUtil.ToLocal(dr.GetSmartDate("aCreated"));
|
|
mModified=DBUtil.ToLocal(dr.GetSmartDate("aModified"));
|
|
mCreator=dr.GetGuid("aCreator");
|
|
mModifier=dr.GetGuid("aModifier");
|
|
|
|
//UnitModelCategory fields
|
|
Active=dr.GetBoolean("AACTIVE");
|
|
|
|
//Important: use property not internal field
|
|
//so that initial broken rule is unbroken on fetch
|
|
Name=dr.GetString("aName");
|
|
|
|
Description=dr.GetString("aDescription");
|
|
|
|
MarkOld();
|
|
|
|
//Get access rights level
|
|
bReadOnly=AyaBizUtils.Right("Object.UnitModel")<(int)SecurityLevelTypes.ReadWrite;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="tr"></param>
|
|
internal void Update(IDbTransaction tr)
|
|
{
|
|
//No need to update if there is nothing changed
|
|
if(!this.IsDirty) return;
|
|
|
|
// 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,"aUnitModelCategory");
|
|
|
|
#region Delete
|
|
if(IsDeleted)
|
|
{
|
|
if(!IsNew)
|
|
{
|
|
//Delete object and child objects
|
|
DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM aUnitModelCategory WHERE aID = @ID;");
|
|
cmDelete.AddInParameter("@ID",DbType.Guid,this.mID);
|
|
DBUtil.DB.ExecuteNonQuery(cmDelete, tr);
|
|
DBUtil.RemoveKeywords(tr,RootObjectTypes.UnitModelCategory,this.mID);
|
|
//-----------------------------
|
|
}
|
|
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 aUnitModelCategory (aID, AACTIVE, aName, aDescription, " +
|
|
"aCreated,aModified,aCreator,aModifier) VALUES (@ID,@Active, " +
|
|
"@Name,@Description,@Created,@Modified,@CurrentUserID,@CurrentUserID)"
|
|
);
|
|
else
|
|
cm=DBUtil.GetCommandFromSQL(
|
|
"UPDATE aUnitModelCategory SET aID=@ID, AACTIVE=@Active, " +
|
|
"aName=@Name, aDescription=@Description, aModifier=@CurrentUserID, " +
|
|
"aModified=@Modified WHERE aID=@ID"
|
|
);
|
|
|
|
|
|
//UnitModelCategory fields
|
|
cm.AddInParameter("@Active",DbType.Boolean,mActive);
|
|
cm.AddLargeStringInParameter("@Description",mDescription);
|
|
cm.AddInParameter("@Name",DbType.String,mName);
|
|
|
|
|
|
//Standard fields
|
|
cm.AddInParameter("@ID",DbType.Guid,this.mID);
|
|
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);
|
|
|
|
//Process keywords
|
|
DBUtil.ProcessKeywords(tr,this.mID,RootObjectTypes.UnitModelCategory,IsNew,AyaBizUtils.Break(false,
|
|
mName,mDescription
|
|
));
|
|
|
|
|
|
MarkOld();//db is now synched with object
|
|
//Successful update so
|
|
//change modification time to match
|
|
this.mModified.Date=dtModified;
|
|
#endregion
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}//end UnitModelCategory
|
|
|
|
}//end namespace GZTW.AyaNova.BLL |