Tips_tutorials   >   Studiojs202   >   Timer Object Emails

Timer Object Emails

Now that we have the oEmail object working we can set up a timer object to email us web stats at the end of every day, week, month, year.

We need to make sure the Timer library external component is being loaded on startup.

  1. F2 Browser > External Components - opens the External Components window.
  2. Expand the External Components node in the treelist.
  3. Scroll down and select the Timer Library node.
  4. If there is a red circle icon next to the Timer Library node, click the Starting Omnis radio button, then click the OK button.
  5. Quit and reopen Omnis Studio if the Timer Library was not already loaded.

Create an object class that is subclassed from the timer external component.

  1. F2 Browser > New Class > Object
  2. Name the new class oWebStatsEmailTimer.
  3. F6 Properties > click the $superclass property combo button and scroll down to the External Components node.
  4. Expand the node and scroll down and select the Timer node, then click the OK button.

Timer Object Method $initialize

The timer object superclass has several public methods which are inherited by our oWebStatsEmailTimer object.

  1. Right-click oWebStatsEmailTimer and select the Interface Manager menu item. This opens the Interface Manager window.
  2. Click the Properties tab. You will see several properties in blue text. Scroll through them and read the Description at the bottom of the window. We will set some of these properties when we initialize the timer object.
  3. Click the Method tab in the Interface Manager window and double-click the $construct method. This takes you directly to the method in the IDE.
  4. Rename the $construct method to $initialize
  5. Add the following parameters to the method.
    1. pToEmailAddr - Character
    2. pkInterval - Long integer

  6. Add the following instance variables.

    1. ikInterval - Long integer
    2. iToEmailAddr - Character
    3. iEndDay - Short Date
    4. iEndWeek - Short Date
    5. iEndMonth - Short Date
    6. iEndYear - Short Date

  7. Enter the following code in the $initialize method.

    ; Set the properties.
    Calculate $cinst.$autoreset as kTrue
    Calculate $cinst.$reentrant as kFalse
    Calculate $cinst.$useseconds as kTrue
    Calculate $cinst.$timervalue as 60*60 ;; Hourly

    ; Copy the parameter values to ivars.
    Calculate iToEmailAddr as pToEmailAddr
    Calculate ikInterval as pkInterval

    ; Set the begin and the end period for the current interval period.
    Calculate iEndDay as #D
    Calculate iEndWeek as lday(kWeek,#D)
    Calculate iEndMonth as lday(kMonth,#D)
    Calculate iEndYear as lday(kYear,#D)

    Quit method kTrue

Timer Object Method $timer

The $timer method is called each time the timer reaches the specified $timervalue duration.

  1. Right-click the $timer method and select Override Method.
  2. Enter the following code in the $timer method.

    ; Is there another method currently running?
    Calculate StackList as sys(192)
    Calculate FlagOK as kTrue ;; Default the flag to true.

    If StackList.$linecount>1
       ; Do nothing, another method is running.
    Else
       
       ; Have we started the day after the current end date?
       If #D>iEndDay
          
          If ikInterval=kDay ;; kWeek, kMonth, kYear
             
             ; Time to send a web stats email.
             Calculate Interval as 'Daily'
             Do $cinst.$sendWebStatsEmail(iEndDay,iEndDay,Interval) Returns FlagOK
             
          End If
          If FlagOK
             
             ; Set the end day to today
             Calculate iEndDay as #D
             
          End If
          If FlagOK
             
             ; Have we started the day after the current end week?
             If #D>iEndWeek
                
                ; Check to make sure the interval is not monthly or yearly.
                If ikInterval<>kMonth&ikInterval<>kYear
                   
                   ; Time to send a web stats email.
                   Calculate BeginIntervalDate as fday(kWeek,iEndWeek)
                   Calculate Interval as 'Weekly'
                   Do $cinst.$sendWebStatsEmail(BeginIntervalDate,iEndWeek,Interval) Returns FlagOK
                   
                End If
                If FlagOK
                   Calculate iEndWeek as dadd(kWeek,1,iEndWeek)
                End If
                
             End If
          End If
          If FlagOK
             
             ; Have we started the day after the current end week?
             If #D>iEndMonth
                
                ; Check to make sure the interval is not yearly.
                If ikInterval<>kYear
                   
                   ; Time to send a web stats email.
                   Calculate BeginIntervalDate as fday(kMonth,iEndMonth)
                   Calculate Interval as 'Monthly'
                   Do $cinst.$sendWebStatsEmail(BeginIntervalDate,iEndMonth,Interval) Returns FlagOK
                   
                End If
                If FlagOK
                   Calculate iEndMonth as lday(kMonth,iEndDay)
                End If
                
             End If
          End If
          If FlagOK
             
             ; Have we started the day after the current end year?
             If #D>iEndYear
                
                ; Time to send a web stats email.
                Calculate BeginIntervalDate as fday(kYear,iEndYear)
                Calculate Interval as 'Yearly'
                Do $cinst.$sendWebStatsEmail(BeginIntervalDate,iEndYear,Interval) Returns FlagOK
                If FlagOK
                   Calculate iEndYear as lday(kMonth,iEndDay)
                End If
                
             End If
          End If
          
       Else
          
          ; Do nothing, autoreset will call the method again later.
          Calculate FlagOK as kTrue
          
       End If
    End If
    If not(FlagOK)
       
       ; We have to report an error to the user.
       ; Since this is a web server, we can't prompt the user.
       ; Send the error to the trace log.
       Do errhndlr.$getonceLastError(Mssg,Method)
       Send to trace log {----- START ERROR MESSAGE ---- [#D] ---- [$cmethod().$name] ----- [$ctask().$name] -----}
       Send to trace log {----- The '[Method]' method logged the following message:}
       Send to trace log {----- [Mssg]}
       Send to trace log {----- END ERROR MESSAGE ----}
       
    End If

    Quit method FlagOK

Timer Object Method $sendWebStatsEmail

  1. Add a $sendWebStatsEmail method to the oWebStatsEmailTimer class.
  2. Add the following parameters to the method.
    1. pBeginDate - Short Date
    2. pEndDate - Short Date
    3. pInterval - Character

  3. Enter the following code in the $sendWebStatsEmail method.

; Get the web stats for the current period.
Do webmon.$retWebStatsList(pBeginDate,pEndDate) Returns StatsList
If StatsList.$colcount
   
   ; Convert the list to text for the email body.
   Do webmon.$convertWebStatsListToText(StatsList) Returns Body
   If len(Body)
      
      ; Send the email.
      Calculate Subject as con("Web App Server Stats - ",pBeginDate," to ",pEndDate)
      If len(pInterval)>0
         Calculate Subject as con(pInterval," ",Subject)
      End If
      Calculate Body as con(kCr,Subject,kCr,kCr,Body)
      Do eml.$sendEmail(Subject,Body,iToEmailAddr) Returns FlagOK
   End If
   
End If
Quit method FlagOK

Initialize Timer Object

We will instantiate and initialize the oWebStatsEmailTimer object from the oWebMonitor object class.

  1. Add the instance variable, ioWebStatsEmailTimer, Object type to the oWebMonitor object class.
  2. Point the ioWebStatsEmailTimer task variable to the oWebStatsEmailTimer object class.
  3. Add the following parameters to the $initialize method of the oWebMonitor object class.
    1. pToEmailAddrWebStats - Character
    2. pkBaseIntervalEmailWebStats - Long Integer

  4. Modify the $initialize method of the oWebMonitor object class so that it reads as follows:

    Do iRow.$definefromsqlclass('tWebappstat')
    Do iRow.$sessionobject.$assign($ctask.dbsessionobj)

    ; Default flag to true.
    Calculate FlagOK as kTrue

    ; Initialize the oEmailWebStatsTime object if a 'To' email address has been provided.
    If len(pToEmailAddrWebStats)
       
       Do ioWebStatsEmailTimer.$initialize(pToEmailAddrWebStats,pkBaseIntervalEmailWebStats) Returns FlagOK
       If FlagOK
          
          ; Start the timer.
          Do ioWebStatsEmailTimer.$starttimer()
          
       End If
    End If

    Quit method FlagOK

  5. Add a $sendWebStatsEmail method to the oWebMonitor object class to facilitate testing the oWebStatsEmailTimer object.

  6. Add the following parameters to the method.

    1. pBeginDate - Short Date
    2. pEndDate - Short Date

  7. Add the following code to the method.

    Do ioWebStatsEmailTimer.$sendWebStatsEmail(pBeginDate,pEndDate) Returns FlagOK
    Quit method FlagOK

We need to modify the $construct method of the Startup_Task to send the parameters we added to the $initialize method of oWebMonitor.

  1. Go to the end of the $construct method of the Startup_Task
  2. Modify the code at Do webmon.$initialize(... to read as follows:

    ; Initialize the web monitoring object.
    Calculate ToEmailAddr as 'Your_Name <your_email_address>'
    Do webmon.$initialize(ToEmailAddr,kDay) Returns FlagOK
    If FlagOK
       
       ; Initialize the email object.
       Do eml.$initialize(SMTPServer,SMTPUser,SMTPPassword,POP3Server,POP3User,POP3Password,DefaultFromEmailAddr,DefaultFromName)
       
    End If
    Quit method FlagOK

Test Timer Object

Test the oWebStatsEmailTimer object using the Programmer Test Method.

  1. Close and reopen the ContactsWeb library in order to initialize the oWebStatsEmailTimer object.
  2. Contacts menu > Programmer Test Method...
  3. After the Breakpoint and Quit method enter the following code inserting your name and email address in the code.

    Calculate BeginDate as fday(kMonth,#D)
    Calculate EndDate as lday(kMonth,#D)
    Do webmon.$sendWebStatsEmail(BeginDate,EndDate) Returns FlagOK

  4. Contact menu > Programmer Test Method...
  5. Double-click the Do webmon.$sendWebStatsEmail line of code to set the Go point to that line.
  6. Click the Step-in button to step into the $sendWebStatsEmail method of the oWebMonitor object.
  7. Step in to the $sendWebStatsEmail method of the oWebStatsEmailTimer object.
  8. Step through the method by clicking the Step-over button repeatedly. All going well a web stats email will be sent to you.
  9. Go to your email application and check your mail. Depending on the speed of the email server you should receive the web stats email within a few minutes.
If you leave the ContactsWeb app open the oWebStatsEmailTimer object should automatically send you an email within an hour of the end of the specified interval period.