Skip to content

Commit

Permalink
Merge pull request #52 from psteiwer/dev-customfilters
Browse files Browse the repository at this point in the history
Dev customfilters
  • Loading branch information
psteiwer authored Feb 24, 2020
2 parents a95a629 + 5eb447f commit ee2998b
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 147 deletions.
139 changes: 73 additions & 66 deletions PivotSubscriptions/Subscription.cls
Original file line number Diff line number Diff line change
@@ -1,66 +1,73 @@
Class PivotSubscriptions.Subscription Extends %Persistent
{

Parameter DEFAULTGLOBAL = "^PivotSubscriptions.Sub";

Property CreatedBy As %String;

Property Pivot As %String(MAXLEN="");

Property DayOfWeek As %String;

Property Hour As %Integer(MINVAL=0, MAXVAL=23);

Property Minute As %Integer(VALUELIST=",0,15,30,45");

Property Format As %String(VALUELIST = ",Excel,PDF");

Property Emails As %String(MAXLEN="");

Property Version As %String [ InitialExpression = {##class(PivotSubscriptions.Utility).GetVersion()} ];

Property Name As %String(MAXLEN=100);

Storage Default
{
<Data name="SubscriptionDefaultData">
<Value name="1">
<Value>%%CLASSNAME</Value>
</Value>
<Value name="2">
<Value>CreatedBy</Value>
</Value>
<Value name="3">
<Value>Pivot</Value>
</Value>
<Value name="4">
<Value>DayOfWeek</Value>
</Value>
<Value name="5">
<Value>Hour</Value>
</Value>
<Value name="6">
<Value>Minute</Value>
</Value>
<Value name="7">
<Value>Format</Value>
</Value>
<Value name="8">
<Value>Emails</Value>
</Value>
<Value name="9">
<Value>Version</Value>
</Value>
<Value name="10">
<Value>Name</Value>
</Value>
</Data>
<DataLocation>^PivotSubscriptions.SubD</DataLocation>
<DefaultData>SubscriptionDefaultData</DefaultData>
<IdLocation>^PivotSubscriptions.SubD</IdLocation>
<IndexLocation>^PivotSubscriptions.SubI</IndexLocation>
<StreamLocation>^PivotSubscriptions.SubS</StreamLocation>
<Type>%Storage.Persistent</Type>
}

}
Class PivotSubscriptions.Subscription Extends %Persistent
{

Parameter DEFAULTGLOBAL = "^PivotSubscriptions.Sub";

Property CreatedBy As %String;

Property Pivot As %String(MAXLEN = "");

Property DayOfWeek As %String;

Property Hour As %Integer(MAXVAL = 23, MINVAL = 0);

Property Minute As %Integer(VALUELIST = ",0,15,30,45");

Property Format As %String(VALUELIST = ",Excel,PDF");

Property Emails As %String(MAXLEN = "");

Property Version As %String [ InitialExpression = {##class(PivotSubscriptions.Utility).GetVersion()} ];

Property Name As %String(MAXLEN = 100);

Property CustomFilters As array Of %String;

Storage Default
{
<Data name="CustomFilters">
<Attribute>CustomFilters</Attribute>
<Structure>subnode</Structure>
<Subscript>"CustomFilters"</Subscript>
</Data>
<Data name="SubscriptionDefaultData">
<Value name="1">
<Value>%%CLASSNAME</Value>
</Value>
<Value name="2">
<Value>CreatedBy</Value>
</Value>
<Value name="3">
<Value>Pivot</Value>
</Value>
<Value name="4">
<Value>DayOfWeek</Value>
</Value>
<Value name="5">
<Value>Hour</Value>
</Value>
<Value name="6">
<Value>Minute</Value>
</Value>
<Value name="7">
<Value>Format</Value>
</Value>
<Value name="8">
<Value>Emails</Value>
</Value>
<Value name="9">
<Value>Version</Value>
</Value>
<Value name="10">
<Value>Name</Value>
</Value>
</Data>
<DataLocation>^PivotSubscriptions.SubD</DataLocation>
<DefaultData>SubscriptionDefaultData</DefaultData>
<IdLocation>^PivotSubscriptions.SubD</IdLocation>
<IndexLocation>^PivotSubscriptions.SubI</IndexLocation>
<StreamLocation>^PivotSubscriptions.SubS</StreamLocation>
<Type>%Storage.Persistent</Type>
}

}
143 changes: 65 additions & 78 deletions PivotSubscriptions/Task.cls
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ Method OnTask() As %Status
// Ignore minute for testing purposes
// This will allow the task to be run on demand and send emails immediately
// Leaving DayOfWeek and Hour so the scheduled task does not spam emails
Set tRS=##class(%SQL.Statement).%ExecDirect(,"SELECT Emails,Name,Pivot,Format,DayOfWeek,""Hour"",""Minute"" FROM PivotSubscriptions.Subscription WHERE DayOfWeek[? AND ""Hour""=?",tDayOfWeek,tHour)
Set tRS=##class(%SQL.Statement).%ExecDirect(,"SELECT ID,Emails,Name,Pivot,Format,DayOfWeek,""Hour"",""Minute"" FROM PivotSubscriptions.Subscription WHERE DayOfWeek[? AND ""Hour""=?",tDayOfWeek,tHour)
} Else {
Set tRS=##class(%SQL.Statement).%ExecDirect(,"SELECT Emails,Name,Pivot,Format,DayOfWeek,""Hour"",""Minute"" FROM PivotSubscriptions.Subscription WHERE DayOfWeek[? AND ""Hour""=? AND ""Minute""=?",tDayOfWeek,tHour,tMinute)
Set tRS=##class(%SQL.Statement).%ExecDirect(,"SELECT ID,Emails,Name,Pivot,Format,DayOfWeek,""Hour"",""Minute"" FROM PivotSubscriptions.Subscription WHERE DayOfWeek[? AND ""Hour""=? AND ""Minute""=?",tDayOfWeek,tHour,tMinute)
}

// Loop through Subscription results and send emails.
While tRS.%Next() {
Set tID=tRS.%Get("ID")
Set tEmails=tRS.%Get("Emails")
Set tPivot=tRS.%Get("Pivot")
Set tFormat=tRS.%Get("Format")
Expand All @@ -31,13 +32,13 @@ Method OnTask() As %Status
Set tMinute=tRS.%Get("Minute")
Set tName=tRS.%Get("Name")

Set tSC = ##class(PivotSubscriptions.Task).SendSubscription(tPivot,tEmails,tFormat,tDayOfWeek,tHour,tMinute,tName)
Set tSC = ##class(PivotSubscriptions.Task).SendSubscription(tID,tPivot,tEmails,tFormat,tDayOfWeek,tHour,tMinute,tName)
}

Quit tSC
}

ClassMethod SendSubscription(pPivot As %String, pEmails As %String, pFormat As %String, pDayOfWeek, pHour, pMinute, pName) As %Status
ClassMethod SendSubscription(pID, pPivot As %String, pEmails As %String, pFormat As %String, pDayOfWeek, pHour, pMinute, pName) As %Status
{
Set tSC=$$$OK

Expand All @@ -49,7 +50,7 @@ ClassMethod SendSubscription(pPivot As %String, pEmails As %String, pFormat As %
}

Try {
Set tSC=##class(PivotSubscriptions.Task).SendEmail(pPivot,pEmails,pFormat,pName)
Set tSC=##class(PivotSubscriptions.Task).SendEmail(pID,pPivot,pEmails,pFormat,pName)
} Catch ex {
Set tSC=ex.AsStatus()
}
Expand All @@ -65,97 +66,83 @@ ClassMethod SendSubscription(pPivot As %String, pEmails As %String, pFormat As %
Quit tSC
}

ClassMethod SendEmail(pPivot As %String, pEmails As %String, pFormat As %String, pName As %String) As %Status
ClassMethod SendEmail(pID, pPivot As %String, pEmails As %String, pFormat As %String, pName As %String) As %Status
{
Set tSC=$$$OK
Set tFilename=""

Set mdx=##class(%DeepSee.Utils).%GetMDXFromPivot(pPivot,.tSC,0)

// Execute MDX to see if there are results
Set tHasResults=0
Set tRS=##class(%DeepSee.ResultSet).%New()
Do tRS.%PrepareMDX(mdx)
Do tRS.%Execute()
If tRS.%Query.queryType="DRILLTHROUGH" {
If tRS.listingRows>0 {
Set tHasResults=1
}
} Else {
If tRS.%GetGrandTotal()'=0 {
Set tHasResults=1
}
}

// Only generate file if requested pivot has results
If tHasResults {
Set pParms("TITLE")=pName
Set pParms("SUBTITLE")=$E(pPivot,1,*-6)_". Report generated on "_$zd($h,6)
Set pParms("MDX")=mdx

Set tFilename=$replace($zstrip(pName,"*PC'W",,"/")_" - "_$zd($h,6),"/","-")_".xls"
Set tSC=##class(%DeepSee.Utils).%ExportExcelToFile(tFilename,.pParms)
}
If $$$ISERR(tSC) Quit tSC
// Iterate through all subscribers and send emails to each individually
For count=1:1:$l(pEmails,",") {
Set tEmail=$p(pEmails,",",count)

// Generate email
If tSC {
// check whether the table generated is too wide/truncated AND whether the table has data
Set tableStatus=##class(PivotSubscriptions.Utility).CheckTable(pName)

Set server=##class(%Net.SMTP).%New()
Set server.smtpserver=##class(%SYS.Task.Config).GetData("MailServer")
Set msg=##class(%Net.MailMessage).%New()
Set msg.From="PivotSubscriptions"

Set validAdminEmail=##class(PivotSubscriptions.Utility).CheckConfigurationGlobal("AdminEmail")
Set tHasResults=0

Set tSubscription=##class(PivotSubscriptions.Subscription).%OpenId(pID)
Set tCustomFilter=tSubscription.CustomFilters.GetAt(tEmail)
Set tSC=##class(PivotSubscriptions.Utility).ExecuteForSubscription(mdx,tCustomFilter,.tHasResults)

If validAdminEmail {
Set msg.ReplyTo=^PivotSubscriptions.Settings("AdminEmail")
If tHasResults {
// Only generate file if requested pivot has results
Set tSC=##class(PivotSubscriptions.Utility).GenerateFileForSubscription("Excel",mdx,tCustomFilter,pName,pPivot,.tFilename)
}

// Generate email
If tSC {
// check whether the table generated is too wide/truncated AND whether the table has data
Set tableStatus=##class(PivotSubscriptions.Utility).CheckTable(pName)

// Remove ".pivot" extension for subject
Set msg.Subject=pName
Set msg.IsBinary=0
Set msg.IsHTML=1 // Set IsHTML to 1 if email uses HTML

If tHasResults {
Set server=##class(%Net.SMTP).%New()
Set server.smtpserver=##class(%SYS.Task.Config).GetData("MailServer")
Set msg=##class(%Net.MailMessage).%New()
Set msg.From="PivotSubscriptions"

// Add attachment
Do ##class(PivotSubscriptions.Utility).WriteEmail(pName, msg, tableStatus)

// if table is too large, then just attach Excel instead
If ((pFormat = "PDF") && (tableStatus)) {
Set tFilename = $EXTRACT(tFilename,1,$LENGTH(tFilename)-3) _ "pdf"
Do ##class(%DeepSee.Utils).%ExportPDFToFile(tFilename,.pParms)
} ElseIf 'tableStatus {
// Check if both configuration settings for admin email is valid before sending error message
If validAdminEmail {
Do ##class(PivotSubscriptions.Utility).SendErrorMessage(pPivot,^PivotSubscriptions.Settings("AdminEmail"),pName)
}
Set validAdminEmail=##class(PivotSubscriptions.Utility).CheckConfigurationGlobal("AdminEmail")

If validAdminEmail {
Set msg.ReplyTo=^PivotSubscriptions.Settings("AdminEmail")
}

Set status=msg.AttachFile("",tFilename)
// Remove ".pivot" extension for subject
Set msg.Subject=pName
Set msg.IsBinary=0
Set msg.IsHTML=1 // Set IsHTML to 1 if email uses HTML

If tHasResults {

// Add attachment
Do ##class(PivotSubscriptions.Utility).WriteEmail(pName, msg, tableStatus)

If $$$ISERR(status) {
Do $System.Status.DisplayError(status)
Quit $$$ERROR()
// if table is too large, then just attach Excel instead
If ((pFormat = "PDF") && (tableStatus)) {
Set tSC=##class(PivotSubscriptions.Utility).GenerateFileForSubscription("PDF",mdx,tCustomFilter,pName,pPivot,.tFilename)
} ElseIf 'tableStatus {
// Check if both configuration settings for admin email is valid before sending error message
If validAdminEmail {
Do ##class(PivotSubscriptions.Utility).SendErrorMessage(pPivot,^PivotSubscriptions.Settings("AdminEmail"),pName)
}
}

Set status=msg.AttachFile("",tFilename)

If $$$ISERR(status) {
Do $System.Status.DisplayError(status)
Quit $$$ERROR()
}
} Else {
Do ##class(PivotSubscriptions.Utility).WriteEmailHeader(msg)
Do msg.TextData.WriteLine("No data for requested pivot.")
Do ##class(PivotSubscriptions.Utility).WriteEmailFooter(msg)
}
} Else {
Do ##class(PivotSubscriptions.Utility).WriteEmailHeader(msg)
Do msg.TextData.WriteLine("No data for requested pivot.")
Do ##class(PivotSubscriptions.Utility).WriteEmailFooter(msg)
}

// iterate through all subscribers and send emails to each individually
For count=1:1:$l(pEmails,",") {
Set tEmail=$p(pEmails,",",count)
Do msg.To.SetAt(tEmail,1)

// Send email
Set tSC=server.Send(msg)
}
Do msg.To.SetAt(tEmail,1)

// Send email
Set tSC=server.Send(msg)
// TODO: Log individual send status?

If tHasResults {
// Delete file
Do ##class(%File).Delete(tFilename)
Expand Down
Loading

0 comments on commit ee2998b

Please sign in to comment.