Tuesday, April 7, 2009

A Utility Class of Tools

This is a class of code I use all the time, sjm_tools The test harness is written as a Windows Form Application. Coding environment Visual Studio 2008, Visual-Basic .NET, .NET Framework 2.0 SP 1

In the designer create a WFA form, with a listbox, and 3 buttons. Properties:
Form1 = 450, 255
Form1.name = form1
form1.text = "Test harness for sjm_tools"

Listbox.name = List1
List1.location = 10,10
list1.size = 300,200

Button1.Name = btnFilename
btnFilename.location = 315,10
btnFilename.size = 70,25
btnFilename.Text = File Name

Button2.Name = btnSendEmail
btnSendEmail.location = 315,41
btnSendEmail.size = 70,25
btnSendEmail.Text = Send Email

Button3.Name = btnWriteToErrorLog
btnWriteToErrorLog.location = 315,72
btnWriteToErrorLog.size = 70,25
btnWriteToErrorLog.Text = Error Log

Now open the form1.vb code file, copy and paste the following code into it.
Imports System.Net.Mail
Imports System.IO
Imports System.Xml
Public Class Form1
' ------------------------------------------------------
' The Test Harness for the class sjm_tools
' ------------------------------------------------------
Dim myTools As New Sjm_Tools
Private Sub btnFilename_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnFilename.Click
Dim i As Integer
Dim s1 As String = "File_"
Dim sExt As String = ".txt"
For i = 0 To 10
List1.Items.Add(myTools.UniqueFilname(s1, sExt))
System.Windows.Forms.Application.DoEvents()
System.Threading.Thread.Sleep(10)
Next
End Sub
Private Sub btnWriteToErrorLog_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnWriteToErrorLog.Click
Dim sLogName As String
dim i as integer
sLogName = My.Application.Info.DirectoryPath & "\" & _
myTools.UniqueFilname("ErrLog_", ".txt")
for i = 1 to 5
myTools.WriteToErrorLog(sLogName, "Sample Text" & i, _
"Error Description" & i)
next
List1.Items.Clear()
List1.Items.Add("Finished - " & System.IO.Path.GetFileName(sLogName))
End Sub
Private Sub btnSendEmail_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSendEmail.Click
' ----------------------------------------------------------
' Note. My mailhost has been removed from this sample.
' You will have to substitute your own by filling in the property
' Typically find it in your email client configuration.
' Example - myIsp.myMailhost.myDomain.com
' ------------------------------------------------------
' My names for To and From targets have been removed.
' Substitute your own on the next two lines for testing
' ------------------------------------------------------
Dim sMailTo As String = "The_Target_Name@TheTargetDomain.xxx"
Dim smailFrom As String = "Your_From_Name@YourSendingDomain.xxx"
Dim sSubject As String = "The email title string"
Dim sBody As String
Dim sLog As String
myTools.MailHost() = "myIsp.myMailhost.myDomain.com"
sLog = My.Application.Info.DirectoryPath & "\"
sLog = sLog & myTools.UniqueFilname("ErrLog_", ".txt")
sBody = "Hi There, glad that you could stick around" & vbCrLf
sBody = sBody & "Introducing Mr Larry Bohay Noel on Rhythm Pole." & vbCrLf
sBody = sBody & "Have to keep myself amused when writing test harnesses."
If (myTools.SendEmail(smailFrom, sMailTo, sSubject, sBody, sLog) <> 0) Then
List1.Items.Add("SendEmail Error - see log file")
List1.Items.Add(System.IO.Path.GetFileName(sLog))
End If
List1.Items.Add("Email sent.")
End Sub
' End of Test Harness Class
End Class

