Thursday, February 21, 2008

working with Entity Class in C#

I got a chance to work with OOPS concept Entity class
Entity class

public class EmailTemplate
{
#region Properties
private long _Id;
///
/// Gets/Sets the flag for Id
///

public long Id
{
get { return _Id; }
set { _Id = value; }
}
private string _TemplateName;
///
/// Gets/Sets the flag for TemplateName
///

public string TemplateName
{
get { return _TemplateName; }
set { _TemplateName = value; }
}

private string _Template;
///
/// Gets/Sets the flag for Template
///

public string Template
{
get { return _Template; }
set { _Template = value; }
}


private string _Site;
///
/// Gets/Sets the flag for Site
///

public string Site
{
get { return _Site; }
set { _Site = value; }
}

///
/// Gets/Sets the flag for IsManageable
///

private bool _IsManageable = true ;
public bool IsManageable
{
get { return _IsManageable; }
set { _IsManageable = value; }
}

///
/// Gets/Sets the flag for IsArchived
///

private bool _IsArchived = false ;
public bool IsArchived
{
get { return _IsArchived; }
set { _IsArchived = value; }
}

///
/// Gets the flag for IsDeleted
///

private bool _IsDeleted = false ;
public bool IsDeleted
{
get { return _IsDeleted; }
}


///
/// Gets/Sets the flag for IsPublished
///

private bool _IsDeployed = true ;
public bool IsDeployed
{
get { return _IsDeployed; }
set { _IsDeployed = value; }
}


#endregion

}




Business Layer

EmailTemplateDL dbLayer = new EmailTemplateDL();
EmailEntity.EmailTemplate emailTemplateEntity = new Palladium.Custom.Entities.EmailTemplate.EmailTemplate();
Helper.EmailTemplate helper = new Palladium.Custom.Helpers.EmailTemplate.EmailTemplate();


///
/// Retrievs All Templates
///

/// Table of Templates
public DataTable GetAllTemplates(EmailEntity.EmailTemplate objEntity)
{
DataTable dtTemplates = null;
Guid userGUID = helper.getUserGUID(); // dbLayer.getUserGUID();
string userName = "user"; // ToDo : get the user

if (!string.IsNullOrEmpty(objEntity.Site) && userGUID != Guid.Empty && !string.IsNullOrEmpty(userName))
{
// Call Get All method of the Data Access Layer
dtTemplates = dbLayer.GetAllTemplate(objEntity.Site, userGUID, userName);
}
return dtTemplates;
}





///
/// save records
///

///
///
public bool SaveTemplate( EmailEntity.EmailTemplate objEntity)
{
bool isSaved = false;
Guid userGUID = helper.getUserGUID() ; // dbLayer.getUserGUID();
string userName = "user"; // ToDo : get the user
if (!string.IsNullOrEmpty(objEntity.TemplateName) && !string.IsNullOrEmpty(objEntity.Template) && userGUID != Guid.Empty && !string.IsNullOrEmpty(objEntity.Site))
{
if (objEntity.Id != null && objEntity.Id > 0)
{
isSaved = dbLayer.UpdateTemplate(objEntity.Site, objEntity.Id, objEntity.TemplateName, objEntity.Template, objEntity.IsManageable, objEntity.IsArchived, userGUID, userName);
}
else
{
isSaved = dbLayer.InsertTemplate(objEntity.Site, objEntity.TemplateName, objEntity.Template, objEntity.IsManageable, userGUID, userName);
}
}
return isSaved;
}
///
/// mark isdeleted = 1 for records
///

///
///
public bool DeleteTemplate(EmailEntity.EmailTemplate objEntity)
{
bool isDeleted = false;
Guid userGUID = helper.getUserGUID(); // dbLayer.getUserGUID();
string userName = "user"; // ToDo : get the user

if (objEntity.Id != null && objEntity.Id > 0 && userGUID != Guid.Empty && !string.IsNullOrEmpty(objEntity.Site) && !string.IsNullOrEmpty(userName))
{
isDeleted = dbLayer.DeleteTemplate(objEntity.Site, objEntity.Id, userGUID, userName);
}
return isDeleted;
}


