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

1799 lines
46 KiB
C#

///////////////////////////////////////////////////////////
// HeadOffice.cs
// Implementation of Class HeadOffice
// CSLA type: Editable Root
// Created on: 07-Jun-2004 8:41:25 AM
// Object design: Joyce
// Coded: John 29-July-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>
///Head office object representing a parent entity grouping one or more <see cref="Client"/> objects
/// </summary>
[Serializable]
public class HeadOffice : 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 Address mGoToAddress;
private Address mMailToAddress;
//private Contacts mContacts;
private string mWebAddress="";
private AssignedDocs mDocs;
private Guid mClientGroupID;
private string mNotes="";
private Guid mRegionID;
private string mAccountNumber="";
private string mContactNotes = "";
//Custom fields
private string mCustom1="";
private string mCustom2="";
private string mCustom3="";
private string mCustom4="";
private string mCustom5="";
private string mCustom6="";
private string mCustom7="";
private string mCustom8="";
private string mCustom9="";
private string mCustom0="";
//Contract / bank stuff
private bool mUsesBanking;
private Guid mContractID;
private SmartDate mContractExpires;
private string mContact = "";
private string mPhone1 = "";
private string mPhone2 = "";
private string mPhone3 = "";
private string mPhone4 = "";
private string mPhone5 = "";
private string mEmail = "";
#endregion
#region Constructor
/// <summary>
/// Private constructor to prevent direct instantiation
/// </summary>
private HeadOffice()
{
//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;
//Built-in "Default" region
mRegionID=Region.DefaultRegionID;//case 58
//Set record history to defaults
mCreated = new SmartDate(DBUtil.CurrentWorkingDateTime);
mModified=new SmartDate();
mCreator=Guid.Empty;
mModifier=Guid.Empty;
//Contract / bank stuff
mUsesBanking=false;
mContractID=Guid.Empty;
mContractExpires=new SmartDate();
}
#endregion
#region Business properties
/// <summary>
/// Optional identifier for display and reporting purposes
/// </summary>
public string AccountNumber
{
get
{
return mAccountNumber;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mAccountNumber!=value)
{
mAccountNumber = value;
BrokenRules.Assert("AccountNumberLength",
"Error.Object.FieldLengthExceeded255,HeadOffice.Label.AccountNumber","AccountNumber",value.Length>255);
MarkDirty();
}
}
}
}
/// <summary>
/// <see cref="ClientGroup"/> ID
/// </summary>
public Guid ClientGroupID
{
get
{
return mClientGroupID;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mClientGroupID!=value)
{
mClientGroupID = value;
MarkDirty();
}
}
}
}
/// <summary>
/// <see cref="Region"/> ID
/// </summary>
public Guid RegionID
{
get
{
return mRegionID;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mRegionID!=value)
{
mRegionID = value;
BrokenRules.Assert("RegionIDRequired",
"Error.Object.RequiredFieldEmpty,O.Region",
"RegionID",value==Guid.Empty);
MarkDirty();
}
}
}
}
/// <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 HeadOffice name
/// </summary>
public string Name
{
get
{
return mName;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mName!=value)
{
mName = value;
BrokenRules.Assert("NameRequired","Error.Object.RequiredFieldEmpty,HeadOffice.Label.Name","Name",value.Length==0);
BrokenRules.Assert("NameLength",
"Error.Object.FieldLengthExceeded255,HeadOffice.Label.Name","Name",value.Length>255);
MarkDirty();
}
}
}
}
/// <summary>
/// Get /set active status of HeadOffice
/// If active = true then HeadOffice is selectable for workorders etc
/// If active = false then HeadOffice 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>
/// Get service address for this HeadOffice
/// </summary>
public Address GoToAddress
{
get
{
return mGoToAddress;
}
}
/// <summary>
/// Get mailing address object for this HeadOffice
/// Returns GoToAddress object if MailToAddress is empty
/// </summary>
public Address MailToAddress
{
get
{
return mMailToAddress;
}
}
///// <summary>
///// Get Contacts collection for this object
///// </summary>
//public Contacts Contacts
//{
// get
// {
// return mContacts;
// }
//}
/// <summary>
/// web address of HeadOffice if applicable
/// can be null
/// ties into hyerlink to web browser
/// </summary>
public string WebAddress
{
get
{
return mWebAddress;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mWebAddress!=value)
{
mWebAddress = value;
BrokenRules.Assert("WebAddressLength",
"Error.Object.FieldLengthExceeded255,Common.Label.WebAddress","WebAddress",value.Length>255);
MarkDirty();
}
}
}
}
/// <summary>
/// Corresponds to clients.generalnotes in AyaNova v1
/// </summary>
public string Notes
{
get
{
return mNotes;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mNotes!=value)
{
mNotes = value;
MarkDirty();
}
}
}
}
/// <summary>
/// Collection of <see cref="AssignedDoc"/> objects tied to this head office object
/// </summary>
public AssignedDocs Docs
{
get
{
return mDocs;
}
}
#region Contact fields
/// <summary>
/// Set/get HeadOffice Contact
/// </summary>
public string Contact
{
get
{
return mContact;
}
set
{
if (bReadOnly)
ThrowSetError();
else
{
if (mContact != value)
{
mContact = value;
BrokenRules.Assert("ContactLength",
"Error.Object.FieldLengthExceeded500,HeadOffice.Label.Contact",
"Contact", value.Length > 500);
MarkDirty();
}
}
}
}
/// <summary>
/// Set/get HeadOffice Phone1
/// </summary>
public string Phone1
{
get
{
return mPhone1;
}
set
{
if (bReadOnly)
ThrowSetError();
else
{
if (mPhone1 != value)
{
mPhone1 = value;
BrokenRules.Assert("Phone1Length",
"Error.Object.FieldLengthExceeded255,HeadOffice.Label.Phone1",
"Phone1", value.Length > 255);
MarkDirty();
}
}
}
}
/// <summary>
/// Set/get HeadOffice Phone2
/// </summary>
public string Phone2
{
get
{
return mPhone2;
}
set
{
if (bReadOnly)
ThrowSetError();
else
{
if (mPhone2 != value)
{
mPhone2 = value;
BrokenRules.Assert("Phone2Length",
"Error.Object.FieldLengthExceeded255,HeadOffice.Label.Phone2",
"Phone2", value.Length > 255);
MarkDirty();
}
}
}
}
/// <summary>
/// Set/get HeadOffice Phone3
/// </summary>
public string Phone3
{
get
{
return mPhone3;
}
set
{
if (bReadOnly)
ThrowSetError();
else
{
if (mPhone3 != value)
{
mPhone3 = value;
BrokenRules.Assert("Phone3Length",
"Error.Object.FieldLengthExceeded255,HeadOffice.Label.Phone3",
"Phone3", value.Length > 255);
MarkDirty();
}
}
}
}
/// <summary>
/// Set/get HeadOffice Phone4
/// </summary>
public string Phone4
{
get
{
return mPhone4;
}
set
{
if (bReadOnly)
ThrowSetError();
else
{
if (mPhone4 != value)
{
mPhone4 = value;
BrokenRules.Assert("Phone4Length",
"Error.Object.FieldLengthExceeded255,HeadOffice.Label.Phone4",
"Phone4", value.Length > 255);
MarkDirty();
}
}
}
}
/// <summary>
/// Set/get HeadOffice Phone5
/// </summary>
public string Phone5
{
get
{
return mPhone5;
}
set
{
if (bReadOnly)
ThrowSetError();
else
{
if (mPhone5 != value)
{
mPhone5 = value;
BrokenRules.Assert("Phone5Length",
"Error.Object.FieldLengthExceeded255,HeadOffice.Label.Phone5",
"Phone5", value.Length > 255);
MarkDirty();
}
}
}
}
/// <summary>
/// Set/get HeadOffice Email
/// </summary>
public string Email
{
get
{
return mEmail;
}
set
{
if (bReadOnly)
ThrowSetError();
else
{
if (mEmail != value)
{
mEmail = value;
BrokenRules.Assert("EmailLength",
"Error.Object.FieldLengthExceeded255,HeadOffice.Label.Email",
"Email", value.Length > 255);
MarkDirty();
}
}
}
}
/// <summary>
/// Contact Notes / Other contacts
/// </summary>
public string ContactNotes
{
get
{
return mContactNotes;
}
set
{
if (bReadOnly)
ThrowSetError();
else
{
if (mContactNotes != value)
{
mContactNotes = value;
MarkDirty();
}
}
}
}
#endregion contact fields
//CUSTOM FIELDS
/// <summary>
/// Custom1
/// </summary>
public string Custom1
{
get
{
return mCustom1;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mCustom1!=value)
{
mCustom1 = value;
BrokenRules.Assert("Custom1Length",
"Error.Object.FieldLengthExceeded500,HeadOffice.Label.Custom1","Custom1",value.Length>500);
MarkDirty();
}
}
}
}
/// <summary>
/// Custom2
/// </summary>
public string Custom2
{
get
{
return mCustom2;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mCustom2!=value)
{
mCustom2 = value;
BrokenRules.Assert("Custom2Length",
"Error.Object.FieldLengthExceeded500,HeadOffice.Label.Custom2","Custom2",value.Length>500);
MarkDirty();
}
}
}
}
/// <summary>
/// Custom3
/// </summary>
public string Custom3
{
get
{
return mCustom3;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mCustom3!=value)
{
mCustom3 = value;
BrokenRules.Assert("Custom3Length",
"Error.Object.FieldLengthExceeded500,HeadOffice.Label.Custom3","Custom3",value.Length>500);
MarkDirty();
}
}
}
}
/// <summary>
/// Custom4
/// </summary>
public string Custom4
{
get
{
return mCustom4;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mCustom4!=value)
{
mCustom4 = value;
BrokenRules.Assert("Custom4Length",
"Error.Object.FieldLengthExceeded500,HeadOffice.Label.Custom4","Custom4",value.Length>500);
MarkDirty();
}
}
}
}
/// <summary>
/// Custom5
/// </summary>
public string Custom5
{
get
{
return mCustom5;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mCustom5!=value)
{
mCustom5 = value;
BrokenRules.Assert("Custom5Length",
"Error.Object.FieldLengthExceeded500,HeadOffice.Label.Custom5","Custom5",value.Length>500);
MarkDirty();
}
}
}
}
/// <summary>
/// Custom6
/// </summary>
public string Custom6
{
get
{
return mCustom6;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mCustom6!=value)
{
mCustom6 = value;
BrokenRules.Assert("Custom6Length",
"Error.Object.FieldLengthExceeded500,HeadOffice.Label.Custom6","Custom6",value.Length>500);
MarkDirty();
}
}
}
}
/// <summary>
/// Custom7
/// </summary>
public string Custom7
{
get
{
return mCustom7;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mCustom7!=value)
{
mCustom7 = value;
BrokenRules.Assert("Custom7Length",
"Error.Object.FieldLengthExceeded500,HeadOffice.Label.Custom7","Custom7",value.Length>500);
MarkDirty();
}
}
}
}
/// <summary>
/// Custom8
/// </summary>
public string Custom8
{
get
{
return mCustom8;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mCustom8!=value)
{
mCustom8 = value;
BrokenRules.Assert("Custom8Length",
"Error.Object.FieldLengthExceeded500,HeadOffice.Label.Custom8","Custom8",value.Length>500);
MarkDirty();
}
}
}
}
/// <summary>
/// Custom9
/// </summary>
public string Custom9
{
get
{
return mCustom9;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mCustom9!=value)
{
mCustom9 = value;
BrokenRules.Assert("Custom9Length",
"Error.Object.FieldLengthExceeded500,HeadOffice.Label.Custom9","Custom9",value.Length>500);
MarkDirty();
}
}
}
}
/// <summary>
/// Custom0
/// </summary>
public string Custom0
{
get
{
return mCustom0;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mCustom0!=value)
{
mCustom0 = value;
BrokenRules.Assert("Custom0Length",
"Error.Object.FieldLengthExceeded500,HeadOffice.Label.Custom0","Custom0",value.Length>500);
MarkDirty();
}
}
}
}
//Contract / bank stuff
/// <summary>
/// If true then banked service is tracked
/// for this object (hours / money / incidents)
/// </summary>
public bool UsesBanking
{
get
{
return mUsesBanking;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mUsesBanking!=value)
{
mUsesBanking = value;
MarkDirty();
}
}
}
}
/// <summary>
/// Contract that applies to this object
/// or Guid.Empty if no contract
/// </summary>
public Guid ContractID
{
get
{
return mContractID;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if(mContractID!=value)
{
mContractID = value;
MarkDirty();
}
}
}
}
/// <summary>
/// Date contract expires and no longer applies
/// or null if contract does not expire ever
/// </summary>
public object ContractExpires
{
get
{
return mContractExpires.DBValue;
}
set
{
if(bReadOnly)
ThrowSetError();
else
{
if (!AyaBizUtils.SmartDateEquals(mContractExpires, value)) //Case 298
{
mContractExpires.DBValue = value;
MarkDirty();
}
}
}
}
/// <summary>
/// If there is a contract and it has not expired returns true
/// otherwise false
/// </summary>
public bool ContractInEffect
{ //Added for Case 231
get
{
if (mContractID == Guid.Empty) return false;
if (mContractExpires.IsEmpty) return true;
if (mContractExpires.Date > DBUtil.CurrentWorkingDateTime) return true;
return false;
}
}
/// <summary>
/// Concatenated contact information from phone and email fields
/// </summary>
/// <returns></returns>
public string GetPrimaryContactDefaultContactInfo()
{
System.Text.StringBuilder b = new System.Text.StringBuilder();
b.Append(AyaBizUtils.SS(LocalizedTextTable.GetLocalizedTextDirect("HeadOffice.Label.Phone1") + ": ", Phone1, "\r\n"));
b.Append(AyaBizUtils.SS(LocalizedTextTable.GetLocalizedTextDirect("HeadOffice.Label.Phone2") + ": ", Phone2, "\r\n"));
b.Append(AyaBizUtils.SS(LocalizedTextTable.GetLocalizedTextDirect("HeadOffice.Label.Phone3") + ": ", Phone3, "\r\n"));
b.Append(AyaBizUtils.SS(LocalizedTextTable.GetLocalizedTextDirect("HeadOffice.Label.Phone4") + ": ", Phone4, "\r\n"));
b.Append(AyaBizUtils.SS(LocalizedTextTable.GetLocalizedTextDirect("HeadOffice.Label.Phone5") + ": ", Phone5, "\r\n"));
b.Append(AyaBizUtils.SS(LocalizedTextTable.GetLocalizedTextDirect("HeadOffice.Label.Email") + ": ", Email, "\r\n"));
return b.ToString();
}
/// <summary>
/// Flag - indicates if current user can open the wiki page for this object
/// See <see cref="WikiPage.ShowWikiLink(RootObjectTypes, Guid)"/> method for details
///
/// This is cached for the lifetime of this object
/// </summary>
public bool CanWiki//case 73
{
get
{
if (!bCanWiki.HasValue)
bCanWiki = WikiPage.ShowWikiLink(RootObjectTypes.HeadOffice, mID);
return bCanWiki.Value;
}
}
//cache the result in case the UI calls this repeatedly
private bool? bCanWiki = null;
/// <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.HeadOffice")
)
);
}
#endregion
#region System.object overrides
/// <summary>
///
/// </summary>
/// <returns></returns>
public override string ToString()
{
return "HeadOffice" + mID.ToString();
}
/// <summary>
///
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(Object obj)
{
if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false;
HeadOffice c=(HeadOffice)obj;
return mID==c.mID;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return ("HeadOffice"+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.HeadOffice")<(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 aRegionID, aCreated, aModified, aCreator, aModifier, aName, " +
"aContactNotes,aContact,aPhone1,aPhone2,aPhone3,aPhone4,aPhone5,aEmail, " +
"aWebAddress, aNotes, AACCOUNTNUMBER, aCustom1, aCustom3, " +
" aCustom4, aCustom2, aCustom5, aCustom6, aCustom7, aCustom8, " +
" aCustom9, aCustom0 FROM aHeadOffice WHERE (aID " +
"= @ID)"
,ID);
if(!dr.Read())
return new SearchResult();//DBUtil.ThrowFetchError("SearchResult for HeadOfficeID: " + ID.ToString());
if (!AyaBizUtils.InYourRegion(dr.GetGuid("aRegionID"))) return new SearchResult();//case 58
sr.Description=dr.GetString("aName");
sb.Append(sr.Description);
sb.Append(" ");
sb.Append(dr.GetString("aWebAddress"));
sb.Append(" ");
sb.Append(dr.GetString("aNotes"));
sb.Append(" ");
sb.Append(dr.GetString("aContactNotes"));
sb.Append(" ");
sb.Append(dr.GetString("aContact"));
sb.Append(" ");
sb.Append(dr.GetString("aPhone1"));
sb.Append(" ");
sb.Append(dr.GetString("aPhone2"));
sb.Append(" ");
sb.Append(dr.GetString("aPhone3"));
sb.Append(" ");
sb.Append(dr.GetString("aPhone4"));
sb.Append(" ");
sb.Append(dr.GetString("aPhone5"));
sb.Append(" ");
sb.Append(dr.GetString("aEmail"));
sb.Append(" ");
sb.Append(dr.GetString("AACCOUNTNUMBER"));
sb.Append(" ");
sb.Append(dr.GetString("aCustom0"));
sb.Append(" ");
sb.Append(dr.GetString("aCustom1"));
sb.Append(" ");
sb.Append(dr.GetString("aCustom2"));
sb.Append(" ");
sb.Append(dr.GetString("aCustom3"));
sb.Append(" ");
sb.Append(dr.GetString("aCustom4"));
sb.Append(" ");
sb.Append(dr.GetString("aCustom5"));
sb.Append(" ");
sb.Append(dr.GetString("aCustom6"));
sb.Append(" ");
sb.Append(dr.GetString("aCustom7"));
sb.Append(" ");
sb.Append(dr.GetString("aCustom8"));
sb.Append(" ");
sb.Append(dr.GetString("aCustom9"));
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();
}
//Added 02-Aug-2006 was not returning address search results :(
#region Address block
try
{
dr = DBUtil.GetReaderFromSQLString(
"SELECT adeliveryaddress, acity, astateprov, acountrycode, acountry, apostal " +
"from aaddress " +
"where " +
"arootobjectid=@ID "
, ID);
while (dr.Read())
{
sb.Append(" ");
sb.Append(dr.GetString("adeliveryaddress"));
sb.Append(" ");
sb.Append(dr.GetString("acity"));
sb.Append(" ");
sb.Append(dr.GetString("astateprov"));
sb.Append(" ");
sb.Append(dr.GetString("acountrycode"));
sb.Append(" ");
sb.Append(dr.GetString("acountry"));
sb.Append(" ");
sb.Append(dr.GetString("apostal"));
}
}
finally
{
if (dr != null) dr.Close();
}
#endregion
//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.HeadOffice;
return sr;
}
#endregion
#region Static methods
/// <summary>
/// Create new HeadOffice
/// </summary>
/// <returns>HeadOffice</returns>
public static HeadOffice NewItem()
{
HeadOffice c;
if(AyaBizUtils.Right("Object.HeadOffice")>(int)SecurityLevelTypes.ReadOnly)
{
c = new HeadOffice();
c.mGoToAddress=Address.NewItem();
c.mGoToAddress.RootObjectID=c.mID;
c.mGoToAddress.RootObjectType=RootObjectTypes.HeadOffice;
c.mGoToAddress.AddressType=AddressTypes.Physical;
c.mMailToAddress=Address.NewItem();
c.mMailToAddress.RootObjectID=c.mID;
c.mMailToAddress.RootObjectType=RootObjectTypes.HeadOffice;
c.mMailToAddress.AddressType=AddressTypes.Postal;
c.mDocs=AssignedDocs.NewItems();
return c;
}
else
throw new System.Security.SecurityException(
string.Format(
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToCreate"),
LocalizedTextTable.GetLocalizedTextDirect("O.HeadOffice")));
}
/// <summary>
/// Fetch existing HeadOffice
/// </summary>
/// <returns>HeadOffice</returns>
/// <param name="_ID">HeadOffice Guid</param>
public static HeadOffice GetItem(Guid _ID)
{
return GetItemMRU(_ID, true);
}
/// <summary>
/// Fetch existing HeadOffice don't track in <see cref="UserMRU"/> system
/// </summary>
/// <param name="_ID"></param>
/// <returns></returns>
public static HeadOffice GetItemNoMRU(Guid _ID)
{
return GetItemMRU(_ID, false);
}
/// <summary>
/// Get item, optionally track it in user MRU system
/// See <see cref="UserMRU"/>
/// </summary>
/// <param name="_ID"></param>
/// <param name="TrackMRU"></param>
/// <returns></returns>
public static HeadOffice GetItemMRU(Guid _ID, bool TrackMRU)
{
if (_ID == AyaBizUtils.NewObjectGuid)
return NewItem();
if (AyaBizUtils.Right("Object.HeadOffice") > (int)SecurityLevelTypes.NoAccess)
return (HeadOffice)DataPortal.Fetch(new Criteria(_ID, TrackMRU));
else
throw new System.Security.SecurityException(
string.Format(
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToRetrieve"),
LocalizedTextTable.GetLocalizedTextDirect("O.HeadOffice")));
}
/// <summary>
/// Delete HeadOffice
/// </summary>
/// <param name="_ID">HeadOffice GUID</param>
public static void DeleteItem(Guid _ID)
{
if(AyaBizUtils.Right("Object.HeadOffice")>(int)SecurityLevelTypes.ReadWrite)
DataPortal.Delete(new Criteria(_ID, true));
else
throw new System.Security.SecurityException(
string.Format(
LocalizedTextTable.GetLocalizedTextDirect("Error.Security.NotAuthorizedToDelete"),
LocalizedTextTable.GetLocalizedTextDirect("O.HeadOffice")));
}
/// <summary>
/// Check for the existance of a HeadOffice
/// in the database by ID OR by Name
/// </summary>
/// <param name="ID">Guid of HeadOffice or Guid.Empty if checking by name</param>
/// <param name="Name">Name of HeadOffice (case sensitive) or empty string "" if checking by ID</param>
/// <returns></returns>
public static bool Exists(Guid ID, string Name)
{
return HeadOfficeExistanceChecker.HeadOfficeExists(ID, Name);
}
//case 1404
/// <summary>
/// Get the ID of a HeadOffice from it's name
/// </summary>
/// <param name="Name">Name of HeadOffice (case sensitive) or empty string "" if checking by ID</param>
/// <returns>Guid empty if not found else the id of the HeadOffice with matching name</returns>
public static Guid IDFromName(string Name)
{
return HeadOfficeExistanceChecker.HeadOfficeExists(Name).mHeadOfficeID;
}
/// <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("AHEADOFFICE", "ANAME", Name);
}
#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 aHeadOffice WHERE aID=@ID;",crit.ID);
if(!dr.Read())
DBUtil.ThrowFetchError("HeadOffice 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");
//Custom fields
mCustom1=dr.GetString("aCustom1");
mCustom2=dr.GetString("aCustom2");
mCustom3=dr.GetString("aCustom3");
mCustom4=dr.GetString("aCustom4");
mCustom5=dr.GetString("aCustom5");
mCustom6=dr.GetString("aCustom6");
mCustom7=dr.GetString("aCustom7");
mCustom8=dr.GetString("aCustom8");
mCustom9=dr.GetString("aCustom9");
mCustom0=dr.GetString("aCustom0");
//HeadOffice fields
mActive=dr.GetBoolean("AACTIVE");
//Important: use property not internal field
//so that initial broken rule is unbroken on fetch
Name = dr.GetString("aName");
mWebAddress = dr.GetString("aWebAddress");
mContactNotes = dr.GetString("aContactNotes");
mContact = dr.GetString("aContact");
mPhone1 = dr.GetString("aPhone1");
mPhone2 = dr.GetString("aPhone2");
mPhone3 = dr.GetString("aPhone3");
mPhone4 = dr.GetString("aPhone4");
mPhone5 = dr.GetString("aPhone5");
mEmail = dr.GetString("aEmail");
mClientGroupID = dr.GetGuid("aClientGroupID");
mNotes = dr.GetString("aNotes");
mRegionID = dr.GetGuid("aRegionID");
mAccountNumber = dr.GetString("AACCOUNTNUMBER");
//Contract / Bank
mUsesBanking = dr.GetBoolean("aUsesBanking");
mContractID = dr.GetGuid("aContractID");
mContractExpires = DBUtil.ToLocal(dr.GetSmartDate("aContractExpires"));
if (dr != null) dr.Close();
/*
* Load child collection objects
*/
//GoToAddress
dr=DBUtil.GetReaderFromSQLString("SELECT * " +
"FROM AADDRESS WHERE aRootObjectID=@ID AND " +
"aRootObjectType=5 AND AADDRESSTYPE=2",crit.ID);
if(dr.Read())
mGoToAddress=Address.GetItem(dr);
if(dr!=null) dr.Close();
//MailToAddress
dr=DBUtil.GetReaderFromSQLString("SELECT * " +
"FROM AADDRESS WHERE aRootObjectID=@ID AND " +
"aRootObjectType=5 AND AADDRESSTYPE=1",crit.ID);
if(dr.Read())
mMailToAddress=Address.GetItem(dr);
if(dr!=null) dr.Close();
//Docs
dr=DBUtil.GetReaderFromSQLString("SELECT * FROM AASSIGNEDDOC WHERE (aRootObjectID=@ID and aRootObjectType=5);",crit.ID);
mDocs = AssignedDocs.GetItems(dr, RootObjectTypes.HeadOffice, RootObjectTypes.HeadOffice);
if(dr!=null) dr.Close();
}
finally
{
if(dr!=null) dr.Close();
}
MarkOld();
if(crit.TrackMRU)
if(AyaBizUtils.AllowAutomaticMRUOnUpdate) AyaBizUtils.MRU.Add(RootObjectTypes.HeadOffice, mID);
//Get access rights level
bReadOnly=AyaBizUtils.Right("Object.HeadOffice")<(int)SecurityLevelTypes.ReadWrite;
}
#endregion
#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,"aHeadOffice");
#region Delete
if(IsDeleted)
{
if(!IsNew)
{
//Delete object and child objects
DBCommandWrapper cmDelete = DBUtil.GetCommandFromSQL("DELETE FROM aHeadOffice WHERE aID = @ID;");
cmDelete.AddInParameter("@ID",DbType.Guid,this.mID);
//Delete all address record types with matching root object ID
DBCommandWrapper cmDeleteAddress = DBUtil.GetCommandFromSQL("DELETE FROM AADDRESS WHERE aRootObjectID = @ID;");
cmDeleteAddress.AddInParameter("@ID",DbType.Guid,this.mID);
using (IDbConnection connection = DBUtil.DB.GetConnection())
{
connection.Open();
IDbTransaction transaction = connection.BeginTransaction();
try
{
DBUtil.RemoveKeywords(transaction,RootObjectTypes.HeadOffice,this.mID);
DBUtil.RemoveDocs(transaction,RootObjectTypes.HeadOffice,this.mID);
DBUtil.DB.ExecuteNonQuery(cmDeleteAddress, transaction);
DBUtil.DB.ExecuteNonQuery(cmDelete, transaction);
// 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 aHeadOffice (aID, AACTIVE, aName, aWebAddress, " +
"aClientGroupID, aNotes, aRegionID, AACCOUNTNUMBER, aCreated,aModified, " +
"aCreator,aModifier, aCustom1, aCustom2, aCustom3, aCustom4, aCustom5, aCustom6, aCustom7, aCustom8, aCustom9, aCustom0, " +
"aUsesBanking, aContractID, aContractExpires, aContactNotes, aContact, aPhone1, aPhone2, aPhone3, aPhone4, aPhone5, aEmail) VALUES " +
"(@ID,@Active,@Name,@WebAddress, @ClientGroupID,@Notes,@RegionID,@AccountNumber, " +
"@Created, @Modified, @CurrentUserID,@CurrentUserID, " +
"@Custom1,@Custom2,@Custom3,@Custom4,@Custom5,@Custom6,@Custom7,@Custom8,@Custom9,@Custom0, " +
"@UsesBanking, @ContractID, @ContractExpires, @ContactNotes, @Contact, @Phone1, @Phone2, @Phone3, @Phone4, @Phone5, @Email)"
);
else
cm=DBUtil.GetCommandFromSQL(
"UPDATE aHeadOffice SET aID=@ID, AACTIVE=@Active, aName=@Name, " +
"aWebAddress=@WebAddress, aClientGroupID=@ClientGroupID, " +
"aNotes=@Notes, " +
"aRegionID=@RegionID, AACCOUNTNUMBER=@AccountNumber, " +
"aModifier=@CurrentUserID, " +
"aModified=@Modified, aCustom1=@Custom1, aCustom2=@Custom2, " +
"aCustom3=@Custom3, aCustom4=@Custom4, " +
"aCustom5=@Custom5, aCustom6=@Custom6, aCustom7=@Custom7, " +
"aCustom8=@Custom8, aCustom9=@Custom9, " +
"aCustom0=@Custom0, aUsesBanking=@UsesBanking, " +
"aContractID=@ContractID, aContractExpires=@ContractExpires, " +
"aContactNotes=@ContactNotes, " +
"aContact=@Contact, aPhone1=@Phone1, aPhone2=@Phone2, aPhone3=@Phone3, aPhone4=@Phone4, aPhone5=@Phone5, aEmail=@Email " +
"WHERE aID=@ID"
);
cm.AddInParameter("@ID",DbType.Guid,mID);
cm.AddInParameter("@Active",DbType.Boolean, mActive);
cm.AddLargeStringInParameter("@Notes", mNotes);
cm.AddInParameter("@RegionID",DbType.Guid, mRegionID);
cm.AddInParameter("@Name",DbType.String, mName);
cm.AddInParameter("@WebAddress",DbType.String, mWebAddress);
cm.AddInParameter("@ClientGroupID",DbType.Guid,mClientGroupID);
cm.AddInParameter("@AccountNumber",DbType.String, mAccountNumber);
cm.AddInParameter("@UsesBanking",DbType.Boolean, mUsesBanking);
cm.AddInParameter("@ContractID",DbType.Guid,mContractID);
//case 763
if (mContractExpires.IsEmpty)
mContractExpires.Date = new DateTime(2100, 1, 1);
cm.AddInParameter("@ContractExpires",DbType.DateTime, mContractExpires.DBValue);
cm.AddLargeStringInParameter("@ContactNotes", mContactNotes);
cm.AddInParameter("@Contact", DbType.String, mContact);
cm.AddInParameter("@Phone1", DbType.String, mPhone1);
cm.AddInParameter("@Phone2", DbType.String, mPhone2);
cm.AddInParameter("@Phone3", DbType.String, mPhone3);
cm.AddInParameter("@Phone4", DbType.String, mPhone4);
cm.AddInParameter("@Phone5", DbType.String, mPhone5);
cm.AddInParameter("@Email", DbType.String, mEmail);
//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));
//Custom fields
cm.AddLargeStringInParameter("@Custom1", mCustom1);
cm.AddLargeStringInParameter("@Custom2", mCustom2);
cm.AddLargeStringInParameter("@Custom3", mCustom3);
cm.AddLargeStringInParameter("@Custom4", mCustom4);
cm.AddLargeStringInParameter("@Custom5", mCustom5);
cm.AddLargeStringInParameter("@Custom6", mCustom6);
cm.AddLargeStringInParameter("@Custom7", mCustom7);
cm.AddLargeStringInParameter("@Custom8", mCustom8);
cm.AddLargeStringInParameter("@Custom9", mCustom9);
cm.AddLargeStringInParameter("@Custom0", mCustom0);
using (IDbConnection connection = DBUtil.DB.GetConnection())
{
connection.Open();
IDbTransaction transaction = connection.BeginTransaction();
try
{
DBUtil.DB.ExecuteNonQuery(cm, transaction);
//Update child objects
//GoToAddress
mGoToAddress.Update(RootObjectTypes.HeadOffice,mID,AddressTypes.Physical,transaction);
//MailToAddress
mMailToAddress.Update(RootObjectTypes.HeadOffice,mID,AddressTypes.Postal,transaction);
//Docs
mDocs.Update(transaction);
//Process keywords
DBUtil.ProcessKeywords(transaction,this.mID,RootObjectTypes.HeadOffice,IsNew,AyaBizUtils.Break(false,
mAccountNumber,mGoToAddress.FullAddress,mMailToAddress.FullAddress,
mName,mNotes, mContactNotes, mWebAddress,
mContact, mPhone1, mPhone2, mPhone3, mPhone4, mPhone5, mEmail,
/*Custom fields*/
mCustom1,mCustom2,mCustom3,mCustom4,mCustom5,mCustom6,mCustom7,mCustom8,mCustom9,mCustom0));
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;
if(AyaBizUtils.AllowAutomaticMRUOnUpdate) AyaBizUtils.MRU.Add(RootObjectTypes.HeadOffice, mID);
}
#endregion
}
#endregion update
#region Delete
/// <summary>
/// Remove a HeadOffice record .
/// </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 aHeadOffice WHERE aID = @ID;");
cmDelete.AddInParameter("@ID",DbType.Guid,crit.ID);
//Delete all address record types with matching root object ID
DBCommandWrapper cmDeleteAddress = DBUtil.GetCommandFromSQL("DELETE FROM AADDRESS WHERE aRootObjectID = @ID;");
cmDeleteAddress.AddInParameter("@ID",DbType.Guid,crit.ID);
using (IDbConnection connection = DBUtil.DB.GetConnection())
{
connection.Open();
IDbTransaction transaction = connection.BeginTransaction();
try
{
DBUtil.RemoveKeywords(transaction,RootObjectTypes.HeadOffice,crit.ID);
DBUtil.RemoveDocs(transaction,RootObjectTypes.HeadOffice,crit.ID);
DBUtil.DB.ExecuteNonQuery(cmDeleteAddress, transaction);
DBUtil.DB.ExecuteNonQuery(cmDelete, transaction);
// Commit the transaction
transaction.Commit();
}
catch
{
// Rollback transaction
transaction.Rollback();
throw;
}
finally
{
connection.Close();
}
AyaBizUtils.MRU.Remove(RootObjectTypes.HeadOffice, crit.ID);
}
//-----------------------------
}
#endregion delete
#endregion
#region Override IsValid / IsDirty
//Override base class version if there are child objects
/// <summary>
///
/// </summary>
public override bool IsValid
{
get
{
return base.IsValid &&
mGoToAddress.IsValid && mMailToAddress.IsValid&&
mDocs.IsValid;
}
}
/// <summary>
///
/// </summary>
public override bool IsDirty
{
get
{
return base.IsDirty ||
mGoToAddress.IsDirty || mMailToAddress.IsDirty || mDocs.IsDirty;
}
}
#endregion
#region criteria
/// <summary>
/// Criteria for identifying existing object
/// </summary>
[Serializable]
private class Criteria
{
public Guid ID;
public bool TrackMRU;
public Criteria(Guid _ID, bool _TrackMRU)
{
ID = _ID;
TrackMRU = _TrackMRU;
}
}
#endregion
}//end HeadOffice
}//end namespace GZTW.AyaNova.BLL