Skip to content

Commit

Permalink
v0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
psteiwer authored Jul 2, 2019
2 parents 99841ab + d4bc495 commit 326ec27
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 34 deletions.
11 changes: 7 additions & 4 deletions PivotSubscriptions/Subscription.cls
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Class PivotSubscriptions.Subscription Extends %Persistent
{

Parameter DEFAULTGLOBAL = "^PivotSubscriptions.Sub";

Property CreatedBy As %String;

Property Pivot As %String(MAXLEN="");
Expand All @@ -15,6 +17,7 @@ Property Format As %String(VALUELIST = ",Excel,PDF");

Property Emails As %String;


Storage Default
{
<Data name="SubscriptionDefaultData">
Expand Down Expand Up @@ -43,11 +46,11 @@ Storage Default
<Value>Emails</Value>
</Value>
</Data>
<DataLocation>^PivotSubscript8A5.SubscriptionD</DataLocation>
<DataLocation>^PivotSubscriptions.SubD</DataLocation>
<DefaultData>SubscriptionDefaultData</DefaultData>
<IdLocation>^PivotSubscript8A5.SubscriptionD</IdLocation>
<IndexLocation>^PivotSubscript8A5.SubscriptionI</IndexLocation>
<StreamLocation>^PivotSubscript8A5.SubscriptionS</StreamLocation>
<IdLocation>^PivotSubscriptions.SubD</IdLocation>
<IndexLocation>^PivotSubscriptions.SubI</IndexLocation>
<StreamLocation>^PivotSubscriptions.SubS</StreamLocation>
<Type>%Storage.Persistent</Type>
}

Expand Down
104 changes: 93 additions & 11 deletions PivotSubscriptions/Task.cls
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,97 @@ Method OnTask() As %Status
Set tDayOfWeek=$zd($h,10)
Set tHour=$p($zt($piece($h,",",2),2),":",1)
Set tMinute=$p($zt($piece($h,",",2),2),":",2)
Set tRS=##class(%SQL.Statement).%ExecDirect(,"SELECT Emails,Pivot FROM PivotSubscriptions.Subscription WHERE DayOfWeek[? AND ""Hour""=? AND ""Minute""=?",tDayOfWeek,tHour,tMinute)

If $G(^PivotSubscriptions.Settings("TestEnv")) {
// 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,Pivot,Format FROM PivotSubscriptions.Subscription WHERE DayOfWeek[? AND ""Hour""=?",tDayOfWeek,tHour)
} Else {
Set tRS=##class(%SQL.Statement).%ExecDirect(,"SELECT Emails,Pivot,Format FROM PivotSubscriptions.Subscription WHERE DayOfWeek[? AND ""Hour""=? AND ""Minute""=?",tDayOfWeek,tHour,tMinute)
}

// Loop through Subscription results and send emails.
While tRS.%Next() {
Set tEmails=tRS.%Get("Emails")
Set tPivot=tRS.%Get("Pivot")
Set tFormat=tRS.%Get("Format")

Do ..SendEmail(tPivot,tEmails)
Do ..SendEmail(tPivot,tEmails,tFormat)
}

Quit tSC
}

