<?xml version="1.0" encoding="UTF-8"?>
<Export generator="Cache" version="25" zv="Cache for Windows (x86-64) 2012.2.5 (Build 962U)" ts="2017-10-27 09:12:01">
<Class name="AGMPI.Task.StatusCheck">
<IncludeCode>Ensemble</IncludeCode>
<Super>%SYS.Task.Definition</Super>
<TimeChanged>64582,41888.286963</TimeChanged>
<TimeCreated>64551,27126.323152</TimeCreated>

<Method name="OnTask">
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
  if $G(^AGMPPRST("STOP")) {Q $$$OK}
  set tCurrentDate = $P($H, ",")
  set productionName="AGMPI.PatientRegistration"  	  
  set tSC=##Class(Ens.Director).GetProductionSummary(.productionInfo)
  if $$$ISOK(tSC)
  {
    if '($D(productionInfo(productionName))) {
	  set tResponse = "No "_productionName_" production within the namespace."
    } else {
	  set ^AGMPPRST("Status") =$List(productionInfo(productionName))
	  set ^AGMPPRST("LastStartTime")=$List(productionInfo(productionName),2)
	  if $List(productionInfo(productionName),3)'=""{
	    set ^AGMPPRST("LastStopTime")=$List(productionInfo(productionName),3)	  
	  }
	  if ^AGMPPRST("Status")="Running"  {
        if $G(^AGMPPRST("LastEmailDate")) {
	      set ^AGMPPRST("LastEmailDate")=""
          set ^AGMPPRST("LastEmailType")=""
        }
        set ^AGMPPRST("LogEntry")=""
	  } else {
		if $G(^AGMPPRST("Status"))=$G(^AGMPPRST("LastEmailType")) {
		  set ^AGMPPRST("LogEntry")=""
		  if tCurrentDate > $G(^AGMPPRST("LastEmailDate")) {			
			set tResponse = ..SendEmail()
		  }
        } else {  		  
          set tResponse = ..SendEmail()
	    }
	  }
	}
  } else {
	set tResponse = ..GetStatusString(tSC)
  }
  If '($D(^AGMPPRST("LogEntry"))) || $G(^AGMPPRST("LogEntry")) {
	$$$LOGASSERT(tResponse)
	set ^AGMPPRST("LogEntry")=""
  }
  Quit $$$OK
]]></Implementation>
</Method>

<Method name="SendEmail">
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[
  set tResponse = "Production Notification eMail not sent, the EmailAlert Business Operation is NOT enabled."
  set ^AGMPPRST("LastEmailType")=^AGMPPRST("Status")
  set ^AGMPPRST("LastEmailDate")=$P($H, ",")
  set ^AGMPPRST("LogEntry")=1

  if (##class(Ens.Director).IsItemEnabled("EmailAlert",.tStatus))
  {
    set tSC = ##Class(Ens.Director).GetAdapterSettings("EmailAlert",.adapterSettings)
    if ($G(adapterSettings("SMTPServer"))'="") && ($G(adapterSettings("SMTPPort"))'="") { 
      set server = ##class(%Net.SMTP).%New()
      set msg = ##class(%Net.MailMessage).%New()
      
      set server.smtpserver=$G(adapterSettings("SMTPServer"))
      set server.port = $G(adapterSettings("SMTPPort"))
      
      // may need to add authentication information if "Credentials" is populated
      
      set msg.From=$G(adapterSettings("From"))

      set tRCPNT = $G(adapterSettings("Recipient"))
      set tRCPNT = $TR(tRCPNT,"; ",",")
      for i=1:1:$L(tRCPNT,",") {
	    if '($P(tRCPNT,",",i)="")
	    {
          set tSC = msg.To.Insert($P(tRCPNT,",",i))
	    }
      }

      set tCC = $G(adapterSettings("Cc"))
      set tCC = $TR(tCC,"; ",",")
      for i=1:1:$L(tCC,",") {
	    if '($P(tCC,",",i) = "")
        {
	      set tSC = msg.Cc.Insert($P(tCC,",",i))
        }
      }

      set msg.Subject = "Ensemble alert from the AGMPI Production"
      set msg.IsBinary=0
      set msg.IsHTML=0
      set tSC = msg.TextData.Write("The AGMPI production is currently stopped with a status of "_$G(^AGMPPRST("Status"))_"!")
      if ($$$ISOK(tSC)) { set tSC = server.Send(msg) }
      if $$$ISOK(tSC) { 
        set tResponse="Production Notification eMail sent."
      } else { 
        set tResponse = "Production Notification eMail not sent, "_..GetStatusString(tSC)_" ." 
      }
    } else {
	  set tResponse="Production Notification eMail not sent, IP Address and/or Port Number Missing."
    }	        
  }  
   Quit tResponse
]]></Implementation>
</Method>

<Method name="GetStatusString">
<FormalSpec>pStatus:%Status=""</FormalSpec>
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[
  set tString = ""
  if pStatus {
	Do ##class(%SYSTEM.Status).DecomposeStatus(tSC,.errTxt,"-d")
    set tString = errTxt(1)
  }
]]></Implementation>
</Method>
</Class>
</Export>
