647 lines
19 KiB
C#
647 lines
19 KiB
C#
///////////////////////////////////////////////////////////
|
|
// ClientNoteEr.cs
|
|
// Implementation of Class ClientNoteEr
|
|
// CSLA type: Editable Root
|
|
// Created on: Feb 24 2015
|
|
// Object design: John
|
|
// Coded: John Feb 24 2015
|
|
///////////////////////////////////////////////////////////
|
|
|
|
using System;
|
|
using System.Data;
|
|
using CSLA.Data;
|
|
using GZTW.Data;
|
|
using CSLA;
|
|
using System.Threading;
|
|
using CSLA.Security;
|
|
|
|
namespace GZTW.AyaNova.BLL
|
|
{
|
|
/// <summary>
|
|
/// Note for a <see cref="Client"/> editable root version
|
|
/// </summary>
|
|
[Serializable]
|
|
public class ClientNoteEr : BusinessBase
|
|
{
|
|
|
|
#region Attributes
|
|
|
|
private bool bReadOnly;
|
|
private Guid mID;
|
|
private SmartDate mCreated;
|
|
private SmartDate mModified;
|
|
private Guid mCreator;
|
|
private Guid mModifier;
|
|
private SmartDate mNoteDate;
|
|
private string mNotes = string.Empty;
|
|
private Guid mClientNoteTypeID = Guid.Empty;
|
|
private Guid mClientID;
|
|
|
|
|
|
#endregion
|
|
|
|
#region Constructor
|
|
|
|
/// <summary>
|
|
/// Private constructor to prevent direct instantiation
|
|
/// </summary>
|
|
private ClientNoteEr()
|
|
{
|
|
//Set to read / write initially so that properties
|
|
//can be set
|
|
bReadOnly = false;
|
|
|
|
//New ID
|
|
mID = Guid.NewGuid();
|
|
mNoteDate = new SmartDate(DBUtil.CurrentWorkingDateTime);
|
|
|
|
//Set record history to defaults
|
|
mCreated = new SmartDate(DBUtil.CurrentWorkingDateTime);
|
|
mModified = new SmartDate();
|
|
//Case 30
|
|
//mCreator = Guid.Empty;
|
|
mCreator = User.CurrentThreadUserID;
|
|
|
|
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>
|
|
///
|
|
/// </summary>
|
|
public string Notes
|
|
{
|
|
get
|
|
{
|
|
return mNotes;
|
|
}
|
|
set
|
|
{
|
|
if (bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if (mNotes != value)
|
|
{
|
|
mNotes = value;
|
|
MarkDirty();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// User selectable data and time of note
|
|
/// </summary>
|
|
public object NoteDate
|
|
{
|
|
get
|
|
{
|
|
return mNoteDate.DBValue;
|
|
}
|
|
set
|
|
{
|
|
if (bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if (!AyaBizUtils.SmartDateEquals(mNoteDate, value)) //Case 298
|
|
{
|
|
mNoteDate.DBValue = value;
|
|
MarkDirty();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// <see cref="Client"/> ID
|
|
/// </summary>
|
|
public Guid ClientID
|
|
{
|
|
get
|
|
{
|
|
return mClientID;
|
|
}
|
|
set
|
|
{
|
|
if (bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if (mClientID != value)
|
|
{
|
|
mClientID = value;
|
|
MarkDirty();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// <see cref="ClientNoteType"/> ID
|
|
/// </summary>
|
|
public Guid ClientNoteTypeID
|
|
{
|
|
get
|
|
{
|
|
return mClientNoteTypeID;
|
|
}
|
|
set
|
|
{
|
|
if (bReadOnly)
|
|
ThrowSetError();
|
|
else
|
|
{
|
|
if (mClientNoteTypeID != value)
|
|
{
|
|
mClientNoteTypeID = value;
|
|
MarkDirty();
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Object is editable if current user is not read only
|
|
/// </summary>
|
|
public bool IsEditable
|
|
{
|
|
get
|
|
{
|
|
if (bReadOnly) return false;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
/// <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.ClientNote")
|
|
)
|
|
);
|
|
}
|
|
#endregion
|
|
|
|
#region System.Object overrides
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public override string ToString()
|
|
{
|
|
return "ClientNoteEr" + mID.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
public override bool Equals(Object obj)
|
|
{
|
|
if (obj == null || GetType() != obj.GetType()) return false;
|
|
ClientNoteEr c = (ClientNoteEr)obj;
|
|
return mID == c.mID;
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public override int GetHashCode()
|
|
{
|
|
return ("ClientNoteEr" + 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.Client") < (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 aClientNote.aCreated, aClientNote.aModified, " +
|
|
" aClientNote.aCreator, aClientNote.aModifier, " +
|
|
" aClientNote.aNotes, aClient.aID AS aClientID, aClient.aRegionID, " +
|
|
" aClient.aName FROM aClientNote LEFT JOIN " +
|
|
"aClient ON aClientNote.aClientID = aClient.aID " +
|
|
"WHERE (aClientNote.aID = @ID)", ID);
|
|
|
|
if (!dr.Read())
|
|
return new SearchResult();//DBUtil.ThrowFetchError("SearchResult for ClientNoteID: " + ID.ToString());
|
|
|
|
if (!AyaBizUtils.InYourRegion(dr.GetGuid("aRegionID"))) return new SearchResult();//case 58
|
|
|
|
sr.Description = dr.GetString("aName");
|
|
sb.Append(dr.GetString("aNotes"));
|
|
sr.AncestorRootObjectID = dr.GetGuid("aClientID");
|
|
sr.AncestorRootObjectType = RootObjectTypes.Client;
|
|
|
|
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;
|
|
|
|
return sr;
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Static methods
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="clientID"></param>
|
|
/// <returns></returns>
|
|
public static ClientNoteEr NewItem(Guid clientID)
|
|
{
|
|
if (clientID == Guid.Empty)
|
|
throw new System.ApplicationException("CLIENT ID NOT VALID");
|
|
|
|
ClientNoteEr c;
|
|
|
|
if (AyaBizUtils.Right("Object.Client") > (int)SecurityLevelTypes.ReadOnly)
|
|
{
|
|
c = new ClientNoteEr();
|
|
c.mClientID = clientID;
|
|
return c;
|
|
}
|
|
else
|
|
throw new System.Security.SecurityException(
|
|
string.Format(
|
|
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToCreate"),
|
|
LocalizedTextTable.GetLocalizedTextDirect("O.ClientNote")));
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="_ID"></param>
|
|
/// <returns></returns>
|
|
public static ClientNoteEr GetItem(Guid _ID)
|
|
{
|
|
if (AyaBizUtils.Right("Object.Client") > (int)SecurityLevelTypes.NoAccess)
|
|
return (ClientNoteEr)DataPortal.Fetch(new Criteria(_ID));
|
|
else
|
|
throw new System.Security.SecurityException(
|
|
string.Format(
|
|
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToRetrieve"),
|
|
LocalizedTextTable.GetLocalizedTextDirect("O.ClientNote")));
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="_ID"></param>
|
|
public static void DeleteItem(Guid _ID)
|
|
{
|
|
if (AyaBizUtils.Right("Object.Client") > (int)SecurityLevelTypes.ReadWrite)
|
|
DataPortal.Delete(new Criteria(_ID));
|
|
else
|
|
throw new System.Security.SecurityException(
|
|
string.Format(
|
|
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToDelete"),
|
|
LocalizedTextTable.GetLocalizedTextDirect("O.ClientNote")));
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
#region DAL DATA ACCESS
|
|
|
|
|
|
#region Fetch
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <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 ACLIENTNOTE WHERE AID=@ID;", crit.ID);
|
|
if (!dr.Read())
|
|
DBUtil.ThrowFetchError("ClientNote 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");
|
|
|
|
//ClientNote fields
|
|
mClientID = dr.GetGuid("aClientID");
|
|
mClientNoteTypeID = dr.GetGuid("aClientNoteTypeID");
|
|
mNoteDate = DBUtil.ToLocal(dr.GetSmartDate("aNoteDate"));
|
|
mNotes = dr.GetString("aNotes");
|
|
}
|
|
finally
|
|
{
|
|
if (dr != null) dr.Close();
|
|
}
|
|
MarkOld();
|
|
|
|
//Get access rights level
|
|
bReadOnly = AyaBizUtils.Right("Object.Client") < (int)SecurityLevelTypes.ReadWrite;
|
|
|
|
}
|
|
#endregion fetch
|
|
|
|
#region Update
|
|
|
|
/// <summary>
|
|
///
|
|
/// </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, "aClientNote");
|
|
|
|
|
|
#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 aClientNote (aID, aNoteDate, aNotes, aClientNoteTypeID, " +
|
|
"aClientID, " +
|
|
"aCreated,aModified,aCreator,aModifier) VALUES (@ID,@NoteDate, " +
|
|
"@Notes,@ClientNoteTypeID,@ClientID, " +
|
|
"@Created,@Modified,@CurrentUserID,@CurrentUserID)"
|
|
);
|
|
else
|
|
cm = DBUtil.GetCommandFromSQL(
|
|
"UPDATE aClientNote SET aID=@ID, aNoteDate=@NoteDate, " +
|
|
"aNotes=@Notes, aClientNoteTypeID=@ClientNoteTypeID, " +
|
|
"aClientID=@ClientID, aModifier=@CurrentUserID, " +
|
|
" aModified=@Modified WHERE aID=@ID"
|
|
);
|
|
|
|
|
|
//ClientNote fields
|
|
cm.AddInParameter("@NoteDate", DbType.DateTime, DBUtil.ToUTC(mNoteDate).DBValue);
|
|
cm.AddLargeStringInParameter("@Notes", mNotes);
|
|
cm.AddInParameter("@ClientNoteTypeID", DbType.Guid, mClientNoteTypeID);
|
|
cm.AddInParameter("@ClientID", DbType.Guid, mClientID);
|
|
|
|
//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));
|
|
|
|
|
|
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.ClientNote, IsNew,
|
|
AyaBizUtils.Break(false, mNotes)
|
|
);
|
|
|
|
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 ClientServiceRequest record .
|
|
/// </summary>
|
|
/// <param Title="Criteria"></param>
|
|
protected override void DataPortal_Delete(object Criteria)
|
|
{
|
|
Criteria crit = (Criteria)Criteria;
|
|
ClientNoteEr rq = ClientNoteEr.GetItem(crit.ID);
|
|
if (!rq.IsEditable)
|
|
throw new System.ApplicationException
|
|
(
|
|
string.Format
|
|
(
|
|
LocalizedTextTable.GetLocalizedTextDirect("Error.Object.NotDeleteable"),
|
|
LocalizedTextTable.GetLocalizedTextDirect("O.ClientNote")
|
|
)
|
|
);
|
|
|
|
rq = null;
|
|
//Delete object and child objects
|
|
DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM ACLIENTNOTE 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.ClientNote, crit.ID);
|
|
|
|
|
|
// Commit the transaction
|
|
transaction.Commit();
|
|
|
|
}
|
|
catch
|
|
{
|
|
// Rollback transaction
|
|
transaction.Rollback();
|
|
throw;
|
|
}
|
|
finally
|
|
{
|
|
connection.Close();
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
#endregion delete
|
|
|
|
#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 ClientNoteEr
|
|
|
|
}//end namespace GZTW.AyaNova.BLL |