// Write improved email format for subscription, including embeded pivot table
ClassMethod WriteEmail(pMsg As %Net.MailMessage, pFilename As %String) {
Do ##class(PivotSubscriptions.Task).GenerateLink(.link)

Do pMsg.TextData.WriteLine("<html><head><style>body {font-family: Verdana,sans-serif;font-size: 0.9em;}table.center {width:70%; margin-left:15%; margin-right:15%;}")
Do pMsg.TextData.WriteLine("table.data {width: 100%;border:0;cellspacing:0;cellpadding:0} table.cell {text-align:center}")
Do pMsg.TextData.WriteLine("table.border{font-family: Trebuchet MS, sans-serif;color: white;background-color: #3eb2b4;width:100%;}</style></head>")
Do pMsg.TextData.WriteLine("<body><table class='center'><tr><td><table class='border'><tr><td>&nbsp;</td></tr></table></tr></td>")
Do pMsg.TextData.WriteLine("<tr><td><table class='table.data'><tr><td>&nbsp;</td></tr><tr><td class='table.cell'>")

// embed pivot table as HTML table to email
Set stream=##class(%Stream.FileCharacter).%New()
Set sc=stream.LinkToFile(pFilename)

// .xls to String stream that is read by line and then written to the email (as HTML) by line
While 'stream.AtEnd {
Set line=stream.ReadLine()

// properly center and format table to match the rest of the style of the email
If line = "<table border=""0"">" {
Do pMsg.TextData.WriteLine("<table style='width:70%; margin-left:15%; margin-right:15%;'>")
} ElseIf line = "<table border=""1"">" {
Do pMsg.TextData.WriteLine("<table border='1' style='width:100%'")
} ElseIf ( (line = "<x:Name>Results</x:Name>")
|| (line = "<x:DoNotDisplayGridlines>False</x:DoNotDisplayGridlines>")
|| (line = "<html xmlns:x=3D""urn:schemas-microsoft-com:office:excel"">")
|| (line = "</html>")
) {
Continue
} Else {
Do pMsg.TextData.WriteLine(line)
}
}

Do pMsg.TextData.WriteLine("</td></tr></table></td></tr>")
Do pMsg.TextData.WriteLine("<tr><td><table><tr><td>&nbsp;</td></tr>")
Do pMsg.TextData.WriteLine("<tr><td>&nbsp;</td></tr><tr><td>You can manage your subscriptions <i><a href='"_link_"'>here</a></i>.</td></tr><tr><td><i>You can edit days of the week/time/format/recipients and delete in the Subscriptions Manager portal</i>.</td></tr><tr><td>&nbsp;</td></tr></table></td></tr><tr><td>")
Do pMsg.TextData.WriteLine("<table class='border'><tr><td>&nbsp;</td></tr><tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i>Pivot Subscriptions</i> by DeepSee</td></tr><tr><td>&nbsp;</td></tr>")
Do pMsg.TextData.WriteLine("<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&copy;&nbsp;2019&nbsp;<b>Intersystems Corporation</b></td></tr><tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;One Memorial Drive, Cambridge, MA 02142</td></tr><tr><td>&nbsp;</td></tr></table></td></tr></table></body></html>")

ClassMethod SendEmail(pPivot As %String, pEmails As %String) As %Status
}

ClassMethod GenerateLink(ByRef link As %String) As %Status
{

Set tSC=$$$OK

Set tSC=##class(%Library.RoutineMgr).GetWebServerPort(.p, .h, .up, .url)

If $E(url,$L(url))="/" {
Set url=$E(url,1,$L(url)-1)
}

If ($$$ISERR(tSC)) || ($G(url)="") {
Set url="http://127.0.0.1:57772"
}

Set url = url_$System.CSP.GetDefaultApp($Namespace)

Set link = url_"/PivotSubscriptions.UI.SubscriptionManager.zen"

Quit tSC
}

ClassMethod SendEmail(pPivot As %String, pEmails As %String, pFormat 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
Expand All @@ -49,9 +124,9 @@ ClassMethod SendEmail(pPivot As %String, pEmails As %String) As %Status
Set pParms("TITLE")=$E(pPivot,1,*-6)
Set pParms("SUBTITLE")="Report generated on "_$zd($h,6)
Set pParms("MDX")=mdx
Set filename=$replace($zstrip($E(pPivot,1,*-6),"*PC'W",,"/")_" - "_$zd($h,6),"/","-")_".xls"

Set tSC=##class(%DeepSee.Utils).%ExportExcelToFile(filename,.pParms)

Set tFilename=$replace($zstrip($E(pPivot,1,*-6),"*PC'W",,"/")_" - "_$zd($h,6),"/","-")_".xls"
Set tSC=##class(%DeepSee.Utils).%ExportExcelToFile(tFilename,.pParms)
}