///
/// get the data by passing template name
///

///
///
public EmailEntity.EmailTemplate GetDatabyTemplateName(EmailEntity.EmailTemplate objEntity)
{
Guid userGUID = helper.getUserGUID(); // dbLayer.getUserGUID();
string userName = "user"; // ToDo : get the user
if (!string.IsNullOrEmpty(objEntity.TemplateName) && userGUID != Guid.Empty && !string.IsNullOrEmpty(objEntity.Site) && !string.IsNullOrEmpty(userName))
{
return dbLayer.GetDatabyTemplateName(objEntity.Site, objEntity.TemplateName, userGUID, userName);
}
else
{
return null;
}

}

///
/// To get the data by passing id
///

///
/// return entity object
public EmailEntity.EmailTemplate GetDatabyId(EmailEntity.EmailTemplate objEntity)
{
Guid userGUID = helper.getUserGUID(); // dbLayer.getUserGUID();
string userName = "user"; // ToDo : get the user
if (objEntity.Id != null && objEntity.Id > 0 && userGUID != Guid.Empty && !string.IsNullOrEmpty(objEntity.Site) && !string.IsNullOrEmpty(userName))
{
return dbLayer.GetDatabyId(objEntity.Site ,objEntity.Id,userGUID,userName);
}
else
{
return null;
}

}




Database Layer



