Example 3: Checking an Email Address |
This example shows how an external component checks if an email address string entered or changed by the user is known to a mail server. This is an example of an invocative constraint implementing a restrictive business rule. The rule is that email addresses that do not exist in the address book of MS Exchange cannot be entered. If this example is implemented in conjunction with example 2 (resolving the email address), then the constraint in example 2 will be evaluated before the constraint in example 3 since corrective constraints are evaluated before restrictive constraints. Normally if example 2 resolves the address successfully, then example 3 will not raise a violation. This is desired behavior. The example uses the Travel Agency case (Expanded Version). To try out this example: •Locate the Samples/Usmail directory on your CD. •Read the README.TXT file for information on installing, running and inspecting the sample component. Component USMAIL Component Prog ID UsMail.Class1 Method CHECK_ADDRESS_EXE Physical Method checkAddress Parameters of CHECK_ADDRESS_EXE 1, In, String 2, Return, Long Integer Query Protocol Component Name = USMAIL Protocol Name = CHECK_ADDRESS Exe Method Name = CHECK_ADDRESS_EXE PERSON Table Add an EMAIL column based on a regular CHAR(60) domain (On Oracle7: VARCHAR2). Constraint CHECK_EMAIL Message: "Email address is not correct." INVOKE usmail.check_address WITH SELECT email FROM person WHERE email is not null
Description of functionality: The component establishes a connection to a Microsoft Exchange server using Microsoft's standard MAPI component. The exchange server returns a value indicating whether the address exists or not. The invoked component then returns 1 if the address is unknown (constraint violation), 0 if the address is correct, and -1 if an error occurs. Visual Basic code of subprogram directly invoked by USoft: Const USOFT_STATUS_VIOLATION = 1 Const USOFT_STATUS_NO_VIOLATION = 0 Const USOFT_STATUS_ERROR = -1 Public Function checkAddress(address As String, _ Optional profile_name As String, _ Optional password As String) _ As Long Dim objSession As Object Dim objMessage As Object On Error GoTo on_error ' Initialize this function to return a violation checkAddress = USOFT_STATUS_VIOLATION If Len(address) = 0 Then checkAddress = USOFT_STATUS_NO_VIOLATION Exit Function End If Set objSession = newSession(profile_name:=profile_name, _ password:=password, _ no_mail:=True) ' Create a new message and check how the address will be resolved Set objMessage = newMessage(objSession, address) If Not resolveMsgAddress(objMessage) = USOFT_UNRESOLVED_ADDRESS Then ' Return absence of violation checkAddress = USOFT_STATUS_NO_VIOLATION End If cleanUp objSession, objMessage Exit Function on_error: If Not ignoreError() Then showError "checkAddress" checkAddress = USOFT_STATUS_ERROR End If cleanUp objSession, objMessage End Function |