Public Class Sjm_Tools
'------------------------------------------------------------------------
' Scope: A collection of tools that are used in various applications
' Author: Scott Mathews - 03Jan2009
'------------------------------------------------------------------------
' Programming Environment = Visual Studio 2008
' = .Net Framework 2.0
' = Visual Basic
' Please feel free to borrow and use.
' Contact me at mathewssj@gmail.com if you would like an addition
' -----------------------------------------------------------------------
Private m_MailHost As String = ""
' -----------------------------------------------------------------------
Public Function UniqueFilname(ByVal prefix As String, _
ByVal extension As String) As String
'--------------------------------------------------------------------
' Scope: Returns a date stamped file name string
' Author: Scott Mathews - 03Jan2009
' -------------------------------------------------------------------
' Constraints - Needs a 10 milli-second delay between calls to
' ensure a unique value
' -------------------------------------------------------------------
Dim sBuff As String
sBuff = prefix & Now() & Now.Millisecond() & extension
sBuff = Replace(sBuff, " ", "")
sBuff = Replace(sBuff, ":", "")
sBuff = Replace(sBuff, "/", "")
Return sBuff
End Function
'----------------------------------------------------------
Public Function SendEmail(ByVal sFrom As String, ByVal sTo As String, _
ByVal sSubject As String, ByVal sBody As String, _
ByVal sErrLog As String) As Integer
'--------------------------------------------------------------------
' This function uses the VB.NET mail system to send a status
' email to the user
' -------------------------------------------------------------------
' Return: 0 = success
' Return: -1 = failure
' -------------------------------------------------------------------
Dim smtp As New System.Net.Mail.SmtpClient
Dim msgMail As New MailMessage(New MailAddress(sFrom), _
New MailAddress(sTo))
Try
With msgMail
.Subject = sSubject
.Body = sBody
End With
' Set the mail host
smtp.Host() = Me.MailHost.ToString
smtp.Send(msgMail)
Catch ex As Exception
WriteToErrorLog(sErrLog, "Email send message failed = ", _
ex.Message)
msgMail.Dispose()
smtp = Nothing
Return -1
End Try
msgMail.Dispose()
smtp = Nothing
Return 0
End Function
' ----------------------------------------------------------
Public Sub WriteToErrorLog(ByVal sErrorlog As String, _
ByVal Message As String, _
ByVal ErrorDescription As String)
' -------------------------------------------------------------------
' Posts a message and description to a debug logging file
' Requires the name of the file to as a parameter
' ------------------------------------------------------------------
' Return: 0 = success
' Return: -1 = failure
' -------------------------------------------------------------------
Dim myFileStream As System.IO.FileStream
Try
'Open a file for append
myFileStream = New System.IO.FileStream(sErrorlog, _
FileMode.Append, FileAccess.Write, _
FileShare.None)
'Create the stream writer
Dim myWriter As New System.IO.StreamWriter(myFileStream)
'Write the log message to the file
myWriter.WriteLine("--------------------" & Now.ToString() & _
"--------------------------")
myWriter.WriteLine(Message)
myWriter.WriteLine(ErrorDescription)
myWriter.Flush() ' Flush before we close
myWriter.Close() ' Close the streamwriter
myFileStream.Close() ' Close the filestream
Catch ex As Exception
' Display error message box if logfile does not exist
MessageBox.Show("Could not open the file. " & _
ex.Message)
End Try
End Sub
' -----------------------------------------------------------
Public Property MailHost() As String
Get
Return m_MailHost
End Get
Set(ByVal value As String)
m_MailHost = value
End Set
End Property
End Class

What is Knowledge Management - Part 2

Is Knowledge Management (KM) a problem that can be solved by a single application, or a collection of interfaced applications? These designed specifically to solve an individual Business Process (BP) task. In my opinion it is the latter choice.

KM is a collection of applications held together by SOA that is hooked tightly to a Workflow Process (WF). The WF is a collection of scheduled business rules and polices that drive a product to its completion. Along the way Data Repositories will be used to store and version data.

The product will be reviewed, tested, inspected and verified that is under budget and on time. That is the dream / goal.

Let's look at the buzzwords and see what kind of exercise this becomes:
1. KM - Potentially a single UI that delivers data at exactly the time needed
2. BP - Business Process, the well-known steps to build your widget.
2. SOA - Service Oriented Architecture. The rules, definitions, standards, interfaces that connect objects together. It is the processing lines in the flowchart.
3. WF - Work flow - All the required process steps to produce a product
4. Schedule - A time-line, calculated to offer the most profit, that a WF is tied into
5. Data Repository - A safe and secure area to keep either meta-data or physical files. Generally there will be security rules around the content.

Whew! I will start tackling some of these in future articles. Next I am going to do something fun and share a little code.

What is Knowledge Management - Part 1

Very simply, in order to do my job effectively, easy and reliable access to relevant data is vital. What are the tools that allow me to search and retrieve all data that I need? What form will that data become? Files, records, drawings, an object's physical attributes.

If I make a change to that data, how does that affect someone else downstream. Is that user notified? Are there rules, checks and balances, data validation in place to ensure my change didn't screw something up?

Knowledge Management then is herding, sorting, sifting, evolving, categorizing, and using all data that is needed at precisely the moment it is required. It is a huge computing problem and one that has not been adequately solved. (ran out of "ing" verbs)

More later, it is time for a beverage - something with malt and hops!

What the hell are SOA and WOA

SOA = Service Oriented Architecture Wikipedia
WOA = Web Oriented Architecture WOA-REST Wiki

Both are methods for hooking discrete systems together thru various means. SOA has been bandied about in the press for the last 7 or 8 years. In my view it is a bus where one application can send its data to another. A simple example would be a web-part that can be placed on the bus and used by multiple applications for user authentication against Active Directory. This prevents the overhead of each application storing its own user credentials.

WOA is an emerging technology that holds a lot of promise, because it uses a URI connection as a component. In other words you can mash together web page URIs and arrive at a functioning personal environment in a fairly brief amount of time. Take a look at Yahoo Pipes for an example of this.

Blog Introduction

Hello, my name is Scott, and yes, I am a dyed-in-the-wool programmer / nerd. I was influenced by Heinlein's "The Moon is a Harsh Mistress" to follow this path. I love this stuff, I still feel there is so much more to be learned. This is after 30 some years of programming and designing software systems. That is the most exciting part of this journey, one is constantly challenged.

This is my chance to learn new programming and development technologies. This includes sharing what might be valuable to others. The focus will be on SOA, WOA, REST, RSS, HTML, CSS, XML, XSLT, PHP, SQL, Linux, and any other weird things that catch my attention.

Some background, I have been a computer hobbyist and software programmer / designer since 1974 starting with an Atari 400. My IT career has spanned about the same length of time. It started with Fortran on a VAX. Currently, I am developing knowledge management systems with web technologies and relational database back-ends.

At times I may have sudden urge to dispense free-advice. Use it or not, its free!

Please leave comments and rebuttal as needed. Discussion is always good.