717 lines
17 KiB
C#
717 lines
17 KiB
C#
///////////////////////////////////////////////////////////
|
|
// UnitMeterReading.cs
|
|
// Implementation of Class UnitMeterReading
|
|
// CSLA type: Editable Root
|
|
// Created on: 07-Jun-2004 8:41:39 AM
|
|
// Object design: Joyce
|
|
// Coded: John 12-Jul-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>
|
|
/// Unit meter reading entry
|
|
/// </summary>
|
|
[Serializable]
|
|
public class UnitMeterReading : BusinessBase {
|
|
|
|
#region Attributes
|
|
private bool bReadOnly;
|
|
private Guid mID;
|
|
private SmartDate mCreated;
|
|
private SmartDate mModified;
|
|
private Guid mCreator;
|
|
private Guid mModifier;
|
|
private string mDescription="";
|
|
|
|
private long mMeter;
|
|
private SmartDate mMeterDate;
|
|
private Guid mUnitID;
|
|
private Guid mWorkorderItemID;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
#region Constructor
|
|
|
|
/// <summary>
|
|
/// Private constructor to prevent direct instantiation
|
|
/// </summary>
|
|
private UnitMeterReading()
|
|
{
|
|
//Set to read write initially so that properties
|
|
//can be set
|
|
bReadOnly=false;
|
|
|
|
//New ID
|
|
mID = Guid.NewGuid();
|
|
|
|
mMeterDate = new SmartDate(DBUtil.CurrentWorkingDateTime);
|
|
|
|
//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>
|
|
/// Short text describing meter reading / extra info
|
|
/// </summary>
|
|
public string Description
|
|
{
|
|
get
|
|
{
|
|
return mDescription;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if(mDescription!=value)
|
|
{
|
|
mDescription = value;
|
|
MarkDirty();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Meter count at time of reading
|
|
/// </summary>
|
|
public long Meter
|
|
{
|
|
get
|
|
{
|
|
return mMeter;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if(mMeter!=value)
|
|
{
|
|
mMeter = value;
|
|
MarkDirty();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Date meterreading taken
|
|
/// </summary>
|
|
public object MeterDate
|
|
{
|
|
get
|
|
{
|
|
return mMeterDate.DBValue;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if (!AyaBizUtils.SmartDateEquals(mMeterDate, value)) //Case 298
|
|
{
|
|
mMeterDate.DBValue = value;
|
|
MarkDirty();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// The ID of the <see cref="Unit"/> object for which this reading was taken.
|
|
/// </summary>
|
|
public Guid UnitID
|
|
{
|
|
get
|
|
{
|
|
return mUnitID;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if(mUnitID!=value)
|
|
{
|
|
mUnitID = value;
|
|
|
|
BrokenRules.Assert("UnitRequired","Error.Object.RequiredFieldEmpty,MeterReading.Label.UnitID","UnitID",value==Guid.Empty);
|
|
|
|
MarkDirty();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// If a meter reading is entered via the workorder screen
|
|
/// then the workorder item is stored here as a back reference
|
|
///
|
|
/// </summary>
|
|
public Guid WorkorderItemID
|
|
{
|
|
get
|
|
{
|
|
return mWorkorderItemID;
|
|
}
|
|
set
|
|
{
|
|
if(bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if(mWorkorderItemID!=value)
|
|
{
|
|
mWorkorderItemID = 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.UnitMeterReading")
|
|
)
|
|
);
|
|
}
|
|
#endregion
|
|
|
|
#region System.Object overrides
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public override string ToString()
|
|
{
|
|
return "UnitMeterReading" + mID.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
public override bool Equals(Object obj)
|
|
{
|
|
if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false;
|
|
UnitMeterReading c=(UnitMeterReading)obj;
|
|
return mID==c.mID;
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public override int GetHashCode()
|
|
{
|
|
return ("UnitMeterReading"+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)
|
|
{
|
|
|
|
|
|
|
|
|
|
SearchResult sr=new SearchResult();
|
|
System.Text.StringBuilder sb = new System.Text.StringBuilder();
|
|
|
|
SafeDataReader dr = null;
|
|
try
|
|
{
|
|
dr=DBUtil.GetReaderFromSQLString(
|
|
|
|
"SELECT aCreated, aModified, aCreator, aModifier, aDescription, " +
|
|
" aUnitID, aWorkorderItemID FROM aUnitMeterReading " +
|
|
"WHERE (aID = @ID)"
|
|
,ID);
|
|
|
|
if(!dr.Read())
|
|
return new SearchResult();//DBUtil.ThrowFetchError("SearchResult for UnitMeterReadingID: " + ID.ToString());
|
|
|
|
|
|
sr.Description=LocalizedTextTable.GetLocalizedTextDirect("O.UnitMeterReading");
|
|
sb.Append(dr.GetString("aDescription"));
|
|
sr.AncestorRootObjectID=dr.GetGuid("aUnitID");
|
|
sr.AncestorRootObjectType=RootObjectTypes.Unit;
|
|
|
|
//No modification in service bank so
|
|
//create and mod are the same
|
|
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();
|
|
}
|
|
|
|
|
|
//Case 58
|
|
UnitPickList upl = UnitPickList.GetListOfOneSpecificUnit(sr.AncestorRootObjectID);
|
|
if(upl.Count>0)
|
|
if (!AyaBizUtils.InYourRegion(
|
|
ObjectRegionIDFetcher.ObjectRegion(new TypeAndID(RootObjectTypes.Client,upl[0].ClientID))
|
|
)) return new SearchResult();//case 58
|
|
|
|
|
|
//Security check..do they have rights to the ancestor object?
|
|
|
|
if(AyaBizUtils.Right("Object.Unit")<(int)SecurityLevelTypes.ReadOnly)
|
|
return new SearchResult();
|
|
|
|
//Formulate results
|
|
ExtractAndRank er = new ExtractAndRank();
|
|
er.Process(sb.ToString().Trim(),searchTerms);
|
|
sr.Extract=er.Extract;
|
|
sr.Rank=er.Ranking;
|
|
|
|
return sr;
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Static methods
|
|
/// <summary>
|
|
/// Create new UnitMeterReading
|
|
/// </summary>
|
|
public static UnitMeterReading NewItem()
|
|
{
|
|
|
|
|
|
if(AyaBizUtils.Right("Object.UnitMeterReading")>(int)SecurityLevelTypes.ReadOnly)
|
|
return new UnitMeterReading();
|
|
else
|
|
throw new System.Security.SecurityException(
|
|
string.Format(
|
|
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToCreate"),
|
|
LocalizedTextTable.GetLocalizedTextDirect("O.UnitMeterReading")));
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fetch existing UnitMeterReading
|
|
/// </summary>
|
|
/// <param name="_ID">Client Guid</param>
|
|
public static UnitMeterReading GetItem(Guid _ID)
|
|
{
|
|
|
|
if(AyaBizUtils.Right("Object.UnitMeterReading")>(int)SecurityLevelTypes.NoAccess)
|
|
return (UnitMeterReading)DataPortal.Fetch(new Criteria(_ID));
|
|
else
|
|
throw new System.Security.SecurityException(
|
|
string.Format(
|
|
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToRetrieve"),
|
|
LocalizedTextTable.GetLocalizedTextDirect("O.UnitMeterReading")));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete UnitMeterReading (if clients assigned to this group, should be notified)
|
|
/// </summary>
|
|
/// <param name="_ID">Client GUID</param>
|
|
public static void DeleteItem(Guid _ID)
|
|
{
|
|
|
|
if(AyaBizUtils.Right("Object.UnitMeterReading")>(int)SecurityLevelTypes.ReadWrite)
|
|
DataPortal.Delete(new Criteria(_ID));
|
|
else
|
|
throw new System.Security.SecurityException(
|
|
string.Format(
|
|
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToDelete"),
|
|
LocalizedTextTable.GetLocalizedTextDirect("O.UnitMeterReading")));
|
|
}
|
|
#endregion
|
|
|
|
#region DAL DATA ACCESS
|
|
|
|
#region Fetch
|
|
///
|
|
/// <param name="Criteria"></param>
|
|
protected override void DataPortal_Fetch(object Criteria)
|
|
{
|
|
//set to false to load items initially
|
|
bReadOnly=false;
|
|
Criteria crit = (Criteria)Criteria;
|
|
SafeDataReader dr = null;
|
|
|
|
try
|
|
{
|
|
dr=DBUtil.GetReaderFromSQLString("SELECT * FROM aUnitMeterReading WHERE aID=@ID;",crit.ID);
|
|
if(!dr.Read())
|
|
DBUtil.ThrowFetchError("UnitMeterReading ID: " + crit.ID.ToString());
|
|
|
|
//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");
|
|
|
|
|
|
//UnitMeterReading fields
|
|
mDescription=dr.GetString("aDescription");
|
|
//bigint field 64 bit LONG type
|
|
mMeter=dr.GetInt64("aMeter");
|
|
mMeterDate=DBUtil.ToLocal(dr.GetSmartDate("aMeterDate"));
|
|
|
|
//Important: use property not internal field
|
|
//so that initial broken rule is unbroken on fetch
|
|
UnitID=dr.GetGuid("aUnitID");
|
|
|
|
mWorkorderItemID=dr.GetGuid("aWorkorderItemID");
|
|
|
|
|
|
|
|
}
|
|
finally
|
|
{
|
|
if(dr!=null) dr.Close();
|
|
}
|
|
MarkOld();
|
|
|
|
|
|
|
|
//Get access rights level
|
|
bReadOnly=AyaBizUtils.Right("Object.UnitMeterReading")<(int)SecurityLevelTypes.ReadWrite;
|
|
}
|
|
#endregion fetch
|
|
|
|
#region Update
|
|
|
|
/// <summary>
|
|
/// Called by DataPortal to delete/add/update data into the database
|
|
/// </summary>
|
|
protected override void DataPortal_Update()
|
|
{
|
|
|
|
// 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,"aUnitMeterReading");
|
|
|
|
#region Delete
|
|
if(IsDeleted)
|
|
{
|
|
if(!IsNew)
|
|
{
|
|
|
|
|
|
//Delete object and child objects
|
|
DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM aUnitMeterReading WHERE aID = @ID;");
|
|
cmDelete.AddInParameter("@ID",DbType.Guid,this.mID);
|
|
|
|
using (IDbConnection connection = DBUtil.DB.GetConnection())
|
|
{
|
|
connection.Open();
|
|
IDbTransaction transaction = connection.BeginTransaction();
|
|
|
|
try
|
|
{
|
|
DBUtil.DB.ExecuteNonQuery(cmDelete, transaction);
|
|
DBUtil.RemoveKeywords(transaction,RootObjectTypes.UnitMeterReading,this.mID);
|
|
// Commit the transaction
|
|
transaction.Commit();
|
|
|
|
}
|
|
catch
|
|
{
|
|
// Rollback transaction
|
|
transaction.Rollback();
|
|
throw;
|
|
}
|
|
finally
|
|
{
|
|
connection.Close();
|
|
}
|
|
}
|
|
|
|
|
|
//-----------------------------
|
|
}
|
|
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 aUnitMeterReading (aDescription, aID, aMeter, " +
|
|
"aMeterDate, aUnitID, aWorkorderItemID, aCreated,aModified,aCreator, " +
|
|
"aModifier) VALUES (@Description,@ID,@Meter,@MeterDate, " +
|
|
"@UnitID,@WorkorderItemID,@Created,@Modified,@CurrentUserID, " +
|
|
"@CurrentUserID)"
|
|
);
|
|
else
|
|
cm=DBUtil.GetCommandFromSQL(
|
|
"UPDATE aUnitMeterReading SET aDescription=@Description, " +
|
|
"aID=@ID, aMeter=@Meter, aMeterDate=@MeterDate, " +
|
|
"aUnitID=@UnitID, aWorkorderItemID=@WorkorderItemID, " +
|
|
"aModifier=@CurrentUserID, aModified=@Modified " +
|
|
"WHERE aID=@ID"
|
|
);
|
|
|
|
|
|
//UnitMeterReading specific fields
|
|
cm.AddInParameter("@ID",DbType.Guid,mID);
|
|
cm.AddInParameter("@Description",DbType.String, mDescription);
|
|
cm.AddInParameter("@Meter",DbType.Int64,mMeter);
|
|
cm.AddInParameter("@MeterDate",DbType.DateTime,DBUtil.ToUTC(mMeterDate).DBValue);
|
|
cm.AddInParameter("@UnitID",DbType.Guid,mUnitID);
|
|
cm.AddInParameter("@WorkorderItemID",DbType.Guid,mWorkorderItemID);
|
|
|
|
//Standard fields
|
|
cm.AddInParameter("@CurrentUserID",DbType.Guid, CurrentUserID);
|
|
cm.AddInParameter("@Created",DbType.DateTime, DBUtil.ToUTC(mCreated).DBValue);
|
|
cm.AddInParameter("@Modified",DbType.DateTime, DBUtil.ToUTC(dtModified));
|
|
|
|
|
|
using (IDbConnection connection = DBUtil.DB.GetConnection())
|
|
{
|
|
connection.Open();
|
|
IDbTransaction transaction = connection.BeginTransaction();
|
|
|
|
try
|
|
{
|
|
DBUtil.DB.ExecuteNonQuery(cm, transaction);
|
|
//Process keywords
|
|
DBUtil.ProcessKeywords(transaction,this.mID,RootObjectTypes.UnitMeterReading,IsNew,AyaBizUtils.Break(false,
|
|
mDescription));
|
|
MarkOld();//db is now synched with object
|
|
// Commit the transaction
|
|
transaction.Commit();
|
|
|
|
}
|
|
catch
|
|
{
|
|
// Rollback transaction
|
|
transaction.Rollback();
|
|
throw;
|
|
}
|
|
finally
|
|
{
|
|
connection.Close();
|
|
}
|
|
//Successful update so
|
|
//change modification time to match
|
|
this.mModified.Date=dtModified;
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
#endregion update
|
|
|
|
#region Delete
|
|
|
|
/// <summary>
|
|
/// Remove a UnitMeterReading record from the database
|
|
/// </summary>
|
|
/// <param name="Criteria"></param>
|
|
protected override void DataPortal_Delete(object Criteria)
|
|
{
|
|
Criteria crit = (Criteria)Criteria;
|
|
//Delete object and child objects
|
|
DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM aUnitMeterReading WHERE aID = @ID;");
|
|
cmDelete.AddInParameter("@ID",DbType.Guid,crit.ID);
|
|
|
|
using (IDbConnection connection = DBUtil.DB.GetConnection())
|
|
{
|
|
connection.Open();
|
|
IDbTransaction transaction = connection.BeginTransaction();
|
|
|
|
try
|
|
{
|
|
DBUtil.DB.ExecuteNonQuery(cmDelete, transaction);
|
|
DBUtil.RemoveKeywords(transaction,RootObjectTypes.UnitMeterReading,crit.ID);
|
|
// Commit the transaction
|
|
transaction.Commit();
|
|
|
|
}
|
|
catch
|
|
{
|
|
// Rollback transaction
|
|
transaction.Rollback();
|
|
throw;
|
|
}
|
|
finally
|
|
{
|
|
connection.Close();
|
|
}
|
|
}
|
|
}
|
|
#endregion delete
|
|
|
|
|
|
#endregion
|
|
|
|
#region Override IsValid / IsDirty
|
|
//Override base class version if there are child objects
|
|
/*
|
|
public override bool IsValid
|
|
{
|
|
get
|
|
{
|
|
return base.IsValid && ChildItem.IsValid;
|
|
}
|
|
}
|
|
public override bool IsDirty
|
|
{
|
|
get
|
|
{
|
|
return base.IsDirty || ChildItem.IsDirty;
|
|
}
|
|
}
|
|
*/
|
|
#endregion
|
|
|
|
#region criteria
|
|
/// <summary>
|
|
/// Criteria for identifying existing object
|
|
/// </summary>
|
|
[Serializable]
|
|
private class Criteria
|
|
{
|
|
public Guid ID;
|
|
public Criteria(Guid _ID)
|
|
{
|
|
ID=_ID;
|
|
}
|
|
|
|
}
|
|
#endregion
|
|
|
|
}//end UnitMeterReading
|
|
|
|
}//end namespace GZTW.AyaNova.BLL |