// Generate email
Expand All @@ -69,13 +144,20 @@ ClassMethod SendEmail(pPivot As %String, pEmails As %String) As %Status
// Remove ".pivot" extension for subject
Set msg.Subject=$E(pPivot,1,*-6)
Set msg.IsBinary=0
Set msg.IsHTML=0
Set msg.IsHTML=1 // Set IsHTML to 1 if email uses HTML

If tHasResults {
Do msg.TextData.WriteLine("Attached is the requested Pivot.")


// Add attachment
Set status=msg.AttachFile("",filename)
Do ##class(PivotSubscriptions.Task).WriteEmail(msg, tFilename)

If (pFormat = "PDF") {
Set tFilename = $EXTRACT(tFilename,1,$LENGTH(tFilename)-3) _ "pdf"
Do ##class(%DeepSee.Utils).%ExportPDFToFile(tFilename,.pParms)
}

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

If $$$ISERR(status)
{
Do $System.Status.DisplayError(status)
Expand All @@ -90,7 +172,7 @@ ClassMethod SendEmail(pPivot As %String, pEmails As %String) As %Status

If tHasResults {
// Delete file
Do ##class(%File).Delete(filename)
Do ##class(%File).Delete(tFilename)
}
}

Expand Down
14 changes: 13 additions & 1 deletion PivotSubscriptions/UI/PivotList.cls
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Include %DeepSee

Class PivotSubscriptions.UI.PivotList Extends PivotSubscriptions.UI.Template [ System = 4 ]
Class PivotSubscriptions.UI.PivotList Extends PivotSubscriptions.UI.Template
{

/// Displayed name of this page.
Expand Down Expand Up @@ -37,6 +37,18 @@ XData contentPane [ XMLNamespace = "http://www.intersystems.com/zen" ]
</pane>
}

/// Get the (localized) title string for the page.
Method %OnGetTitle() As %String [ Internal ]
{
Quit $$$Text("Pivot List","PivotSubscriptions")
}

/// Get the (localized) name of the page.
Method %OnGetPageName() As %String [ Internal ]
{
Quit $$$Text("Pivot List","PivotSubscriptions")
}

Method ExecuteRS(pRS As %ResultSet, Output pSC As %Status, pInfo As %ZEN.Auxiliary.QueryInfo) As %Boolean
{
quit pRS.%Execute($G(pInfo.filters("POwner")),$G(pInfo.filters("PName")))
Expand Down
26 changes: 9 additions & 17 deletions PivotSubscriptions/UI/SubscriptionManager.cls
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Include %DeepSee

Class PivotSubscriptions.UI.SubscriptionManager Extends PivotSubscriptions.UI.Template [ System = 4 ]
Class PivotSubscriptions.UI.SubscriptionManager Extends PivotSubscriptions.UI.Template
{

/// Displayed name of this page.
Expand All @@ -16,10 +16,9 @@ XData contentPane [ XMLNamespace = "http://www.intersystems.com/zen" ]
<spacer height="20" />
<tableNavigatorBar id="tableNavigator" tablePaneId="table" showPageSize="true" />
<tablePane id="table"
tableName="PivotSubscriptions.Subscription"
cellSpacing="2"
fixedHeaders="false"
queryClass="PivotSubscriptions.UI.SubscriptionManager"
queryName="GetSubscriptionsForDisplay"
fixedHeaders="false"
useSnapshot="true"
showQuery="false"
showZebra="true"
Expand All @@ -28,11 +27,11 @@ XData contentPane [ XMLNamespace = "http://www.intersystems.com/zen" ]
maxRows="1000"
valueColumn="ID"
>
<column colName="ID" header="Subscription ID" hidden="true" />
<column id="Pivot" header="Pivot" colName="Pivot" link="./_DeepSee.UserPortal.Analyzer.zen?PIVOT=#(%query.Pivot)#"/>
<column colName="Emails" header="Emails" />
<column onclick="zenPage.editSubscription(#(%query.ID)#,'#(%query.Pivot)#')" linkCaption="Edit" link="#"/>
<column onclick="return zenPage.deleteSubscription('#(%query.ID)#');" linkCaption="Delete" link="#" />
<column colName="ID" header="Subscription ID" hidden="true" />
<column id="Pivot" colName="Pivot" header="Pivot" link="./_DeepSee.UserPortal.Analyzer.zen?PIVOT=#(%query.Pivot)#" filterType="text" filterOp="UP[" />
<column id="Emails" colName="Emails" header="Emails" filterType="text" filterOp="UP[" />
<column onclick="zenPage.editSubscription(#(%query.ID)#,'#(%query.Pivot)#')" linkCaption="Edit" link="#"/>
<column onclick="return zenPage.deleteSubscription('#(%query.ID)#');" linkCaption="Delete" link="#" />
</tablePane>
</vgroup>
</hgroup>
Expand Down Expand Up @@ -80,7 +79,7 @@ ClientMethod onPopupAction(popupName, action, value) [ Internal, Language = java
}

/// Creates edit popup.
ClientMethod editSubscription(subscriptionId,pivotName) [ Internal, Language = javascript ]
ClientMethod editSubscription(subscriptionId, pivotName) [ Internal, Language = javascript ]
{
var parms = {
MODE:"models",
Expand Down Expand Up @@ -116,11 +115,4 @@ Method Delete(pID As %Integer) As %String [ ZenMethod ]

Quit tMsg
}

/// Query to get subscriptions for Subscription Manager display table.
Query GetSubscriptionsForDisplay() As %SQLQuery
{
SELECT ID,Pivot,Emails FROM PivotSubscriptions.Subscription
}

}
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
## PivotSubscriptions
# PivotSubscriptions

PivotSubsctions allows users to subscribe to a pivot table and recieve a scheduled email containing the specified pivot table.

## Installation
1. Use the Download ZIP option for this project
2. Extract the files and copy path
* This is the path to the directory that contains README.md and LICENSE
3. Open terminal and ZN to desired namespace
4. Run the following commands:
```
set path="<PATH FROM STEP 2>"
do $system.OBJ.LoadDir(path_"/PivotSubscriptions/","ck",,1)
```
5. Follow the Configuration steps

## Configuration steps
### Configure Default Namespace
To ensure that links to the SubscriptionManager page are correctly generated, the Default Application of the namespace you are working in is correctly configured.
1. Go to Management Portal
2. Go to System Administration > Security > Applications > Web Applications
3. Click on /csp/??? where ??? is the namespace you are working in
4. Check the checkbox next to Namespace Default Application and Save

### Configure Task Manager Email Settings
Subscriptions are delivered by Email. The Task Manager Email must be configured to allow alerts to be delivered by Email. At a minimum, the SMTP Server must be assigned in the Task Manager Email Settings (Management Portal -> System Administration -> Configuration -> Additional Settings -> Task Manager Email). For more information, please see the <a href="http://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cls?KEY=RACS_Category_TaskManagerEmail">documentation</a>.

### Initialize or Update Task in IRIS
In order for the Task of sending scheduled emails to be initalized or updated, the user must call the ConfigureTask() method in Terminal.
1. Open terminal and ZN to desired namespace
* You can use the IRIS Launcher (system icon on bottom right in Windows) by right-clicking the icon and selecting "Terminal".
2. Run the following command:
```
set status = ##class(PivotSubscriptions.Task).ConfigureTask()
```
3. Optionally, you can write the status to Terminal output to verify the task was initalized. Note, if the task was already initalized and the command was run again to cause the Task to update, the status will return 1 regardless of whether the update failed or succeeded.
```
write status
```

0 comments on commit 326ec27

Please sign in to comment.