:Class Contest2010Sample : MildPage
:Include #.HTMLInput ⍝ Useful functions for creating HTML pages
:Field Public HomePrice←150000 ⍝ Home purchase price
:Field Public ClosingCosts←5000 ⍝ One-time Closing costs
:Field Public LoanTerm←360 ⍝ Term of loan (months)
:Field Public BankAmount←75000 ⍝ Borrowed from bank
:Field Public BankRate←10 ⍝ At annual mortgage rate
:Field Public DadAmount←30000 ⍝ Borrowed from parents
:Field Public DadRate←5 ⍝ At annual mortgage rate
:Field Public Inflation←1.5 ⍝ Annual inflation rate
:Field Public MarginalTax←25 ⍝ Marginal income tax rate
:Field Public PropertyTax←1.75 ⍝ Annual property tax rate
:Field Public SavingsRate←3 ⍝ Savings/investment rate
:Field Public InitialRent←600.00 ⍝ Monthly apartment rent
:Field Public Action←'' ⍝ Will contain name of button pressed
:Field Public Upload←'' ⍝ File Upload
:Field Public Uploaded←'' ⍝ Name of uploaded file
:Field Public DisplayReport←1 ⍝ Checkbox selecting a report
:Field Public DisplayChart←1 ⍝ Checkbox selecting a chart
:Field Public MonthlyDetails←1 ⍝ Display monthly details
:Field Public OutCol1←1 ⍝ Checkboxes for output column selection
:Field Public OutCol2←1
:Field Public OutCol3←1
:Field Public OutCol4←1
:Field Public OutCol5←1
:Field Public OutCol6←1
:Field Public OutCol7←1
:Field Public OutCol8←1
:Field Public OutCol9←1
:Field Public OutCol10←1
:Field Public OutCol11←1
:Field Public OutCol12←1
:Field Public OutCol13←1
:Field Public OutCol14←1
:Field Public OutCol15←1
:Field Public ChartLine1←'' ⍝ Chart Lines
:Field Public ChartLine2←''
:Field Public ChartLine3←''
:Field Public ChartLine4←''
nl←⎕UCS 10
getnum←{⍬⍴2⊃⎕VFI ⍕⍵} ⍝ Extract numbers from string
edit←{v←⍵~' '⋄ (⍵,':') (v Edit (⍕⍎v) 10)} ⍝ Define Labelled HTML edit on field ⍵
OutputCols←'[none selected]' '#' 'Bank mortgage interest' 'Bank principal repaid' 'Bank loan balance'
OutputCols,←'Parent mortgage interest' 'Parent principal repaid' 'Parent loan balance'
OutputCols,←'Mortgage tax savings' 'Property taxes' 'Value of home' 'Savings while owning'
OutputCols,←'Home cashout value' 'Rent payment' 'Savings while renting' 'Advantage (disadv.) of buying'
OutputTags←'Month' 'BankInterest' 'BankRepaid' 'BankBalance' 'ParentInterest' 'ParentRepaid' 'ParentBalance'
OutputTags,←'TaxSavings' 'PropertyTaxes' 'HomeValue' 'SavingsOwning' 'CashoutValue' 'Rent' 'SavingsRenting' 'AdvantageOfBuying'
InputParams←'HomePrice' 'ClosingCosts' 'LoanTerm' 'BankAmount' 'BankRate' 'DadAmount' 'DadRate'
InputParams,←'Inflation' 'MarginalTax' 'PropertyTax' 'SavingsRate' 'InitialRent'
TableStyle←'',nl
∇ Render req;html;tbl;z;report;chart;go
:Access Public
DoAction ⍝ Process actions
go←'Action'Submit'Run' ⍝ Action button
tbl←4 4⍴↑edit¨'Loan Term' 'Initial Rent' 'Home Price' 'Closing Costs' 'Inflation' 'Savings Rate' 'Marginal Tax' 'Property Tax'
tbl⍪←'b'Enclose¨'Lender' 'Amount' 'Interest' ' '
tbl⍪←('Bank' 'Parents',2 2⍴2⊃¨edit¨'Bank Amount' 'Bank Rate' 'Dad Amount' 'Dad Rate'),⊂' '
html←TableStyle
html,←(0≠⍴Uploaded)/'p'Enclose'(Parameters uploaded from file "',Uploaded,'")'
html,←Table 1 2⍴(Table tbl'border=1')go
html,←Table 1 2⍴('Or upload XML file:')('Upload'File 40) ⍝ File Upload field, 40 chars wide
html,←DownloadXML
tbl←{v←'OutCol',⍕⍵ ⋄ ' ',(v CheckBox(⍎v)),' ',(⍵+1)⊃OutputCols}¨⍳¯1+⍴OutputCols
report←Table 5 3⍴1↓tbl,'' ''
tbl←'Blue' 'Red'{
hr←' ______ '
hr,('ChartLine',⍕⍵)DropDown OutputCols((1+⍵⊃ChartLines)⊃OutputCols)}¨1 2 ⍝ 3 4
tbl←Table⍪(⊂(('DisplayChart'CheckBox DisplayChart),'b'Enclose'Display Chart for Selected Columns:')),tbl
html,←'
',Table 1 2⍴tbl go
html,←GraphHtml
z←('DisplayReport'CheckBox DisplayReport),'b'Enclose'Display Report for Selected Columns '
z,←('MonthlyDetails'CheckBox MonthlyDetails),'Display Monthly Details'
tbl←Table⍪z report
html,←'
',Table 1 2⍴tbl go
html,←ReportHtml
html←req('post'Form)html ⍝ Wrap it all in a 'post' form around it
html←('h2'Enclose'Dyalog Programming Contest 2010'),html ⍝ Header
html,←'a href="/"'Enclose'Home' ⍝ Link to home page
:If 0≠⍴Error ⋄ html←('font color="red"'Enclose Error),'
',html ⋄ :EndIf
req.Return html
⍎(2↓⍕('←OutCol'∘,¨⍕¨⍳15),'←ChartLine'∘,¨⍕¨⍳4),'←0' ⍝ Clear many checkboxes
DisplayReport←DisplayChart←MonthlyDetails←0 ⍝ Clear more checkboxes
∇
∇ DoAction;data;result;tbl;m;on;chart;url;filename;i;z;pmts
DownloadXML←Error←''
:If 0≠⍴Upload ⍝ File Uploaded?
Uploaded data←Upload
:AndIf 0≠⍴data ⍝ And not empty
:Trap 0
data←GetBuyRentParams data
⍎'(',(⍕InputParams),')←data'
:Else
Error←'Invalid XML uploaded' ⋄ →0
:EndTrap
:Else ⍝ Pick up values from variables
data←getnum¨⍎⍕InputParams
Uploaded←''
:EndIf
on←'on'(,'1')1 ⍝ Values that might be reported by a checkbox which is on
DisplayChart DisplayReport MonthlyDetails←DisplayChart DisplayReport MonthlyDetails∊on
OutCols←(⍎¨'OutCol'∘,¨⍕¨⍳15)∊on
ChartLines←(⍴OutputCols)|¯1+OutputCols⍳ChartLine1 ChartLine2 ChartLine3 ChartLine4
ChartLine1
(pmts result)←BuyOrRent data
:If DisplayChart∧0≠⍴i←(ChartLines≠0)/⍳⍴ChartLines
i←ChartLines[i]
chart←MakeChart(⍉result[;i])(OutputCols[1+i])(⍕¨result[;1])
GraphHtml←'
'
:Else ⋄ GraphHtml←''
:EndIf
(filename url)←MakeTempFile'xml'
(FormatBuyRentResult data pmts result)#.Files.PutText filename
:If ~MonthlyDetails
result←12 '↓++↓++↓++↓+↓+++'AggregateSeries result
result[;1]←⍳1↑⍴result ⍝ Year numbers
:EndIf
tbl←result[;1],{↓((⍴⍵),17)⍴'M<(>N<)>Q< >CF17.2'⎕FMT ⍵}0 1↓(1,1↓OutCols)/result
:If MonthlyDetails ⋄ m←({⍵+⌊⍵÷12}1↑⍴tbl)⍴1 12/0 1 ⍝ Insert (bold) labels every 12 months
:Else ⋄ m←0,(1↑⍴tbl)⍴1 ⋄ :EndIf ⍝ Else just at the top
tbl←m⍀tbl ⋄ m←~m
z←('Year' 'Month')[1+MonthlyDetails]
tbl[m/⍳⍴m;]←((+/m),2⊃⍴tbl)⍴'b'Enclose¨(0 1,1↓OutCols)/(⊂''),z,2↓OutputCols
DownloadXML←'
',Table 1 1⍴⊂'Download Results as XML'
ReportHtml←'
Monthly payments: Bank ',(,'CF12.2'⎕FMT 1⊃pmts),', Parents ',,'CF12.2'⎕FMT 2⊃pmts
ReportHtml,←DisplayReport/'
',DownloadXML,Table tbl'border="1"' 'class="right"'
∇
∇ r←MakeChart(data legend xlabels);⎕USING;dll;sp;t;outfile
⍝ --- Set up environment ---
dll←(2 ⎕NQ'.' 'GetEnvironment' 'Dyalog'),'bin\sharpplot.dll'
⎕USING←('Causeway,',dll)'System.Windows.Forms,System.Windows.Forms.dll' 'System.Drawing,System.Drawing.dll' ''
⍝ --- Create Chart ---
sp←⎕NEW SharpPlot
sp.SetBackground Color.FromName⊂'DCE1EB' ⍝ Same as web page
sp.SetLabelFont'Times' 10 FontStyle.Regular
sp.SetLineStyles LineStyle.Solid
sp.SetPenWidths 1
sp.SetKeyText⊂,⊂legend
sp.SetXLabels⊂,⊂xlabels
sp.Heading←'Buy or Rent?'
sp.XCaption←'Months'
:If (0<⌈/,data)∧0>⌊/,data ⍝ 0 is inside the range of data
sp.YIntercept←0 ⍝ Put the YAxis at zero
:EndIf
sp.DrawLineGraph⊂{1⊃¨((⎕DR¨⍵),¨645)⎕DR¨⍵}↓data
(outfile r)←MakeTempFile'png'
sp.SaveImage outfile Imaging.ImageFormat.Png 120 ⍝ Export Chart as file
∇
∇ (filename url)←MakeTempFile extn;tn
(tn filename)←#.Files.CreateTemp req.Server.Root,'TempFiles/*.',extn
url←'/',(⍴req.Server.Root)↓filename ⍝ Web URL
⎕NUNTIE tn
∇
∇ xml←FormatBuyRentResult(params pmts result);r;z
⍝ tags: (Parameter Names) (Payment Names) (Result Column Names)
⍝ 1st result column expect to be month
r←1 4⍴0 'BuyRentCase' ''⍬
r⍪←1 'BuyRentParams' ''(InputParams,[1.5]params)
z←((1⊃InputParams)⍳⊂'LoanTerm')⊃params ⍝ Extract LoanTerm from params
r⍪←1 'BuyRentResult' ''(('LoanTerm' 'BankPayment' 'ParentPayment'),[1.5]z,2⍕¨pmts)
r←⊃⍪/(⊂r),{(2 'Month' ''('id'(1⊃⍵)))⍪3,(1↓OutputTags),(2⍕¨1↓⍵),[1.5]⊂⍬}¨↓result
xml←⎕XML r
((xml='¯')/xml)←'-' ⍝ High minuses to low
∇
⍝ --- Overwrite the following functions with your own solutions ---
⍝ These functions do the bare minimum required to return sufficiently well-formed
⍝ results, in order to prevent the web page from crashing
∇ r←GetBuyRentParams xml
r←150000 5000 360 75000 10 30000 5 1.5 25 1.75 3 600
∇
∇ r←BuyOrRent Params
r←(123.45 67.89)(↑[0.5]15⍴⊂⍳3⊃Params)
∇
∇ r←ctl AggregateSeries data
r←(2,¯1↑⍴data)⍴data ⍝ Return 2 rows of data regardless of ctl params
∇
:EndClass