public class EmailTemplateDL
{


///
/// insert records
///

///
///
///
///
///
///
///
public bool InsertTemplate(string site,string templateName, string template, bool isManageable, Guid createdById,string createdByUser)
{
bool isSaved = false;
if (!string.IsNullOrEmpty(site) && !string.IsNullOrEmpty(templateName) && !string.IsNullOrEmpty(template) && createdById != Guid.Empty && !string.IsNullOrEmpty(createdByUser) )
{
string spName = "PalEmailTemplatesCreate";
object[] objParameters = new object[6];
objParameters[0] = site ;
objParameters[1] = templateName;
objParameters[2] = template;
objParameters[3] = isManageable;
objParameters[4] = createdById;
objParameters[5] = createdByUser; // ToDo : get user name from helper class
StoredProcedure sp = new StoredProcedure();
int recordsaffected = -3;
recordsaffected = sp.ExecuteNonQuery(spName, objParameters);

// checking for -1 since the strored procdure is returning -1 for successfull transaction
// ToDo: change the stored procedure such that it returns 1 on success
if (recordsaffected > 0 || recordsaffected == -1)
{
isSaved = true;
}
sp = null;
}
return isSaved;
}

///
/// update the records
///

///
///
///
///
///
///
///
///
///
public bool UpdateTemplate(string site, long id, string templateName, string template, bool isManageable, bool isArchived, Guid modifiedById,string modifiedByUser)
{
bool isSaved = false;
if (!string.IsNullOrEmpty(site) && id != null && !string.IsNullOrEmpty(templateName) && modifiedById != Guid.Empty && !string.IsNullOrEmpty(modifiedByUser))
{
string spName = "PalEmailTemplatesUpdate";
object[] objParameters = new object[8];
objParameters[0] = site ;
objParameters[1] = id;
objParameters[2] = templateName;
objParameters[3] = template;
objParameters[4] = isManageable;
objParameters[5] = isArchived;
objParameters[6] = modifiedById;
objParameters[7] = modifiedByUser;
StoredProcedure sp = new StoredProcedure();
sp.ExecuteNonQuery(spName, objParameters);
int recordsaffected = -3;
recordsaffected = sp.ExecuteNonQuery(spName, objParameters);
// checking for -1 since the strored procdure is returning -1 for successfull transaction
// ToDo: change the stored procedure such that it returns 1 on success
if (recordsaffected > 0 || recordsaffected == -1)
{
isSaved = true;
}
sp = null;
}
return isSaved;
}




///
/// To delete records
///

///
///
///
///
///
public bool DeleteTemplate(string site ,long id, Guid modifiedById, string modifiedByUser)
{
bool isDeleted = false;
if (!string.IsNullOrEmpty(site) && id != null && modifiedById != Guid.Empty && !string.IsNullOrEmpty(modifiedByUser))
{
string spName = "PalEmailTemplatesDelete";
object[] objParameters = new object[4];
objParameters[0] = site;
objParameters[1] = id;
objParameters[2] = modifiedById;
objParameters[3] = modifiedByUser ;
StoredProcedure sp = new StoredProcedure();
int recordsaffected = -3;
// checking for -1 since the strored procdure is returning -1 for successfull transaction
// ToDo: change the stored procedure such that it returns 1 on success
recordsaffected = sp.ExecuteNonQuery(spName, objParameters);
if (recordsaffected > 0 || recordsaffected == -1)
{
isDeleted = true;
}
sp = null;
}
return isDeleted;
}

///
/// Retrievs all email template, marked as not deleted
///

/// Data table
public DataTable GetAllTemplate(string site,Guid lastModifiedByID,string lastModifiedUser)
{
DataTable returnData = null;

string spName = "PalEmailTemplatesList";

// Build the storedprocedure input parameters
object[] spValues = new object[3];
spValues[0] = site;
spValues[1] = lastModifiedByID;
spValues[2] = lastModifiedUser;

//call datalayer method to update the database
Palladium.Data.Database.StoredProcedure sp = new StoredProcedure();
DataSet dsTemplates = sp.ExecuteDataset(spName, spValues);
if (dsTemplates != null && dsTemplates.Tables.Count > 0)
{
returnData = dsTemplates.Tables[0];
}
sp = null;

return returnData;
}

///
/// Retrieves the value of the name
///

///
///
/// Value of the property
public string GetTemplate(string templateName)
{
string returnData = string.Empty;
if (!string.IsNullOrEmpty(templateName))
{
IDataReader dr = null;
string spName = "PalEmailTemplatesTemplateNameOutput";
object[] objParameters = new object[2];
objParameters[0] = templateName;
StoredProcedure sp = new StoredProcedure();
dr = sp.ExecuteReader(spName, objParameters);
if (dr != null)
{
if (dr.Read())
{
if (Convert.ToBoolean(dr["IsDeleted"]) == false)
{
returnData = dr["Template"] as string;
}
}
dr.Dispose();
dr = null;
}
sp = null;
}
return returnData;
}


///
/// Get the data by passing template name
///

///
///
///
///
///
public EmailEntity.EmailTemplate GetDatabyTemplateName(string site, string templateName, Guid lastModifiedByID, string lastModifiedUser)
{
EmailEntity.EmailTemplate EmailEntity = new Palladium.Custom.Entities.EmailTemplate.EmailTemplate();
if (!string.IsNullOrEmpty(templateName) && lastModifiedByID != Guid.Empty && !string.IsNullOrEmpty(site) && !string.IsNullOrEmpty(lastModifiedUser))
{
StoredProcedure sp = null;
try
{
sp = new StoredProcedure();
string spName = "PalEmailTemplatesTemplateNameOutput";
object[] objParameters = new object[9];
objParameters[0] = site; //site
objParameters[1] = DBNull.Value; // id
objParameters[2] = templateName; // @TemplateName
objParameters[3] = DBNull.Value; // @Template
objParameters[4] = DBNull.Value; // @IsManageable
objParameters[5] = DBNull.Value; // @IsArchived
objParameters[6] = DBNull.Value; // @@IsDeployed
objParameters[7] = lastModifiedByID; // @@LastModifiedById
objParameters[8] = lastModifiedUser; // @LastModifiedByUserName

Hashtable htOutputParameters = null;
htOutputParameters = sp.ExecuteStoredProcedureGetOutputParameters(spName, objParameters);
if (htOutputParameters != null)
{
int outputParametersCount = htOutputParameters.Count;
if (outputParametersCount > 0)
{
string[] keys = new string[htOutputParameters.Keys.Count];
htOutputParameters.Keys.CopyTo(keys, 0);
for (int index = 0; index < outputParametersCount; index++)
{
object keyValue = htOutputParameters[keys[index]];

if (keys[index].Substring(1).ToLower() == "id")
{
long id = 0;
try
{
long.TryParse(keyValue.ToString(), out id);
}
catch { }
if( id > 0 )
EmailEntity.Id = id;
}
else if (keys[index].Substring(1).ToLower() == "template")
{
EmailEntity.Template = keyValue.ToString();
}
else if (keys[index].Substring(1).ToLower() == "ismanageable")
{
if (keyValue.ToString().ToLower() == "true")
{
EmailEntity.IsManageable = true;
}
else
{
EmailEntity.IsManageable = false;
}
}
else if (keys[index].Substring(1).ToLower() == "isarchived")
{
if (keyValue.ToString().ToLower() == "true")
{
EmailEntity.IsArchived = true;
}
else
{
EmailEntity.IsArchived = false;
}
}
else if (keys[index].Substring(1).ToLower() == "isdeployed")
{
if (keyValue.ToString().ToLower() == "true")
{
EmailEntity.IsDeployed = true;
}
else
{
EmailEntity.IsDeployed = false;
}
}

}
}
htOutputParameters.Clear();
htOutputParameters = null;
}
}
catch (Exception ex)
{
return null;
throw ex;
}
finally
{
if (sp != null)
sp = null;
}
}
else
{
throw new Exception("Not able to set properties");
}
return EmailEntity;
}


///
/// Get data by passing id
///

///
///
///
///
///
public EmailEntity.EmailTemplate GetDatabyId(string site, long id, Guid lastModifiedByID, string lastModifiedUser)
{
EmailEntity.EmailTemplate EmailEntity = new Palladium.Custom.Entities.EmailTemplate.EmailTemplate();
if (!object.Equals(null, id) && id > 0 && lastModifiedByID != Guid.Empty && !string.IsNullOrEmpty(site) && !string.IsNullOrEmpty(lastModifiedUser))
{
StoredProcedure sp = null;
try
{
sp = new StoredProcedure();
string spName = "PalEmailTemplatesIdOutput";
object[] objParameters = new object[9];
objParameters[0] = site; //site
objParameters[1] = id; // id
objParameters[2] = DBNull.Value; // @TemplateName out
objParameters[3] = DBNull.Value; // @Template
objParameters[4] = DBNull.Value; // @IsManageable
objParameters[5] = DBNull.Value; // @IsArchived
objParameters[6] = DBNull.Value; // @@IsDeployed
objParameters[7] = lastModifiedByID; // @@LastModifiedById
objParameters[8] = lastModifiedUser; // @LastModifiedByUserName

Hashtable htOutputParameters = null;
htOutputParameters = sp.ExecuteStoredProcedureGetOutputParameters(spName, objParameters);
if (htOutputParameters != null)
{
int outputParametersCount = htOutputParameters.Count;
if (outputParametersCount > 0)
{
string[] keys = new string[htOutputParameters.Keys.Count];
htOutputParameters.Keys.CopyTo(keys, 0);
for (int index = 0; index < outputParametersCount; index++)
{
object keyValue = htOutputParameters[keys[index]];

if (keys[index].Substring(1).ToLower() == "templatename")
{
EmailEntity.TemplateName = keyValue.ToString();
}
else if (keys[index].Substring(1).ToLower() == "template")
{
EmailEntity.Template = keyValue.ToString();
}
else if (keys[index].Substring(1).ToLower() == "ismanageable")
{
if (keyValue.ToString().ToLower() == "true")
{
EmailEntity.IsManageable = true;
}
else
{
EmailEntity.IsManageable = false;
}
}
else if (keys[index].Substring(1).ToLower() == "isarchived")
{
if (keyValue.ToString().ToLower() == "true")
{
EmailEntity.IsArchived = true;
}
else
{
EmailEntity.IsArchived = false;
}
}
else if (keys[index].Substring(1).ToLower() == "isdeployed")
{
if (keyValue.ToString().ToLower() == "true")
{
EmailEntity.IsDeployed = true;
}
else
{
EmailEntity.IsDeployed = false;
}
}

}
}
htOutputParameters.Clear();
htOutputParameters = null;
}
}
catch (Exception ex)
{
return null;
throw ex;
}
finally
{
if (sp != null)
sp = null;
}
}
else
{
throw new Exception("Not able to set properties");
}
return EmailEntity;
}

Friday, February 15, 2008

Exception Handling & Logging Application Block: Enterprise Library

I got a chance to work on POC on Logging and Exception handling using Enterprise Library method,... It was cool stuff ....

After so many R&D ,...I am able to configure the config to specify log file path and other stuff for Exception and log

below is the sample web.config which says the methods how we can configure








defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">

header="---------------------------------------- DBLayerException Log"
footer="---------------------------------------- Copy Right : DBLayer2008"
formatter="Text Formatter" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
traceOutputOptions="DateTime" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="StarMark Exception FlatFile TraceListener" />
header="---------------------------------------- Country List Logger"
footer="----------------------------------------Copy Right DBLayer"
formatter="Text Formatter" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
traceOutputOptions="DateTime" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="Starmark FlatFile TraceListener" />
log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="Formatted EventLog TraceListener" />
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.XmlTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.XmlTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="Starmark XML Trace Listener" />


type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="Text Formatter" />





































postHandlingAction="NotifyRethrow" name="Exception">

severity="Error" title="Enterprise Library Starmark Exception Handling"
formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
priority="0" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="Starmark Exception Log Handler" />







providerName="System.Data.SqlClient" />












Check this links
http://www.codeproject.com/KB/architecture/GetLoggingWithEntLib.aspx
http://www.codeproject.com/KB/aspnet/ExceptionHandling.aspx
for more details.........

Kodachadri Trip

After so many R&D , As per the Plan we statrted our Kodachadri trip fom Bangalore on Friday 4th January night and reached Shimoga in the morning about 5.30 in Shimoga passanger train.
booked a room in shimoga for 1hr and finished all morning work,and early morning i somehow managed to get a small bottel..since everybody is telling we wont get anything there in kodachadri

From shimoga, we took a private bus to Hosa nagara, around 12.30 the bus people dropped at the place Kareghatta where mud road start..
And our trecking started from there,
everybody started with full energy and josh. On the way we had lunch chapathi,fruits .
After 3 hour journey everybody was so tiered and some how managed to reach till Kaka's hotel and had lime juice.

We enquired about availability of jeep from there and got to know that, no way of getting jeep from that place, we have to walk till the top.

Everybody started scolding sandeep,...again our trecking started,..as we go to the top, everybody was so tiered that we used to take rest for about 10 mins walk. later we reduced our luggage by finishing the food items.

The condition of some people in the group has become worst,we thougt we should carry dilip to top,as he was fully gone.
but somehow he managed to come. we got a samll hotel at the top from where we can see Bhats house, we again had lime juice and started the journey.
the scenary was too good, but we are not in a condition to enjoy the nature at that time.

When we are walking we saw some local people from shimoga surrounding, who are walking easily there with out any struggle.

At last we reached the top around 6.30 pm , as soon as we go there one person asked us about staying in his house,but we already decided to stay in bhats house.

Sandeep has somehow managed to collect bhats son mobile no. who is working in bangalore as system admin, and he is a classmate of our Praveen kulakarni.

So, with this link we approached to bhats house and we agreed to all there conditions. dropped all our luggage


To be continued..............